-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjwtsign.py
More file actions
58 lines (45 loc) · 1.38 KB
/
jwtsign.py
File metadata and controls
58 lines (45 loc) · 1.38 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
import time
import jwt
from fastapi import HTTPException
from pydantic import BaseModel
import secrets
JWT_SECRET = secrets.token_hex(16)
JWT_ALGORITHM = "HS256"
print(JWT_SECRET)
def sign(email):
payload = {
"email": email,
}
token = jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM)
return token
def decode(token):
try:
decoded_token = jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM])
return decoded_token
except:
raise HTTPException(status_code=401, detail="Invalid token")
class SignUpSchema(BaseModel):
name: str
email: str
password: str
class SignInSchema(BaseModel):
email: str
password: str
userlist = []
def signup(name, email, password):
for user in userlist:
if user.email == email:
raise HTTPException(status_code=400, detail="Email already registered")
user = SignUpSchema(name=name, email=email, password=password)
userlist.append(user)
token = sign(user.email)
return token
def signin(email, password):
for user in userlist:
if user.email == email:
if user.password == password:
token = sign(user.email)
return token
else:
raise HTTPException(status_code=400, detail="Incorrect password")
raise HTTPException(status_code=400, detail="Email not registered")