-
Notifications
You must be signed in to change notification settings - Fork 11
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
OBACK-290: Fix issue with python Authenticate JWT locally logic #209
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
GetJWKSResponse, | ||
GetResponse, | ||
RevokeResponse, | ||
Session, | ||
Session, AuthenticateJWTLocalResponse, | ||
) | ||
from stytch.core.api_base import ApiBase | ||
from stytch.core.http.client import AsyncClient, SyncClient | ||
|
@@ -24,12 +24,12 @@ | |
|
||
class Sessions: | ||
def __init__( | ||
self, | ||
api_base: ApiBase, | ||
sync_client: SyncClient, | ||
async_client: AsyncClient, | ||
jwks_client: jwt.PyJWKClient, | ||
project_id: str, | ||
self, | ||
api_base: ApiBase, | ||
sync_client: SyncClient, | ||
async_client: AsyncClient, | ||
jwks_client: jwt.PyJWKClient, | ||
project_id: str, | ||
) -> None: | ||
self.api_base = api_base | ||
self.sync_client = sync_client | ||
|
@@ -38,8 +38,8 @@ def __init__( | |
self.project_id = project_id | ||
|
||
def get( | ||
self, | ||
user_id: str, | ||
self, | ||
user_id: str, | ||
) -> GetResponse: | ||
"""List all active Sessions for a given `user_id`. All timestamps are formatted according to the RFC 3339 standard and are expressed in UTC, e.g. `2021-12-29T12:33:09Z`. | ||
|
||
|
@@ -56,8 +56,8 @@ def get( | |
return GetResponse.from_json(res.response.status_code, res.json) | ||
|
||
async def get_async( | ||
self, | ||
user_id: str, | ||
self, | ||
user_id: str, | ||
) -> GetResponse: | ||
"""List all active Sessions for a given `user_id`. All timestamps are formatted according to the RFC 3339 standard and are expressed in UTC, e.g. `2021-12-29T12:33:09Z`. | ||
|
||
|
@@ -74,11 +74,11 @@ async def get_async( | |
return GetResponse.from_json(res.response.status, res.json) | ||
|
||
def authenticate( | ||
self, | ||
session_token: Optional[str] = None, | ||
session_duration_minutes: Optional[int] = None, | ||
session_jwt: Optional[str] = None, | ||
session_custom_claims: Optional[Dict[str, Any]] = None, | ||
self, | ||
session_token: Optional[str] = None, | ||
session_duration_minutes: Optional[int] = None, | ||
session_jwt: Optional[str] = None, | ||
session_custom_claims: Optional[Dict[str, Any]] = None, | ||
) -> AuthenticateResponse: | ||
"""Authenticate a session token or session JWT and retrieve associated session data. If `session_duration_minutes` is included, update the lifetime of the session to be that many minutes from now. All timestamps are formatted according to the RFC 3339 standard and are expressed in UTC, e.g. `2021-12-29T12:33:09Z`. This endpoint requires exactly one `session_jwt` or `session_token` as part of the request. If both are included, you will receive a `too_many_session_arguments` error. | ||
|
||
|
@@ -108,11 +108,11 @@ def authenticate( | |
return AuthenticateResponse.from_json(res.response.status_code, res.json) | ||
|
||
async def authenticate_async( | ||
self, | ||
session_token: Optional[str] = None, | ||
session_duration_minutes: Optional[int] = None, | ||
session_jwt: Optional[str] = None, | ||
session_custom_claims: Optional[Dict[str, Any]] = None, | ||
self, | ||
session_token: Optional[str] = None, | ||
session_duration_minutes: Optional[int] = None, | ||
session_jwt: Optional[str] = None, | ||
session_custom_claims: Optional[Dict[str, Any]] = None, | ||
) -> AuthenticateResponse: | ||
"""Authenticate a session token or session JWT and retrieve associated session data. If `session_duration_minutes` is included, update the lifetime of the session to be that many minutes from now. All timestamps are formatted according to the RFC 3339 standard and are expressed in UTC, e.g. `2021-12-29T12:33:09Z`. This endpoint requires exactly one `session_jwt` or `session_token` as part of the request. If both are included, you will receive a `too_many_session_arguments` error. | ||
|
||
|
@@ -142,10 +142,10 @@ async def authenticate_async( | |
return AuthenticateResponse.from_json(res.response.status, res.json) | ||
|
||
def revoke( | ||
self, | ||
session_id: Optional[str] = None, | ||
session_token: Optional[str] = None, | ||
session_jwt: Optional[str] = None, | ||
self, | ||
session_id: Optional[str] = None, | ||
session_token: Optional[str] = None, | ||
session_jwt: Optional[str] = None, | ||
) -> RevokeResponse: | ||
"""Revoke a Session, immediately invalidating all of its session tokens. You can revoke a session in three ways: using its ID, or using one of its session tokens, or one of its JWTs. This endpoint requires exactly one of those to be included in the request. It will return an error if multiple are present. | ||
|
||
|
@@ -168,10 +168,10 @@ def revoke( | |
return RevokeResponse.from_json(res.response.status_code, res.json) | ||
|
||
async def revoke_async( | ||
self, | ||
session_id: Optional[str] = None, | ||
session_token: Optional[str] = None, | ||
session_jwt: Optional[str] = None, | ||
self, | ||
session_id: Optional[str] = None, | ||
session_token: Optional[str] = None, | ||
session_jwt: Optional[str] = None, | ||
) -> RevokeResponse: | ||
"""Revoke a Session, immediately invalidating all of its session tokens. You can revoke a session in three ways: using its ID, or using one of its session tokens, or one of its JWTs. This endpoint requires exactly one of those to be included in the request. It will return an error if multiple are present. | ||
|
||
|
@@ -194,8 +194,8 @@ async def revoke_async( | |
return RevokeResponse.from_json(res.response.status, res.json) | ||
|
||
def get_jwks( | ||
self, | ||
project_id: str, | ||
self, | ||
project_id: str, | ||
) -> GetJWKSResponse: | ||
"""Get the JSON Web Key Set (JWKS) for a project. | ||
|
||
|
@@ -222,8 +222,8 @@ def get_jwks( | |
return GetJWKSResponse.from_json(res.response.status_code, res.json) | ||
|
||
async def get_jwks_async( | ||
self, | ||
project_id: str, | ||
self, | ||
project_id: str, | ||
) -> GetJWKSResponse: | ||
"""Get the JSON Web Key Set (JWKS) for a project. | ||
|
||
|
@@ -254,11 +254,11 @@ async def get_jwks_async( | |
# ADDIMPORT: import jwt | ||
# ADDIMPORT: import time | ||
def authenticate_jwt( | ||
self, | ||
session_jwt: str, | ||
max_token_age_seconds: Optional[int] = None, | ||
session_custom_claims: Optional[Dict[str, Any]] = None, | ||
) -> Optional[Session]: | ||
self, | ||
session_jwt: str, | ||
max_token_age_seconds: Optional[int] = None, | ||
session_custom_claims: Optional[Dict[str, Any]] = None, | ||
) -> Optional[AuthenticateJWTLocalResponse]: | ||
"""Parse a JWT and verify the signature, preferring local verification | ||
over remote. | ||
|
||
|
@@ -269,22 +269,41 @@ def authenticate_jwt( | |
zero or use the authenticate method instead. | ||
""" | ||
# Return the local_result if available, otherwise call the Stytch API | ||
return ( | ||
self.authenticate_jwt_local( | ||
session_jwt=session_jwt, | ||
max_token_age_seconds=max_token_age_seconds, | ||
# Return the local_result if available, otherwise call the Stytch API | ||
local_token = self.authenticate_jwt_local( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [dust] |
||
session_jwt=session_jwt, | ||
max_token_age_seconds=max_token_age_seconds, | ||
) | ||
if local_token is not None: | ||
return AuthenticateJWTLocalResponse.from_json( | ||
status_code=200, | ||
json={ | ||
"session": local_token, | ||
"session_jwt": session_jwt, | ||
"status_code": 200, | ||
"request_id": "", | ||
}, | ||
) | ||
or self.authenticate( | ||
else: | ||
authenticate_response = self.authenticate( | ||
session_custom_claims=session_custom_claims, session_jwt=session_jwt | ||
).session | ||
) | ||
) | ||
return AuthenticateJWTLocalResponse.from_json( | ||
status_code=authenticate_response.status_code, | ||
json={ | ||
"session": authenticate_response.session, | ||
"session_jwt": authenticate_response.session_jwt, | ||
"status_code": authenticate_response.status_code, | ||
"request_id": authenticate_response.request_id, | ||
} | ||
) | ||
|
||
async def authenticate_jwt_async( | ||
self, | ||
session_jwt: str, | ||
max_token_age_seconds: Optional[int] = None, | ||
session_custom_claims: Optional[Dict[str, Any]] = None, | ||
) -> Optional[Session]: | ||
self, | ||
session_jwt: str, | ||
max_token_age_seconds: Optional[int] = None, | ||
session_custom_claims: Optional[Dict[str, Any]] = None, | ||
) -> Optional[AuthenticateJWTLocalResponse]: | ||
"""Parse a JWT and verify the signature, preferring local verification | ||
over remote. | ||
|
||
|
@@ -295,28 +314,44 @@ async def authenticate_jwt_async( | |
zero or use the authenticate method instead. | ||
""" | ||
# Return the local_result if available, otherwise call the Stytch API | ||
return ( | ||
self.authenticate_jwt_local( | ||
session_jwt=session_jwt, | ||
max_token_age_seconds=max_token_age_seconds, | ||
) | ||
or ( | ||
await self.authenticate_async( | ||
session_custom_claims=session_custom_claims, session_jwt=session_jwt | ||
) | ||
).session | ||
local_token = self.authenticate_jwt_local( | ||
session_jwt=session_jwt, | ||
max_token_age_seconds=max_token_age_seconds, | ||
) | ||
if local_token is not None: | ||
return AuthenticateJWTLocalResponse.from_json( | ||
status_code=200, | ||
json={ | ||
"session": local_token, | ||
"session_jwt": session_jwt, | ||
"status_code": 200, | ||
"request_id": "", | ||
}, | ||
) | ||
else: | ||
authenticate_response = await self.authenticate_async( | ||
session_custom_claims=session_custom_claims, session_jwt=session_jwt | ||
) | ||
return AuthenticateJWTLocalResponse.from_json( | ||
status_code=authenticate_response.status_code, | ||
json={ | ||
"session": authenticate_response.session, | ||
"session_jwt": authenticate_response.session_jwt, | ||
"status_code": authenticate_response.status_code, | ||
"request_id": authenticate_response.request_id, | ||
} | ||
) | ||
|
||
# ENDMANUAL(authenticate_jwt) | ||
|
||
# MANUAL(authenticate_jwt_local)(SERVICE_METHOD) | ||
# ADDIMPORT: from stytch.consumer.models.sessions import Session | ||
# ADDIMPORT: from stytch.shared import jwt_helpers | ||
def authenticate_jwt_local( | ||
self, | ||
session_jwt: str, | ||
max_token_age_seconds: Optional[int] = None, | ||
leeway: int = 0, | ||
self, | ||
session_jwt: str, | ||
max_token_age_seconds: Optional[int] = None, | ||
leeway: int = 0, | ||
) -> Optional[Session]: | ||
_session_claim = "https://stytch.com/session" | ||
generic_claims = jwt_helpers.authenticate_jwt_local( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "10.1.0" | ||
__version__ = "11.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import unittest | ||
from time import sleep | ||
|
||
from test.constants import ( | ||
TEST_CRYPTO_SIGNATURE, | ||
TEST_CRYPTO_WALLET_ADDRESS, | ||
|
@@ -17,7 +19,7 @@ | |
TEST_TOTP_CODE, | ||
TEST_TOTP_RECOVERY_CODE, | ||
TEST_TOTP_USER_ID, | ||
TEST_USERS_NAME, | ||
TEST_USERS_NAME, TEST_EXPIRED_JWT, | ||
) | ||
from test.integration_base import CreatedTestUser, IntegrationTestBase | ||
|
||
|
@@ -232,6 +234,21 @@ def test_webauthn(self) -> None: | |
# TODO: No test public key credential (see skipTest above) | ||
self.assertTrue(api.authenticate(public_key_credential="").is_success) | ||
|
||
def test_authenticate(self) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sweet! Thanks for adding a test! |
||
api = self.b2c_client.sessions | ||
|
||
with self._get_temporary_user() as user: | ||
assert isinstance(user, CreatedTestUser) | ||
self.assertTrue(api.get(user_id=user.user_id).is_success) | ||
# Grab a recent JWT token and verify it's valid | ||
auth_response = api.authenticate(session_token=TEST_SESSION_TOKEN) | ||
response = self.b2c_client.sessions.authenticate_jwt(session_jwt=auth_response.session_jwt) | ||
self.assertEquals(auth_response.session_jwt, response.session_jwt) | ||
|
||
def test_authenticate_jwt_local_returns_none_for_expired_token(self) -> None: | ||
api = self.b2c_client.sessions | ||
self.assertIsNone(api.authenticate_jwt_local(session_jwt=TEST_EXPIRED_JWT)) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicated comment?