2016-04-20 17:25:20 +01:00
|
|
|
import uuid
|
2024-05-23 13:59:51 -07:00
|
|
|
from datetime import timedelta
|
2016-06-22 15:27:28 +01:00
|
|
|
|
2024-11-15 08:50:01 -08:00
|
|
|
from sqlalchemy import func, or_, select
|
2021-03-10 13:55:06 +00:00
|
|
|
|
2017-11-28 10:35:16 +00:00
|
|
|
from app import db
|
2021-04-14 07:11:01 +01:00
|
|
|
from app.dao.dao_utils import autocommit, version_class
|
2016-01-19 12:07:00 +00:00
|
|
|
from app.models import ApiKey
|
2024-05-23 13:59:51 -07:00
|
|
|
from app.utils import utc_now
|
2016-01-19 12:07:00 +00:00
|
|
|
|
2016-04-20 17:25:20 +01:00
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
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:
|
2023-08-29 14:54:30 -07:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
2016-06-22 15:27:28 +01:00
|
|
|
@version_class(ApiKey)
|
|
|
|
|
def expire_api_key(service_id, api_key_id):
|
2024-11-15 08:50:01 -08:00
|
|
|
api_key = (
|
|
|
|
|
db.session.execute(
|
2024-12-19 07:29:37 -08:00
|
|
|
select(ApiKey).where(
|
|
|
|
|
ApiKey.id == api_key_id, ApiKey.service_id == service_id
|
|
|
|
|
)
|
2024-11-15 08:50:01 -08:00
|
|
|
)
|
2024-12-19 07:46:14 -08:00
|
|
|
.scalars()
|
2024-11-15 08:50:01 -08:00
|
|
|
.one()
|
|
|
|
|
)
|
2024-05-23 13:59:51 -07:00
|
|
|
api_key.expiry_date = utc_now()
|
2016-06-22 15:27:28 +01:00
|
|
|
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:
|
2024-12-19 07:26:05 -08:00
|
|
|
return (
|
|
|
|
|
db.session.execute(
|
2024-12-19 07:29:37 -08:00
|
|
|
select(ApiKey).where(
|
|
|
|
|
ApiKey.id == id,
|
|
|
|
|
ApiKey.service_id == service_id,
|
2024-12-19 11:10:03 -08:00
|
|
|
ApiKey.expiry_date == None, # noqa
|
2024-12-19 07:29:37 -08:00
|
|
|
)
|
2024-12-19 07:26:05 -08:00
|
|
|
)
|
2024-12-19 07:37:22 -08:00
|
|
|
.scalars()
|
2024-12-19 07:26:05 -08:00
|
|
|
.one()
|
|
|
|
|
)
|
2024-05-23 13:59:51 -07:00
|
|
|
seven_days_ago = utc_now() - timedelta(days=7)
|
2024-11-18 11:36:20 -08:00
|
|
|
return (
|
|
|
|
|
db.session.execute(
|
|
|
|
|
select(ApiKey).where(
|
|
|
|
|
or_(
|
|
|
|
|
ApiKey.expiry_date == None, # noqa
|
|
|
|
|
func.date(ApiKey.expiry_date) > seven_days_ago, # noqa
|
|
|
|
|
),
|
|
|
|
|
ApiKey.service_id == service_id,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.scalars()
|
|
|
|
|
.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.
|
|
|
|
|
"""
|
2024-12-19 07:26:05 -08:00
|
|
|
api_keys = (
|
|
|
|
|
db.session.execute(
|
2024-12-19 07:29:37 -08:00
|
|
|
select(ApiKey).where(
|
|
|
|
|
ApiKey.service_id == service_id, ApiKey.expiry_date == None # noqa
|
|
|
|
|
)
|
2024-12-19 07:26:05 -08:00
|
|
|
)
|
2024-12-19 07:37:22 -08:00
|
|
|
.scalars()
|
2024-12-19 07:26:05 -08:00
|
|
|
.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.
|
|
|
|
|
"""
|
2024-12-19 07:26:05 -08:00
|
|
|
api_key = (
|
2024-12-19 07:29:37 -08:00
|
|
|
db.session.execute(
|
2024-12-19 11:10:03 -08:00
|
|
|
select(ApiKey).where(
|
|
|
|
|
ApiKey.id == key_id, ApiKey.expiry_date == None # noqa
|
|
|
|
|
)
|
2024-12-19 07:29:37 -08:00
|
|
|
)
|
2024-12-19 07:37:22 -08:00
|
|
|
.scalars()
|
2024-12-19 07:26:05 -08:00
|
|
|
.one()
|
|
|
|
|
)
|
2017-06-19 14:32:22 +01:00
|
|
|
return api_key.secret
|