2017-09-13 17:23:23 +01:00
|
|
|
from flask import request, _request_ctx_stack, current_app, g
|
2016-02-09 18:48:02 +00:00
|
|
|
from notifications_python_client.authentication import decode_jwt_token, get_token_issuer
|
2016-11-03 17:05:25 +00:00
|
|
|
from notifications_python_client.errors import TokenDecodeError, TokenExpiredError, TokenIssuerError
|
2017-09-13 17:23:23 +01:00
|
|
|
from sqlalchemy.exc import DataError
|
|
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
2016-06-29 14:15:32 +01:00
|
|
|
|
2017-05-05 15:19:57 +01:00
|
|
|
from app.dao.services_dao import dao_fetch_service_by_id_with_api_keys
|
2016-01-15 16:22:03 +00:00
|
|
|
|
2015-12-15 10:47:20 +00:00
|
|
|
|
2016-06-29 17:37:20 +01:00
|
|
|
class AuthError(Exception):
|
|
|
|
|
def __init__(self, message, code):
|
|
|
|
|
self.message = {"token": [message]}
|
2016-11-01 10:33:34 +00:00
|
|
|
self.short_message = message
|
2016-06-29 17:37:20 +01:00
|
|
|
self.code = code
|
2015-12-15 10:47:20 +00:00
|
|
|
|
2016-11-01 10:33:34 +00:00
|
|
|
def to_dict_v2(self):
|
2016-11-02 14:58:39 +00:00
|
|
|
return {
|
|
|
|
|
'status_code': self.code,
|
2016-11-09 14:56:54 +00:00
|
|
|
"errors": [
|
|
|
|
|
{
|
|
|
|
|
"error": "AuthError",
|
|
|
|
|
"message": self.short_message
|
|
|
|
|
}
|
|
|
|
|
]
|
2016-11-02 14:58:39 +00:00
|
|
|
}
|
2016-11-01 10:33:34 +00:00
|
|
|
|
2015-12-15 10:47:20 +00:00
|
|
|
|
2016-06-29 17:37:20 +01:00
|
|
|
def get_auth_token(req):
|
|
|
|
|
auth_header = req.headers.get('Authorization', None)
|
2015-12-15 10:47:20 +00:00
|
|
|
if not auth_header:
|
2016-06-29 17:37:20 +01:00
|
|
|
raise AuthError('Unauthorized, authentication token must be provided', 401)
|
2015-12-15 10:47:20 +00:00
|
|
|
|
2016-11-07 10:45:18 +00:00
|
|
|
auth_scheme = auth_header[:7].title()
|
2015-12-15 10:47:20 +00:00
|
|
|
|
|
|
|
|
if auth_scheme != 'Bearer ':
|
2016-06-29 17:37:20 +01:00
|
|
|
raise AuthError('Unauthorized, authentication bearer scheme must be used', 401)
|
|
|
|
|
|
|
|
|
|
return auth_header[7:]
|
|
|
|
|
|
2015-12-15 10:47:20 +00:00
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
def requires_no_auth():
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2017-03-15 16:52:44 +00:00
|
|
|
def requires_admin_auth():
|
|
|
|
|
auth_token = get_auth_token(request)
|
2017-03-16 18:15:49 +00:00
|
|
|
client = __get_token_issuer(auth_token)
|
2017-03-15 16:52:44 +00:00
|
|
|
|
|
|
|
|
if client == current_app.config.get('ADMIN_CLIENT_USER_NAME'):
|
|
|
|
|
g.service_id = current_app.config.get('ADMIN_CLIENT_USER_NAME')
|
|
|
|
|
return handle_admin_key(auth_token, current_app.config.get('ADMIN_CLIENT_SECRET'))
|
|
|
|
|
else:
|
|
|
|
|
raise AuthError('Unauthorized, admin authentication token required', 401)
|
|
|
|
|
|
|
|
|
|
|
2016-06-29 17:37:20 +01:00
|
|
|
def requires_auth():
|
|
|
|
|
auth_token = get_auth_token(request)
|
2017-03-16 18:15:49 +00:00
|
|
|
client = __get_token_issuer(auth_token)
|
2016-06-29 17:37:20 +01:00
|
|
|
|
2016-09-23 11:07:49 +01:00
|
|
|
try:
|
2017-05-05 15:19:57 +01:00
|
|
|
service = dao_fetch_service_by_id_with_api_keys(client)
|
2016-09-23 11:07:49 +01:00
|
|
|
except DataError:
|
|
|
|
|
raise AuthError("Invalid token: service id is not the right data type", 403)
|
2016-11-10 11:07:12 +00:00
|
|
|
except NoResultFound:
|
|
|
|
|
raise AuthError("Invalid token: service not found", 403)
|
|
|
|
|
|
|
|
|
|
if not service.api_keys:
|
|
|
|
|
raise AuthError("Invalid token: service has no API keys", 403)
|
|
|
|
|
|
|
|
|
|
if not service.active:
|
|
|
|
|
raise AuthError("Invalid token: service is archived", 403)
|
|
|
|
|
|
|
|
|
|
for api_key in service.api_keys:
|
2016-06-29 17:37:20 +01:00
|
|
|
try:
|
2017-06-19 14:32:22 +01:00
|
|
|
get_decode_errors(auth_token, api_key.secret)
|
2016-06-29 17:37:20 +01:00
|
|
|
except TokenDecodeError:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if api_key.expiry_date:
|
2016-09-16 08:45:48 +01:00
|
|
|
raise AuthError("Invalid token: API key revoked", 403)
|
2016-06-29 17:37:20 +01:00
|
|
|
|
2016-11-30 10:59:55 +00:00
|
|
|
g.service_id = api_key.service_id
|
2017-05-05 15:19:57 +01:00
|
|
|
_request_ctx_stack.top.authenticated_service = service
|
2016-06-29 17:37:20 +01:00
|
|
|
_request_ctx_stack.top.api_user = api_key
|
2017-05-05 15:19:57 +01:00
|
|
|
|
2016-06-29 17:37:20 +01:00
|
|
|
return
|
|
|
|
|
else:
|
2016-11-10 11:07:12 +00:00
|
|
|
# service has API keys, but none matching the one the user provided
|
2016-09-23 11:07:49 +01:00
|
|
|
raise AuthError("Invalid token: signature, api token is not valid", 403)
|
2016-06-29 17:37:20 +01:00
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
def __get_token_issuer(auth_token):
|
|
|
|
|
try:
|
|
|
|
|
client = get_token_issuer(auth_token)
|
|
|
|
|
except TokenIssuerError:
|
|
|
|
|
raise AuthError("Invalid token: iss field not provided", 403)
|
|
|
|
|
except TokenDecodeError as e:
|
|
|
|
|
raise AuthError("Invalid token: signature, api token is not valid", 403)
|
|
|
|
|
return client
|
|
|
|
|
|
|
|
|
|
|
2016-06-29 17:37:20 +01:00
|
|
|
def handle_admin_key(auth_token, secret):
|
|
|
|
|
try:
|
|
|
|
|
get_decode_errors(auth_token, secret)
|
|
|
|
|
return
|
2016-11-03 17:05:25 +00:00
|
|
|
except TokenDecodeError as e:
|
2017-03-16 18:15:49 +00:00
|
|
|
raise AuthError("Invalid token: signature, api token is not valid", 403)
|
2016-06-29 17:37:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_decode_errors(auth_token, unsigned_secret):
|
2016-06-29 16:33:02 +01:00
|
|
|
try:
|
|
|
|
|
decode_jwt_token(auth_token, unsigned_secret)
|
2016-09-23 11:07:49 +01:00
|
|
|
except TokenExpiredError:
|
2017-01-17 10:44:00 +00:00
|
|
|
raise AuthError("Invalid token: expired, check that your system clock is accurate", 403)
|