recoder-code
Version:
🚀 AI-powered development platform - Chat with 32+ models, build projects, automate workflows. Free models included!
85 lines (84 loc) • 2.81 kB
TypeScript
/**
* AuthService
* Handles user authentication, session management, and API key operations
*/
import { Config } from '../config';
import { User } from '../entities/User';
import { ApiKey, ApiKeyScope } from '../entities/ApiKey';
export interface AuthResult {
success: boolean;
user?: User;
error?: string;
}
export interface TokenValidationResult {
valid: boolean;
user?: User;
apiKey?: ApiKey;
error?: string;
}
export interface TokenResult {
token: string;
expiresAt: Date;
}
export interface ApiKeyResult {
success: boolean;
key?: string;
apiKey?: ApiKey;
error?: string;
}
export interface ApiKeyCreateOptions {
name: string;
scopes: ApiKeyScope[];
description?: string;
expires_at?: Date;
restrictions?: any;
}
export declare class AuthService {
private readonly jwtSecret;
private readonly tokenExpiry;
private readonly saltRounds;
private userRepository;
private apiKeyRepository;
private readonly logger;
constructor(config?: Config);
authenticateUser(usernameOrEmail: string, password: string): Promise<AuthResult>;
generateToken(payload: any): string;
validateToken(token: string): Promise<TokenValidationResult>;
private validateJwtToken;
private validateApiKey;
generateSessionToken(userId: string): Promise<TokenResult>;
createApiKey(userId: string, options: ApiKeyCreateOptions): Promise<ApiKeyResult>;
getUserApiKeys(userId: string): Promise<ApiKey[]>;
revokeApiKey(userId: string, keyId: string): Promise<{
success: boolean;
error?: string;
}>;
revokeToken(token: string): Promise<void>;
revokeAllTokens(userId: string, exceptKeyId?: string): Promise<void>;
verifyPassword(userId: string, password: string): Promise<boolean>;
changePassword(userId: string, newPassword: string): Promise<{
success: boolean;
error?: string;
}>;
hashPassword(password: string): Promise<string>;
generatePasswordResetToken(): Promise<string>;
generateVerificationToken(): Promise<string>;
checkAuthRateLimit(identifier: string): Promise<boolean>;
recordFailedAttempt(identifier: string): Promise<void>;
createSession(userId: string, userAgent?: string, ip?: string): Promise<string>;
validateSession(sessionId: string): Promise<{
valid: boolean;
userId?: string;
}>;
destroySession(sessionId: string): Promise<void>;
generateTotpSecret(userId: string): Promise<string>;
verifyTotpToken(userId: string, token: string): Promise<boolean>;
suspendUser(userId: string, reason: string): Promise<{
success: boolean;
error?: string;
}>;
unsuspendUser(userId: string): Promise<{
success: boolean;
error?: string;
}>;
}