25 lines
878 B
Python
25 lines
878 B
Python
import secrets
|
|
from datetime import datetime
|
|
from app.extensions import db
|
|
|
|
|
|
class ApiKey(db.Model):
|
|
"""Per-user API keys for programmatic access to sub-applications."""
|
|
__tablename__ = 'api_keys'
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
user_id = db.Column(db.Integer, db.ForeignKey('portal_users.id'), nullable=False)
|
|
key = db.Column(db.String(64), unique=True, nullable=False, index=True)
|
|
app_name = db.Column(db.String(50), nullable=False)
|
|
description = db.Column(db.String(200), nullable=True)
|
|
is_active = db.Column(db.Boolean, default=True)
|
|
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
|
last_used_at = db.Column(db.DateTime, nullable=True)
|
|
|
|
@staticmethod
|
|
def generate_key():
|
|
return secrets.token_hex(32)
|
|
|
|
def __repr__(self):
|
|
return f'<ApiKey user={self.user_id} app={self.app_name}>'
|