diff --git a/apps/api/src/auth/auth.service.ts b/apps/api/src/auth/auth.service.ts index fd0d912..738f6ba 100644 --- a/apps/api/src/auth/auth.service.ts +++ b/apps/api/src/auth/auth.service.ts @@ -13,23 +13,9 @@ export class AuthService { ) {} public async login(login: string, password: string) { - const { - displayName, - department, - title, - mail, - sAMAccountName: username, - } = await this.ldapService.authenticate(login, password); + const user = await this.ldapService.authenticate(login, password); + const { username } = user; - const user = { - username, - domain: process.env.LDAP_DOMAIN, - displayName, - department, - position: title, - mail, - domainName: `${process.env.LDAP_DOMAIN}\\${username}`, - }; await this.usersCache.addUser(username, user); const payload: TokenPayload = { diff --git a/apps/api/src/ldap/ldap.service.ts b/apps/api/src/ldap/ldap.service.ts index 3c15d1b..bc7f88d 100644 --- a/apps/api/src/ldap/ldap.service.ts +++ b/apps/api/src/ldap/ldap.service.ts @@ -1,6 +1,7 @@ import { Injectable } from '@nestjs/common'; import type { AuthenticationOptions } from 'ldap-authentication'; import { authenticate } from 'ldap-authentication'; +import type { User } from '../types/user'; import type { LdapUser } from './types/user'; @Injectable() @@ -19,8 +20,24 @@ export class LdapService { verifyUserExists: password === undefined, }; - const ldapUser: LdapUser = await authenticate(options); + const { + displayName, + department, + title, + mail, + sAMAccountName: username, + }: LdapUser = await authenticate(options); - return ldapUser; + const user: User = { + username, + domain: process.env.LDAP_DOMAIN, + displayName, + department, + position: title, + mail, + domainName: `${process.env.LDAP_DOMAIN}\\${username}`, + }; + + return user; } } diff --git a/apps/api/src/users/types/user.ts b/apps/api/src/types/user.ts similarity index 100% rename from apps/api/src/users/types/user.ts rename to apps/api/src/types/user.ts diff --git a/apps/api/src/users/users.cache.ts b/apps/api/src/users/users.cache.ts index 5e1d24f..1433e84 100644 --- a/apps/api/src/users/users.cache.ts +++ b/apps/api/src/users/users.cache.ts @@ -1,6 +1,6 @@ import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common'; import { Cache } from 'cache-manager'; -import type { User } from './types/user'; +import type { User } from '../types/user'; @Injectable() export class UsersCache { diff --git a/apps/api/src/users/users.module.ts b/apps/api/src/users/users.module.ts index 2bed8f0..1528709 100644 --- a/apps/api/src/users/users.module.ts +++ b/apps/api/src/users/users.module.ts @@ -1,6 +1,7 @@ import { CacheModule, Module } from '@nestjs/common'; import * as redisStore from 'cache-manager-ioredis'; import type { RedisOptions } from 'ioredis'; +import { LdapModule } from '../ldap/ldap.module'; import { UsersCache } from './users.cache'; import { UsersController } from './users.controller'; import { UsersService } from './users.service'; @@ -13,6 +14,7 @@ import { UsersService } from './users.service'; port: Number.parseInt(process.env.REDIS_PORT, 10) || 6379, ttl: Number.parseInt(process.env.CACHE_TTL, 10), }), + LdapModule, ], controllers: [UsersController], providers: [UsersService, UsersCache], diff --git a/apps/api/src/users/users.service.ts b/apps/api/src/users/users.service.ts index 22ccbd7..2a48f00 100644 --- a/apps/api/src/users/users.service.ts +++ b/apps/api/src/users/users.service.ts @@ -1,17 +1,30 @@ import { Injectable } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; +import { LdapService } from '../ldap/ldap.service'; import type { DecodedToken } from '../auth/types/jwt'; import { UsersCache } from './users.cache'; @Injectable() export class UsersService { - constructor(private readonly usersCache: UsersCache, private readonly jwtService: JwtService) {} + constructor( + private readonly usersCache: UsersCache, + private readonly jwtService: JwtService, + private readonly ldapService: LdapService + ) {} public async getUser(token: string) { const { username } = this.jwtService.decode(token) as DecodedToken; - const user = await this.usersCache.getUser(username); + const cachedUser = await this.usersCache.getUser(username); - return user; + if (!cachedUser) { + const user = await this.ldapService.authenticate(username); + + await this.usersCache.addUser(username, user); + + return user; + } + + return cachedUser; } }