py2http.auth¶
Framework-independent credential checks, shared by the aiohttp middleware
(py2http.middleware) and the Bottle plugins (py2http.bottle_plugins), so that
both refuse the same tokens and keys for the same reasons.
>>> bearer_token('Bearer abc'), bearer_token('bearer abc'), bearer_token('Basic abc')
('abc', 'abc', '')
>>> key_matches('k-123', 'k-123'), key_matches('k-12', 'k-123'), key_matches(None, 'k')
(True, False, False)
Functions
|
The token of an |
|
The claims of |
|
Validate a JWT verification setup, returning the algorithms as a list. |
|
Validate extra |
|
Whether the |
|
|
Classes
What a plugin must be to guard a Flask app (see |
Exceptions
A request's credentials were refused. |
- class py2http.auth.AuthPlugin[source]¶
Bases:
objectWhat a plugin must be to guard a Flask app (see
py2http.flask_plugins): a subclass with achallenge(theWWW-Authenticatevalue of a refusal) and anauthenticatethat raisesCredentialsRejectedto refuse a request. It is a marker class rather than a duck-typed method name, so that a plugin which merely has someauthenticatemethod (returningFalseto refuse, say) can’t be mistaken for one and let everything through.
- exception py2http.auth.CredentialsRejected[source]¶
Bases:
ExceptionA request’s credentials were refused. The message is safe to show the client: it never contains the token, nor anything about the server’s key.
- py2http.auth.bearer_token(authorization_header)[source]¶
The token of an
Authorization: Bearer <token>header, or''.- Return type:
- py2http.auth.decode_jwt(token, secret, *, algorithms, verify=True, decode_kwargs=None)[source]¶
The claims of
token, orCredentialsRejectedwith a client-safe reason.With
verify=Falseneither the signature nor the claims (exp…) are checked.decode_kwargs(audience,issuer,leeway…) go tojwt.decode.- Return type:
- py2http.auth.jwt_algorithms(secret, *, verify=True, algorithms=('HS256',), allow_mixed_algorithm_families=False)[source]¶
Validate a JWT verification setup, returning the algorithms as a list.
Raises at construction time, rather than accepting forgeries at request time, when:
algorithmsis empty, a bare string, or names an unknown or unsigned (none) algorithm (PyJWT names are case-sensitive);verifyis True andsecretis empty (an empty HMAC secret lets anyone sign tokens), or an HMAC algorithm is asked for with a non-str/bytessecret;verifyis True and HMAC and public-key algorithms are mixed with onesecret(the classic algorithm-confusion setup), unlessallow_mixed_algorithm_familiesis True.
>>> jwt_algorithms('s3cret') ['HS256'] >>> jwt_algorithms('', algorithms=['HS256']) Traceback (most recent call last): ... ValueError: secret must be a non-empty key when verify=True- Return type:
- py2http.auth.jwt_decode_kwargs(decode_kwargs=None)[source]¶
Validate extra
jwt.decodearguments (audience,issuer,leeway…), refusing those the verification setup owns.>>> jwt_decode_kwargs({'audience': 'my-api'}) {'audience': 'my-api'} >>> jwt_decode_kwargs({'options': {'verify_signature': False}}) Traceback (most recent call last): ... ValueError: decode_kwargs may not set ['options']; use verify= and algorithms=- Return type: