forked from markomirosavljev/fastapi-cognito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
42 lines (32 loc) · 1.03 KB
/
example.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
from typing import Any
from fastapi import FastAPI, Depends
from pydantic_settings import BaseSettings
from fastapi_cognito import CognitoAuth, CognitoSettings, CognitoToken
app = FastAPI()
class Settings(BaseSettings):
check_expiration: bool = True
jwt_header_prefix: str = "Bearer"
jwt_header_name: str = "Authorization"
userpools: dict[str, dict[str, Any]] = {
"eu": {
"region": "USERPOOL_REGION",
"userpool_id": "USERPOOL_ID",
"app_client_id": "APP_CLIENT_ID"
},
"us": {
"region": "USERPOOL_REGION",
"userpool_id": "USERPOOL_ID",
"app_client_id": "APP_CLIENT_ID"
}
}
settings = Settings()
cognito_eu = CognitoAuth(
settings=CognitoSettings.from_global_settings(settings)
)
cognito_us = CognitoAuth(
settings=CognitoSettings.from_global_settings(settings),
userpool_name="us"
)
@app.get("/")
def hello_world(auth: CognitoToken = Depends(cognito_us.auth_required)):
return {"message": "Hello world"}