|
| 1 | +from fastapi import FastAPI, HTTPException, Depends, Request |
| 2 | +from fastapi.responses import JSONResponse |
| 3 | +from fastapi_jwt_auth import AuthJWT |
| 4 | +from fastapi_jwt_auth.exceptions import AuthJWTException |
| 5 | +from pydantic import BaseModel |
| 6 | + |
| 7 | +""" |
| 8 | +By default, the CRSF cookies will be called csrf_access_token and |
| 9 | +csrf_refresh_token, and in protected endpoints we will look |
| 10 | +for the CSRF token in the 'X-CSRF-Token' headers. only certain |
| 11 | +methods should define CSRF token in headers default is ('POST','PUT','PATCH','DELETE') |
| 12 | +""" |
| 13 | + |
| 14 | +app = FastAPI() |
| 15 | + |
| 16 | +class User(BaseModel): |
| 17 | + username: str |
| 18 | + password: str |
| 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 | + # Only allow JWT cookies to be sent over https |
| 25 | + authjwt_cookie_secure: bool = False |
| 26 | + # Enable csrf double submit protection. default is True |
| 27 | + authjwt_cookie_csrf_protect: bool = True |
| 28 | + # Change to 'lax' in production to make your website more secure from CSRF Attacks, default is None |
| 29 | + # authjwt_cookie_samesite: str = 'lax' |
| 30 | + |
| 31 | +@AuthJWT.load_config |
| 32 | +def get_config(): |
| 33 | + return Settings() |
| 34 | + |
| 35 | +@app.exception_handler(AuthJWTException) |
| 36 | +def authjwt_exception_handler(request: Request, exc: AuthJWTException): |
| 37 | + return JSONResponse( |
| 38 | + status_code=exc.status_code, |
| 39 | + content={"detail": exc.message} |
| 40 | + ) |
| 41 | + |
| 42 | +@app.post('/login') |
| 43 | +def login(user: User, Authorize: AuthJWT = Depends()): |
| 44 | + """ |
| 45 | + With authjwt_cookie_csrf_protect set to True, set_access_cookies() and |
| 46 | + set_refresh_cookies() will now also set the non-httponly CSRF cookies |
| 47 | + """ |
| 48 | + if user.username != "test" and user.password != "test": |
| 49 | + raise HTTPException(status_code=401,detail="Bad username or password") |
| 50 | + |
| 51 | + # Create the tokens and passing to set_access_cookies or set_refresh_cookies |
| 52 | + access_token = Authorize.create_access_token(subject=user.username) |
| 53 | + refresh_token = Authorize.create_refresh_token(subject=user.username) |
| 54 | + |
| 55 | + # Set the JWT and CSRF double submit cookies in the response |
| 56 | + Authorize.set_access_cookies(access_token) |
| 57 | + Authorize.set_refresh_cookies(refresh_token) |
| 58 | + return {"msg":"Successfully login"} |
| 59 | + |
| 60 | +@app.post('/refresh') |
| 61 | +def refresh(Authorize: AuthJWT = Depends()): |
| 62 | + Authorize.jwt_refresh_token_required() |
| 63 | + |
| 64 | + current_user = Authorize.get_jwt_subject() |
| 65 | + new_access_token = Authorize.create_access_token(subject=current_user) |
| 66 | + # Set the JWT and CSRF double submit cookies in the response |
| 67 | + Authorize.set_access_cookies(new_access_token) |
| 68 | + return {"msg":"The token has been refresh"} |
| 69 | + |
| 70 | +@app.delete('/logout') |
| 71 | +def logout(Authorize: AuthJWT = Depends()): |
| 72 | + """ |
| 73 | + Because the JWT are stored in an httponly cookie now, we cannot |
| 74 | + log the user out by simply deleting the cookie in the frontend. |
| 75 | + We need the backend to send us a response to delete the cookies. |
| 76 | + """ |
| 77 | + Authorize.jwt_required() |
| 78 | + |
| 79 | + Authorize.unset_jwt_cookies() |
| 80 | + return {"msg":"Successfully logout"} |
| 81 | + |
| 82 | +@app.get('/protected') |
| 83 | +def protected(Authorize: AuthJWT = Depends()): |
| 84 | + Authorize.jwt_required() |
| 85 | + |
| 86 | + current_user = Authorize.get_jwt_subject() |
| 87 | + return {"user": current_user} |
0 commit comments