Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

refactor(auth): hydrate access-token user from JWT payload

johaven31f2e9f01855
diff --git a/backend/src/authentication/guards/auth-token-access.guard.spec.ts b/backend/src/authentication/guards/auth-token-access.guard.spec.ts
index 83f3f154..e4be9cde 100644
--- a/backend/src/authentication/guards/auth-token-access.guard.spec.ts
+++ b/backend/src/authentication/guards/auth-token-access.guard.spec.ts
@@ -8,8 +8,7 @@ import { PassportModule } from '@nestjs/passport'
import { Test, TestingModule } from '@nestjs/testing'
import { PinoLogger } from 'nestjs-pino'
import crypto from 'node:crypto'
-import { UserModel } from '../../applications/users/models/user.model'
-import { UsersManager } from '../../applications/users/services/users-manager.service'
+import { USER_PERMISSION, USER_ROLE } from '../../applications/users/constants/user'
import { WEB_DAV_CONTEXT, WebDAVContext } from '../../applications/webdav/decorators/webdav-context.decorator'
import { exportConfiguration } from '../../configuration/config.environment'
import { AuthConfig } from '../auth.config'
@@ -17,7 +16,7 @@ import { AuthManager } from '../auth.service'
import { CSRF_ERROR } from '../constants/auth'
import { AuthTokenOptional } from '../decorators/auth-token-optional.decorator'
import { AUTH_TOKEN_SKIP, AuthTokenSkip } from '../decorators/auth-token-skip.decorator'
-import { JwtPayload } from '../interfaces/jwt-payload.interface'
+import { JwtIdentityPayload, JwtPayload } from '../interfaces/jwt-payload.interface'
import { TOKEN_TYPE } from '../interfaces/token.interface'
import { AuthAnonymousGuard } from './auth-anonymous.guard'
import { AuthAnonymousStrategy } from './auth-anonymous.strategy'
@@ -26,6 +25,15 @@ import { AuthTokenAccessStrategy } from './auth-token-access.strategy'
describe(AuthTokenAccessGuard.name, () => {
const csrfToken: string = crypto.randomUUID()
+ const accessTokenIdentity: JwtIdentityPayload = {
+ id: 1,
+ login: 'foo',
+ email: '[email protected]',
+ fullName: 'Foo Bar',
+ language: 'fr',
+ role: USER_ROLE.USER,
+ applications: [USER_PERMISSION.DESKTOP_APP, USER_PERMISSION.PERSONAL_SPACE]
+ }
let authConfig: AuthConfig
let jwtService: JwtService
let authAccessGuard: AuthTokenAccessGuard
@@ -36,9 +44,6 @@ describe(AuthTokenAccessGuard.name, () => {
let accessToken: string
let temporaryTwoFaToken: string
let context: DeepMocked<ExecutionContext>
- const usersManager = {
- fromAuthToken: vi.fn(async (user: UserModel): Promise<UserModel | null> => user)
- }
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
@@ -55,7 +60,6 @@ describe(AuthTokenAccessGuard.name, () => {
AuthAnonymousStrategy,
AuthAnonymousGuard,
AuthManager,
- { provide: UsersManager, useValue: usersManager },
{
provide: PinoLogger,
useValue: {
@@ -72,13 +76,13 @@ describe(AuthTokenAccessGuard.name, () => {
authAnonymousStrategy = module.get<AuthAnonymousStrategy>(AuthAnonymousStrategy)
authAnonymousGuard = module.get<AuthAnonymousGuard>(AuthAnonymousGuard)
accessToken = await jwtService.signAsync(
- { tokenType: TOKEN_TYPE.ACCESS, identity: { id: 1, login: 'foo' }, [TOKEN_TYPE.CSRF]: csrfToken } as JwtPayload,
+ { tokenType: TOKEN_TYPE.ACCESS, identity: accessTokenIdentity, [TOKEN_TYPE.CSRF]: csrfToken } as JwtPayload,
{
secret: authConfig.token.access.secret,
expiresIn: 30
}
)
- accessTokenWithoutCSRF = await jwtService.signAsync({ tokenType: TOKEN_TYPE.ACCESS, identity: { id: 1, login: 'foo' } } as JwtPayload, {
+ accessTokenWithoutCSRF = await jwtService.signAsync({ tokenType: TOKEN_TYPE.ACCESS, identity: accessTokenIdentity } as JwtPayload, {
secret: authConfig.token.access.secret,
expiresIn: 30
})
@@ -125,15 +129,19 @@ describe(AuthTokenAccessGuard.name, () => {
expect(await authAccessGuard.canActivate(context)).toBe(true)
})
- it('should reject a valid access token when the current user is unavailable', async () => {
- usersManager.fromAuthToken.mockResolvedValueOnce(null)
- context.switchToHttp().getRequest.mockReturnValue({
+ it('should hydrate user from the access token identity', async () => {
+ const request = {
raw: { user: '' },
headers: {
authorization: `Bearer ${accessToken}`
}
- })
- await expect(authAccessGuard.canActivate(context)).rejects.toThrow('Unauthorized')
+ } as any
+ context.switchToHttp().getRequest.mockReturnValue(request)
+
+ expect(await authAccessGuard.canActivate(context)).toBe(true)
+ expect(request.user).toMatchObject(accessTokenIdentity)
+ expect(request.user.haveRole(USER_ROLE.USER)).toBe(true)
+ expect(request.user.havePermission(USER_PERMISSION.DESKTOP_APP)).toBe(true)
})
it('should throw an error with an invalid access token in cookies', async () => {
diff --git a/backend/src/authentication/guards/auth-token-access.strategy.ts b/backend/src/authentication/guards/auth-token-access.strategy.ts
index 62e497c5..34eb4827 100755
--- a/backend/src/authentication/guards/auth-token-access.strategy.ts
+++ b/backend/src/authentication/guards/auth-token-access.strategy.ts
@@ -4,7 +4,6 @@ import { FastifyRequest } from 'fastify'
import { PinoLogger } from 'nestjs-pino'
import { ExtractJwt, Strategy } from 'passport-jwt'
import { UserModel } from '../../applications/users/models/user.model'
-import { UsersManager } from '../../applications/users/services/users-manager.service'
import { configuration } from '../../configuration/config.environment'
import { AuthManager } from '../auth.service'
import { JwtPayload } from '../interfaces/jwt-payload.interface'
@@ -16,7 +15,6 @@ export class AuthTokenAccessStrategy extends PassportStrategy(Strategy, 'tokenAc
constructor(
private readonly authManager: AuthManager,
- private readonly usersManager: UsersManager,
private readonly logger: PinoLogger
) {
super({
@@ -28,17 +26,13 @@ export class AuthTokenAccessStrategy extends PassportStrategy(Strategy, 'tokenAc
AuthTokenAccessStrategy.accessCookieName = configuration.auth.token.access.name
}
- async validate(req: FastifyRequest, jwtPayload: JwtPayload): Promise<UserModel> {
+ validate(req: FastifyRequest, jwtPayload: JwtPayload): UserModel {
if (jwtPayload.tokenType !== TOKEN_TYPE.ACCESS) {
throw new UnauthorizedException()
}
this.logger.assign({ user: jwtPayload.identity.login })
this.authManager.csrfValidation(req, jwtPayload, TOKEN_TYPE.ACCESS)
- const user = await this.usersManager.fromAuthToken(new UserModel(jwtPayload.identity))
- if (!user) {
- throw new UnauthorizedException()
- }
- return user
+ return new UserModel(jwtPayload.identity)
}
static extractJWTFromCookie(req: FastifyRequest): string | null {