From 43e65ddbbeafbd12619ad3ae952fdb9598cfdef4 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Tue, 14 May 2024 12:40:02 +0300 Subject: [PATCH] apps/api: remove token methods from ldap controller --- apps/api/src/ldap/ldap.controller.ts | 4 +-- apps/api/src/ldap/ldap.service.ts | 47 +++++++++++----------------- 2 files changed, 20 insertions(+), 31 deletions(-) diff --git a/apps/api/src/ldap/ldap.controller.ts b/apps/api/src/ldap/ldap.controller.ts index bc933e2..a1f9dfc 100644 --- a/apps/api/src/ldap/ldap.controller.ts +++ b/apps/api/src/ldap/ldap.controller.ts @@ -76,9 +76,7 @@ export class LdapController implements BaseAuthController { if (!token) throw new UnauthorizedException(); - const { username } = this.ldapService.parseToken(token); - - const user = await this.ldapService.getUser(username); + const user = await this.ldapService.getUser(token); if (!user) throw new UnauthorizedException('User not found'); diff --git a/apps/api/src/ldap/ldap.service.ts b/apps/api/src/ldap/ldap.service.ts index 16442fe..a24415b 100644 --- a/apps/api/src/ldap/ldap.service.ts +++ b/apps/api/src/ldap/ldap.service.ts @@ -1,6 +1,7 @@ import type { DecodedToken, TokenPayload } from './types/jwt'; import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Inject, Injectable, UnauthorizedException } from '@nestjs/common'; +import type { JwtSignOptions } from '@nestjs/jwt'; import { JwtService } from '@nestjs/jwt'; import { Cache } from 'cache-manager'; import { env } from 'src/config/env'; @@ -14,23 +15,7 @@ export class LdapService { private readonly jwtService: JwtService ) {} - public parseToken(token: string) { - try { - return this.jwtService.decode(token) as DecodedToken; - } catch { - throw new UnauthorizedException('Invalid token'); - } - } - - public checkToken(token: string) { - try { - return this.jwtService.verify(token) as DecodedToken; - } catch { - throw new UnauthorizedException('Invalid token'); - } - } - - public async login({ login, password }: Credentials) { + public async login({ login, password }: Credentials, options?: JwtSignOptions) { try { const user = await ldap.authenticate(login, password); const { username } = user; @@ -42,14 +27,14 @@ export class LdapService { username, }; - return this.jwtService.sign(payload); + return this.jwtService.sign(payload, options); } catch (error) { throw new UnauthorizedException(error); } } public async logout(token: string) { - const { username } = this.parseToken(token); + const { username } = this.jwtService.decode(token) as DecodedToken; if (this.cacheManager.get(username)) { await this.cacheManager.del(username); @@ -58,7 +43,7 @@ export class LdapService { public async refreshToken(token: string) { try { - const { username } = this.parseToken(token); + const { username } = this.jwtService.decode(token) as DecodedToken; const user = await ldap.authenticate(username); await this.cacheManager.set(username, user); @@ -74,17 +59,23 @@ export class LdapService { } } - public async getUser(username: string) { - const cachedUser = (await this.cacheManager.get(username)) as ldap.User; + public async getUser(token: string) { + try { + const { username } = this.jwtService.verify(token) as DecodedToken; - if (!cachedUser) { - const user = await ldap.authenticate(username); + const cachedUser = (await this.cacheManager.get(username)) as ldap.User; - await this.cacheManager.set(username, user); + if (!cachedUser) { + const user = await ldap.authenticate(username); - return user; + await this.cacheManager.set(username, user); + + return user; + } + + return cachedUser; + } catch { + throw new UnauthorizedException('Invalid token'); } - - return cachedUser; } }