Refactor ApiKeys.secret and ServiceInboundApi.bearer_token to use the same encryption method and get rid of the duplicate code.

This commit is contained in:
Rebecca Law
2017-06-19 14:32:22 +01:00
parent 6202da7dea
commit 3a66027d6a
15 changed files with 55 additions and 79 deletions

View File

@@ -1,14 +1,13 @@
import uuid
from datetime import datetime
from app import db
from app import db, encryption
from app.models import ApiKey
from app.dao.dao_utils import (
transactional,
version_class
)
from app.authentication.utils import generate_secret
@transactional
@@ -16,7 +15,7 @@ from app.authentication.utils import generate_secret
def save_model_api_key(api_key):
if not api_key.id:
api_key.id = uuid.uuid4() # must be set now so version history model can use same id
api_key.secret = generate_secret(uuid.uuid4())
api_key.secret = uuid.uuid4()
db.session.add(api_key)
@@ -39,7 +38,7 @@ def get_unsigned_secrets(service_id):
This method can only be exposed to the Authentication of the api calls.
"""
api_keys = ApiKey.query.filter_by(service_id=service_id, expiry_date=None).all()
keys = [x.unsigned_secret for x in api_keys]
keys = [x.secret for x in api_keys]
return keys
@@ -48,4 +47,4 @@ def get_unsigned_secret(key_id):
This method can only be exposed to the Authentication of the api calls.
"""
api_key = ApiKey.query.filter_by(id=key_id, expiry_date=None).one()
return api_key.unsigned_secret
return api_key.secret

View File

@@ -1,7 +1,6 @@
from datetime import datetime
from app import db, create_uuid
from app.authentication.utils import generate_secret
from app.dao.dao_utils import transactional, version_class
from app.models import ServiceInboundApi
@@ -11,7 +10,7 @@ from app.models import ServiceInboundApi
def save_service_inbound_api(service_inbound_api):
service_inbound_api.id = create_uuid()
service_inbound_api.created_at == datetime.utcnow()
service_inbound_api.bearer_token = generate_secret(service_inbound_api.bearer_token)
service_inbound_api.bearer_token = service_inbound_api.bearer_token
db.session.add(service_inbound_api)
@@ -21,7 +20,7 @@ def reset_service_inbound_api(service_inbound_api, updated_by_id, url=None, bear
if url:
service_inbound_api.url = url
if bearer_token:
service_inbound_api.bearer_token = generate_secret(bearer_token)
service_inbound_api.bearer_token = bearer_token
service_inbound_api.updated_by_id = updated_by_id
service_inbound_api.updated_at = datetime.utcnow()