Skip to content

Commit

Permalink
Add Users.send_magic_auth_code() method
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatascastro12 committed Aug 22, 2023
1 parent fffb80f commit 7639493
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
15 changes: 15 additions & 0 deletions tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,18 @@ def test_verify_email(self, capture_and_mock_request, mock_user):

assert url[0].endswith("users/verify_email")
assert response["id"] == "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"

def test_send_magic_auth_code(
self, capture_and_mock_request, mock_magic_auth_challenge_response
):
email = "[email protected]"

url, request = capture_and_mock_request(
"post", mock_magic_auth_challenge_response, 200
)

response = self.users.send_magic_auth_code(email=email)

assert url[0].endswith("users/send_magic_auth_code")
assert request["json"]["email"] == email
assert response["id"] == "auth_challenge_01E4ZCR3C56J083X43JQXF3JK5"
32 changes: 31 additions & 1 deletion workos/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
USER_PASSWORD_RESET_PATH = "users/password_reset"
USER_SEND_VERIFICATION_EMAIL_PATH = "users/{0}/send_verification_email"
USER_VERIFY_EMAIL_PATH = "users/verify_email"
USER_SEND_MAGIC_AUTH_PATH = "users/magic_auth/send"

RESPONSE_LIMIT = 10

Expand Down Expand Up @@ -423,7 +424,7 @@ def send_verification_email(
user (str): The unique ID of the User whose email address will be verified.
Returns:
dict: User response from WorkOS.
dict: MagicAuthChallenge response from WorkOS.
"""

headers = {}
Expand Down Expand Up @@ -469,3 +470,32 @@ def verify_email(
)

return WorkOSUser.construct_from_response(response).to_dict()

def send_magic_auth_code(
self,
email,
):
"""Creates a one-time Magic Auth code and emails it to the user.
Kwargs:
email (str): The email address the one-time code will be sent to.
Returns:
dict: MagicAuthChallenge response from WorkOS.
"""

headers = {}

payload = {
"email_address": email,
}

response = self.request_helper.request(
USER_SEND_MAGIC_AUTH_PATH,
method=REQUEST_METHOD_POST,
headers=headers,
params=payload,
token=workos.api_key,
)

return WorkOSMagicAuthChallenge.construct_from_response(response).to_dict()

0 comments on commit 7639493

Please sign in to comment.