Skip to content

Commit

Permalink
Add Users.authenticate_with_password()
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatascastro12 committed Aug 21, 2023
1 parent b8aa973 commit adb54f3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,39 @@ 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"]["ip_address"] == ip_address
assert request["json"]["client_id"] == "client_b27needthisforssotemxo"
assert request["json"]["client_secret"] == "sk_abdsomecharactersm284"
assert (
request["json"]["grant_type"]
== "urn:workos:oauth:grant-type:magic-auth:code"
)

def test_authenticate_with_password(
self, capture_and_mock_request, mock_auth_response
):
code = "test_auth"
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_password(
code=code,
expires_in=expires_in,
user_agent=user_agent,
ip_address=ip_address,
)

assert url[0].endswith("users/session/token")
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"
assert request["json"]["grant_type"] == "authorization_code"
48 changes: 48 additions & 0 deletions workos/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,51 @@ def authenticate_with_magic_auth(
)

return WorkOSAuthenticationResponse.construct_from_response(response).to_dict()

def authenticate_with_password(
self,
code,
expires_in=None,
ip_address=None,
user_agent=None,
):
"""Authenticates a user with email and password and optionally creates a session.
Kwargs:
code (str):
expires_in (int) (Optional)
ip_address (str) (Optional)
user_agent (str) (Optional)
Returns:
(dict): Authentication response from WorkOS.
[user] (dict): User response from WorkOS
[session] (dict): Session response from WorkOS
"""

headers = {}

payload = {
"client_id": workos.client_id,
"client_secret": workos.api_key,
"code": 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,
method=REQUEST_METHOD_POST,
headers=headers,
params=payload,
)

return WorkOSAuthenticationResponse.construct_from_response(response).to_dict()

0 comments on commit adb54f3

Please sign in to comment.