-
Notifications
You must be signed in to change notification settings - Fork 208
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
enable custom OTP on okta_authenticator #1864
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,9 +31,6 @@ func resourceAuthenticator() *schema.Resource { | |
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Display name of the Authenticator", | ||
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { | ||
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. Why do you need to remove this? |
||
return true | ||
}, | ||
}, | ||
"settings": { | ||
Type: schema.TypeString, | ||
|
@@ -157,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("key").(string)) | ||
authenticator, _ := findAuthenticator(ctx, m, d.Get("name").(string), d.Get("key").(string)) | ||
if authenticator == nil { | ||
// otherwise hard create | ||
authenticator, err = buildAuthenticator(d) | ||
|
@@ -168,10 +165,26 @@ 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"){ | ||
var otp *sdk.OTP | ||
otp, err = buildOTP(d) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
_, err = getOktaClientFromMetadata(m).Authenticator.SetSettingsOTP(ctx, *otp, authenticator.Id) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
} | ||
} | ||
|
||
d.SetId(authenticator.Id) | ||
|
@@ -184,6 +197,17 @@ 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 { | ||
|
@@ -289,13 +313,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 | ||
} | ||
} | ||
|
||
|
@@ -311,6 +337,20 @@ func buildAuthenticator(d *schema.ResourceData) (*sdk.Authenticator, error) { | |
return &authenticator, nil | ||
} | ||
|
||
func buildOTP(d *schema.ResourceData) (*sdk.OTP, error) { | ||
otp := sdk.OTP{} | ||
if s, ok := d.GetOk("settings"); ok { | ||
var settings sdk.AuthenticatorSettingsOTP | ||
err := json.Unmarshal([]byte(s.(string)), &settings) | ||
if err != nil { | ||
return nil, err | ||
} | ||
otp.Settings = &settings | ||
} | ||
|
||
return &otp, nil | ||
} | ||
|
||
func validateAuthenticator(d *schema.ResourceData) error { | ||
typ := d.Get("type").(string) | ||
if typ == "security_key" { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// DO NOT EDIT LOCAL SDK - USE v3 okta-sdk-golang FOR API CALLS THAT DO NOT EXIST IN LOCAL SDK | ||
package sdk | ||
|
||
import "encoding/json" | ||
|
||
type AuthenticatorSettingsOTP struct { | ||
AcceptableAdjacentIntervals int `json:"acceptableAdjacentIntervals"` | ||
Algorithm string `json:"algorithm"` | ||
Encoding string `json:"encoding"` | ||
PassCodeLength int `json:"passCodeLength"` | ||
Protocol string `json:"protocol"` | ||
TimeIntervalInSeconds int `json:"timeIntervalInSeconds"` | ||
} | ||
|
||
func (a *AuthenticatorSettingsOTP) MarshalJSON() ([]byte, error) { | ||
type Alias AuthenticatorSettingsOTP | ||
type local struct { | ||
*Alias | ||
} | ||
result := local{Alias: (*Alias)(a)} | ||
return json.Marshal(&result) | ||
} | ||
|
||
func (a *AuthenticatorSettingsOTP) UnmarshalJSON(data []byte) error { | ||
type Alias AuthenticatorSettingsOTP | ||
|
||
result := &struct { | ||
*Alias | ||
}{ | ||
Alias: (*Alias)(a), | ||
} | ||
if err := json.Unmarshal(data, &result); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This will cause breaking change