UNPKG

@nxtoai/jwtette

Version:

JWT authentication package for NxtoAI microservices

125 lines 5.03 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.JwtHelper = void 0; const common_1 = require("@nestjs/common"); const config_1 = require("@nestjs/config"); const jwt_1 = require("@nestjs/jwt"); const gati_1 = require("@nxtoai/gati"); const aag_1 = require("@nxtoai/aag"); let JwtHelper = class JwtHelper { constructor(configService, jwtService, gati, aag) { this.configService = configService; this.jwtService = jwtService; this.gati = gati; this.aag = aag; this.TOKEN_PREFIX = 'token:'; this.USER_PREFIX = 'user:'; this.TOKEN_EXPIRY = 24 * 60 * 60; } getTokenKey(token) { return `${this.TOKEN_PREFIX}${token}`; } getUserKey(userid) { return `${this.USER_PREFIX}${userid}`; } async cacheToken(token, userid) { try { const tokenKey = this.getTokenKey(token); const userKey = this.getUserKey(userid); await this.gati.put(tokenKey, { value: userid }, { ttl: this.TOKEN_EXPIRY }); 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) { 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) { 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) { 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) { 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) { 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; } } }; exports.JwtHelper = JwtHelper; exports.JwtHelper = JwtHelper = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [config_1.ConfigService, jwt_1.JwtService, gati_1.GatiService, aag_1.AagService]) ], JwtHelper); //# sourceMappingURL=jwt.helper.js.map