This repository was archived by the owner on Jan 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add new method signatures #105
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
08304de
feat: adds new factory method for PassageError
ctran88 0993fbb
feat: adds auth class to handle jwt validation and magic link creation
ctran88 63d222d
feat: adds user class to handle user operations
ctran88 441d672
feat: reworks passage class to be a wrapper around auth and user
ctran88 127f218
test: fix test assertions to match new error messages
ctran88 7acfc3a
fix: reverts audience validation to switch on host
ctran88 464f18d
fix: changes create magic link return to the full magic link response…
ctran88 62e2fc7
test: organizes test by class and adds one for mapping passage error
ctran88 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| """Provides the Auth class for interacting with the Passage API.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| import jwt | ||
| import jwt.algorithms | ||
|
|
||
| from passageidentity.errors import PassageError | ||
| from passageidentity.helper import fetch_app | ||
| from passageidentity.openapi_client.api.magic_links_api import MagicLinksApi | ||
| from passageidentity.openapi_client.exceptions import ApiException | ||
| from passageidentity.openapi_client.models.create_magic_link_request import CreateMagicLinkRequest | ||
|
|
||
| if TYPE_CHECKING: | ||
| from passageidentity.openapi_client.models.magic_link import MagicLink | ||
|
|
||
| CreateMagicLinkArgs = CreateMagicLinkRequest | ||
|
|
||
|
|
||
| class Auth: | ||
| """Auth class for handling operations to authenticate and validate JWTs.""" | ||
|
|
||
| def __init__(self, app_id: str, request_headers: dict[str, str]) -> None: | ||
| """Initialize the Auth class with the app ID and request headers.""" | ||
| self.app_id = app_id | ||
| self.request_headers = request_headers | ||
| self.jwks = jwt.PyJWKClient( | ||
| f"https://auth.passage.id/v1/apps/{self.app_id}/.well-known/jwks.json", | ||
| # must set a user agent to avoid 403 from CF | ||
| headers={"User-Agent": "passageidentity/python"}, | ||
| ) | ||
| self.app = fetch_app(self.app_id) | ||
bertrmz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| self.magic_links_api = MagicLinksApi() | ||
|
|
||
| def validate_jwt(self, token: str) -> str: | ||
| """Verify the JWT and return the user ID for the authenticated user, or throw a PassageError.""" | ||
| try: | ||
| kid = jwt.get_unverified_header(token)["kid"] | ||
| public_key = self.jwks.get_signing_key(kid) | ||
| claims = jwt.decode( | ||
| token, | ||
| public_key, | ||
| audience=[self.app_id] if self.app["hosted"] else self.app["auth_origin"], | ||
| algorithms=["RS256"], | ||
| ) | ||
|
|
||
| return claims["sub"] | ||
| except Exception as e: | ||
| msg = f"JWT is not valid: {e}" | ||
| raise PassageError(msg) from e | ||
|
|
||
| def create_magic_link(self, args: CreateMagicLinkArgs) -> MagicLink: | ||
| """Create a Magic Link for your app.""" | ||
| magic_link_req = {} | ||
| args_dict = args.to_dict() if isinstance(args, CreateMagicLinkRequest) else args | ||
|
|
||
| magic_link_req["user_id"] = args_dict.get("user_id") or "" | ||
| magic_link_req["email"] = args_dict.get("email") or "" | ||
| magic_link_req["phone"] = args_dict.get("phone") or "" | ||
|
|
||
| magic_link_req["language"] = args_dict.get("language") or "" | ||
| magic_link_req["magic_link_path"] = args_dict.get("magic_link_path") or "" | ||
| magic_link_req["redirect_url"] = args_dict.get("redirect_url") or "" | ||
| magic_link_req["send"] = args_dict.get("send") or False | ||
| magic_link_req["ttl"] = args_dict.get("ttl") or 0 | ||
| magic_link_req["type"] = args_dict.get("type") or "login" | ||
|
|
||
| if args_dict.get("email"): | ||
| magic_link_req["channel"] = args_dict.get("channel") or "email" | ||
| elif args_dict.get("phone"): | ||
| magic_link_req["channel"] = args_dict.get("channel") or "phone" | ||
|
|
||
| try: | ||
| return self.magic_links_api.create_magic_link( | ||
| self.app_id, | ||
| magic_link_req, # type: ignore[arg-type] | ||
| _headers=self.request_headers, | ||
| ).magic_link | ||
| except ApiException as e: | ||
| msg = "Could not create a magic link for this app" | ||
| raise PassageError.from_response_error(e, msg) from e | ||
This file contains hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.