auth via email

This commit is contained in:
knst-kotov
2021-08-25 11:31:51 +03:00
parent 3c538d3724
commit 805db9814a
8 changed files with 60 additions and 20 deletions

View File

@@ -8,10 +8,11 @@ from starlette.requests import HTTPConnection
from auth.credentials import AuthCredentials, AuthUser
from auth.token import Token
from auth.authorize import Authorize
from exceptions import InvalidToken, OperationNotAllowed
from orm import User
from redis import redis
from settings import JWT_AUTH_HEADER
from settings import JWT_AUTH_HEADER, EMAIL_TOKEN_LIFE_SPAN
class _Authenticate:
@@ -68,6 +69,25 @@ class JWTAuthenticate(AuthenticationBackend):
scopes = User.get_permission(user_id=payload.user_id)
return AuthCredentials(user_id=payload.user_id, scopes=scopes, logged_in=True), AuthUser(user_id=payload.user_id)
class EmailAuthenticate:
@staticmethod
async def get_email_token(user):
token = await Authorize.authorize(
user,
device="email",
life_span=EMAIL_TOKEN_LIFE_SPAN
)
return token
@staticmethod
async def authenticate(token):
payload = await _Authenticate.verify(token)
if payload is None:
return
if payload.device != "email":
return;
auth_token = Authorize.authorize(payload.user)
return (auth_token, payload.user)
def login_required(func):
@wraps(func)