forked from LAION-AI/Open-Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.py
37 lines (29 loc) · 1.02 KB
/
auth.py
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
from datetime import datetime, timedelta
from typing import Optional
from jose import jwt
from oasst_backend.config import Settings
from oasst_backend.models import Account
from sqlmodel import Session
def create_access_token(data: dict) -> str:
"""
Create an encoded JSON Web Token (JWT) using the given data.
"""
expires_delta = timedelta(minutes=Settings.AUTH_ACCESS_TOKEN_EXPIRE_MINUTES)
to_encode = data.copy()
expire = datetime.utcnow() + expires_delta
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, Settings.AUTH_SECRET, algorithm=Settings.AUTH_ALGORITHM)
return encoded_jwt
def get_account_from_discord_id(db: Session, discord_id: str) -> Optional[Account]:
"""
Get the Open-Assistant Account associated with the given Discord ID.
"""
account: Account = (
db.query(Account)
.filter(
Account.provider == "discord",
Account.provider_account_id == discord_id,
)
.first()
)
return account