auth0_jwt_validator.http_bearer

View Source
 0# Built-in packages
 1import http
 2import re
 3import typing
 4
 5# Third-party packages
 6
 7# Local packages
 8
 9
10def get_token(authorization_header: str) -> typing.Optional[str]:
11    """
12    Get a token from a authorization header with a bearer authentication schema.
13
14    Parameters
15    ----------
16    authorization_header : str
17        The authorization header which one we want to get the bearer token.
18
19    Returns
20    -------
21    token : str or None
22        The bearer token is `None` only when the authorization header mismatch
23        the bearer authentication schema for bearer tokens.
24
25    Examples
26    --------
27
28    >>> from auth0_jwt_validator import get_token
29    >>> auth_header = "Bearer super-secret-token"
30    >>> get_token(auth_header)
31    "super-secret-token"
32
33    >>> from auth0_jwt_validator import get_token
34    >>> auth_header = None
35    >>> get_token(auth_header)
36    None
37
38    >>> from auth0_jwt_validator import get_token
39    >>> auth_header = "Token super-secret-token"
40    >>> get_token(auth_header)
41    None
42
43    >>> from auth0_jwt_validator import get_token
44    >>> auth_header = "Bearer super-secret-token extra-secret-token"
45    >>> get_token(auth_header)
46    None
47
48    >>> from auth0_jwt_validator import get_token
49    >>> auth_header = "Bearer "
50    >>> get_token(auth_header)
51    None
52    """
53
54    if not authorization_header or not isinstance(authorization_header, str):
55        return None
56
57    if not re.match(r"^bearer", authorization_header, re.IGNORECASE):
58        return None
59
60    # check bearer format
61    if not re.match(r"^bearer [\w\.-]+$", authorization_header, re.IGNORECASE):
62        return None
63
64    _, token = authorization_header.split()
65
66    return token
#   def get_token(authorization_header: str) -> Optional[str]:
View Source
11def get_token(authorization_header: str) -> typing.Optional[str]:
12    """
13    Get a token from a authorization header with a bearer authentication schema.
14
15    Parameters
16    ----------
17    authorization_header : str
18        The authorization header which one we want to get the bearer token.
19
20    Returns
21    -------
22    token : str or None
23        The bearer token is `None` only when the authorization header mismatch
24        the bearer authentication schema for bearer tokens.
25
26    Examples
27    --------
28
29    >>> from auth0_jwt_validator import get_token
30    >>> auth_header = "Bearer super-secret-token"
31    >>> get_token(auth_header)
32    "super-secret-token"
33
34    >>> from auth0_jwt_validator import get_token
35    >>> auth_header = None
36    >>> get_token(auth_header)
37    None
38
39    >>> from auth0_jwt_validator import get_token
40    >>> auth_header = "Token super-secret-token"
41    >>> get_token(auth_header)
42    None
43
44    >>> from auth0_jwt_validator import get_token
45    >>> auth_header = "Bearer super-secret-token extra-secret-token"
46    >>> get_token(auth_header)
47    None
48
49    >>> from auth0_jwt_validator import get_token
50    >>> auth_header = "Bearer "
51    >>> get_token(auth_header)
52    None
53    """
54
55    if not authorization_header or not isinstance(authorization_header, str):
56        return None
57
58    if not re.match(r"^bearer", authorization_header, re.IGNORECASE):
59        return None
60
61    # check bearer format
62    if not re.match(r"^bearer [\w\.-]+$", authorization_header, re.IGNORECASE):
63        return None
64
65    _, token = authorization_header.split()
66
67    return token

Get a token from a authorization header with a bearer authentication schema.

Parameters
  • authorization_header (str): The authorization header which one we want to get the bearer token.
Returns
  • token (str or None): The bearer token is None only when the authorization header mismatch the bearer authentication schema for bearer tokens.
Examples
>>> from auth0_jwt_validator import get_token
>>> auth_header = "Bearer super-secret-token"
>>> get_token(auth_header)
"super-secret-token"
>>> from auth0_jwt_validator import get_token
>>> auth_header = None
>>> get_token(auth_header)
None
>>> from auth0_jwt_validator import get_token
>>> auth_header = "Token super-secret-token"
>>> get_token(auth_header)
None
>>> from auth0_jwt_validator import get_token
>>> auth_header = "Bearer super-secret-token extra-secret-token"
>>> get_token(auth_header)
None
>>> from auth0_jwt_validator import get_token
>>> auth_header = "Bearer "
>>> get_token(auth_header)
None