Skip to content
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

Return User instead of MagicAuthChallenge response #204

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 14 additions & 22 deletions tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def test_authenticate_with_magic_auth(
):
code = "test_auth"
expires_in = 3600
magic_auth_challenge_id = "magic_auth_challenge_id"
user = "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
ip_address = "192.0.0.1"

Expand All @@ -217,7 +217,7 @@ def test_authenticate_with_magic_auth(
response = self.users.authenticate_with_magic_auth(
code=code,
expires_in=expires_in,
magic_auth_challenge_id=magic_auth_challenge_id,
user=user,
user_agent=user_agent,
ip_address=ip_address,
)
Expand All @@ -228,7 +228,7 @@ def test_authenticate_with_magic_auth(
assert request["json"]["code"] == code
assert request["json"]["user_agent"] == user_agent
assert request["json"]["expires_in"] == expires_in
assert request["json"]["magic_auth_challenge_id"] == magic_auth_challenge_id
assert request["json"]["user_id"] == user
assert request["json"]["ip_address"] == ip_address
assert request["json"]["client_id"] == "client_b27needthisforssotemxo"
assert request["json"]["client_secret"] == "sk_abdsomecharactersm284"
Expand Down Expand Up @@ -331,46 +331,38 @@ def test_complete_password_reset(self, capture_and_mock_request, mock_user):
assert request["json"]["token"] == token
assert request["json"]["new_password"] == new_password

def test_send_verification_email(
self, capture_and_mock_request, mock_magic_auth_challenge_response
):
def test_send_verification_email(self, capture_and_mock_request, mock_user):
user = "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"

url, _ = capture_and_mock_request(
"post", mock_magic_auth_challenge_response, 200
)
url, _ = capture_and_mock_request("post", mock_user, 200)

response = self.users.send_verification_email(user=user)

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

def test_verify_email(self, capture_and_mock_request, mock_user):
magic_auth_challenge_id = "auth_challenge_123"
user = "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"
code = "code_123"

url, _ = capture_and_mock_request("post", mock_user, 200)
url, request = capture_and_mock_request("post", mock_user, 200)

response = self.users.verify_email(
magic_auth_challenge_id=magic_auth_challenge_id, code=code
)
response = self.users.verify_email(user=user, code=code)

assert url[0].endswith("users/verify_email")
assert request["json"]["user_id"] == user
assert request["json"]["code"] == code
assert response["id"] == "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"

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

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

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

assert url[0].endswith("users/magic_auth/send")
assert request["json"]["email_address"] == email
assert response["id"] == "auth_challenge_01E4ZCR3C56J083X43JQXF3JK5"
assert response["id"] == "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"
13 changes: 0 additions & 13 deletions workos/resources/magic_auth_challenge_response.py

This file was deleted.

19 changes: 9 additions & 10 deletions workos/users.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import workos
from workos.resources.authentication_response import WorkOSAuthenticationResponse
from workos.resources.magic_auth_challenge_response import WorkOSMagicAuthChallenge
from workos.resources.password_challenge_response import WorkOSPasswordChallengeResponse
from workos.resources.list import WorkOSListResource
from workos.resources.users import (
Expand Down Expand Up @@ -198,7 +197,7 @@ def remove_user_from_organization(self, user, organization):
def authenticate_with_magic_auth(
self,
code,
magic_auth_challenge_id,
user,
expires_in=None,
ip_address=None,
user_agent=None,
Expand All @@ -207,7 +206,7 @@ def authenticate_with_magic_auth(

Kwargs:
code (str): The one-time code that was emailed to the user.
magic_auth_challenge_id (str): The challenge ID returned from when the one-time code was sent to the user.
user (str): The unique ID of the User who will be authenticated.
expires_in (int): The length of the session in minutes. Defaults to 1 day, 1440. (Optional)
ip_address (str): The IP address of the request from the user who is attempting to authenticate. (Optional)
user_agent (str): The user agent of the request from the user who is attempting to authenticate. (Optional)
Expand All @@ -224,7 +223,7 @@ def authenticate_with_magic_auth(
"client_id": workos.client_id,
"client_secret": workos.api_key,
"code": code,
"magic_auth_challenge_id": magic_auth_challenge_id,
"user_id": user,
"grant_type": "urn:workos:oauth:grant-type:magic-auth:code",
}

Expand Down Expand Up @@ -436,19 +435,19 @@ def send_verification_email(
token=workos.api_key,
)

return WorkOSMagicAuthChallenge.construct_from_response(response).to_dict()
return WorkOSUser.construct_from_response(response).to_dict()

def verify_email(
self,
magic_auth_challenge_id,
user,
code,
):
"""Verifies user email using one-time code that was sent to the user.

Kwargs:
magic_auth_challenge_id (str): The challenge ID returned from the send verification email endpoint.
user (str): The unique ID of the User whose email address will be verified.

code (str): The unique ID of the User whose email address will be verified.
code (str): The one-time code emailed to the user.

Returns:
dict: User response from WorkOS.
Expand All @@ -457,7 +456,7 @@ def verify_email(
headers = {}

payload = {
"magic_auth_challenge_id": magic_auth_challenge_id,
"user_id": user,
"code": code,
}

Expand Down Expand Up @@ -498,4 +497,4 @@ def send_magic_auth_code(
token=workos.api_key,
)

return WorkOSMagicAuthChallenge.construct_from_response(response).to_dict()
return WorkOSUser.construct_from_response(response).to_dict()