forked from LouisYZK/Frodo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
80 lines (70 loc) · 2.71 KB
/
main.py
File metadata and controls
80 lines (70 loc) · 2.71 KB
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
76
77
78
79
import json
import asyncio
from datetime import datetime, timedelta
from fastapi import FastAPI, Depends, HTTPException, Request, Form
from fastapi.security import OAuth2PasswordRequestForm
from fastapi.staticfiles import StaticFiles
from starlette.datastructures import Headers
from starlette.middleware.sessions import SessionMiddleware
import typing
from typing import Optional
from models import schemas, user
from views import admin, index, blog, comment, activity
from ext import mako, oauth2_scheme
import config
app = FastAPI()
app.__name__ = 'fast_blog'
mako.init_app(app)
app.mount('/static/', StaticFiles(directory='static'), name='static')
app.add_middleware(SessionMiddleware, secret_key='fast')
app.include_router(
admin.router,
prefix='/api'
)
app.include_router(index.router)
app.include_router(blog.router)
app.include_router(comment.router, prefix='/j')
app.include_router(activity.router, prefix='/j')
@app.get('/admin')
@mako.template('admin.html')
async def admin(request: Request):
return {}
@app.middleware('http')
async def process_auth(req: Request, call_next):
""" modfiy the request body of authentication """
if str(req.url).endswith('/auth') and req.headers['referer'].endswith('admin'):
new_header = req.headers.mutablecopy()
new_header['content-type'] = 'application/x-www-form-urlencoded'
req.scope['headers'] = new_header.raw
recv = await req.receive()
async def custome_recv():
body_dct = json.loads(recv['body'].decode())
recv['body'] = f"username={body_dct['username']}&password={body_dct['password']}".encode()
return recv
new_req = Request(req.scope, custome_recv)
response = await call_next(new_req)
elif req.scope['path'] == '/' or 'page' in req.scope['path']:
req.state.partials = config.partials
response = await call_next(req)
else:
response = await call_next(req)
return response
@app.post('/auth')
async def login(req: Request, username: str=Form(...), password: str=Form(...)):
user_auth: schemas.User = \
await user.authenticate_user(username, password)
# await user.authenticate_user(user_data.username, user_data.password)
if not user_auth:
raise HTTPException(status_code=400,
detail='Incorrect User Auth.')
access_token_expires = timedelta(
minutes=int(config.ACCESS_TOKEN_EXPIRE_MINUTES)
)
access_token = await user.create_access_token(
data={'sub': user_auth.name},
expires_delta=access_token_expires)
return {
'access_token': access_token,
'refresh_token': access_token,
'token_type': 'bearer'
}