authentication
General authentication classes for external service interactions.
Classes
BearerAuthSession
class BearerAuthSession(bearer_token: str):Session implementation that uses bearer authentication and auto-retry.
Has no notion of token expiration or (re)authentication, simply using the
provided token as-is. If reauthentication is required,
see ExternallyManagedJWTSession.
Initializes a BearerAuthSession with the provided bearer token.
Ancestors
- requests.sessions.Session
- requests.sessions.SessionRedirectMixin
Methods
request
def request( self, method, url, params=None, data=None, headers=None, **kwargs,) ‑> requests.models.Response:Performs an HTTP request.
Overrides requests.session.request, appending our access token to the request headers or API keys if present.
ExternallyManagedJWT
class ExternallyManagedJWT( *, jwt: Optional[str] = None, expires: Optional[datetime] = None, get_token: Callable[[], tuple[str, datetime]],):Externally managed JWT.
Variables
- static
expires : Optional[datetime.datetime]
- static
get_token : Callable[[], tuple[str, datetime.datetime]]
- static
jwt : Optional[str]
ExternallyManagedJWTSession
class ExternallyManagedJWTSession( authentication_handler: GenericExternallyManagedJWTHandler,):Manages session-based interactions with authentication handled externally.
Extends requests.Session, appending an access token to the
authorization of any requests made if an access token is present
When the token expires it will request a new token prior to sending the web request.
Ancestors
- requests.sessions.Session
- requests.sessions.SessionRedirectMixin
Variables
authenticated : bool- Returns true if we have an unexpired access token.
-
request_headers : dict- Returns metadata for authenticating external service.Handles (re)authentication against the external service if needed.
Methods
authenticate
def authenticate(self) ‑> None:Authenticates session to allow protected requests.
request
def request( self, method, url, params=None, data=None, headers=None, **kwargs,) ‑> requests.models.Response:Performs an HTTP request.
Overrides requests.session.request, appending our access token to the request headers or API keys if present.
GenericExternallyManagedJWTHandler
class GenericExternallyManagedJWTHandler( jwt: Optional[str], expires: Optional[datetime], get_token: Callable[[], tuple[str, datetime]], **kwargs: Any,):Authenticates user via JWT from an external source.
The Bitfount library hands responsibility for management of the token to the external source.
Whenever a new token is needed it makes a call to the get_token
hook which provides one.
Creates a new instance of GenericExternallyManagedJWTHandler.
Can be supplied an initial JWT and expiration time to use or ignore these and rely on the provided hook to get the first token.
Arguments
jwt: Optional. Initial JWT token to use. If provided,expiresmust also be provided.expires: Optional. Initial expiration time to use. If provided,jwtmust also be provided.get_token: Callable that returns a tuple of a JWT and its expiration time.- **
**kwargs**: Other kwargs to be passed to the superclass.
Subclasses
Variables
authenticated : bool- Whether the token is still valid.
-
request_headers : dict- Header for authenticated requests.Checking that the call is authenticated is the responsibility of the calling code.
Raises: AuthenticationError: If the JWT is not present or is expired.
Methods
authenticate
def authenticate(self) ‑> None:Retrieves a token from the token source.
Calls the hook provided on object creation to retrieve a new token.
get_valid_token
def get_valid_token(self) ‑> str:Get a valid token to use.
JWT
class JWT(jwt: str, expires: datetime):Thin container for JSON web token and its expiry.