This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/auth.py

20 lines
656 B
Python
Raw Normal View History

2018-04-27 17:16:43 +00:00
from sqlalchemy.exc import DataError
from werkzeug.exceptions import Unauthorized
2023-03-21 11:08:13 +00:00
from ereuse_devicehub.resources.user.models import Session, User
from ereuse_devicehub.teal.auth import TokenAuth
from ereuse_devicehub.teal.db import ResourceNotFound
2018-04-10 15:06:39 +00:00
class Auth(TokenAuth):
2018-04-27 17:16:43 +00:00
def authenticate(self, token: str, *args, **kw) -> User:
try:
2021-04-15 19:19:20 +00:00
user = User.query.filter_by(token=token).first()
if user:
return user
ses = Session.query.filter_by(token=token).one()
return ses.user
2018-04-27 17:16:43 +00:00
except (ResourceNotFound, DataError):
raise Unauthorized('Provide a suitable token.')