2016-04-25 16:28:08 +01:00
|
|
|
import uuid
|
2017-08-22 16:04:15 +01:00
|
|
|
|
2016-01-19 14:01:26 +00:00
|
|
|
from flask import current_app
|
2016-02-09 18:48:02 +00:00
|
|
|
from notifications_python_client.authentication import create_jwt_token
|
2017-08-22 16:04:15 +01:00
|
|
|
|
2016-06-30 13:21:35 +01:00
|
|
|
from app.dao.api_key_dao import save_model_api_key
|
2016-04-25 16:28:08 +01:00
|
|
|
from app.dao.services_dao import dao_fetch_service_by_id
|
2024-01-16 14:46:17 -05:00
|
|
|
from app.enums import KeyType
|
|
|
|
|
from app.models import ApiKey
|
2016-01-15 16:22:03 +00:00
|
|
|
|
|
|
|
|
|
2024-01-16 14:46:17 -05:00
|
|
|
def create_service_authorization_header(service_id, key_type=KeyType.NORMAL):
|
2021-08-04 15:12:09 +01:00
|
|
|
client_id = str(service_id)
|
|
|
|
|
secrets = ApiKey.query.filter_by(service_id=service_id, key_type=key_type).all()
|
2016-04-25 16:28:08 +01:00
|
|
|
|
2021-08-04 15:12:09 +01:00
|
|
|
if secrets:
|
|
|
|
|
secret = secrets[0].secret
|
2016-01-19 14:01:26 +00:00
|
|
|
else:
|
2021-08-04 15:12:09 +01:00
|
|
|
service = dao_fetch_service_by_id(service_id)
|
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
"service": service,
|
|
|
|
|
"name": uuid.uuid4(),
|
|
|
|
|
"created_by": service.created_by,
|
|
|
|
|
"key_type": key_type,
|
2021-08-04 15:12:09 +01:00
|
|
|
}
|
|
|
|
|
api_key = ApiKey(**data)
|
|
|
|
|
save_model_api_key(api_key)
|
|
|
|
|
secret = api_key.secret
|
2016-01-19 14:01:26 +00:00
|
|
|
|
2016-05-04 16:08:23 +01:00
|
|
|
token = create_jwt_token(secret=secret, client_id=client_id)
|
2023-08-29 14:54:30 -07:00
|
|
|
return "Authorization", "Bearer {}".format(token)
|
2016-11-11 12:43:51 +00:00
|
|
|
|
|
|
|
|
|
2021-08-04 15:12:09 +01:00
|
|
|
def create_admin_authorization_header():
|
2023-08-29 14:54:30 -07:00
|
|
|
client_id = current_app.config["ADMIN_CLIENT_ID"]
|
2021-08-04 15:24:49 +01:00
|
|
|
return create_internal_authorization_header(client_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_internal_authorization_header(client_id):
|
2023-08-29 14:54:30 -07:00
|
|
|
secret = current_app.config["INTERNAL_CLIENT_API_KEYS"][client_id][0]
|
2021-08-04 15:12:09 +01:00
|
|
|
token = create_jwt_token(secret=secret, client_id=client_id)
|
2024-02-26 20:07:23 -05:00
|
|
|
return "Authorization", f"Bearer {token}"
|
2021-08-04 15:12:09 +01:00
|
|
|
|
|
|
|
|
|
2016-11-11 12:43:51 +00:00
|
|
|
def unwrap_function(fn):
|
|
|
|
|
"""
|
|
|
|
|
Given a function, returns its undecorated original.
|
|
|
|
|
"""
|
2023-08-29 14:54:30 -07:00
|
|
|
while hasattr(fn, "__wrapped__"):
|
2016-11-11 12:43:51 +00:00
|
|
|
fn = fn.__wrapped__
|
|
|
|
|
return fn
|