1
|
from fastapi import Depends, APIRouter, Form
|
2
|
from fastapi import Request
|
3
|
from fastapi.responses import HTMLResponse
|
4
|
from fastapi.templating import Jinja2Templates
|
5
|
from fastapi_jwt_auth import AuthJWT
|
6
|
from pydantic import BaseModel
|
7
|
|
8
|
# Path to html templates used in this file
|
9
|
templates = Jinja2Templates(directory="templates/auth")
|
10
|
|
11
|
# prefix used for all endpoints in this file
|
12
|
auth = APIRouter(prefix="")
|
13
|
|
14
|
|
15
|
|
16
|
class Settings(BaseModel):
|
17
|
authjwt_secret_key: str = "secret"
|
18
|
# Configure application to store and get JWT from cookies
|
19
|
authjwt_token_location: set = {"cookies"}
|
20
|
# Disable CSRF Protection for this example. default is True
|
21
|
authjwt_cookie_csrf_protect: bool = False
|
22
|
|
23
|
|
24
|
@AuthJWT.load_config
|
25
|
def get_config():
|
26
|
return Settings()
|
27
|
|
28
|
# admin username and password
|
29
|
fake_users_db = {
|
30
|
"admin": {
|
31
|
"username": "admin",
|
32
|
"password": "admin"
|
33
|
}
|
34
|
}
|
35
|
|
36
|
|
37
|
@auth.get("/login", response_class=HTMLResponse)
|
38
|
async def login_get(request: Request):
|
39
|
"""
|
40
|
return html template for login
|
41
|
"""
|
42
|
return templates.TemplateResponse("login.html", {"request": request})
|
43
|
|
44
|
|
45
|
@auth.post("/login", response_class=HTMLResponse)
|
46
|
async def login(username: str = Form(...), password: str = Form(...), Authorize: AuthJWT = Depends()):
|
47
|
"""
|
48
|
Endpoint called from login template. Checks if given username and password aligns with admin
|
49
|
username and password and returns token for browser according to given username and password
|
50
|
"""
|
51
|
user_dict = fake_users_db.get(username)
|
52
|
if user_dict != None:
|
53
|
if user_dict["username"] == username and user_dict["password"] == password:
|
54
|
access_token = Authorize.create_access_token(subject="admin", expires_time=False)
|
55
|
refresh_token = Authorize.create_refresh_token(subject="admin", expires_time=False)
|
56
|
else:
|
57
|
access_token = Authorize.create_access_token(subject="host", expires_time=False)
|
58
|
refresh_token = Authorize.create_refresh_token(subject="host", expires_time=False)
|
59
|
else:
|
60
|
access_token = Authorize.create_access_token(subject="host", expires_time=False)
|
61
|
refresh_token = Authorize.create_refresh_token(subject="host", expires_time=False)
|
62
|
|
63
|
# Set the JWT cookies in the response
|
64
|
Authorize.set_access_cookies(access_token)
|
65
|
Authorize.set_refresh_cookies(refresh_token)
|
66
|
return """
|
67
|
<html>
|
68
|
<head>
|
69
|
<title>Login</title>
|
70
|
</head>
|
71
|
<body>
|
72
|
<h1>Logged in</h1>
|
73
|
<form action="/logs-web" method="get">
|
74
|
<input type="submit" value="Back" />
|
75
|
</form>
|
76
|
</body>
|
77
|
</html>
|
78
|
"""
|
79
|
|
80
|
|
81
|
@auth.post('/refresh')
|
82
|
def refresh(Authorize: AuthJWT = Depends()):
|
83
|
"""
|
84
|
endpoint for refreshing browser token. Not used at the moment since lifetime of given tokens are
|
85
|
unlimited.
|
86
|
"""
|
87
|
Authorize.jwt_refresh_token_required()
|
88
|
current_user = Authorize.get_jwt_subject()
|
89
|
new_access_token = Authorize.create_access_token(subject=current_user)
|
90
|
# Set the JWT cookies in the response
|
91
|
Authorize.set_access_cookies(new_access_token)
|
92
|
return {"msg": "The token has been refresh"}
|
93
|
|
94
|
|
95
|
@auth.get('/logout', response_class=HTMLResponse)
|
96
|
def logout(Authorize: AuthJWT = Depends()):
|
97
|
"""
|
98
|
Endpoint for deleting cookie token with acces role.
|
99
|
"""
|
100
|
Authorize.jwt_optional()
|
101
|
|
102
|
Authorize.unset_jwt_cookies()
|
103
|
return """
|
104
|
<html>
|
105
|
<head>
|
106
|
<title>Logout</title>
|
107
|
</head>
|
108
|
<body>
|
109
|
<h1>Logged Out</h1>
|
110
|
<form action="/logs-web" method="get">
|
111
|
<input type="submit" value="Back" />
|
112
|
</form>
|
113
|
</body>
|
114
|
</html>
|
115
|
"""
|