2016-04-20 17:25:20 +01:00
|
|
|
import uuid
|
2016-06-22 15:27:28 +01:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
2017-06-19 14:32:22 +01:00
|
|
|
from app import db, encryption
|
2016-01-19 12:07:00 +00:00
|
|
|
from app.models import ApiKey
|
|
|
|
|
|
2016-04-20 17:25:20 +01:00
|
|
|
from app.dao.dao_utils import (
|
|
|
|
|
transactional,
|
2016-04-21 18:10:57 +01:00
|
|
|
version_class
|
2016-04-20 17:25:20 +01:00
|
|
|
)
|
2016-01-19 12:07:00 +00:00
|
|
|
|
2016-04-20 17:25:20 +01:00
|
|
|
|
|
|
|
|
@transactional
|
2016-04-21 18:10:57 +01:00
|
|
|
@version_class(ApiKey)
|
2016-06-22 15:27:28 +01:00
|
|
|
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
|
2017-06-19 14:32:22 +01:00
|
|
|
api_key.secret = uuid.uuid4()
|
2016-06-22 15:27:28 +01:00
|
|
|
db.session.add(api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@transactional
|
|
|
|
|
@version_class(ApiKey)
|
|
|
|
|
def expire_api_key(service_id, api_key_id):
|
|
|
|
|
api_key = ApiKey.query.filter_by(id=api_key_id, service_id=service_id).one()
|
|
|
|
|
api_key.expiry_date = datetime.utcnow()
|
|
|
|
|
db.session.add(api_key)
|
2016-01-19 12:07:00 +00:00
|
|
|
|
|
|
|
|
|
2016-01-20 14:48:44 +00:00
|
|
|
def get_model_api_keys(service_id, id=None):
|
|
|
|
|
if id:
|
|
|
|
|
return ApiKey.query.filter_by(id=id, service_id=service_id, expiry_date=None).one()
|
|
|
|
|
return ApiKey.query.filter_by(service_id=service_id).all()
|
2016-01-19 12:07:00 +00:00
|
|
|
|
|
|
|
|
|
2016-01-19 18:25:21 +00:00
|
|
|
def get_unsigned_secrets(service_id):
|
2016-01-19 12:07:00 +00:00
|
|
|
"""
|
|
|
|
|
This method can only be exposed to the Authentication of the api calls.
|
|
|
|
|
"""
|
2016-01-19 18:25:21 +00:00
|
|
|
api_keys = ApiKey.query.filter_by(service_id=service_id, expiry_date=None).all()
|
2017-06-19 14:32:22 +01:00
|
|
|
keys = [x.secret for x in api_keys]
|
2016-01-19 18:25:21 +00:00
|
|
|
return keys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
2017-06-19 14:32:22 +01:00
|
|
|
return api_key.secret
|