Skip to content

Commit bca966b

Browse files
authored
Add API changes for invitations and Magic Auth (#262)
* Add events and API changes for invitations and Magic Auth * Check new invitation attribute in test
1 parent 91eb847 commit bca966b

File tree

5 files changed

+136
-2
lines changed

5 files changed

+136
-2
lines changed

tests/test_user_management.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from tests.utils.fixtures.mock_auth_factor_totp import MockAuthFactorTotp
77
from tests.utils.fixtures.mock_invitation import MockInvitation
8+
from tests.utils.fixtures.mock_magic_auth import MockMagicAuth
89
from tests.utils.fixtures.mock_organization_membership import MockOrganizationMembership
910
from tests.utils.fixtures.mock_session import MockSession
1011
from tests.utils.fixtures.mock_user import MockUser
@@ -212,6 +213,10 @@ def mock_auth_factors(self):
212213
}
213214
return dict_response
214215

216+
@pytest.fixture
217+
def mock_magic_auth(self):
218+
return MockMagicAuth("magic_auth_ABCDE").to_dict()
219+
215220
@pytest.fixture
216221
def mock_invitation(self):
217222
return MockInvitation("invitation_ABCDE").to_dict()
@@ -847,12 +852,33 @@ def test_verify_email(self, capture_and_mock_request, mock_user):
847852
assert request["json"]["code"] == code
848853
assert response["id"] == "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"
849854

855+
def test_get_magic_auth(self, mock_magic_auth, capture_and_mock_request):
856+
url, request_kwargs = capture_and_mock_request("get", mock_magic_auth, 200)
857+
858+
magic_auth = self.user_management.get_magic_auth("magic_auth_ABCDE")
859+
860+
assert url[0].endswith("user_management/magic_auth/magic_auth_ABCDE")
861+
assert magic_auth["id"] == "magic_auth_ABCDE"
862+
863+
def test_create_magic_auth(self, capture_and_mock_request, mock_magic_auth):
864+
865+
url, _ = capture_and_mock_request("post", mock_magic_auth, 201)
866+
867+
magic_auth = self.user_management.create_magic_auth(email=email)
868+
869+
assert url[0].endswith("user_management/magic_auth")
870+
assert magic_auth["email"] == email
871+
850872
def test_send_magic_auth_code(self, capture_and_mock_request):
851873
852874

853875
url, request = capture_and_mock_request("post", None, 200)
854876

855-
response = self.user_management.send_magic_auth_code(email=email)
877+
with pytest.warns(
878+
DeprecationWarning,
879+
match="'send_magic_auth_code' is deprecated. Please use 'create_magic_auth' instead. This method will be removed in a future major version.",
880+
):
881+
response = self.user_management.send_magic_auth_code(email=email)
856882

857883
assert url[0].endswith("user_management/magic_auth/send")
858884
assert request["json"]["email"] == email

tests/utils/fixtures/mock_invitation.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ def __init__(self, id):
1010
self.accepted_at = None
1111
self.revoked_at = None
1212
self.expires_at = datetime.datetime.now()
13-
self.token = "ABCDE12345"
13+
self.token = "Z1uX3RbwcIl5fIGJJJCXXisdI"
14+
self.accept_invitation_url = (
15+
"https://myauthkit.com/invite?invitation_token=Z1uX3RbwcIl5fIGJJJCXXisdI"
16+
)
1417
self.organization_id = "org_12345"
1518
self.created_at = datetime.datetime.now()
1619
self.updated_at = datetime.datetime.now()
@@ -23,6 +26,7 @@ def __init__(self, id):
2326
"revoked_at",
2427
"expires_at",
2528
"token",
29+
"accept_invitation_url",
2630
"organization_id",
2731
"created_at",
2832
"updated_at",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import datetime
2+
from workos.resources.base import WorkOSBaseResource
3+
4+
5+
class MockMagicAuth(WorkOSBaseResource):
6+
def __init__(self, id):
7+
self.id = id
8+
self.user_id = "user_01HWZBQAY251RZ9BKB4RZW4D4A"
9+
self.email = "[email protected]"
10+
self.expires_at = datetime.datetime.now()
11+
self.code = "123456"
12+
self.created_at = datetime.datetime.now()
13+
self.updated_at = datetime.datetime.now()
14+
15+
OBJECT_FIELDS = [
16+
"id",
17+
"user_id",
18+
"email",
19+
"expires_at",
20+
"code",
21+
"created_at",
22+
"updated_at",
23+
]

workos/resources/user_management.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,31 @@ class WorkOSInvitation(WorkOSBaseResource):
9090
"revoked_at",
9191
"expires_at",
9292
"token",
93+
"accept_invitation_url",
9394
"organization_id",
9495
"created_at",
9596
"updated_at",
9697
]
9798

9899

100+
class WorkOSMagicAuth(WorkOSBaseResource):
101+
"""Representation of a MagicAuth object as returned by WorkOS through User Management features.
102+
103+
Attributes:
104+
OBJECT_FIELDS (list): List of fields a WorkOSMagicAuth comprises.
105+
"""
106+
107+
OBJECT_FIELDS = [
108+
"id",
109+
"user_id",
110+
"email",
111+
"expires_at",
112+
"code",
113+
"created_at",
114+
"updated_at",
115+
]
116+
117+
99118
class WorkOSOrganizationMembership(WorkOSBaseResource):
100119
"""Representation of an Organization Membership as returned by WorkOS through User Management features.
101120

workos/user_management.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from requests import Request
2+
from warnings import warn
23
import workos
34
from workos.resources.list import WorkOSListResource
45
from workos.resources.mfa import WorkOSAuthenticationFactorTotp, WorkOSChallenge
56
from workos.resources.user_management import (
67
WorkOSAuthenticationResponse,
78
WorkOSRefreshTokenAuthenticationResponse,
89
WorkOSInvitation,
10+
WorkOSMagicAuth,
911
WorkOSOrganizationMembership,
1012
WorkOSPasswordChallengeResponse,
1113
WorkOSUser,
@@ -32,6 +34,8 @@
3234
USER_RESET_PASSWORD_PATH = "user_management/password_reset/confirm"
3335
USER_SEND_VERIFICATION_EMAIL_PATH = "user_management/users/{0}/email_verification/send"
3436
USER_VERIFY_EMAIL_CODE_PATH = "user_management/users/{0}/email_verification/confirm"
37+
MAGIC_AUTH_DETAIL_PATH = "user_management/magic_auth/{0}"
38+
MAGIC_AUTH_PATH = "user_management/magic_auth"
3539
USER_SEND_MAGIC_AUTH_PATH = "user_management/magic_auth/send"
3640
USER_AUTH_FACTORS_PATH = "user_management/users/{0}/auth_factors"
3741
INVITATION_PATH = "user_management/invitations"
@@ -872,16 +876,74 @@ def verify_email(
872876

873877
return WorkOSUser.construct_from_response(response["user"]).to_dict()
874878

879+
def get_magic_auth(self, magic_auth_id):
880+
"""Get the details of a Magic Auth object.
881+
882+
Args:
883+
magic_auth_id (str) - The unique ID of the Magic Auth object.
884+
885+
Returns:
886+
dict: MagicAuth response from WorkOS.
887+
"""
888+
headers = {}
889+
890+
response = self.request_helper.request(
891+
MAGIC_AUTH_DETAIL_PATH.format(magic_auth_id),
892+
method=REQUEST_METHOD_GET,
893+
headers=headers,
894+
token=workos.api_key,
895+
)
896+
897+
return WorkOSMagicAuth.construct_from_response(response).to_dict()
898+
899+
def create_magic_auth(
900+
self,
901+
email,
902+
invitation_token=None,
903+
):
904+
"""Creates a Magic Auth code challenge that can be sent to a user's email for authentication.
905+
906+
Args:
907+
email: The email address of the user.
908+
invitation_token: The token of an Invitation, if required. (Optional)
909+
910+
Returns:
911+
dict: MagicAuth response from WorkOS.
912+
"""
913+
headers = {}
914+
915+
params = {
916+
"email": email,
917+
"invitation_token": invitation_token,
918+
}
919+
920+
response = self.request_helper.request(
921+
MAGIC_AUTH_PATH,
922+
method=REQUEST_METHOD_POST,
923+
params=params,
924+
headers=headers,
925+
token=workos.api_key,
926+
)
927+
928+
return WorkOSMagicAuth.construct_from_response(response).to_dict()
929+
875930
def send_magic_auth_code(
876931
self,
877932
email,
878933
):
879934
"""Creates a one-time Magic Auth code and emails it to the user.
880935
936+
Deprecated: Please use `create_magic_auth` instead. This method will be removed in a future major version.
937+
881938
Kwargs:
882939
email (str): The email address the one-time code will be sent to.
883940
"""
884941

942+
warn(
943+
"'send_magic_auth_code' is deprecated. Please use 'create_magic_auth' instead. This method will be removed in a future major version.",
944+
DeprecationWarning,
945+
)
946+
885947
headers = {}
886948

887949
payload = {

0 commit comments

Comments
 (0)