|
| 1 | +""" |
| 2 | +User and authentication models. |
| 3 | +""" |
| 4 | +import uuid |
| 5 | +import hashlib |
| 6 | +import secrets |
| 7 | +from datetime import datetime |
| 8 | +from enum import Enum |
| 9 | +from typing import Optional, Dict, Any, List |
| 10 | + |
| 11 | + |
| 12 | +class UserRole(Enum): |
| 13 | + """User role enumeration.""" |
| 14 | + ADMIN = "admin" |
| 15 | + RESEARCHER = "researcher" |
| 16 | + DEVELOPER = "developer" |
| 17 | + VIEWER = "viewer" |
| 18 | + |
| 19 | + |
| 20 | +class UserStatus(Enum): |
| 21 | + """User account status enumeration.""" |
| 22 | + PENDING = "pending" |
| 23 | + ACTIVE = "active" |
| 24 | + INACTIVE = "inactive" |
| 25 | + SUSPENDED = "suspended" |
| 26 | + |
| 27 | + |
| 28 | +class User: |
| 29 | + """User model for authentication and authorization.""" |
| 30 | + |
| 31 | + ROLE_PERMISSIONS = { |
| 32 | + UserRole.ADMIN: [ |
| 33 | + 'user:create', 'user:read', 'user:update', 'user:delete', |
| 34 | + 'model:create', 'model:read', 'model:update', 'model:delete', |
| 35 | + 'experiment:create', 'experiment:read', 'experiment:update', 'experiment:delete', |
| 36 | + 'api_key:create', 'api_key:read', 'api_key:update', 'api_key:delete', |
| 37 | + 'system:monitor', 'system:configure' |
| 38 | + ], |
| 39 | + UserRole.RESEARCHER: [ |
| 40 | + 'model:create', 'model:read', 'model:update', |
| 41 | + 'experiment:create', 'experiment:read', 'experiment:update', 'experiment:delete', |
| 42 | + 'api_key:create', 'api_key:read', 'api_key:update' |
| 43 | + ], |
| 44 | + UserRole.DEVELOPER: [ |
| 45 | + 'model:read', 'experiment:read', 'experiment:create', |
| 46 | + 'api_key:create', 'api_key:read' |
| 47 | + ], |
| 48 | + UserRole.VIEWER: [ |
| 49 | + 'model:read', 'experiment:read' |
| 50 | + ] |
| 51 | + } |
| 52 | + |
| 53 | + def __init__( |
| 54 | + self, |
| 55 | + username: str = "", |
| 56 | + email: str = "", |
| 57 | + role: UserRole = UserRole.VIEWER, |
| 58 | + status: UserStatus = UserStatus.PENDING |
| 59 | + ): |
| 60 | + self.id = str(uuid.uuid4()) |
| 61 | + self.username = username |
| 62 | + self.email = email |
| 63 | + self.role = role |
| 64 | + self.status = status |
| 65 | + self.password_hash: Optional[str] = None |
| 66 | + self.failed_login_attempts: int = 0 |
| 67 | + self.locked_until: Optional[datetime] = None |
| 68 | + self.last_login: Optional[datetime] = None |
| 69 | + self.created_at: datetime = datetime.utcnow() |
| 70 | + |
| 71 | + def set_password(self, password: str) -> None: |
| 72 | + """Hash and store password with salt.""" |
| 73 | + salt = secrets.token_hex(16) |
| 74 | + hash_obj = hashlib.sha256((salt + password).encode()) |
| 75 | + self.password_hash = f"{salt}:{hash_obj.hexdigest()}" |
| 76 | + |
| 77 | + def verify_password(self, password: str) -> bool: |
| 78 | + """Verify password against stored hash.""" |
| 79 | + if not self.password_hash: |
| 80 | + return False |
| 81 | + |
| 82 | + try: |
| 83 | + salt, stored_hash = self.password_hash.split(':') |
| 84 | + hash_obj = hashlib.sha256((salt + password).encode()) |
| 85 | + return hash_obj.hexdigest() == stored_hash |
| 86 | + except ValueError: |
| 87 | + return False |
| 88 | + |
| 89 | + def is_active(self) -> bool: |
| 90 | + """Check if user account is active and not locked.""" |
| 91 | + if self.status != UserStatus.ACTIVE: |
| 92 | + return False |
| 93 | + |
| 94 | + if self.locked_until and self.locked_until > datetime.utcnow(): |
| 95 | + return False |
| 96 | + |
| 97 | + return True |
| 98 | + |
| 99 | + def has_permission(self, permission: str) -> bool: |
| 100 | + """Check if user has the specified permission.""" |
| 101 | + permissions = self.ROLE_PERMISSIONS.get(self.role, []) |
| 102 | + return permission in permissions |
| 103 | + |
| 104 | + def to_dict(self, include_sensitive: bool = False) -> Dict[str, Any]: |
| 105 | + """Convert user to dictionary.""" |
| 106 | + data = { |
| 107 | + 'id': self.id, |
| 108 | + 'username': self.username, |
| 109 | + 'email': self.email, |
| 110 | + 'role': self.role.value, |
| 111 | + 'status': self.status.value, |
| 112 | + 'created_at': self.created_at.isoformat() if self.created_at else None, |
| 113 | + 'last_login': self.last_login.isoformat() if self.last_login else None |
| 114 | + } |
| 115 | + |
| 116 | + if include_sensitive: |
| 117 | + data['failed_login_attempts'] = self.failed_login_attempts |
| 118 | + data['locked_until'] = self.locked_until.isoformat() if self.locked_until else None |
| 119 | + |
| 120 | + return data |
| 121 | + |
| 122 | + @classmethod |
| 123 | + def from_dict(cls, data: Dict[str, Any]) -> 'User': |
| 124 | + """Create user from dictionary.""" |
| 125 | + role = UserRole(data.get('role', 'viewer')) |
| 126 | + status = UserStatus(data.get('status', 'pending')) |
| 127 | + |
| 128 | + user = cls( |
| 129 | + username=data.get('username', ''), |
| 130 | + email=data.get('email', ''), |
| 131 | + role=role, |
| 132 | + status=status |
| 133 | + ) |
| 134 | + |
| 135 | + if 'id' in data: |
| 136 | + user.id = data['id'] |
| 137 | + |
| 138 | + return user |
| 139 | + |
| 140 | + |
| 141 | +class APIKey: |
| 142 | + """API Key model for programmatic access.""" |
| 143 | + |
| 144 | + def __init__( |
| 145 | + self, |
| 146 | + user_id: str = "", |
| 147 | + name: str = "", |
| 148 | + permissions: Optional[List[str]] = None, |
| 149 | + expires_at: Optional[datetime] = None, |
| 150 | + is_active: bool = True |
| 151 | + ): |
| 152 | + self.id = str(uuid.uuid4()) |
| 153 | + self.user_id = user_id |
| 154 | + self.name = name |
| 155 | + self.permissions = permissions or [] |
| 156 | + self.expires_at = expires_at |
| 157 | + self.is_active = is_active |
| 158 | + self.key_prefix: str = "" |
| 159 | + self.key_hash: str = "" |
| 160 | + self.usage_count: int = 0 |
| 161 | + self.last_used: Optional[datetime] = None |
| 162 | + self.created_at: datetime = datetime.utcnow() |
| 163 | + |
| 164 | + @staticmethod |
| 165 | + def generate_key() -> str: |
| 166 | + """Generate a new API key string.""" |
| 167 | + return f"llm_opt_{secrets.token_hex(24)}" |
| 168 | + |
| 169 | + def set_key(self, key_string: str) -> None: |
| 170 | + """Hash and store API key with prefix for lookup.""" |
| 171 | + self.key_prefix = key_string[:8] |
| 172 | + hash_obj = hashlib.sha256(key_string.encode()) |
| 173 | + self.key_hash = hash_obj.hexdigest() |
| 174 | + |
| 175 | + def verify_key(self, key_string: str) -> bool: |
| 176 | + """Verify API key against stored hash.""" |
| 177 | + if not self.key_hash: |
| 178 | + return False |
| 179 | + |
| 180 | + hash_obj = hashlib.sha256(key_string.encode()) |
| 181 | + return hash_obj.hexdigest() == self.key_hash |
| 182 | + |
| 183 | + def is_valid(self) -> bool: |
| 184 | + """Check if API key is valid (active and not expired).""" |
| 185 | + if not self.is_active: |
| 186 | + return False |
| 187 | + |
| 188 | + if self.expires_at and self.expires_at < datetime.utcnow(): |
| 189 | + return False |
| 190 | + |
| 191 | + return True |
| 192 | + |
| 193 | + def record_usage(self) -> None: |
| 194 | + """Record API key usage.""" |
| 195 | + self.usage_count += 1 |
| 196 | + self.last_used = datetime.utcnow() |
| 197 | + |
| 198 | + def to_dict(self) -> Dict[str, Any]: |
| 199 | + """Convert API key to dictionary (without sensitive data).""" |
| 200 | + return { |
| 201 | + 'id': self.id, |
| 202 | + 'user_id': self.user_id, |
| 203 | + 'name': self.name, |
| 204 | + 'key_prefix': self.key_prefix, |
| 205 | + 'permissions': self.permissions, |
| 206 | + 'is_active': self.is_active, |
| 207 | + 'expires_at': self.expires_at.isoformat() if self.expires_at else None, |
| 208 | + 'usage_count': self.usage_count, |
| 209 | + 'last_used': self.last_used.isoformat() if self.last_used else None, |
| 210 | + 'created_at': self.created_at.isoformat() if self.created_at else None |
| 211 | + } |
0 commit comments