Skip to content

Commit

Permalink
🔒 Fix token validation and add new method
Browse files Browse the repository at this point in the history
Added a validate method to the Token class and refactored the create method to include an optional expires_delta parameter.
  • Loading branch information
oliv10 authored and gitbutler-client committed Jun 28, 2024
1 parent abdac73 commit 0468633
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 10 additions & 1 deletion FastAuth/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

class Token:

@staticmethod
def create(user: User, expires_delta: timedelta | None = None) -> str:
NOW = datetime.now()
nbf = NOW
Expand All @@ -30,4 +31,12 @@ def create(user: User, expires_delta: timedelta | None = None) -> str:
jwt_token.pop(key)

encoded_jwt = jwt.encode(jwt_token, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
return encoded_jwt

@staticmethod
def validate(token) -> bool:
try:
jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
return True
except:
return False
10 changes: 9 additions & 1 deletion test_app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from FastAuth import User, Token
from datetime import timedelta
from time import sleep

u = User("1", "oliver")

t = Token.create(u)
t = Token.create(u, timedelta(seconds=5))

print(Token.validate(t))
print(t)

sleep(5)

print(Token.validate(t))
print(t)

0 comments on commit 0468633

Please sign in to comment.