Skip to content

Commit

Permalink
fix: failsafe if cookie auth fails
Browse files Browse the repository at this point in the history
  • Loading branch information
pwall2222 committed Oct 28, 2023
1 parent 4be7722 commit 8122426
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions ValVault/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from dataclasses_json import DataClassJsonMixin

from ValLib import Auth, User, authenticate, cookie_token
from ValLib import Auth, AuthException, User, authenticate, cookie_token

from .debug import Level, log

Expand Down Expand Up @@ -35,14 +35,23 @@ def cookies_expired(auth: Auth):
return (time() - auth.created) > ONE_DAY * 30


def best_auth(user: User, auth: Auth, remember: bool):
if auth.remember and not cookies_expired(auth):
def try_cookies(auth: Auth) -> Optional[Auth]:
try:
token, cookies = cookie_token(auth.cookies)
auth.token = token
auth.cookies = cookies
auth.remember = remember
auth.created = time()
return auth
except AuthException:
return None


def best_auth(user: User, auth: Auth, remember: bool):
if auth.remember and not cookies_expired(auth):
new_auth = try_cookies(auth)
if new_auth is not None:
return new_auth

log(Level.DEBUG, f"Expired cookies for {user.username}")
return authenticate(user, remember)

Expand Down

0 comments on commit 8122426

Please sign in to comment.