Add tokenService
This commit is contained in:
parent
4c534973d2
commit
ecba13594b
91
src/services/token.ts
Normal file
91
src/services/token.ts
Normal file
@ -0,0 +1,91 @@
|
||||
import { TokenData } from '../models/process.model';
|
||||
|
||||
export default class TokenService {
|
||||
private static instance: TokenService;
|
||||
public tokens: Map<string, TokenData> = new Map();
|
||||
private readonly STORAGE_KEY = 'ihm_tokens';
|
||||
|
||||
private constructor() {
|
||||
this.loadTokensFromStorage();
|
||||
// Nettoyer les tokens expirés toutes les minutes
|
||||
setInterval(() => this.cleanExpiredTokens(), 60 * 1000);
|
||||
}
|
||||
|
||||
static async getInstance(): Promise<TokenService> {
|
||||
if (!TokenService.instance) {
|
||||
TokenService.instance = new TokenService();
|
||||
}
|
||||
return TokenService.instance;
|
||||
}
|
||||
|
||||
private loadTokensFromStorage(): void {
|
||||
const storedTokens = localStorage.getItem(this.STORAGE_KEY);
|
||||
if (storedTokens) {
|
||||
const tokenArray = JSON.parse(storedTokens);
|
||||
this.tokens = new Map(tokenArray);
|
||||
console.log('Tokens loaded from storage:', this.tokens);
|
||||
}
|
||||
}
|
||||
|
||||
private saveTokensToStorage(): void {
|
||||
const tokenArray = Array.from(this.tokens.entries());
|
||||
localStorage.setItem(this.STORAGE_KEY, JSON.stringify(tokenArray));
|
||||
}
|
||||
|
||||
generateSessionToken(origin: string): string {
|
||||
const token = crypto.randomUUID();
|
||||
const expiration = Date.now() + (30 * 60 * 1000); // 30 minutes
|
||||
|
||||
this.tokens.set(token, {
|
||||
token,
|
||||
origin,
|
||||
expiration,
|
||||
createdAt: Date.now()
|
||||
});
|
||||
|
||||
this.saveTokensToStorage();
|
||||
console.log('Token generated:', token);
|
||||
console.log('Tokens map:', this.tokens);
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
validateToken(token: string, origin: string): boolean {
|
||||
this.loadTokensFromStorage(); // Recharger les tokens avant validation
|
||||
const tokenData = this.tokens.get(token);
|
||||
|
||||
console.log('Validating token:', token);
|
||||
console.log('Current tokens:', this.tokens);
|
||||
console.log('Token data found:', tokenData);
|
||||
|
||||
if (!tokenData) return false;
|
||||
if (tokenData.origin !== origin) return false;
|
||||
if (tokenData.expiration < Date.now()) {
|
||||
console.log(`Token expired: ${token}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
removeToken(token: string): void {
|
||||
this.tokens.delete(token);
|
||||
this.saveTokensToStorage();
|
||||
}
|
||||
|
||||
cleanExpiredTokens(): void {
|
||||
const now = Date.now();
|
||||
let hasExpired = false;
|
||||
|
||||
for (const [token, data] of this.tokens.entries()) {
|
||||
if (data.expiration < now) {
|
||||
this.tokens.delete(token);
|
||||
hasExpired = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (hasExpired) {
|
||||
this.saveTokensToStorage();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user