HTTPSciTokenAuth¶
- class requests_scitokens.HTTPSciTokenAuth(token=None, audience=None, *, force=None)[source]¶
Bases:
AuthBaseAuth handler for SciTokens.
- Parameters:
token (
scitokens.SciToken,None,True) – TheSciTokento use for bearer authorisation. Default (None) will dynamically discover a token for each request.token=Trueis an alias fortoken=None, force=True.audience (
str, optional) – Theaudclaim to require when discovering tokens.force (
bool, optional) –If
True, preemptively discover a token and generate anAuthorizationheader for all requests.If
False, wait for the remote server to respond with an authorisation challenge via a 401Unauthorizedresponse that includes aWWW-Authenticateheader.
Methods Summary
__call__(request)Augment the
Requestrequestwith anAuthorizationheader.authenticate_bearer(response, **kwargs)Re-send a request in response to a Bearer challenge.
find_token([url, error, find_func])Find a bearer token for authorization.
generate_bearer_header([response])Generate a bearer token header, possibly based on a response.
handle_401(response, **kwargs)Handle 401 response.
handle_response(response, **kwargs)Handle a response.
Methods Documentation
- __call__(request)[source]¶
Augment the
Requestrequestwith anAuthorizationheader.- Parameters:
request (
requests.Request) – The request to authorise.- Returns:
request (
requests.Request) – The original request, which may now have an HTTPAuthorizationheader attached.
- authenticate_bearer(response, **kwargs)[source]¶
Re-send a request in response to a Bearer challenge.
This method attemps to find a token and construct an HTTP Authorization header for the response endpoint. If that is successful, a new request will be sent using the header.
- Parameters:
response (
requests.Response) – The HTTP response to handle.kwargs – Other keyword arguments are passed to the new request.
- Returns:
response (
requests.Response)
- find_token(url=None, *, error=True, find_func=<function SciToken.discover>, **discover_kwargs)[source]¶
Find a bearer token for authorization.
- Parameters:
- Returns:
token (
scitokens.SciToken,None) – The discovered token, orNoneif a token is not foudn anderror=Falseis given.- Raises:
OSError – If
error=Trueis given (default) and a token discovery fails.
- generate_bearer_header(response=None)[source]¶
Generate a bearer token header, possibly based on a response.
- Parameters:
response (
requests.Response, optional) – The HTTP response to handle.- Returns:
auth (
strorNone) – The value of the HTTP Authorization header to apply, orNoneif not found or needed.
- handle_401(response, **kwargs)[source]¶
Handle 401 response.
- Parameters:
response (
requests.Response) – The HTTP response to handle.kwargs – Other keyword arguments are passed to the new request.
- Returns:
response (
requests.Response) – Either the originalresponsegiven as input, or a new response to a new request that includedAuthorization.
- handle_response(response, **kwargs)[source]¶
Handle a response.
If the response is a 401 (Unauthorized) and this is first attempt, a SciToken will be discovered and attached via an HTTP Authorization header, and the request retried.
If any other circumstances, the response is returned with no action.
- Parameters:
response (
requests.Response) – The HTTP response to handle.kwargs – Other keyword arguments are passed to the new request.
- Returns:
response (
requests.Response) – Either the originalresponsegiven as input, or a new response to a new request that includedAuthorization.