Utils
Contains helper functions that is used throughout the project that doesn't pertain to a specific file or module.
build_error_response(exc, request, skip_redirect_on_exc=(OAuth2Error,))
Generate an OAuth HTTP response from the given exception
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exc
|
Exception
|
Exception used to generate HTTP response |
required |
request
|
Request
|
oauth request object |
required |
skip_redirect_on_exc
|
Tuple[Type[OAuth2Error], ...]
|
Exception types to skip redirect on |
(OAuth2Error,)
|
Returns:
Type | Description |
---|---|
Response
|
OAuth HTTP response |
Source code in aioauth/utils.py
build_uri(url, query_params=None, fragment=None)
Builds an URI string from passed url
, query_params
, and
fragment
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
URL string. |
required |
query_params
|
Optional[Dict]
|
Paramaters that contain the query. |
None
|
fragment
|
Optional[Dict]
|
Fragment of the page. |
None
|
Returns:
Type | Description |
---|---|
str
|
URL containing the original |
str
|
|
Source code in aioauth/utils.py
catch_errors_and_unavailability(skip_redirect_on_exc=(OAuth2Error,))
Decorator that adds error catching to the function passed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f
|
A callable. |
required |
Returns:
Type | Description |
---|---|
Callable[..., Callable[..., Coroutine[Any, Any, Response]]]
|
A callable with error catching capabilities. |
Source code in aioauth/utils.py
create_s256_code_challenge(code_verifier)
Create S256 code challenge with the passed code_verifier
.
Note
This function implements: base64url(sha256(ascii(code_verifier)))
.
Args: code_verifier: Code verifier string.
Returns:
Type | Description |
---|---|
str
|
Representation of the S256 code challenge with the passed |
str
|
|
Source code in aioauth/utils.py
decode_auth_headers(authorization)
Decodes an encoded HTTP basic authentication string.
Returns a tuple of the form (client_id, client_secret)
, and
raises a aioauth.errors.InvalidClientError
exception if nothing
could be decoded.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
authorization
|
str
|
Authorization header string. |
required |
Returns:
Type | Description |
---|---|
Tuple[str, str]
|
Tuple of the form |
Raises:
Type | Description |
---|---|
ValueError
|
Invalid |
Source code in aioauth/utils.py
encode_auth_headers(client_id, client_secret)
Encodes the authentication header using base64 encoding.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_id
|
str
|
The client's id. |
required |
client_secret
|
str
|
The client's secret. |
required |
Returns:
Type | Description |
---|---|
HTTPHeaderDict
|
A case insensitive dictionary that contains the |
HTTPHeaderDict
|
|
HTTPHeaderDict
|
header. |
Source code in aioauth/utils.py
enforce_list(scope)
Converts a space separated string to a list of scopes.
Note
If an iterable is passed to this method it will return a list
representation of the iterable. Use enforce_str
to
convert iterables to a scope string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scope
|
Optional[Union[str, List, Set, Tuple]]
|
An iterable or string that contains scopes. |
required |
Returns:
Type | Description |
---|---|
List
|
A list of scopes. |
Source code in aioauth/utils.py
enforce_str(scope)
Converts a list of scopes to a space separated string.
Note
If a string is passed to this method it will simply return an
empty string back. Use enforce_list
to convert
strings to scope lists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scope
|
List
|
An iterable or string that contains a list of scope. |
required |
Returns:
Type | Description |
---|---|
str
|
A string of scopes seperated by spaces. |
Raises:
Type | Description |
---|---|
TypeError
|
The |
Source code in aioauth/utils.py
generate_token(length=30, chars=UNICODE_ASCII_CHARACTER_SET)
Generates a non-guessable OAuth token. OAuth (1 and 2) does not specify the format of tokens except that they should be strings of random characters. Tokens should not be guessable and entropy when generating the random characters is important. Which is why SystemRandom is used instead of the default random.choice method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
length
|
int
|
Length of the generated token. |
30
|
chars
|
str
|
The characters to use to generate the string. |
UNICODE_ASCII_CHARACTER_SET
|
Returns:
Type | Description |
---|---|
str
|
Random string of length |
Source code in aioauth/utils.py
get_authorization_scheme_param(authorization_header_value)
Retrieves the authorization schema parameters from the authorization header.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
authorization_header_value
|
str
|
Value of the authorization header. |
required |
Returns:
Type | Description |
---|---|
Tuple[str, str]
|
Tuple of the format |