@nxtoai/jwtette
Version:
JWT authentication package for NxtoAI microservices
116 lines (101 loc) • 4.3 kB
text/typescript
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import { GatiService } from '@nxtoai/gati';
import { AagService } from '@nxtoai/aag';
export class JwtHelper {
private readonly TOKEN_PREFIX = 'token:';
private readonly USER_PREFIX = 'user:';
private readonly TOKEN_EXPIRY = 24 * 60 * 60; // 24 hours in seconds
constructor(
private readonly configService: ConfigService,
private readonly jwtService: JwtService,
private readonly gati: GatiService,
private readonly aag: AagService
) {}
private getTokenKey(token: string): string {
return `${this.TOKEN_PREFIX}${token}`;
}
private getUserKey(userid: string): string {
return `${this.USER_PREFIX}${userid}`;
}
async cacheToken(token: string, userid: string): Promise<void> {
try {
const tokenKey = this.getTokenKey(token);
const userKey = this.getUserKey(userid);
// Store token -> userid mapping
await this.gati.put(tokenKey, { value: userid }, { ttl: this.TOKEN_EXPIRY });
// Store userid -> token mapping
await this.gati.put(userKey, { value: token }, { ttl: this.TOKEN_EXPIRY });
this.aag.debug(`Token cached for user: ${userid}`);
} catch (error) {
this.aag.error(`Error caching token: ${error.message}`, error.stack);
throw error;
}
}
async getTokenUser(token: string): Promise<string | null> {
try {
const tokenKey = this.getTokenKey(token);
const cachedUser = await this.gati.get(tokenKey);
return typeof cachedUser?.value === 'string' ? cachedUser.value : null;
} catch (error) {
this.aag.error(`Error getting token user: ${error.message}`, error.stack);
return null;
}
}
async getUserToken(userid: string): Promise<string | null> {
try {
const userKey = this.getUserKey(userid);
const cachedToken = await this.gati.get(userKey);
return typeof cachedToken?.value === 'string' ? cachedToken.value : null;
} catch (error) {
this.aag.error(`Error getting user token: ${error.message}`, error.stack);
return null;
}
}
async invalidateToken(token: string): Promise<void> {
try {
const tokenKey = this.getTokenKey(token);
const userid = await this.getTokenUser(token);
if (userid) {
const userKey = this.getUserKey(userid);
await Promise.all([
this.gati.remove(tokenKey),
this.gati.remove(userKey)
]);
this.aag.debug(`Token invalidated for user: ${userid}`);
}
} catch (error) {
this.aag.error(`Error invalidating token: ${error.message}`, error.stack);
throw error;
}
}
async invalidateUserToken(userid: string): Promise<void> {
try {
const userKey = this.getUserKey(userid);
const cachedToken = await this.gati.get(userKey);
if (cachedToken?.value && typeof cachedToken.value === 'string') {
const tokenKey = this.getTokenKey(cachedToken.value);
await Promise.all([
this.gati.remove(tokenKey),
this.gati.remove(userKey)
]);
this.aag.debug(`All tokens invalidated for user: ${userid}`);
}
} catch (error) {
this.aag.error(`Error invalidating user tokens: ${error.message}`, error.stack);
throw error;
}
}
async isTokenBlacklisted(token: string): Promise<boolean> {
try {
const tokenKey = this.getTokenKey(token);
const cachedUser = await this.gati.get(tokenKey);
return !cachedUser?.value || typeof cachedUser.value !== 'string';
} catch (error) {
this.aag.error(`Error checking token blacklist: ${error.message}`, error.stack);
return true; // If there's an error, consider the token as blacklisted for safety
}
}
}