|
1 | | -import requests |
2 | | -from cachetools import TTLCache |
3 | | -from fastapi import HTTPException |
4 | | -from fastapi.security.utils import get_authorization_scheme_param |
5 | | -from jose import JWTError, jwt |
6 | | -from starlette.requests import Request |
7 | | -from starlette.status import HTTP_401_UNAUTHORIZED |
8 | | - |
9 | | -from dispatch.config import JWKS_URL, DISPATCH_AUTH_HEADER_KEY |
10 | | - |
11 | | -jwk_key_cache = TTLCache(maxsize=1, ttl=60 * 60) |
12 | | - |
13 | | - |
14 | | -def from_bearer_token(request: Request): |
15 | | - """Fetches user from provided bearer token.""" |
16 | | - authorization: str = request.headers.get("Authorization") |
17 | | - scheme, param = get_authorization_scheme_param(authorization) |
18 | | - if not authorization or scheme.lower() != "bearer": |
19 | | - return |
20 | | - |
21 | | - token = authorization.split()[1] |
22 | | - |
23 | | - # TODO should we warm this cache up on application start? |
24 | | - try: |
25 | | - key = jwk_key_cache[JWKS_URL] |
26 | | - except Exception: |
27 | | - key = requests.get(JWKS_URL).json()["keys"][0] |
28 | | - jwk_key_cache[JWKS_URL] = key |
29 | | - |
30 | | - try: |
31 | | - data = jwt.decode(token, key) |
32 | | - except JWTError: |
33 | | - return |
34 | | - |
35 | | - return data["email"] |
| 1 | +""" |
| 2 | +.. module: dispatch.auth.service |
| 3 | + :platform: Unix |
| 4 | + :copyright: (c) 2019 by Netflix Inc., see AUTHORS for more |
| 5 | + :license: Apache, see LICENSE for more details. |
| 6 | +""" |
| 7 | +import logging |
36 | 8 |
|
| 9 | +from starlette.requests import Request |
| 10 | +from dispatch.plugins.base import plugins |
| 11 | +from dispatch.config import ( |
| 12 | + DISPATCH_AUTHENTICATION_PROVIDER_SLUG, |
| 13 | + DISPATCH_AUTHENTICATION_DEFAULT_USER, |
| 14 | +) |
37 | 15 |
|
38 | | -def from_headers(request: Request): |
39 | | - """Fetches user from provider headers. (Email header specified by DISPATCH_AUTH_HEADER)""" |
40 | | - return request.headers.get(DISPATCH_AUTH_HEADER_KEY) |
| 16 | +log = logging.getLogger(__name__) |
41 | 17 |
|
42 | 18 |
|
43 | 19 | def get_current_user(*, request: Request): |
44 | | - """Gets the current user based on the token.""" |
45 | | - credentials_exception = HTTPException( |
46 | | - status_code=HTTP_401_UNAUTHORIZED, detail="Could not validate credentials" |
47 | | - ) |
48 | | - |
49 | | - user_email = from_bearer_token(request) |
50 | | - |
51 | | - if not user_email: |
52 | | - raise credentials_exception |
53 | | - authorization: str = request.headers.get("Authorization") |
54 | | - scheme, param = get_authorization_scheme_param(authorization) |
55 | | - if not authorization or scheme.lower() != "bearer": |
56 | | - raise credentials_exception |
57 | | - |
58 | | - token = authorization.split()[1] |
59 | | - |
60 | | - # TODO should we warm this cache up on application start? |
61 | | - try: |
62 | | - key = jwk_key_cache[JWKS_URL] |
63 | | - except Exception: |
64 | | - key = requests.get(JWKS_URL).json()["keys"][0] |
65 | | - jwk_key_cache[JWKS_URL] = key |
66 | | - |
67 | | - try: |
68 | | - data = jwt.decode(token, key) |
69 | | - except JWTError: |
70 | | - raise credentials_exception |
71 | | - |
72 | | - return data["email"] |
| 20 | + """Attempts to get the current user depending on the configured authentication provider.""" |
| 21 | + if DISPATCH_AUTHENTICATION_PROVIDER_SLUG: |
| 22 | + auth_plugin = plugins.get(DISPATCH_AUTHENTICATION_PROVIDER_SLUG) |
| 23 | + return auth_plugin.get_current_user(request) |
| 24 | + else: |
| 25 | + log.warning( |
| 26 | + "No authentication provider has been provided. There is currently no user authentication." |
| 27 | + ) |
| 28 | + return DISPATCH_AUTHENTICATION_DEFAULT_USER |
0 commit comments