Mixin class to add token issuance and validation capabilities
using a TokenFactory.
Source code in src/alpha/mixins/jwt_provider.py
| class JWTProviderMixin:
"""Mixin class to add token issuance and validation capabilities
using a TokenFactory.
"""
token_factory: TokenFactory | None = None
def validate(self, token: Token) -> Identity:
"""Validate a token and return the associated identity
Parameters
----------
token
Token object to be validated
Returns
-------
Identity object representing the subject
"""
if not self.token_factory:
raise exceptions.MissingDependencyException(
"Token factory is not configured"
)
try:
self.token_factory.validate(token.value)
except Exception as e:
raise e
payload = self.token_factory.get_payload(token.value)
subject = payload.get("subject")
if not subject:
raise ValueError(
"Token payload does not contain mandatory 'subject' field"
)
return Identity.from_dict(payload)
def issue_token(self, identity: Identity) -> Token:
"""Issue a token for the given identity
Parameters
----------
identity
Identity object for which to issue a token
Returns
-------
Token object
"""
if not self.token_factory:
raise exceptions.MissingDependencyException(
"Token factory is not configured"
)
payload = identity.to_dict()
token_value = self.token_factory.create(
subject=identity.subject, payload=payload
)
return Token(token_value, token_type="Bearer")
|
Methods:
validate
Validate a token and return the associated identity
| Parameters: |
-
token
(Token)
–
Token object to be validated
|
| Returns: |
-
Identity object representing the subject
–
|
Source code in src/alpha/mixins/jwt_provider.py
| def validate(self, token: Token) -> Identity:
"""Validate a token and return the associated identity
Parameters
----------
token
Token object to be validated
Returns
-------
Identity object representing the subject
"""
if not self.token_factory:
raise exceptions.MissingDependencyException(
"Token factory is not configured"
)
try:
self.token_factory.validate(token.value)
except Exception as e:
raise e
payload = self.token_factory.get_payload(token.value)
subject = payload.get("subject")
if not subject:
raise ValueError(
"Token payload does not contain mandatory 'subject' field"
)
return Identity.from_dict(payload)
|
issue_token
Issue a token for the given identity
| Parameters: |
-
identity
(Identity)
–
Identity object for which to issue a token
|
Source code in src/alpha/mixins/jwt_provider.py
| def issue_token(self, identity: Identity) -> Token:
"""Issue a token for the given identity
Parameters
----------
identity
Identity object for which to issue a token
Returns
-------
Token object
"""
if not self.token_factory:
raise exceptions.MissingDependencyException(
"Token factory is not configured"
)
payload = identity.to_dict()
token_value = self.token_factory.create(
subject=identity.subject, payload=payload
)
return Token(token_value, token_type="Bearer")
|