Skip to content

Commit

Permalink
Allow password to be saved to keychain with skip-prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
casperbiering committed Sep 9, 2021
1 parent f3d590d commit 854f9a6
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions cmd/saml2aws/commands/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const OneLoginOAuthPath = "/auth/oauth2/v2/token"
func Configure(configFlags *flags.CommonFlags) error {

idpAccountName := configFlags.IdpAccount
idpAccountPassword := configFlags.Password

// pass in alternative location of saml2aws config file, if set.
cfgm, err := cfg.NewConfigManager(configFlags.ConfigFile)
Expand All @@ -43,13 +44,27 @@ func Configure(configFlags *flags.CommonFlags) error {
return errors.Wrap(err, "failed to input configuration")
}

if credentials.SupportsStorage() {
if err := storeCredentials(configFlags, account); err != nil {
return err
if credentials.SupportsStorage() && idpAccountPassword == "" {
password := prompter.Password("Password")
if password != "" {
if confirmPassword := prompter.Password("Confirm"); confirmPassword == password {
idpAccountPassword = password
} else {
log.Println("Passwords did not match")
os.Exit(1)
}
} else {
log.Println("No password supplied")
}
}
}

if credentials.SupportsStorage() {
if err := storeCredentials(configFlags, account, idpAccountPassword); err != nil {
return err
}
}

err = cfgm.SaveIDPAccount(idpAccountName, account)
if err != nil {
return errors.Wrap(err, "failed to save configuration")
Expand All @@ -63,28 +78,14 @@ func Configure(configFlags *flags.CommonFlags) error {
return nil
}

func storeCredentials(configFlags *flags.CommonFlags, account *cfg.IDPAccount) error {
func storeCredentials(configFlags *flags.CommonFlags, account *cfg.IDPAccount, idpAccountPassword string) error {
if configFlags.DisableKeychain {
return nil
}
if configFlags.Password != "" {
if err := credentials.SaveCredentials(account.URL, account.Username, configFlags.Password); err != nil {
if idpAccountPassword != "" {
if err := credentials.SaveCredentials(account.URL, account.Username, idpAccountPassword); err != nil {
return errors.Wrap(err, "error storing password in keychain")
}
} else {
password := prompter.Password("Password")
if password != "" {
if confirmPassword := prompter.Password("Confirm"); confirmPassword == password {
if err := credentials.SaveCredentials(account.URL, account.Username, password); err != nil {
return errors.Wrap(err, "error storing password in keychain")
}
} else {
log.Println("Passwords did not match")
os.Exit(1)
}
} else {
log.Println("No password supplied")
}
}
if account.Provider == onelogin.ProviderName {
if configFlags.ClientID == "" || configFlags.ClientSecret == "" {
Expand Down

0 comments on commit 854f9a6

Please sign in to comment.