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
622 B
Python
Raw Normal View History

2018-04-27 17:16:43 +00:00
from sqlalchemy.exc import DataError
2018-09-07 10:38:02 +00:00
from teal.auth import TokenAuth
from teal.db import ResourceNotFound
2018-04-27 17:16:43 +00:00
from werkzeug.exceptions import Unauthorized
2021-04-15 19:19:20 +00:00
from ereuse_devicehub.resources.user.models import User, Session
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.')