-
Notifications
You must be signed in to change notification settings - Fork 56
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
Implement MFA authentication APIs #331
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0facdcd
Implement MFA authentication APIs
ewanharris 51b8aea
Refactor client auth handling in oauth and passwordless
ewanharris 35d4ca9
Improve doc and naming
ewanharris 18a2e2f
Remove some mfa tokens
ewanharris 431b223
Link to docs
ewanharris b5c2f29
Fix test recording casing
ewanharris 62f9bd3
Apply suggestions from code review
ewanharris 716bdad
Check if ClientAssertionType is empty and add required check
ewanharris a19778b
Merge branch 'main' into feat/mfa-auth-apis
ewanharris 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
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,137 @@ | ||
package authentication | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/auth0/go-auth0/authentication/mfa" | ||
"github.com/auth0/go-auth0/authentication/oauth" | ||
) | ||
|
||
// MFA exposes requesting an MFA challenge and verifying MFA methods. | ||
type MFA manager | ||
|
||
// Challenge requests a challenge for multi-factor authentication (MFA) based on the challenge types supported by the application and user. | ||
// | ||
// See: https://auth0.com/docs/api/authentication#challenge-request | ||
func (m *MFA) Challenge(ctx context.Context, body mfa.ChallengeRequest, opts ...RequestOption) (c *mfa.ChallengeResponse, err error) { | ||
missing := []string{} | ||
check(&missing, "ClientID", (body.ClientID != "" || m.authentication.clientID != "")) | ||
check(&missing, "MFAToken", body.MFAToken != "") | ||
check(&missing, "ChallengeType", body.ChallengeType != "") | ||
|
||
if len(missing) > 0 { | ||
return nil, fmt.Errorf("Missing required fields: %s", strings.Join(missing, ", ")) | ||
} | ||
|
||
err = m.authentication.addClientAuthenticationToClientAuthStruct(&body.ClientAuthentication) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = m.authentication.Request(ctx, "POST", m.authentication.URI("mfa", "challenge"), body, &c, opts...) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return | ||
} | ||
|
||
// VerifyWithOTP verifies an MFA challenge using a one-time password (OTP). | ||
// | ||
// See: https://auth0.com/docs/api/authentication#verify-with-one-time-password-otp- | ||
func (m *MFA) VerifyWithOTP(ctx context.Context, body mfa.VerifyWithOTPRequest, opts ...RequestOption) (t *oauth.TokenSet, err error) { | ||
missing := []string{} | ||
check(&missing, "ClientID", (body.ClientID != "" || m.authentication.clientID != "")) | ||
check(&missing, "MFAToken", body.MFAToken != "") | ||
check(&missing, "OTP", body.OTP != "") | ||
|
||
if len(missing) > 0 { | ||
return nil, fmt.Errorf("Missing required fields: %s", strings.Join(missing, ", ")) | ||
} | ||
|
||
data := url.Values{ | ||
"mfa_token": []string{body.MFAToken}, | ||
"grant_type": []string{"http://auth0.com/oauth/grant-type/mfa-otp"}, | ||
"otp": []string{body.OTP}, | ||
} | ||
|
||
err = m.authentication.addClientAuthenticationToURLValues(body.ClientAuthentication, data, true) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = m.authentication.Request(ctx, "POST", m.authentication.URI("oauth", "token"), data, &t, opts...) | ||
|
||
return | ||
} | ||
|
||
// VerifyWithOOB verifies an MFA challenge using an out-of-band challenge (OOB), either Push notification, | ||
// SMS, or Voice. | ||
ewanharris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// See: https://auth0.com/docs/api/authentication#verify-with-out-of-band-oob- | ||
func (m *MFA) VerifyWithOOB(ctx context.Context, body mfa.VerifyWithOOBRequest, opts ...RequestOption) (t *oauth.TokenSet, err error) { | ||
missing := []string{} | ||
check(&missing, "ClientID", (body.ClientID != "" || m.authentication.clientID != "")) | ||
check(&missing, "MFAToken", body.MFAToken != "") | ||
check(&missing, "OOBCode", body.OOBCode != "") | ||
|
||
if len(missing) > 0 { | ||
return nil, fmt.Errorf("Missing required fields: %s", strings.Join(missing, ", ")) | ||
} | ||
|
||
data := url.Values{ | ||
"mfa_token": []string{body.MFAToken}, | ||
"grant_type": []string{"http://auth0.com/oauth/grant-type/mfa-oob"}, | ||
"oob_code": []string{body.OOBCode}, | ||
} | ||
|
||
if body.BindingCode != "" { | ||
data.Set("binding_code", body.BindingCode) | ||
} | ||
|
||
err = m.authentication.addClientAuthenticationToURLValues(body.ClientAuthentication, data, true) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = m.authentication.Request(ctx, "POST", m.authentication.URI("oauth", "token"), data, &t, opts...) | ||
|
||
return | ||
} | ||
|
||
// VerifyWithRecoveryCode verifies an MFA challenge using a recovery code. | ||
// | ||
// See: https://auth0.com/docs/api/authentication#verify-with-recovery-code | ||
func (m *MFA) VerifyWithRecoveryCode(ctx context.Context, body mfa.VerifyWithRecoveryCodeRequest, opts ...RequestOption) (t *mfa.VerifyWithRecoveryCodeResponse, err error) { | ||
missing := []string{} | ||
check(&missing, "ClientID", (body.ClientID != "" || m.authentication.clientID != "")) | ||
check(&missing, "MFAToken", body.MFAToken != "") | ||
check(&missing, "RecoveryCode", body.RecoveryCode != "") | ||
|
||
if len(missing) > 0 { | ||
return nil, fmt.Errorf("Missing required fields: %s", strings.Join(missing, ", ")) | ||
} | ||
|
||
data := url.Values{ | ||
"mfa_token": []string{body.MFAToken}, | ||
"grant_type": []string{"http://auth0.com/oauth/grant-type/mfa-recovery-code"}, | ||
"recovery_code": []string{body.RecoveryCode}, | ||
} | ||
|
||
err = m.authentication.addClientAuthenticationToURLValues(body.ClientAuthentication, data, true) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = m.authentication.Request(ctx, "POST", m.authentication.URI("oauth", "token"), data, &t, opts...) | ||
|
||
return | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
Should we check that
params.ClientAssertionType
is not empty? Like e.g.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.
Yeah that makes sense, will fix
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.
Added in 716bdad and also took the opportunity to extend
addClientAuthenticationToClientAuthStruct
to allow specifying if a client secret/client assertion is required