Utilsο
from aioauth import utils
Contains helper functions that is used throughout the project that doesnβt pertain to a specific file or module.
- get_authorization_scheme_param(authorization_header_value: str) Tuple[str, str] [source]ο
Retrieves the authorization schema parameters from the authorization header.
- Parameters:
authorization_header_value β Value of the authorization header.
- Returns:
Tuple of the format
(scheme, param)
.
- enforce_str(scope: List) str [source]ο
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:
scope β An iterable or string that contains a list of scope.
- Returns:
A string of scopes seperated by spaces.
- Raises:
TypeError β The
scope
value passed is not of the proper type.
- enforce_list(scope: str | List | Set | Tuple | None) List [source]ο
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:
scope β An iterable or string that contains scopes.
- Returns:
A list of scopes.
- generate_token(length: int = 30, chars: str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') str [source]ο
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:
length β Length of the generated token.
chars β The characters to use to generate the string.
- Returns:
Random string of length
length
and characters inchars
.
- build_uri(url: str, query_params: Dict | None = None, fragment: Dict | None = None) str [source]ο
Builds an URI string from passed
url
,query_params
, andfragment
.- Parameters:
url β URL string.
query_params β Paramaters that contain the query.
fragment β Fragment of the page.
- Returns:
URL containing the original
url
, and the addedquery_params
andfragment
.
- encode_auth_headers(client_id: str, client_secret: str) HTTPHeaderDict [source]ο
Encodes the authentication header using base64 encoding.
- Parameters:
client_id β The clientβs id.
client_secret β The clientβs secret.
- Returns:
A case insensitive dictionary that contains the
Authorization
header set tobasic
and the authorization header.
- decode_auth_headers(authorization: str) Tuple[str, str] [source]ο
Decodes an encoded HTTP basic authentication string. Returns a tuple of the form
(client_id, client_secret)
, and raises aaioauth.errors.InvalidClientError
exception if nothing could be decoded.- Parameters:
authorization β Authorization header string.
- Returns:
Tuple of the form
(client_id, client_secret)
.- Raises:
ValueError β Invalid authorization header string.
- create_s256_code_challenge(code_verifier: str) str [source]ο
Create S256 code challenge with the passed
code_verifier
.Note
This function implements
base64url(sha256(ascii(code_verifier)))
.- Parameters:
code_verifier β Code verifier string.
- Returns:
Representation of the S256 code challenge with the passed
code_verifier
.
Decorator that adds error catching to the function passed.
- Parameters:
f β A callable.
- Returns:
A callable with error catching capabilities.