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 <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-09-04 18:03:41 +02:00 committed by David Mehren
parent 5ecb0c0694
commit 216baa42a1
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
9 changed files with 11 additions and 18 deletions

View file

@ -23,7 +23,7 @@ import {
ApiUnauthorizedResponse, ApiUnauthorizedResponse,
} from '@nestjs/swagger'; } from '@nestjs/swagger';
import { TokenAuthGuard } from '../../../auth/token-auth.guard'; import { TokenAuthGuard } from '../../../auth/token.strategy';
import { NotInDBError } from '../../../errors/errors'; import { NotInDBError } from '../../../errors/errors';
import { HistoryEntryUpdateDto } from '../../../history/history-entry-update.dto'; import { HistoryEntryUpdateDto } from '../../../history/history-entry-update.dto';
import { HistoryEntryDto } from '../../../history/history-entry.dto'; import { HistoryEntryDto } from '../../../history/history-entry.dto';

View file

@ -31,7 +31,7 @@ import {
ApiUnauthorizedResponse, ApiUnauthorizedResponse,
} from '@nestjs/swagger'; } from '@nestjs/swagger';
import { TokenAuthGuard } from '../../../auth/token-auth.guard'; import { TokenAuthGuard } from '../../../auth/token.strategy';
import { import {
ClientError, ClientError,
MediaBackendError, MediaBackendError,

View file

@ -13,7 +13,7 @@ import {
ApiUnauthorizedResponse, ApiUnauthorizedResponse,
} from '@nestjs/swagger'; } from '@nestjs/swagger';
import { TokenAuthGuard } from '../../../auth/token-auth.guard'; import { TokenAuthGuard } from '../../../auth/token.strategy';
import { MonitoringService } from '../../../monitoring/monitoring.service'; import { MonitoringService } from '../../../monitoring/monitoring.service';
import { ServerStatusDto } from '../../../monitoring/server-status.dto'; import { ServerStatusDto } from '../../../monitoring/server-status.dto';
import { import {

View file

@ -29,7 +29,7 @@ import {
ApiUnauthorizedResponse, ApiUnauthorizedResponse,
} from '@nestjs/swagger'; } from '@nestjs/swagger';
import { TokenAuthGuard } from '../../../auth/token-auth.guard'; import { TokenAuthGuard } from '../../../auth/token.strategy';
import { import {
AlreadyInDBError, AlreadyInDBError,
ForbiddenIdError, ForbiddenIdError,

View file

@ -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') {}

View file

@ -4,13 +4,16 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { Injectable, UnauthorizedException } from '@nestjs/common'; import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport'; import { AuthGuard, PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-http-bearer'; import { Strategy } from 'passport-http-bearer';
import { NotInDBError, TokenNotValidError } from '../errors/errors'; import { NotInDBError, TokenNotValidError } from '../errors/errors';
import { User } from '../users/user.entity'; import { User } from '../users/user.entity';
import { AuthService } from './auth.service'; import { AuthService } from './auth.service';
@Injectable()
export class TokenAuthGuard extends AuthGuard('token') {}
@Injectable() @Injectable()
export class TokenStrategy extends PassportStrategy(Strategy, 'token') { export class TokenStrategy extends PassportStrategy(Strategy, 'token') {
constructor(private authService: AuthService) { constructor(private authService: AuthService) {

View file

@ -14,7 +14,7 @@ import request from 'supertest';
import { PublicApiModule } from '../../src/api/public/public-api.module'; import { PublicApiModule } from '../../src/api/public/public-api.module';
import { AuthModule } from '../../src/auth/auth.module'; import { AuthModule } from '../../src/auth/auth.module';
import { MockAuthGuard } from '../../src/auth/mock-auth.guard'; 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 appConfigMock from '../../src/config/mock/app.config.mock';
import mediaConfigMock from '../../src/config/mock/media.config.mock'; import mediaConfigMock from '../../src/config/mock/media.config.mock';
import { GroupsModule } from '../../src/groups/groups.module'; import { GroupsModule } from '../../src/groups/groups.module';

View file

@ -14,7 +14,7 @@ import request from 'supertest';
import { PublicApiModule } from '../../src/api/public/public-api.module'; import { PublicApiModule } from '../../src/api/public/public-api.module';
import { AuthModule } from '../../src/auth/auth.module'; import { AuthModule } from '../../src/auth/auth.module';
import { MockAuthGuard } from '../../src/auth/mock-auth.guard'; 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 appConfigMock from '../../src/config/mock/app.config.mock';
import mediaConfigMock from '../../src/config/mock/media.config.mock'; import mediaConfigMock from '../../src/config/mock/media.config.mock';
import { GroupsModule } from '../../src/groups/groups.module'; import { GroupsModule } from '../../src/groups/groups.module';

View file

@ -14,7 +14,7 @@ import request from 'supertest';
import { PublicApiModule } from '../../src/api/public/public-api.module'; import { PublicApiModule } from '../../src/api/public/public-api.module';
import { AuthModule } from '../../src/auth/auth.module'; import { AuthModule } from '../../src/auth/auth.module';
import { MockAuthGuard } from '../../src/auth/mock-auth.guard'; 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 appConfigMock from '../../src/config/mock/app.config.mock';
import mediaConfigMock from '../../src/config/mock/media.config.mock'; import mediaConfigMock from '../../src/config/mock/media.config.mock';
import { NotInDBError } from '../../src/errors/errors'; import { NotInDBError } from '../../src/errors/errors';