Skip to content

Commit

Permalink
Fix authenticate methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatascastro12 committed Aug 25, 2023
1 parent 65d8130 commit 549c858
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 206 deletions.
18 changes: 3 additions & 15 deletions tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -250,7 +246,6 @@ def test_authenticate_with_password(
):
email = "[email protected]"
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"

Expand All @@ -259,44 +254,37 @@ 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"
assert request["json"]["grant_type"] == "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"

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

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"
Expand Down
7 changes: 0 additions & 7 deletions workos/resources/authentication_response.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from workos.resources.base import WorkOSBaseResource
from workos.resources.session import WorkOSSession
from workos.resources.users import WorkOSUser


Expand All @@ -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):
Expand All @@ -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
1 change: 0 additions & 1 deletion workos/resources/password_challenge_response.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from workos.resources.base import WorkOSBaseResource
from workos.resources.session import WorkOSSession
from workos.resources.users import WorkOSUser


Expand Down
159 changes: 0 additions & 159 deletions workos/resources/session.py

This file was deleted.

6 changes: 1 addition & 5 deletions workos/resources/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
23 changes: 4 additions & 19 deletions workos/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -210,7 +210,6 @@ def authenticate_with_magic_auth(
self,
code,
user,
expires_in=None,
ip_address=None,
user_agent=None,
):
Expand All @@ -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)
Expand All @@ -239,17 +237,14 @@ 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

if user_agent:
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,
Expand All @@ -261,7 +256,6 @@ def authenticate_with_password(
self,
email,
password,
expires_in=None,
ip_address=None,
user_agent=None,
):
Expand All @@ -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)
Expand All @@ -290,17 +283,14 @@ def authenticate_with_password(
"grant_type": "password",
}

if expires_in:
payload["expires_in"] = expires_in

if ip_address:
payload["ip_address"] = ip_address

if user_agent:
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,
Expand All @@ -311,7 +301,6 @@ def authenticate_with_password(
def authenticate_with_code(
self,
code,
expires_in=None,
ip_address=None,
user_agent=None,
):
Expand All @@ -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)
Expand All @@ -339,17 +327,14 @@ def authenticate_with_code(
"grant_type": "authorization_code",
}

if expires_in:
payload["expires_in"] = expires_in

if ip_address:
payload["ip_address"] = ip_address

if user_agent:
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,
Expand Down

0 comments on commit 549c858

Please sign in to comment.