recoder-code
Version:
Complete AI-powered development platform with ML model training, plugin registry, real-time collaboration, monitoring, infrastructure automation, and enterprise deployment capabilities
122 lines (121 loc) • 3.16 kB
TypeScript
/**
* Authentication Service for Collaboration Service
* Handles user authentication and authorization for real-time collaboration
*/
import { EventEmitter } from 'events';
export interface User {
id: string;
username: string;
email: string;
avatar_url?: string;
role: 'admin' | 'user' | 'collaborator';
}
export interface AuthToken {
userId: string;
username: string;
email: string;
role: string;
iat: number;
exp: number;
}
export interface AuthResult {
success: boolean;
user?: User;
error?: string;
}
export declare class AuthService extends EventEmitter {
private jwtSecret;
private jwtExpiration;
private connectedUsers;
private userSockets;
constructor(jwtSecret?: string, jwtExpiration?: string);
/**
* Authenticate a user with JWT token
*/
authenticateToken(token: string): Promise<AuthResult>;
/**
* Generate a JWT token for a user
*/
generateToken(user: User): string;
/**
* Register a connected user with socket ID
*/
registerConnection(socketId: string, user: User): void;
/**
* Unregister a disconnected user
*/
unregisterConnection(socketId: string): User | undefined;
/**
* Get user by socket ID
*/
getUserBySocket(socketId: string): User | undefined;
/**
* Get all socket IDs for a user
*/
getUserSockets(userId: string): string[];
/**
* Check if user is connected
*/
isUserConnected(userId: string): boolean;
/**
* Get all connected users
*/
getConnectedUsers(): User[];
/**
* Check if user has permission to access a document
*/
canAccessDocument(userId: string, documentId: string): Promise<boolean>;
/**
* Check if user has permission to edit a document
*/
canEditDocument(userId: string, documentId: string): Promise<boolean>;
/**
* Check if user has admin privileges
*/
isAdmin(userId: string): boolean;
/**
* Authenticate socket connection
*/
authenticateSocket(socketId: string, token: string): Promise<{
success: boolean;
user?: User;
error?: string;
}>;
/**
* Validate token without full authentication
*/
validateToken(token: string): {
valid: boolean;
decoded?: AuthToken;
error?: string;
};
/**
* Verify token and return user data (alias for authenticateToken)
*/
verifyToken(token: string): Promise<AuthResult>;
/**
* Refresh a token (if it's close to expiration)
*/
refreshToken(token: string): {
success: boolean;
newToken?: string;
error?: string;
};
/**
* Get authentication statistics
*/
getStats(): {
connectedUsers: number;
uniqueUsers: number;
totalConnections: number;
usersByRole: Record<string, number>;
};
/**
* Disconnect all sessions for a user
*/
disconnectUser(userId: string): string[];
/**
* Clean up expired connections (call this periodically)
*/
cleanupExpiredConnections(): void;
}