-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth2.py
52 lines (35 loc) · 1.47 KB
/
oauth2.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
from jose import JWTError, jwt
from datetime import datetime, timedelta
from fastapi import Depends, status, HTTPException
from fastapi.security import OAuth2PasswordBearer
from fastapi.responses import RedirectResponse
scheme = OAuth2PasswordBearer(tokenUrl="login")
#Secret key has been created by running "openssl rand -hex 32"
# command on a terminal
def get_current_user(token: str = Depends(scheme)):
#print("here is the token " + token)
credentials_exception = HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail=f"Could not validate credentials", headers={"WWW-Authenticate": "Bearer"})
token = verify_access_token(token, credentials_exception)
print("Token expired")
#return RedirectResponse("http://127.0.0.1:8000/books")
return token
SECRET_KEY = "002b619e2036cf40cc5fd5fa6675fd76d1c8b82b8df8754a620bd1bcb847de0d"
expiretime = 60
def create_token(data:dict):
to_encode = data.copy()
expire = datetime.utcnow() + timedelta(minutes=expiretime)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm="HS256")
return encoded_jwt
def verify_access_token(token :str , credentials_exception):
try:
data = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
id: str = data.get("user_id")
except:
print("bello")
id=None
#print(id)
if id is None:
raise credentials_exception
return id