paijo77 commited on
Commit
098e7b6
·
verified ·
1 Parent(s): ed58be2

update app/auth.py

Browse files
Files changed (1) hide show
  1. app/auth.py +32 -0
app/auth.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from datetime import datetime, timedelta
3
+ from typing import Optional
4
+ from jose import jwt, JWTError
5
+ from passlib.context import CryptContext
6
+ from app.config import settings
7
+
8
+ # Settings from central config
9
+ SECRET_KEY = settings.SECRET_KEY
10
+ ALGORITHM = "HS256"
11
+ ACCESS_TOKEN_EXPIRE_MINUTES = 60 * 24 * 7
12
+
13
+ pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
14
+
15
+
16
+ def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
17
+ to_encode = data.copy()
18
+ if expires_delta:
19
+ expire = datetime.utcnow() + expires_delta
20
+ else:
21
+ expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
22
+ to_encode.update({"exp": expire})
23
+ encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
24
+ return encoded_jwt
25
+
26
+
27
+ def verify_token(token: str) -> Optional[dict]:
28
+ try:
29
+ payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
30
+ return payload
31
+ except JWTError:
32
+ return None