This repository has been archived by the owner on Mar 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
53 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
""" | ||
Common auth checks for writing. | ||
""" | ||
from flask import g | ||
from functools import wraps | ||
|
||
|
||
def check(admin_api): | ||
"""Decorator that does auth checks prior to storing data. | ||
""" | ||
def decorator(func): | ||
@wraps(func) | ||
def new_func(*args, **kwargs): | ||
data_group = kwargs['data_group'] | ||
data_type = kwargs['data_type'] | ||
g.data_set_config = admin_api.get_data_set(data_group, data_type) | ||
_validate_config(g.data_set_config) | ||
_validate_auth(g.data_set_config) | ||
return func(*args, **kwargs) | ||
return new_func | ||
return decorator | ||
|
||
|
||
def _validate_auth(data_set_config): | ||
try: | ||
auth_header = request.headers['Authorization'] | ||
except KeyError: | ||
abort(401, 'Expected header of form: Authorization: Bearer <token>') | ||
|
||
if not auth_header_is_valid(data_set_config, auth_header): | ||
token = extract_bearer_token(auth_header) | ||
abort(401, | ||
'Unauthorized: Invalid bearer token \'{0}\' for \'{1}\''.format( | ||
token, data_set_config['name'])) | ||
|
||
|
||
def _validate_config(data_set_config): | ||
if data_set_config is None: | ||
abort(404, 'Could not find data_set_config') | ||
|
||
g.data_set_name = data_set_config['name'] |