Skip to content

Commit

Permalink
update findAuthenticator
Browse files Browse the repository at this point in the history
  • Loading branch information
duytiennguyen-okta committed Feb 23, 2024
1 parent fd97dd9 commit a1f11b6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
10 changes: 8 additions & 2 deletions okta/data_source_okta_authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,15 @@ func findAuthenticator(ctx context.Context, m interface{}, name, key string) (*s
return nil, err
}
for _, authenticator := range authenticators {
if authenticator.Name == name && authenticator.Key == key {
if authenticator.Name == name {
return authenticator, nil
}
if authenticator.Key == key {
return authenticator, nil
}
}
if key != "" {
return nil, fmt.Errorf("authenticator with key '%s' does not exist", key)
}
return nil, fmt.Errorf("authenticator with name '%s' and/or key '%s' does not exist", name, key)
return nil, fmt.Errorf("authenticator with name '%s' does not exist", name)
}
38 changes: 11 additions & 27 deletions okta/resource_okta_authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func resourceAuthenticatorCreate(ctx context.Context, d *schema.ResourceData, m

var err error
// soft create if the authenticator already exists
authenticator, _ := findAuthenticator(ctx, m, d.Get("name").(string), d.Get("key").(string))
authenticator, _ := findAuthenticator(ctx, m, "", d.Get("key").(string))
if authenticator == nil {
// otherwise hard create
authenticator, err = buildAuthenticator(d)
Expand All @@ -165,16 +165,11 @@ func resourceAuthenticatorCreate(ctx context.Context, d *schema.ResourceData, m
qp := &query.Params{
Activate: boolPtr(activate),
}
if(d.Get("key").(string) == "custom_otp"){
qp = &query.Params{
Activate: boolPtr(false),
}
}
authenticator, _, err = getOktaClientFromMetadata(m).Authenticator.CreateAuthenticator(ctx, *authenticator, qp)
if err != nil {
return diag.FromErr(err)
}
if(d.Get("key").(string) == "custom_otp"){
if d.Get("key").(string) == "custom_otp" {
var otp *sdk.OTP
otp, err = buildOTP(d)
if err != nil {
Expand All @@ -197,17 +192,6 @@ func resourceAuthenticatorCreate(ctx context.Context, d *schema.ResourceData, m
if status.(string) == statusInactive {
authenticator, _, err = getOktaClientFromMetadata(m).Authenticator.DeactivateAuthenticator(ctx, d.Id())
} else {
if(d.Get("key").(string) == "custom_otp"){
var otp *sdk.OTP
otp, err = buildOTP(d)
if err != nil {
return diag.FromErr(err)
}
_, err = getOktaClientFromMetadata(m).Authenticator.SetSettingsOTP(ctx, *otp, d.Id())
if err != nil {
return diag.FromErr(err)
}
}
authenticator, _, err = getOktaClientFromMetadata(m).Authenticator.ActivateAuthenticator(ctx, d.Id())
}
if err != nil {
Expand Down Expand Up @@ -287,9 +271,7 @@ func buildAuthenticator(d *schema.ResourceData) (*sdk.Authenticator, error) {
Key: d.Get("key").(string),
Name: d.Get("name").(string),
}
if d.Get("key").(string) == "custom_otp" {

} else if d.Get("type").(string) == "security_key" {
if d.Get("type").(string) == "security_key" {
authenticator.Provider = &sdk.AuthenticatorProvider{
Type: d.Get("provider_type").(string),
Configuration: &sdk.AuthenticatorProviderConfiguration{
Expand All @@ -315,13 +297,15 @@ func buildAuthenticator(d *schema.ResourceData) (*sdk.Authenticator, error) {
},
}
} else {
if s, ok := d.GetOk("settings"); ok {
var settings sdk.AuthenticatorSettings
err := json.Unmarshal([]byte(s.(string)), &settings)
if err != nil {
return nil, err
if d.Get("key").(string) != "custom_otp" {
if s, ok := d.GetOk("settings"); ok {
var settings sdk.AuthenticatorSettings
err := json.Unmarshal([]byte(s.(string)), &settings)
if err != nil {
return nil, err
}
authenticator.Settings = &settings
}
authenticator.Settings = &settings
}
}

Expand Down

0 comments on commit a1f11b6

Please sign in to comment.