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

Add Users.authenticate_with_password() #197

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
32 changes: 32 additions & 0 deletions tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,42 @@ 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
):
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"

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

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 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"
51 changes: 51 additions & 0 deletions workos/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,54 @@ def authenticate_with_magic_auth(
)

return WorkOSAuthenticationResponse.construct_from_response(response).to_dict()

def authenticate_with_password(
self,
email,
password,
expires_in=None,
ip_address=None,
user_agent=None,
):
"""Authenticates a user with email and password and optionally creates a session.

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)

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,
"email": email,
"password": 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,
method=REQUEST_METHOD_POST,
headers=headers,
params=payload,
)

return WorkOSAuthenticationResponse.construct_from_response(response).to_dict()