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 in chars.

build_uri(url: str, query_params: Dict | None = None, fragment: Dict | None = None) str[source]

Builds an URI string from passed url, query_params, and fragment.

Parameters:
  • url – URL string.

  • query_params – Paramaters that contain the query.

  • fragment – Fragment of the page.

Returns:

URL containing the original url, and the added query_params and fragment.

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 to basic 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 a aioauth.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.

catch_errors_and_unavailability(skip_redirect_on_exc: ~typing.Tuple[~typing.Type[~aioauth.errors.OAuth2Error], ...] = (<class 'aioauth.errors.OAuth2Error'>,)) Callable[[...], Callable[[...], Coroutine[Any, Any, Response]]][source]

Decorator that adds error catching to the function passed.

Parameters:

f – A callable.

Returns:

A callable with error catching capabilities.