From 549c858accdcf246aaa336cfd58bbd329cea373a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B4natas=20Santos?= Date: Fri, 25 Aug 2023 09:47:25 -0300 Subject: [PATCH] Fix authenticate methods --- tests/test_users.py | 18 +- workos/resources/authentication_response.py | 7 - .../resources/password_challenge_response.py | 1 - workos/resources/session.py | 159 ------------------ workos/resources/users.py | 6 +- workos/users.py | 23 +-- 6 files changed, 8 insertions(+), 206 deletions(-) delete mode 100644 workos/resources/session.py diff --git a/tests/test_users.py b/tests/test_users.py index 3e68dbcd..b8144067 100644 --- a/tests/test_users.py +++ b/tests/test_users.py @@ -215,7 +215,6 @@ def test_authenticate_with_magic_auth( self, capture_and_mock_request, mock_auth_response ): code = "test_auth" - expires_in = 3600 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" @@ -224,18 +223,15 @@ def test_authenticate_with_magic_auth( response = self.users.authenticate_with_magic_auth( code=code, - expires_in=expires_in, user=user, user_agent=user_agent, ip_address=ip_address, ) - assert url[0].endswith("users/session/token") + assert url[0].endswith("users/authenticate") assert response["user"]["id"] == "user_01H7ZGXFP5C6BBQY6Z7277ZCT0" - assert response["session"]["id"] == "session_01E4ZCR3C56J083X43JQXF3JK5" assert request["json"]["code"] == code assert request["json"]["user_agent"] == user_agent - assert request["json"]["expires_in"] == expires_in assert request["json"]["user_id"] == user assert request["json"]["ip_address"] == ip_address assert request["json"]["client_id"] == "client_b27needthisforssotemxo" @@ -250,7 +246,6 @@ def test_authenticate_with_password( ): email = "marcelina@foo-corp.com" password = "test123" - expires_in = 3600 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" @@ -259,18 +254,15 @@ def test_authenticate_with_password( response = self.users.authenticate_with_password( email=email, password=password, - expires_in=expires_in, user_agent=user_agent, ip_address=ip_address, ) - assert url[0].endswith("users/session/token") + assert url[0].endswith("users/authenticate") assert response["user"]["id"] == "user_01H7ZGXFP5C6BBQY6Z7277ZCT0" - assert response["session"]["id"] == "session_01E4ZCR3C56J083X43JQXF3JK5" assert request["json"]["email"] == email assert request["json"]["password"] == password assert request["json"]["user_agent"] == user_agent - assert request["json"]["expires_in"] == expires_in assert request["json"]["ip_address"] == ip_address assert request["json"]["client_id"] == "client_b27needthisforssotemxo" assert request["json"]["client_secret"] == "sk_abdsomecharactersm284" @@ -278,7 +270,6 @@ def test_authenticate_with_password( def test_authenticate_with_code(self, capture_and_mock_request, mock_auth_response): code = "test_code" - expires_in = 3600 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" @@ -286,17 +277,14 @@ def test_authenticate_with_code(self, capture_and_mock_request, mock_auth_respon response = self.users.authenticate_with_code( code=code, - expires_in=expires_in, user_agent=user_agent, ip_address=ip_address, ) - assert url[0].endswith("users/session/token") + assert url[0].endswith("users/authenticate") assert response["user"]["id"] == "user_01H7ZGXFP5C6BBQY6Z7277ZCT0" - assert response["session"]["id"] == "session_01E4ZCR3C56J083X43JQXF3JK5" assert request["json"]["code"] == code assert request["json"]["user_agent"] == user_agent - assert request["json"]["expires_in"] == expires_in assert request["json"]["ip_address"] == ip_address assert request["json"]["client_id"] == "client_b27needthisforssotemxo" assert request["json"]["client_secret"] == "sk_abdsomecharactersm284" diff --git a/workos/resources/authentication_response.py b/workos/resources/authentication_response.py index e79076ce..5db03989 100644 --- a/workos/resources/authentication_response.py +++ b/workos/resources/authentication_response.py @@ -1,5 +1,4 @@ from workos.resources.base import WorkOSBaseResource -from workos.resources.session import WorkOSSession from workos.resources.users import WorkOSUser @@ -15,9 +14,6 @@ def construct_from_response(cls, response): user = WorkOSUser.construct_from_response(response["user"]) authentication_response.user = user - session = WorkOSSession.construct_from_response(response["session"]) - authentication_response.session = session - return authentication_response def to_dict(self): @@ -28,7 +24,4 @@ def to_dict(self): user_dict = self.user.to_dict() authentication_response_dict["user"] = user_dict - session_dict = self.session.to_dict() - authentication_response_dict["session"] = session_dict - return authentication_response_dict diff --git a/workos/resources/password_challenge_response.py b/workos/resources/password_challenge_response.py index 386d3605..8d39b851 100644 --- a/workos/resources/password_challenge_response.py +++ b/workos/resources/password_challenge_response.py @@ -1,5 +1,4 @@ from workos.resources.base import WorkOSBaseResource -from workos.resources.session import WorkOSSession from workos.resources.users import WorkOSUser diff --git a/workos/resources/session.py b/workos/resources/session.py deleted file mode 100644 index 417ae99e..00000000 --- a/workos/resources/session.py +++ /dev/null @@ -1,159 +0,0 @@ -from workos.resources.base import WorkOSBaseResource - - -class WorkOSUserOrganization(WorkOSBaseResource): - """Contains the id and name of the associated Organization. - - Attributes: - OBJECT_FIELDS (list): List of fields a WorkOSUserOrganization comprises. - """ - - OBJECT_FIELDS = [ - "id", - "name", - ] - - -class WorkOSUnauthorizedOrganizationReason(WorkOSBaseResource): - """Contains the id and name of the associated Organization. - - Attributes: - OBJECT_FIELDS (list): List of fields a WorkOSUnauthorizedOrganizationReason comprises. - """ - - OBJECT_FIELDS = [ - "type", - "allowed_authentication_methods", - ] - - -class WorkOSAuthorizedOrganization(WorkOSBaseResource): - """Contains the id and name of the associated Organization. - - Attributes: - OBJECT_FIELDS (list): List of fields a WorkOSUserOrganization comprises. - """ - - @classmethod - def construct_from_response(cls, response): - authorized_organization = super( - WorkOSAuthorizedOrganization, cls - ).construct_from_response(response) - - organization = WorkOSUserOrganization.construct_from_response( - response["organization"] - ) - authorized_organization.organization = organization - - return authorized_organization - - def to_dict(self): - authorized_organizations_dict = super( - WorkOSAuthorizedOrganization, self - ).to_dict() - - organization_dict = self.organization.to_dict() - authorized_organizations_dict["organization"] = organization_dict - - return authorized_organizations_dict - - -class WorkOSUnauthorizedOrganization(WorkOSBaseResource): - """Contains the unauthorized organization and reasons for the non authorization. - - Attributes: - OBJECT_FIELDS (list): List of fields a WorkOSUserOrganization comprises. - """ - - @classmethod - def construct_from_response(cls, response): - unauthorized_organization = super( - WorkOSUnauthorizedOrganization, cls - ).construct_from_response(response) - - organization = WorkOSUserOrganization.construct_from_response( - response["organization"] - ) - unauthorized_organization.organization = organization - - unauthorized_organization.reason = [] - - for reason in response["reasons"]: - reason = WorkOSUnauthorizedOrganizationReason.construct_from_response( - reason - ) - unauthorized_organization.reason.append(reason) - - return unauthorized_organization - - def to_dict(self): - unauthorized_organizations_dict = super( - WorkOSUnauthorizedOrganization, self - ).to_dict() - - organization_dict = self.organization.to_dict() - unauthorized_organizations_dict["organization"] = organization_dict - - unauthorized_organizations_dict["reasons"] = [] - - for reason in self.reason: - reason_dict = reason.to_dict() - unauthorized_organizations_dict["reasons"].append(reason_dict) - - return unauthorized_organizations_dict - - -class WorkOSSession(WorkOSBaseResource): - """Representation of a Session response as returned by WorkOS through User Management features. - - Attributes: - OBJECT_FIELDS (list): List of fields a class WorkOSSession comprises. - """ - - OBJECT_FIELDS = [ - "id", - "token", - "created_at", - "updated_at", - ] - - @classmethod - def construct_from_response(cls, response): - session = super(WorkOSSession, cls).construct_from_response(response) - - session.authorized_organizations = [] - session.unauthorized_organizations = [] - - for authorized_organization in response["authorized_organizations"]: - authorized_organization = ( - WorkOSAuthorizedOrganization.construct_from_response( - authorized_organization - ) - ) - session.authorized_organizations.append(authorized_organization) - - for unauthorized_organization in response["unauthorized_organizations"]: - unauthorized_organization = ( - WorkOSUnauthorizedOrganization.construct_from_response( - unauthorized_organization - ) - ) - session.unauthorized_organizations.append(unauthorized_organization) - - return session - - def to_dict(self): - session_dict = super(WorkOSSession, self).to_dict() - - session_dict["authorized_organizations"] = [] - session_dict["unauthorized_organizations"] = [] - - for organization in self.authorized_organizations: - organization_dict = organization.to_dict() - session_dict["authorized_organizations"].append(organization_dict) - - for organization in self.unauthorized_organizations: - organization_dict = organization.to_dict() - session_dict["unauthorized_organizations"].append(organization_dict) - - return session_dict diff --git a/workos/resources/users.py b/workos/resources/users.py index 052720be..786c3f14 100644 --- a/workos/resources/users.py +++ b/workos/resources/users.py @@ -13,11 +13,7 @@ class WorkOSUser(WorkOSBaseResource): "email", "first_name", "last_name", - "user_type", - "sso_profile_id", - "email_verified_at", - "google_oauth_profile_id", - "microsoft_oauth_profile_id", + "email_verified", "created_at", "updated_at", ] diff --git a/workos/users.py b/workos/users.py index fc009b07..e397afc1 100644 --- a/workos/users.py +++ b/workos/users.py @@ -17,7 +17,7 @@ USER_PATH = "users" USER_DETAIL_PATH = "users/{0}" USER_ORGANIZATION_PATH = "users/{0}/organization/{1}" -USER_SESSION_TOKEN_PATH = "users/session/token" +USER_AUTHENTICATE_PATH = "users/authenticate" USER_PASSWORD_RESET_CHALLENGE_PATH = "users/password_reset_challenge" USER_PASSWORD_RESET_PATH = "users/password_reset" USER_SEND_VERIFICATION_EMAIL_PATH = "users/{0}/send_verification_email" @@ -210,7 +210,6 @@ def authenticate_with_magic_auth( self, code, user, - expires_in=None, ip_address=None, user_agent=None, ): @@ -219,7 +218,6 @@ def authenticate_with_magic_auth( Kwargs: code (str): The one-time code that was emailed 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) @@ -239,9 +237,6 @@ def authenticate_with_magic_auth( "grant_type": "urn:workos:oauth:grant-type:magic-auth:code", } - if expires_in: - payload["expires_in"] = expires_in - if ip_address: payload["ip_address"] = ip_address @@ -249,7 +244,7 @@ def authenticate_with_magic_auth( payload["user_agent"] = user_agent response = self.request_helper.request( - USER_SESSION_TOKEN_PATH, + USER_AUTHENTICATE_PATH, method=REQUEST_METHOD_POST, headers=headers, params=payload, @@ -261,7 +256,6 @@ def authenticate_with_password( self, email, password, - expires_in=None, ip_address=None, user_agent=None, ): @@ -270,7 +264,6 @@ def authenticate_with_password( Kwargs: email (str): The email address of the user. password (str): The password of the user. - 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) @@ -290,9 +283,6 @@ def authenticate_with_password( "grant_type": "password", } - if expires_in: - payload["expires_in"] = expires_in - if ip_address: payload["ip_address"] = ip_address @@ -300,7 +290,7 @@ def authenticate_with_password( payload["user_agent"] = user_agent response = self.request_helper.request( - USER_SESSION_TOKEN_PATH, + USER_AUTHENTICATE_PATH, method=REQUEST_METHOD_POST, headers=headers, params=payload, @@ -311,7 +301,6 @@ def authenticate_with_password( def authenticate_with_code( self, code, - expires_in=None, ip_address=None, user_agent=None, ): @@ -320,7 +309,6 @@ def authenticate_with_code( Kwargs: code (str): The authorization value which was passed back as a query parameter in the callback to the Redirect URI. - 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) @@ -339,9 +327,6 @@ def authenticate_with_code( "grant_type": "authorization_code", } - if expires_in: - payload["expires_in"] = expires_in - if ip_address: payload["ip_address"] = ip_address @@ -349,7 +334,7 @@ def authenticate_with_code( payload["user_agent"] = user_agent response = self.request_helper.request( - USER_SESSION_TOKEN_PATH, + USER_AUTHENTICATE_PATH, method=REQUEST_METHOD_POST, headers=headers, params=payload,