mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-04-29 09:52:36 +02:00
api: add bearer token support
This commit is contained in:
parent
82d94a7490
commit
ad6c6a4abb
1 changed files with 15 additions and 0 deletions
|
@ -26,12 +26,27 @@ def need_auth(f):
|
||||||
auth = request.authorization
|
auth = request.authorization
|
||||||
project_id = kwargs.get("project_id")
|
project_id = kwargs.get("project_id")
|
||||||
|
|
||||||
|
# Use Basic Auth
|
||||||
if auth and project_id and auth.username == project_id:
|
if auth and project_id and auth.username == project_id:
|
||||||
project = Project.query.get(auth.username)
|
project = Project.query.get(auth.username)
|
||||||
if project and check_password_hash(project.password, auth.password):
|
if project and check_password_hash(project.password, auth.password):
|
||||||
# The whole project object will be passed instead of project_id
|
# The whole project object will be passed instead of project_id
|
||||||
kwargs.pop("project_id")
|
kwargs.pop("project_id")
|
||||||
return f(*args, project=project, **kwargs)
|
return f(*args, project=project, **kwargs)
|
||||||
|
else:
|
||||||
|
# Use Bearer token Auth
|
||||||
|
auth_header = request.headers.get('Authorization', '')
|
||||||
|
auth_token = ''
|
||||||
|
try:
|
||||||
|
auth_token = auth_header.split(" ")[1]
|
||||||
|
except IndexError:
|
||||||
|
abort(401)
|
||||||
|
project_id = Project.verify_token(auth_token, token_type='non_timed_token')
|
||||||
|
if auth_token and project_id:
|
||||||
|
project = Project.query.get(project_id)
|
||||||
|
if project:
|
||||||
|
kwargs.pop("project_id")
|
||||||
|
return f(*args, project=project, **kwargs)
|
||||||
abort(401)
|
abort(401)
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue