-
Notifications
You must be signed in to change notification settings - Fork 15
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
[pr] Okta Challenge Preferences #93
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ var providerDuration int | |
|
||
// Okta | ||
var baseURL string | ||
var challenges []string | ||
|
||
func init() { | ||
// OneLogin | ||
|
@@ -43,6 +44,7 @@ func init() { | |
|
||
// Okta | ||
cmdProvidersCreateOkta.Flags().StringVar(&baseURL, "base-url", "", "Okta base URL") | ||
cmdProvidersCreateOkta.Flags().StringArrayVar(&challenges, "challenges", []string{"push", "token:software:totp"}, "Challenges to be used with preference") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
cmdProvidersCreateOkta.Flags().StringVar(&username, "username", "", | ||
"Don't ask for a username and use this instead") | ||
cmdProvidersCreateOkta.Flags().IntVar(&providerDuration, "duration", 0, "(Optional) Default session duration in seconds") | ||
|
@@ -176,10 +178,11 @@ var cmdProvidersCreateOkta = &cobra.Command{ | |
log.Fatalf(color.RedString("Provider '%s' already exists"), name) | ||
} | ||
|
||
conf := map[string]string{ | ||
"base-url": baseURL, | ||
"type": "okta", | ||
"username": username, | ||
conf := map[string]interface{}{ | ||
"base-url": baseURL, | ||
"type": "okta", | ||
"username": username, | ||
"challenges": challenges, | ||
} | ||
if providerDuration != 0 { | ||
// Duration specified - validate value | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,18 +38,20 @@ type GetSessionTokenResponse struct { | |
StateToken string `json:"stateToken"` | ||
Status string `json:"status"` | ||
Embedded struct { | ||
Factors []struct { | ||
ID string `json:"id"` | ||
Links struct { | ||
Verify struct { | ||
Href string `json:"href"` | ||
} `json:"verify"` | ||
} `json:"_links"` | ||
FactorType string `json:"factorType"` | ||
} `json:"factors"` | ||
Factors []factor `json:"factors"` | ||
} `json:"_embedded"` | ||
} | ||
|
||
type factor struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
ID string `json:"id"` | ||
Links struct { | ||
Verify struct { | ||
Href string `json:"href"` | ||
} `json:"verify"` | ||
} `json:"_links"` | ||
FactorType string `json:"factorType"` | ||
} | ||
|
||
// GetSessionToken performs a login operation against the Okta API and returns a session token upon | ||
// successful login. | ||
// | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package okta | |
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
"time" | ||
|
||
"github.com/allcloud-io/clisso/aws" | ||
|
@@ -77,6 +78,15 @@ func Get(app, provider string, duration int64) (*aws.Credentials, error) { | |
st = resp.SessionToken | ||
case StatusMFARequired: | ||
factor := resp.Embedded.Factors[0] | ||
|
||
if len(p.Challenges) > 0 { | ||
// use preferred list if available | ||
factor, err = preferredFactor(resp.Embedded.Factors, p.Challenges) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
stateToken := resp.StateToken | ||
|
||
var vfResp *VerifyFactorResponse | ||
|
@@ -163,3 +173,34 @@ func Get(app, provider string, duration int64) (*aws.Credentials, error) { | |
|
||
return creds, err | ||
} | ||
|
||
func typeMapOf(factors []factor) (m map[string]factor) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code all looks good, but I don't see any accompanying unit tests. I strongly recommend adding these to protect the code against future changes that could unintentionally break things; and it may also be a good clean place to start with that if you haven't already as there aren't many dependencies. |
||
m = make(map[string]factor) | ||
|
||
for _, v := range factors { | ||
m[v.FactorType] = v | ||
} | ||
return | ||
} | ||
|
||
func keys(m map[string]factor) (keys []string) { | ||
keys = make([]string, len(m)) | ||
|
||
i := 0 | ||
for k := range m { | ||
keys[i] = k | ||
i++ | ||
} | ||
return | ||
} | ||
|
||
func preferredFactor(factors []factor, pref []string) (factor, error) { | ||
fmap := typeMapOf(factors) | ||
for _, p := range pref { | ||
if f, ok := fmap[p]; ok { | ||
return f, nil | ||
} | ||
} | ||
|
||
return factor{}, fmt.Errorf("no supported MFA type found in: %v", strings.Join(keys(fmap), ",")) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although not precisely the same, could the
OktaProviderConfig
fromconfig/config.go
be used here, and theOneLoginProviderConfig
be used above?