-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
230 lines (187 loc) · 6.71 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# Base FastAPI Platform
from fastapi import FastAPI, Request, Depends, BackgroundTasks, HTTPException, status
from pydantic import BaseModel
from enum import Enum
# Database
import models
import schemas
from database import SessionLocal, engine
from sqlalchemy.orm import Session
# For allowing accessing the data from Vue App
from fastapi.middleware.cors import CORSMiddleware
# Security
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
import jwt
from jwt import PyJWTError
from passlib.context import CryptContext
from datetime import datetime, timedelta
# JWT
# replace the secret key with the output of `openssl rand -hex 32`
SECRET_KEY = "47eefvueed944c5javascript019f7fastapi336248a5d20569yongkiewiyogo"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 5
# =======
# Security: OAuth and passlib functions
# ===========================#
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/login")
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
# FastAPI App main
app = FastAPI()
models.Base.metadata.create_all(bind=engine)
# CORS section
# ===========================
# Do not use http://localhost!
origins = [
"http://127.0.0.1",
"http://127.0.0.1:8080",
"http://localhost",
"http://localhost:8080",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
user_count=0
def verify_password(plain_password, hashed_password):
res = pwd_context.verify(plain_password, hashed_password)
return res
def get_password_hash(password):
hashed_pass = pwd_context.hash(password)
return hashed_pass
def authenticate_user(email: str, password: str):
db = SessionLocal()
user = db.query(
models.Profile).filter(models.Profile.email == email).first()
if not user:
return False
if not verify_password(password, user.hashed_passwd):
print("Debug password cannot be verified")
return False
return user
# Security: JWT functions
# =======================
def create_access_token(data: dict, expires_delta: timedelta = None):
to_encode = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=15)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
async def get_current_user(token: str = Depends(oauth2_scheme)):
"""token is access_token in login function"""
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
email: str = payload.get("sub")
if email is None:
raise credentials_exception
except PyJWTError:
raise credentials_exception
db = SessionLocal()
user = db.query(
models.Profile).filter(models.Profile.email == email).first()
if user is None:
raise credentials_exception
return user
async def get_current_active_user(
current_user: models.Profile = Depends(get_current_user)):
if current_user.membership is models.Membership.disabled:
raise HTTPException(status_code=400, detail="Inactive user")
return current_user
@app.post("/login") #must be the same endpoint as in OAuth2PasswordBearer
async def login_for_access_token(
form_data: OAuth2PasswordRequestForm = Depends()):
# OAuth form data has username, but the frontend passes the email
user = authenticate_user(form_data.username, form_data.password)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
# the sub key should have a unique identifier across the entire application
# and it should be a string.
access_token = create_access_token(data={"sub": user.email},
expires_delta=access_token_expires)
return {"access_token": access_token, "token_type": "bearer"}
# ======================
# Database Helper function
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
# API Endpoints
# ==============
@app.get("/profile/me")
async def read_my_profile(
current_user: models.Profile = Depends(get_current_user)):
return current_user
@app.get("/profiles")
async def read_profiles(req: Request,
token: str = Depends(get_current_active_user)):
db = SessionLocal()
return db.query(models.Profile).all()
@app.get("/")
async def home(req: Request, db: Session = Depends(get_db)):
profiles = db.query(models.Profile)
return True
@app.get("/profiles/{profile_id}", response_model=schemas.ProfileBase)
async def read_profiles(req: Request):
db = SessionLocal()
return db.query(
models.Profile).filter(models.Profile.username == profile_id).all()
@app.post("/profile")
def create_profile(profile_req: schemas.ProfileCreate,
background_tasks: BackgroundTasks,
db: Session = Depends(get_db)):
""" Create a profile to DB"""
# create an object of Stock if not exist
global user_count
profile = db.query(models.Profile).filter(
models.Profile.email == profile_req.email).first()
if profile:
print("Email exists, please choose another email.")
return False
# create a new profile
profile = models.Profile()
profile.username = "user"+str(user_count)
user_count=user_count+1
hashed_password = get_password_hash(profile_req.password)
profile.hashed_passwd = hashed_password
profile.email = profile_req.email
# add to db
db.add(profile)
db.commit()
db.refresh(profile)
return profile
@app.put("/profile")
def update_profile(profile_req: schemas.ProfileUpdate,
background_tasks: BackgroundTasks,
db: Session = Depends(get_db)):
db = SessionLocal()
profile = db.query(models.Profile).filter(
models.Profile.username == profile_req.username).first()
if profile:
profile.details = profile_req.details
fake_hashed_password = get_password_hash(profile_req.password)
profile.hashed_passwd = fake_hashed_password
profile.language = profile_req.language
profile.height = profile_req.height
profile.smoker = profile_req.smoker
db.commit()
else:
print("Object not found")
db.close()
return profile