-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
75 lines (53 loc) · 2.07 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
import logging
from starlette.requests import Request
from starlette.middleware.sessions import SessionMiddleware
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from authlib.integrations.starlette_client import OAuth
from config import settings
LOG_FORMAT = ('%(levelname) -10s %(asctime)s %(name) -30s %(funcName) '
'-35s %(lineno) -5d: %(message)s')
LOGGER = logging.getLogger(__name__)
app = FastAPI()
origins = [
'http://localhost:8000',
'http://127.0.0.1:8000'
]
app.add_middleware(CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*'])
app.add_middleware(SessionMiddleware, secret_key=settings.OIDC_CLIENT_SECRET)
app.mount('/static', StaticFiles(directory='static'), name='static')
templates = Jinja2Templates(directory='templates')
oauth = OAuth()
oauth.register(
name='keycloak',
client_id=settings.OIDC_CLIENT_ID,
client_secret=settings.OIDC_CLIENT_SECRET.get_secret_value(),
server_metadata_url=settings.OIDC_METADATA_URL,
client_kwargs={
'scope': settings.OIDC_SCOPE,
}
)
@app.get('/login/')
async def login(request: Request):
redirect_uri = f'{settings.BASE_URL}/auth/'
return await oauth.keycloak.authorize_redirect(request, redirect_uri)
@app.get('/auth/', response_class=RedirectResponse)
async def auth(request: Request):
token = await oauth.keycloak.authorize_access_token(request)
user = token.get('userinfo')
request.session['user'] = user
return RedirectResponse('/')
@app.get('/', response_class=HTMLResponse)
async def home(request: Request):
user = request.session.get('user', None)
if user is not None:
return templates.TemplateResponse('home.html', {'request': request, 'user': dict(user)})
else:
return RedirectResponse(f'{settings.BASE_URL}/login/')