-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tokens table and tokens functionality for oauth2 stuff (revoke/provid…
…e/..)
- Loading branch information
Showing
9 changed files
with
222 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# /server/app/crud/token.py | ||
from datetime import datetime, timedelta | ||
|
||
from sqlalchemy.orm import Session | ||
|
||
from server.app.core.config import settings | ||
from server.app.core.security import create_access_token, create_refresh_token | ||
from server.app.models import Token | ||
|
||
|
||
class CRUDToken: | ||
def create_user_tokens(self, db: Session, user_id: int) -> Token: | ||
access_token = create_access_token(str(user_id)) | ||
refresh_token = create_refresh_token(str(user_id)) | ||
|
||
now = datetime.utcnow() | ||
token = Token( | ||
user_id=user_id, | ||
access_token=access_token, | ||
refresh_token=refresh_token, | ||
access_token_expires_at=int((now + timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)).timestamp()), | ||
refresh_token_expires_at=int((now + timedelta(days=settings.REFRESH_TOKEN_EXPIRE_DAYS)).timestamp()), | ||
) | ||
db.add(token) | ||
db.commit() | ||
db.refresh(token) | ||
return token | ||
|
||
def get_by_access_token(self, db: Session, access_token: str) -> Token | None: | ||
return db.query(Token).filter(Token.access_token == access_token, Token.is_active == True).first() | ||
|
||
def get_by_refresh_token(self, db: Session, refresh_token: str) -> Token | None: | ||
return db.query(Token).filter(Token.refresh_token == refresh_token, Token.is_active == True).first() | ||
|
||
def revoke_all_user_tokens(self, db: Session, user_id: int) -> None: | ||
db.query(Token).filter(Token.user_id == user_id).update({"is_active": False}) | ||
db.commit() | ||
|
||
def revoke_token(self, db: Session, token: Token) -> None: | ||
token.is_active = False | ||
db.add(token) | ||
db.commit() | ||
|
||
|
||
token = CRUDToken() |
Oops, something went wrong.