-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
64 lines (54 loc) · 2.1 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
from fastapi import FastAPI, FastAPI, HTTPException, status, Depends, Security, Header
from typing import List, Optional
from fastapi.security import (
SecurityScopes
)
from jose import JWTError
from pydantic import BaseModel, ValidationError
from client import OIDCClient
class TokenData(BaseModel):
user_id: Optional[str] = None
scopes: List[str] = []
app = FastAPI()
class OidcClientInject:
def __init__(self):
self.oidc_client = OIDCClient(os.getenv('ISSUER_BASE_URL'),
os.getenv('CLIENT_ID'),
os.getenv('CLIENT_SECRET'),
os.getenv('REDIRECT_URI'))
def __call__(self):
return self.oidc_client
client_injector = OidcClientInject()
async def get_user_from_token(
security_scopes: SecurityScopes, authorization: str = Header(None), client: OIDCClient = Depends(client_injector)):
if security_scopes.scopes:
authenticate_value = f'Bearer scope="{security_scopes.scope_str}"'
else:
authenticate_value = f"Bearer"
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": authenticate_value},
)
try:
token = authorization.replace('Bearer ', '')
payload = await client.decode(token)
user_id: str = payload.get("sub")
if user_id is None:
raise credentials_exception
token_scopes = payload.get("scp", [])
token_data = TokenData(scopes=token_scopes, user_id=user_id)
except (JWTError, ValidationError):
raise credentials_exception
for scope in security_scopes.scopes:
if scope not in token_data.scopes:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not enough permissions",
headers={"WWW-Authenticate": authenticate_value},
)
return token_data
@app.get('/protected')
async def protected(current_user: TokenData = Security(get_user_from_token, scopes=["profile"])):
return 'you are authorized'