Projekt

Obecné

Profil

Stáhnout (3.42 KB) Statistiky
| Větev: | Tag: | Revize:
1

    
2

    
3
from fastapi import Depends, APIRouter, Form
4
from fastapi import Request
5
from fastapi.responses import HTMLResponse
6
from fastapi.templating import Jinja2Templates
7
from fastapi.responses import HTMLResponse
8
from fastapi_jwt_auth import AuthJWT
9
from pydantic import BaseModel
10

    
11

    
12
# Path to html templates used in this file
13
templates = Jinja2Templates(directory="templates/auth")
14

    
15
# prefix used for all endpoints in this file
16
auth = APIRouter(prefix="")
17

    
18

    
19

    
20
class Settings(BaseModel):
21
    authjwt_secret_key: str = "secret"
22
    # Configure application to store and get JWT from cookies
23
    authjwt_token_location: set = {"cookies"}
24
    # Disable CSRF Protection for this example. default is True
25
    authjwt_cookie_csrf_protect: bool = False
26

    
27

    
28
@AuthJWT.load_config
29
def get_config():
30
    return Settings()
31

    
32

    
33
fake_users_db = {
34
    "admin": {
35
        "username": "admin",
36
        "password": "admin"
37
    }
38
}
39

    
40

    
41
@auth.get("/login", response_class=HTMLResponse)
42
async def login_get(request: Request):
43
    return templates.TemplateResponse("login.html", {"request": request})
44

    
45

    
46
@auth.post("/login", response_class=HTMLResponse)
47
async def login(username: str = Form(...), password: str = Form(...), Authorize: AuthJWT = Depends()):
48
    user_dict = fake_users_db.get(username)
49

    
50
    if user_dict != None:
51
        if user_dict["username"] == username and user_dict["password"] == password:
52
            access_token = Authorize.create_access_token(subject="admin", expires_time=False)
53
            refresh_token = Authorize.create_refresh_token(subject="admin", expires_time=False)
54
        else:
55
            access_token = Authorize.create_access_token(subject="host", expires_time=False)
56
            refresh_token = Authorize.create_refresh_token(subject="host", expires_time=False)
57
    else:
58
        access_token = Authorize.create_access_token(subject="host", expires_time=False)
59
        refresh_token = Authorize.create_refresh_token(subject="host", expires_time=False)
60

    
61
    # Set the JWT cookies in the response
62
    Authorize.set_access_cookies(access_token)
63
    Authorize.set_refresh_cookies(refresh_token)
64
    return """
65
    <html>
66
        <head>
67
            <title>Login</title>
68
        </head>
69
        <body>
70
            <h1>Logged in</h1>
71
            <form action="/logs-web" method="get">
72
                <input type="submit" value="Back" />
73
            </form>
74
        </body>
75
    </html>
76
    """
77

    
78

    
79
@auth.post('/refresh')
80
def refresh(Authorize: AuthJWT = Depends()):
81
    Authorize.jwt_refresh_token_required()
82

    
83
    current_user = Authorize.get_jwt_subject()
84
    new_access_token = Authorize.create_access_token(subject=current_user)
85
    # Set the JWT cookies in the response
86
    Authorize.set_access_cookies(new_access_token)
87
    return {"msg": "The token has been refresh"}
88

    
89

    
90
@auth.get('/logout', response_class=HTMLResponse)
91
def logout(Authorize: AuthJWT = Depends()):
92
    """
93
    Because the JWT are stored in an httponly cookie now, we cannot
94
    log the user out by simply deleting the cookies in the frontend.
95
    We need the backend to send us a response to delete the cookies.
96
    """
97
    Authorize.jwt_optional()
98

    
99
    Authorize.unset_jwt_cookies()
100
    return """
101
        <html>
102
            <head>
103
                <title>Logout</title>
104
            </head>
105
            <body>
106
                <h1>Logged Out</h1>
107
                <form action="/logs-web" method="get">
108
                    <input type="submit" value="Back" />
109
                </form>
110
            </body>
111
        </html>
112
        """
(2-2/13)