package gokeys import ( "fmt" "time" "github.rackspace.com/gorax/clients/goctk" "github.rackspace.com/gorax/clients/raxIdentity" ) // SSO function will take a username string and attempt to gather the // user Password from the system keychain, if there was an error, or if the // Password is expired, it will prompt for new credentials and // store them back into the keychain with a new Expire timer. func (s *Settings) SSO() (key Credential, err error) { logPrint("getting SSO information...") if key, err = s.getCredential(s.User, SvcSSO); err == nil { return } ssoPass, err := ui.Ask("Please enter SSO Password", options) if err != nil { err = fmt.Errorf("SSO:ui.Ask:%s", err) logPrint("error at SSO/ui.Ask") return } expire := time.Unix(time.Now().Unix(), 0).Add(ssoExpire).Unix() return s.setCredential(s.User, ssoPass, SvcSSO, expire) } // Identity function will take a username string and attempt to gather the // identity token from the system keychain, if there was an error, or if the // Password is expired, it will attempt to gather a new token with the GetSSO // Password and store it back into the keychain with a new Expire timer. func (s *Settings) Identity(staging bool) (key Credential, err error) { logPrint("getting Identity credentials...") service := SvcIdentity if staging { logPrint("using staging environment") service = SvcIdentityStg } if key, err = s.getCredential(s.User, service); err == nil { return } logPrint("regenerating Identity Token...") ssoCred, err := s.SSO() if err != nil { logPrint("error at GetIdentity/GetSSO") return } id, err := raxidentity.InternalAuth(s.User, ssoCred.Password, staging) if err != nil { logPrint("error at GetIdentity/raxidentity.InternalAuth") return } return s.setCredential(s.User, id.APIKey(), service, id.Access.Token.Expire.Unix()) } // Core function will take a username string and attempt to gather the // CORE token from the system keychain, if there was an error, or if the // token is expired, it will prompt for new RSA token, gather a new token from CTK // and store them back into the keychain with a new Expire timer. func (s *Settings) Core(staging bool) (key Credential, err error) { logPrint("getting Core token credentials...") service := SvcCore if staging { logPrint("using staging environment") service = SvcCoreStg } key, err = s.getCredential(s.User, service) if err == nil { return } // Attempt to gather the password from the users Firefox session. if s, err := GetFirefoxToken(); err == nil { logPrint("attempting to gather new token from firefox") key.Password = s } // If the core token is past its original expire time, start checking CTK // for validity. if key.Password != "" { logPrint("past expire time. checking ctk for validity") // Gather user info from CTK ui, _ := goctk.UserInfo(key.Password, staging) // If the token value is still valid per CTK, return again with // the same token value. if ui.Valid { // Reset expire to 5 minutes logPrint("valid token. resetting expire timer...") expire := time.Unix(time.Now().Unix(), 0).Add(coreExpire).Unix() return s.setCredential(s.User, key.Password, service, expire) } logPrint("invalid token") } logPrint("regenerating Core Token...") if rsaToken == "" || !staging { rsaToken, err = ui.Ask("Please enter RSA Token", options) if err != nil { logPrint("error at GetCoreToken/ui.Ask") return } } ctk, err := goctk.New(s.User, rsaToken, staging) if err != nil { logPrint("error at GetCoreToken/goctk.New") return } expire := time.Unix(time.Now().Unix(), 0).Add(coreExpire).Unix() return s.setCredential(s.User, ctk.Token, service, expire) } // ACS function will take a username string and attempt to gather the // user Password from the system keychain, if there was an error, or if the // Password is expired, it will prompt for new credentials and // store them back into the keychain with a new Expire timer. func (s *Settings) ACS() (key Credential, err error) { logPrint("getting ACS credentials...") if key, err = s.getCredential(s.User, SvcACS); err == nil { return } acsPass, err := ui.Ask("Please enter Cisco ACS Password", options) if err != nil { logPrint("error at GetACS/ui.Ask") return } expire := time.Unix(time.Now().Unix(), 0).Add(acsExpire).Unix() return s.setCredential(s.User, acsPass, SvcACS, expire) }