apps/api: remove token methods from ldap controller

This commit is contained in:
vchikalkin 2024-05-14 12:40:02 +03:00
parent 410a35d451
commit 43e65ddbbe
2 changed files with 20 additions and 31 deletions

View File

@ -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');

View File

@ -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;
}
}