mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-03 13:18:32 -04:00
create new method to validate secret header, new tests
This commit is contained in:
@@ -7,6 +7,7 @@ from sqlalchemy.exc import DataError
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
from app.dao.services_dao import dao_fetch_service_by_id_with_api_keys
|
||||
from flask import jsonify
|
||||
|
||||
|
||||
class AuthError(Exception):
|
||||
@@ -44,16 +45,18 @@ def requires_no_auth():
|
||||
pass
|
||||
|
||||
|
||||
def restrict_ip_sms():
|
||||
def check_route_secret():
|
||||
# Check route of inbound sms (Experimental)
|
||||
# Temporary custom header for route security
|
||||
# if request.headers.get("X-Custom-forwarder"):
|
||||
# current_app.logger.info("X-Custom-forwarder received")
|
||||
if request.headers.get("X-Custom-Forwarder"):
|
||||
route_secret_key = request.headers.get("X-Custom-Forwarder")
|
||||
|
||||
# if route_secret_key is None:
|
||||
# raise AuthError('invalid secret key', 403)
|
||||
|
||||
key_1 = current_app.config.get('ROUTE_SECRET_KEY_1')
|
||||
key_2 = current_app.config.get('ROUTE_SECRET_KEY_2')
|
||||
key_used = 0
|
||||
key_used = None
|
||||
route_allowed = False
|
||||
if route_secret_key == key_1:
|
||||
key_used = 1
|
||||
@@ -63,12 +66,21 @@ def restrict_ip_sms():
|
||||
route_allowed = True
|
||||
|
||||
current_app.logger.info({
|
||||
'message': 'X-Custom-forwarder key {} is used'.format(key_used),
|
||||
'message': 'X-Custom-Forwarder key {} is used'.format(key_used),
|
||||
'log_contents': {
|
||||
'passed': route_allowed,
|
||||
}
|
||||
})
|
||||
|
||||
# if not key_used:
|
||||
# raise AuthError('X-Custom-Forwarder, wrong secret', 403)
|
||||
|
||||
return jsonify(key_used=key_used), 200
|
||||
|
||||
|
||||
def restrict_ip_sms():
|
||||
check_route_secret()
|
||||
|
||||
# Check IP of SMS providers
|
||||
if request.headers.get("X-Forwarded-For"):
|
||||
# X-Forwarded-For looks like "203.0.113.195, 70.41.3.18, 150.172.238.178"
|
||||
|
||||
Reference in New Issue
Block a user