From 216baa42a109d93bef6b6e7219cee14fc591158f Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Sat, 4 Sep 2021 18:03:41 +0200 Subject: [PATCH] refactor: move TokenAuthGuard in the same file as TokenStrategy This should help to make clear why code is executed when the TokenAuthGuard is encountered by a request. Currently, one has to connect both files via the string 'token', which is a bit cryptic Signed-off-by: Philip Molares --- src/api/public/me/me.controller.ts | 2 +- src/api/public/media/media.controller.ts | 2 +- src/api/public/monitoring/monitoring.controller.ts | 2 +- src/api/public/notes/notes.controller.ts | 2 +- src/auth/token-auth.guard.ts | 10 ---------- src/auth/token.strategy.ts | 5 ++++- test/public-api/me.e2e-spec.ts | 2 +- test/public-api/media.e2e-spec.ts | 2 +- test/public-api/notes.e2e-spec.ts | 2 +- 9 files changed, 11 insertions(+), 18 deletions(-) delete mode 100644 src/auth/token-auth.guard.ts diff --git a/src/api/public/me/me.controller.ts b/src/api/public/me/me.controller.ts index fd5bcff84..ae05cbaac 100644 --- a/src/api/public/me/me.controller.ts +++ b/src/api/public/me/me.controller.ts @@ -23,7 +23,7 @@ import { ApiUnauthorizedResponse, } from '@nestjs/swagger'; -import { TokenAuthGuard } from '../../../auth/token-auth.guard'; +import { TokenAuthGuard } from '../../../auth/token.strategy'; import { NotInDBError } from '../../../errors/errors'; import { HistoryEntryUpdateDto } from '../../../history/history-entry-update.dto'; import { HistoryEntryDto } from '../../../history/history-entry.dto'; diff --git a/src/api/public/media/media.controller.ts b/src/api/public/media/media.controller.ts index 0401be2a5..76fbafd9b 100644 --- a/src/api/public/media/media.controller.ts +++ b/src/api/public/media/media.controller.ts @@ -31,7 +31,7 @@ import { ApiUnauthorizedResponse, } from '@nestjs/swagger'; -import { TokenAuthGuard } from '../../../auth/token-auth.guard'; +import { TokenAuthGuard } from '../../../auth/token.strategy'; import { ClientError, MediaBackendError, diff --git a/src/api/public/monitoring/monitoring.controller.ts b/src/api/public/monitoring/monitoring.controller.ts index 91a589dc0..030c10384 100644 --- a/src/api/public/monitoring/monitoring.controller.ts +++ b/src/api/public/monitoring/monitoring.controller.ts @@ -13,7 +13,7 @@ import { ApiUnauthorizedResponse, } from '@nestjs/swagger'; -import { TokenAuthGuard } from '../../../auth/token-auth.guard'; +import { TokenAuthGuard } from '../../../auth/token.strategy'; import { MonitoringService } from '../../../monitoring/monitoring.service'; import { ServerStatusDto } from '../../../monitoring/server-status.dto'; import { diff --git a/src/api/public/notes/notes.controller.ts b/src/api/public/notes/notes.controller.ts index 97691b8d2..81c719b5d 100644 --- a/src/api/public/notes/notes.controller.ts +++ b/src/api/public/notes/notes.controller.ts @@ -29,7 +29,7 @@ import { ApiUnauthorizedResponse, } from '@nestjs/swagger'; -import { TokenAuthGuard } from '../../../auth/token-auth.guard'; +import { TokenAuthGuard } from '../../../auth/token.strategy'; import { AlreadyInDBError, ForbiddenIdError, diff --git a/src/auth/token-auth.guard.ts b/src/auth/token-auth.guard.ts deleted file mode 100644 index ad919a6d9..000000000 --- a/src/auth/token-auth.guard.ts +++ /dev/null @@ -1,10 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) - * - * SPDX-License-Identifier: AGPL-3.0-only - */ -import { Injectable } from '@nestjs/common'; -import { AuthGuard } from '@nestjs/passport'; - -@Injectable() -export class TokenAuthGuard extends AuthGuard('token') {} diff --git a/src/auth/token.strategy.ts b/src/auth/token.strategy.ts index 65c952ac5..1a6010eab 100644 --- a/src/auth/token.strategy.ts +++ b/src/auth/token.strategy.ts @@ -4,13 +4,16 @@ * SPDX-License-Identifier: AGPL-3.0-only */ import { Injectable, UnauthorizedException } from '@nestjs/common'; -import { PassportStrategy } from '@nestjs/passport'; +import { AuthGuard, PassportStrategy } from '@nestjs/passport'; import { Strategy } from 'passport-http-bearer'; import { NotInDBError, TokenNotValidError } from '../errors/errors'; import { User } from '../users/user.entity'; import { AuthService } from './auth.service'; +@Injectable() +export class TokenAuthGuard extends AuthGuard('token') {} + @Injectable() export class TokenStrategy extends PassportStrategy(Strategy, 'token') { constructor(private authService: AuthService) { diff --git a/test/public-api/me.e2e-spec.ts b/test/public-api/me.e2e-spec.ts index 18b3a1a28..1c89b2b3b 100644 --- a/test/public-api/me.e2e-spec.ts +++ b/test/public-api/me.e2e-spec.ts @@ -14,7 +14,7 @@ import request from 'supertest'; import { PublicApiModule } from '../../src/api/public/public-api.module'; import { AuthModule } from '../../src/auth/auth.module'; import { MockAuthGuard } from '../../src/auth/mock-auth.guard'; -import { TokenAuthGuard } from '../../src/auth/token-auth.guard'; +import { TokenAuthGuard } from '../../src/auth/token.strategy'; import appConfigMock from '../../src/config/mock/app.config.mock'; import mediaConfigMock from '../../src/config/mock/media.config.mock'; import { GroupsModule } from '../../src/groups/groups.module'; diff --git a/test/public-api/media.e2e-spec.ts b/test/public-api/media.e2e-spec.ts index ec21f6ead..464255543 100644 --- a/test/public-api/media.e2e-spec.ts +++ b/test/public-api/media.e2e-spec.ts @@ -14,7 +14,7 @@ import request from 'supertest'; import { PublicApiModule } from '../../src/api/public/public-api.module'; import { AuthModule } from '../../src/auth/auth.module'; import { MockAuthGuard } from '../../src/auth/mock-auth.guard'; -import { TokenAuthGuard } from '../../src/auth/token-auth.guard'; +import { TokenAuthGuard } from '../../src/auth/token.strategy'; import appConfigMock from '../../src/config/mock/app.config.mock'; import mediaConfigMock from '../../src/config/mock/media.config.mock'; import { GroupsModule } from '../../src/groups/groups.module'; diff --git a/test/public-api/notes.e2e-spec.ts b/test/public-api/notes.e2e-spec.ts index f72b0d258..531a527a5 100644 --- a/test/public-api/notes.e2e-spec.ts +++ b/test/public-api/notes.e2e-spec.ts @@ -14,7 +14,7 @@ import request from 'supertest'; import { PublicApiModule } from '../../src/api/public/public-api.module'; import { AuthModule } from '../../src/auth/auth.module'; import { MockAuthGuard } from '../../src/auth/mock-auth.guard'; -import { TokenAuthGuard } from '../../src/auth/token-auth.guard'; +import { TokenAuthGuard } from '../../src/auth/token.strategy'; import appConfigMock from '../../src/config/mock/app.config.mock'; import mediaConfigMock from '../../src/config/mock/media.config.mock'; import { NotInDBError } from '../../src/errors/errors';