-
Notifications
You must be signed in to change notification settings - Fork 20
Add users.EnrollAuthFactor()
method
#254
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
Merged
jonatascastro12
merged 1 commit into
main
from
feature/usrld-1074-add-enrollauthfactor-method
Sep 8, 2023
Merged
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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"encoding/json" | ||
"github.com/stretchr/testify/require" | ||
"github.com/workos/workos-go/v2/pkg/common" | ||
"github.com/workos/workos-go/v2/pkg/mfa" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
|
@@ -699,7 +700,7 @@ func TestAuthenticateUserWithPassword(t *testing.T) { | |
scenario string | ||
client *Client | ||
options AuthenticateWithPasswordOpts | ||
expected AuthenticationResponse | ||
expected UserResponse | ||
err bool | ||
}{{ | ||
scenario: "Request without API Key returns an error", | ||
|
@@ -714,7 +715,7 @@ func TestAuthenticateUserWithPassword(t *testing.T) { | |
Email: "[email protected]", | ||
Password: "test_123", | ||
}, | ||
expected: AuthenticationResponse{ | ||
expected: UserResponse{ | ||
User: User{ | ||
ID: "testUserID", | ||
FirstName: "John", | ||
|
@@ -749,7 +750,7 @@ func TestAuthenticateUserWithCode(t *testing.T) { | |
scenario string | ||
client *Client | ||
options AuthenticateWithCodeOpts | ||
expected AuthenticationResponse | ||
expected UserResponse | ||
err bool | ||
}{{ | ||
scenario: "Request without API Key returns an error", | ||
|
@@ -763,7 +764,7 @@ func TestAuthenticateUserWithCode(t *testing.T) { | |
ClientID: "project_123", | ||
Code: "test_123", | ||
}, | ||
expected: AuthenticationResponse{ | ||
expected: UserResponse{ | ||
User: User{ | ||
ID: "testUserID", | ||
FirstName: "John", | ||
|
@@ -798,7 +799,7 @@ func TestAuthenticateUserWithMagicAuth(t *testing.T) { | |
scenario string | ||
client *Client | ||
options AuthenticateWithMagicAuthOpts | ||
expected AuthenticationResponse | ||
expected UserResponse | ||
err bool | ||
}{{ | ||
scenario: "Request without API Key returns an error", | ||
|
@@ -813,7 +814,7 @@ func TestAuthenticateUserWithMagicAuth(t *testing.T) { | |
Code: "test_123", | ||
User: "user_123", | ||
}, | ||
expected: AuthenticationResponse{ | ||
expected: UserResponse{ | ||
User: User{ | ||
ID: "testUserID", | ||
FirstName: "John", | ||
|
@@ -851,7 +852,7 @@ func authenticationResponseTestHandler(w http.ResponseWriter, r *http.Request) { | |
return | ||
} | ||
if secret, exists := payload["client_secret"].(string); exists && secret != "" { | ||
response := AuthenticationResponse{ | ||
response := UserResponse{ | ||
User: User{ | ||
ID: "testUserID", | ||
FirstName: "John", | ||
|
@@ -1282,3 +1283,98 @@ func sendMagicAuthCodeTestHandler(w http.ResponseWriter, r *http.Request) { | |
w.WriteHeader(http.StatusOK) | ||
w.Write(body) | ||
} | ||
|
||
func TestEnrollAuthFactor(t *testing.T) { | ||
tests := []struct { | ||
scenario string | ||
client *Client | ||
options EnrollAuthFactorOpts | ||
expected AuthenticationResponse | ||
err bool | ||
}{ | ||
{ | ||
scenario: "Request without API Key returns an error", | ||
client: NewClient(""), | ||
err: true, | ||
}, | ||
{ | ||
scenario: "Request returns User", | ||
client: NewClient("test"), | ||
options: EnrollAuthFactorOpts{ | ||
User: "user_01E3JC5F5Z1YJNPGVYWV9SX6GH", | ||
Type: mfa.TOTP, | ||
}, | ||
expected: AuthenticationResponse{ | ||
Factor: mfa.Factor{ | ||
ID: "auth_factor_test123", | ||
CreatedAt: "2022-02-17T22:39:26.616Z", | ||
UpdatedAt: "2022-02-17T22:39:26.616Z", | ||
Type: "generic_otp", | ||
}, | ||
Challenge: mfa.Challenge{ | ||
ID: "auth_challenge_test123", | ||
CreatedAt: "2022-02-17T22:39:26.616Z", | ||
UpdatedAt: "2022-02-17T22:39:26.616Z", | ||
FactorID: "auth_factor_test123", | ||
ExpiresAt: "2022-02-17T22:39:26.616Z", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.scenario, func(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(enrollAuthFactorTestHandler)) | ||
defer server.Close() | ||
|
||
client := test.client | ||
client.Endpoint = server.URL | ||
client.HTTPClient = server.Client() | ||
|
||
user, err := client.EnrollAuthFactor(context.Background(), test.options) | ||
if test.err { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
require.Equal(t, test.expected, user) | ||
}) | ||
} | ||
} | ||
|
||
func enrollAuthFactorTestHandler(w http.ResponseWriter, r *http.Request) { | ||
auth := r.Header.Get("Authorization") | ||
if auth != "Bearer test" { | ||
http.Error(w, "bad auth", http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
var body []byte | ||
var err error | ||
|
||
if r.URL.Path == "/users/user_01E3JC5F5Z1YJNPGVYWV9SX6GH/auth/factors" { | ||
body, err = json.Marshal(AuthenticationResponse{ | ||
Factor: mfa.Factor{ | ||
ID: "auth_factor_test123", | ||
CreatedAt: "2022-02-17T22:39:26.616Z", | ||
UpdatedAt: "2022-02-17T22:39:26.616Z", | ||
Type: "generic_otp", | ||
}, | ||
Challenge: mfa.Challenge{ | ||
ID: "auth_challenge_test123", | ||
CreatedAt: "2022-02-17T22:39:26.616Z", | ||
UpdatedAt: "2022-02-17T22:39:26.616Z", | ||
FactorID: "auth_factor_test123", | ||
ExpiresAt: "2022-02-17T22:39:26.616Z", | ||
}, | ||
}) | ||
} | ||
|
||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
w.Write(body) | ||
} |
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.
In ruby I think this is called
AuthenticationFactorAndChallenge
(or was 6 days ago, we may want to discuss standardization on this)