Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow password to be saved to keychain with skip-prompt #547

Merged
merged 10 commits into from
Dec 30, 2023
41 changes: 22 additions & 19 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,16 @@ 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")
}
log.Println("No password supplied")
}
if account.Provider == onelogin.ProviderName {
if configFlags.ClientID == "" || configFlags.ClientSecret == "" {
Expand Down
Loading