mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-14 23:24:46 -04:00
auth: adds token-auth to public api
adds auth service adds auth module adds token-auth strategy adds token-auth to all public api calls Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
parent
4784a1aea2
commit
2ab950c5c3
11 changed files with 174 additions and 18 deletions
|
@ -31,8 +31,8 @@
|
||||||
"@nestjs/platform-express": "7.6.5",
|
"@nestjs/platform-express": "7.6.5",
|
||||||
"@nestjs/swagger": "4.7.12",
|
"@nestjs/swagger": "4.7.12",
|
||||||
"@nestjs/typeorm": "7.1.5",
|
"@nestjs/typeorm": "7.1.5",
|
||||||
"@types/bcrypt": "^3.0.0",
|
|
||||||
"@types/passport-http-bearer": "^1.0.36",
|
"@types/passport-http-bearer": "^1.0.36",
|
||||||
|
"@types/bcrypt": "^3.0.0",
|
||||||
"bcrypt": "^5.0.0",
|
"bcrypt": "^5.0.0",
|
||||||
"class-transformer": "0.3.2",
|
"class-transformer": "0.3.2",
|
||||||
"class-validator": "0.13.1",
|
"class-validator": "0.13.1",
|
||||||
|
|
|
@ -13,6 +13,8 @@ import {
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
Param,
|
Param,
|
||||||
Put,
|
Put,
|
||||||
|
UseGuards,
|
||||||
|
Request,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
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';
|
||||||
|
@ -22,6 +24,7 @@ import { NoteMetadataDto } from '../../../notes/note-metadata.dto';
|
||||||
import { NotesService } from '../../../notes/notes.service';
|
import { NotesService } from '../../../notes/notes.service';
|
||||||
import { UserInfoDto } from '../../../users/user-info.dto';
|
import { UserInfoDto } from '../../../users/user-info.dto';
|
||||||
import { UsersService } from '../../../users/users.service';
|
import { UsersService } from '../../../users/users.service';
|
||||||
|
import { TokenAuthGuard } from '../../../auth/token-auth.guard';
|
||||||
|
|
||||||
@Controller('me')
|
@Controller('me')
|
||||||
export class MeController {
|
export class MeController {
|
||||||
|
@ -34,29 +37,36 @@ export class MeController {
|
||||||
this.logger.setContext(MeController.name);
|
this.logger.setContext(MeController.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UseGuards(TokenAuthGuard)
|
||||||
@Get()
|
@Get()
|
||||||
async getMe(): Promise<UserInfoDto> {
|
async getMe(@Request() req): Promise<UserInfoDto> {
|
||||||
return this.usersService.toUserDto(
|
return this.usersService.toUserDto(
|
||||||
await this.usersService.getUserByUsername('hardcoded'),
|
await this.usersService.getUserByUsername(req.user.userName),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UseGuards(TokenAuthGuard)
|
||||||
@Get('history')
|
@Get('history')
|
||||||
getUserHistory(): HistoryEntryDto[] {
|
getUserHistory(@Request() req): HistoryEntryDto[] {
|
||||||
return this.historyService.getUserHistory('someone');
|
return this.historyService.getUserHistory(req.user.userName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UseGuards(TokenAuthGuard)
|
||||||
@Put('history/:note')
|
@Put('history/:note')
|
||||||
updateHistoryEntry(
|
updateHistoryEntry(
|
||||||
|
@Request() req,
|
||||||
@Param('note') note: string,
|
@Param('note') note: string,
|
||||||
@Body() entryUpdateDto: HistoryEntryUpdateDto,
|
@Body() entryUpdateDto: HistoryEntryUpdateDto,
|
||||||
): HistoryEntryDto {
|
): HistoryEntryDto {
|
||||||
|
// ToDo: Check if user is allowed to pin this history entry
|
||||||
return this.historyService.updateHistoryEntry(note, entryUpdateDto);
|
return this.historyService.updateHistoryEntry(note, entryUpdateDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UseGuards(TokenAuthGuard)
|
||||||
@Delete('history/:note')
|
@Delete('history/:note')
|
||||||
@HttpCode(204)
|
@HttpCode(204)
|
||||||
deleteHistoryEntry(@Param('note') note: string) {
|
deleteHistoryEntry(@Request() req, @Param('note') note: string) {
|
||||||
|
// ToDo: Check if user is allowed to delete note
|
||||||
try {
|
try {
|
||||||
return this.historyService.deleteHistoryEntry(note);
|
return this.historyService.deleteHistoryEntry(note);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -64,8 +74,9 @@ export class MeController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UseGuards(TokenAuthGuard)
|
||||||
@Get('notes')
|
@Get('notes')
|
||||||
getMyNotes(): NoteMetadataDto[] {
|
getMyNotes(@Request() req): NoteMetadataDto[] {
|
||||||
return this.notesService.getUserNotes('someone');
|
return this.notesService.getUserNotes(req.user.userName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,8 +12,10 @@ import {
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
Param,
|
Param,
|
||||||
Post,
|
Post,
|
||||||
|
Request,
|
||||||
UnauthorizedException,
|
UnauthorizedException,
|
||||||
UploadedFile,
|
UploadedFile,
|
||||||
|
UseGuards,
|
||||||
UseInterceptors,
|
UseInterceptors,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { FileInterceptor } from '@nestjs/platform-express';
|
import { FileInterceptor } from '@nestjs/platform-express';
|
||||||
|
@ -25,6 +27,7 @@ import {
|
||||||
import { ConsoleLoggerService } from '../../../logger/console-logger.service';
|
import { ConsoleLoggerService } from '../../../logger/console-logger.service';
|
||||||
import { MediaService } from '../../../media/media.service';
|
import { MediaService } from '../../../media/media.service';
|
||||||
import { MulterFile } from '../../../media/multer-file.interface';
|
import { MulterFile } from '../../../media/multer-file.interface';
|
||||||
|
import { TokenAuthGuard } from '../../../auth/token-auth.guard';
|
||||||
|
|
||||||
@Controller('media')
|
@Controller('media')
|
||||||
export class MediaController {
|
export class MediaController {
|
||||||
|
@ -35,14 +38,16 @@ export class MediaController {
|
||||||
this.logger.setContext(MediaController.name);
|
this.logger.setContext(MediaController.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UseGuards(TokenAuthGuard)
|
||||||
@Post()
|
@Post()
|
||||||
@UseInterceptors(FileInterceptor('file'))
|
@UseInterceptors(FileInterceptor('file'))
|
||||||
async uploadMedia(
|
async uploadMedia(
|
||||||
|
@Request() req,
|
||||||
@UploadedFile() file: MulterFile,
|
@UploadedFile() file: MulterFile,
|
||||||
@Headers('HedgeDoc-Note') noteId: string,
|
@Headers('HedgeDoc-Note') noteId: string,
|
||||||
) {
|
) {
|
||||||
//TODO: Get user from request
|
//TODO: Get user from request
|
||||||
const username = 'hardcoded';
|
const username = req.user.userName;
|
||||||
this.logger.debug(
|
this.logger.debug(
|
||||||
`Recieved filename '${file.originalname}' for note '${noteId}' from user '${username}'`,
|
`Recieved filename '${file.originalname}' for note '${noteId}' from user '${username}'`,
|
||||||
'uploadImage',
|
'uploadImage',
|
||||||
|
@ -64,10 +69,11 @@ export class MediaController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UseGuards(TokenAuthGuard)
|
||||||
@Delete(':filename')
|
@Delete(':filename')
|
||||||
async deleteMedia(@Param('filename') filename: string) {
|
async deleteMedia(@Request() req, @Param('filename') filename: string) {
|
||||||
//TODO: Get user from request
|
//TODO: Get user from request
|
||||||
const username = 'hardcoded';
|
const username = req.user.userName;
|
||||||
try {
|
try {
|
||||||
await this.mediaService.deleteFile(filename, username);
|
await this.mediaService.deleteFile(filename, username);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
@ -4,18 +4,21 @@
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Controller, Get } from '@nestjs/common';
|
import { Controller, Get, UseGuards } from '@nestjs/common';
|
||||||
import { MonitoringService } from '../../../monitoring/monitoring.service';
|
import { MonitoringService } from '../../../monitoring/monitoring.service';
|
||||||
|
import { TokenAuthGuard } from '../../../auth/token-auth.guard';
|
||||||
|
|
||||||
@Controller('monitoring')
|
@Controller('monitoring')
|
||||||
export class MonitoringController {
|
export class MonitoringController {
|
||||||
constructor(private monitoringService: MonitoringService) {}
|
constructor(private monitoringService: MonitoringService) {}
|
||||||
|
|
||||||
|
@UseGuards(TokenAuthGuard)
|
||||||
@Get()
|
@Get()
|
||||||
getStatus() {
|
getStatus() {
|
||||||
return this.monitoringService.getServerStatus();
|
return this.monitoringService.getServerStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UseGuards(TokenAuthGuard)
|
||||||
@Get('prometheus')
|
@Get('prometheus')
|
||||||
getPrometheusStatus() {
|
getPrometheusStatus() {
|
||||||
return '';
|
return '';
|
||||||
|
|
|
@ -14,6 +14,8 @@ import {
|
||||||
Param,
|
Param,
|
||||||
Post,
|
Post,
|
||||||
Put,
|
Put,
|
||||||
|
Request,
|
||||||
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { NotInDBError } from '../../../errors/errors';
|
import { NotInDBError } from '../../../errors/errors';
|
||||||
import { ConsoleLoggerService } from '../../../logger/console-logger.service';
|
import { ConsoleLoggerService } from '../../../logger/console-logger.service';
|
||||||
|
@ -21,6 +23,7 @@ import { NotePermissionsUpdateDto } from '../../../notes/note-permissions.dto';
|
||||||
import { NotesService } from '../../../notes/notes.service';
|
import { NotesService } from '../../../notes/notes.service';
|
||||||
import { RevisionsService } from '../../../revisions/revisions.service';
|
import { RevisionsService } from '../../../revisions/revisions.service';
|
||||||
import { MarkdownBody } from '../../utils/markdownbody-decorator';
|
import { MarkdownBody } from '../../utils/markdownbody-decorator';
|
||||||
|
import { TokenAuthGuard } from '../../../auth/token-auth.guard';
|
||||||
|
|
||||||
@Controller('notes')
|
@Controller('notes')
|
||||||
export class NotesController {
|
export class NotesController {
|
||||||
|
@ -32,14 +35,18 @@ export class NotesController {
|
||||||
this.logger.setContext(NotesController.name);
|
this.logger.setContext(NotesController.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UseGuards(TokenAuthGuard)
|
||||||
@Post()
|
@Post()
|
||||||
async createNote(@MarkdownBody() text: string) {
|
async createNote(@Request() req, @MarkdownBody() text: string) {
|
||||||
|
// ToDo: provide user for createNoteDto
|
||||||
this.logger.debug('Got raw markdown:\n' + text);
|
this.logger.debug('Got raw markdown:\n' + text);
|
||||||
return this.noteService.createNoteDto(text);
|
return this.noteService.createNoteDto(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UseGuards(TokenAuthGuard)
|
||||||
@Get(':noteIdOrAlias')
|
@Get(':noteIdOrAlias')
|
||||||
async getNote(@Param('noteIdOrAlias') noteIdOrAlias: string) {
|
async getNote(@Request() req, @Param('noteIdOrAlias') noteIdOrAlias: string) {
|
||||||
|
// ToDo: check if user is allowed to view this note
|
||||||
try {
|
try {
|
||||||
return await this.noteService.getNoteDtoByIdOrAlias(noteIdOrAlias);
|
return await this.noteService.getNoteDtoByIdOrAlias(noteIdOrAlias);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -50,17 +57,25 @@ export class NotesController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UseGuards(TokenAuthGuard)
|
||||||
@Post(':noteAlias')
|
@Post(':noteAlias')
|
||||||
async createNamedNote(
|
async createNamedNote(
|
||||||
|
@Request() req,
|
||||||
@Param('noteAlias') noteAlias: string,
|
@Param('noteAlias') noteAlias: string,
|
||||||
@MarkdownBody() text: string,
|
@MarkdownBody() text: string,
|
||||||
) {
|
) {
|
||||||
|
// ToDo: check if user is allowed to view this note
|
||||||
this.logger.debug('Got raw markdown:\n' + text);
|
this.logger.debug('Got raw markdown:\n' + text);
|
||||||
return this.noteService.createNoteDto(text, noteAlias);
|
return this.noteService.createNoteDto(text, noteAlias);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UseGuards(TokenAuthGuard)
|
||||||
@Delete(':noteIdOrAlias')
|
@Delete(':noteIdOrAlias')
|
||||||
async deleteNote(@Param('noteIdOrAlias') noteIdOrAlias: string) {
|
async deleteNote(
|
||||||
|
@Request() req,
|
||||||
|
@Param('noteIdOrAlias') noteIdOrAlias: string,
|
||||||
|
) {
|
||||||
|
// ToDo: check if user is allowed to delete this note
|
||||||
this.logger.debug('Deleting note: ' + noteIdOrAlias);
|
this.logger.debug('Deleting note: ' + noteIdOrAlias);
|
||||||
try {
|
try {
|
||||||
await this.noteService.deleteNoteByIdOrAlias(noteIdOrAlias);
|
await this.noteService.deleteNoteByIdOrAlias(noteIdOrAlias);
|
||||||
|
@ -74,11 +89,14 @@ export class NotesController {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UseGuards(TokenAuthGuard)
|
||||||
@Put(':noteIdOrAlias')
|
@Put(':noteIdOrAlias')
|
||||||
async updateNote(
|
async updateNote(
|
||||||
|
@Request() req,
|
||||||
@Param('noteIdOrAlias') noteIdOrAlias: string,
|
@Param('noteIdOrAlias') noteIdOrAlias: string,
|
||||||
@MarkdownBody() text: string,
|
@MarkdownBody() text: string,
|
||||||
) {
|
) {
|
||||||
|
// ToDo: check if user is allowed to change this note
|
||||||
this.logger.debug('Got raw markdown:\n' + text);
|
this.logger.debug('Got raw markdown:\n' + text);
|
||||||
try {
|
try {
|
||||||
return await this.noteService.updateNoteByIdOrAlias(noteIdOrAlias, text);
|
return await this.noteService.updateNoteByIdOrAlias(noteIdOrAlias, text);
|
||||||
|
@ -90,9 +108,14 @@ export class NotesController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UseGuards(TokenAuthGuard)
|
||||||
@Get(':noteIdOrAlias/content')
|
@Get(':noteIdOrAlias/content')
|
||||||
@Header('content-type', 'text/markdown')
|
@Header('content-type', 'text/markdown')
|
||||||
async getNoteContent(@Param('noteIdOrAlias') noteIdOrAlias: string) {
|
async getNoteContent(
|
||||||
|
@Request() req,
|
||||||
|
@Param('noteIdOrAlias') noteIdOrAlias: string,
|
||||||
|
) {
|
||||||
|
// ToDo: check if user is allowed to view this notes content
|
||||||
try {
|
try {
|
||||||
return await this.noteService.getNoteContent(noteIdOrAlias);
|
return await this.noteService.getNoteContent(noteIdOrAlias);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -103,8 +126,13 @@ export class NotesController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UseGuards(TokenAuthGuard)
|
||||||
@Get(':noteIdOrAlias/metadata')
|
@Get(':noteIdOrAlias/metadata')
|
||||||
async getNoteMetadata(@Param('noteIdOrAlias') noteIdOrAlias: string) {
|
async getNoteMetadata(
|
||||||
|
@Request() req,
|
||||||
|
@Param('noteIdOrAlias') noteIdOrAlias: string,
|
||||||
|
) {
|
||||||
|
// ToDo: check if user is allowed to view this notes metadata
|
||||||
try {
|
try {
|
||||||
return await this.noteService.getNoteMetadata(noteIdOrAlias);
|
return await this.noteService.getNoteMetadata(noteIdOrAlias);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -115,11 +143,14 @@ export class NotesController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UseGuards(TokenAuthGuard)
|
||||||
@Put(':noteIdOrAlias/metadata/permissions')
|
@Put(':noteIdOrAlias/metadata/permissions')
|
||||||
async updateNotePermissions(
|
async updateNotePermissions(
|
||||||
|
@Request() req,
|
||||||
@Param('noteIdOrAlias') noteIdOrAlias: string,
|
@Param('noteIdOrAlias') noteIdOrAlias: string,
|
||||||
@Body() updateDto: NotePermissionsUpdateDto,
|
@Body() updateDto: NotePermissionsUpdateDto,
|
||||||
) {
|
) {
|
||||||
|
// ToDo: check if user is allowed to view this notes permissions
|
||||||
try {
|
try {
|
||||||
return await this.noteService.updateNotePermissions(
|
return await this.noteService.updateNotePermissions(
|
||||||
noteIdOrAlias,
|
noteIdOrAlias,
|
||||||
|
@ -133,8 +164,13 @@ export class NotesController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UseGuards(TokenAuthGuard)
|
||||||
@Get(':noteIdOrAlias/revisions')
|
@Get(':noteIdOrAlias/revisions')
|
||||||
async getNoteRevisions(@Param('noteIdOrAlias') noteIdOrAlias: string) {
|
async getNoteRevisions(
|
||||||
|
@Request() req,
|
||||||
|
@Param('noteIdOrAlias') noteIdOrAlias: string,
|
||||||
|
) {
|
||||||
|
// ToDo: check if user is allowed to view this notes revisions
|
||||||
try {
|
try {
|
||||||
return await this.revisionsService.getNoteRevisionMetadatas(
|
return await this.revisionsService.getNoteRevisionMetadatas(
|
||||||
noteIdOrAlias,
|
noteIdOrAlias,
|
||||||
|
@ -147,11 +183,14 @@ export class NotesController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UseGuards(TokenAuthGuard)
|
||||||
@Get(':noteIdOrAlias/revisions/:revisionId')
|
@Get(':noteIdOrAlias/revisions/:revisionId')
|
||||||
async getNoteRevision(
|
async getNoteRevision(
|
||||||
|
@Request() req,
|
||||||
@Param('noteIdOrAlias') noteIdOrAlias: string,
|
@Param('noteIdOrAlias') noteIdOrAlias: string,
|
||||||
@Param('revisionId') revisionId: number,
|
@Param('revisionId') revisionId: number,
|
||||||
) {
|
) {
|
||||||
|
// ToDo: check if user is allowed to view this notes revision
|
||||||
try {
|
try {
|
||||||
return await this.revisionsService.getNoteRevision(
|
return await this.revisionsService.getNoteRevision(
|
||||||
noteIdOrAlias,
|
noteIdOrAlias,
|
||||||
|
|
|
@ -18,6 +18,7 @@ import { NotesModule } from './notes/notes.module';
|
||||||
import { PermissionsModule } from './permissions/permissions.module';
|
import { PermissionsModule } from './permissions/permissions.module';
|
||||||
import { RevisionsModule } from './revisions/revisions.module';
|
import { RevisionsModule } from './revisions/revisions.module';
|
||||||
import { UsersModule } from './users/users.module';
|
import { UsersModule } from './users/users.module';
|
||||||
|
import { AuthModule } from './auth/auth.module';
|
||||||
import appConfig from './config/app.config';
|
import appConfig from './config/app.config';
|
||||||
import mediaConfig from './config/media.config';
|
import mediaConfig from './config/media.config';
|
||||||
import hstsConfig from './config/hsts.config';
|
import hstsConfig from './config/hsts.config';
|
||||||
|
@ -55,6 +56,7 @@ import authConfig from './config/auth.config';
|
||||||
GroupsModule,
|
GroupsModule,
|
||||||
LoggerModule,
|
LoggerModule,
|
||||||
MediaModule,
|
MediaModule,
|
||||||
|
AuthModule,
|
||||||
],
|
],
|
||||||
controllers: [],
|
controllers: [],
|
||||||
providers: [],
|
providers: [],
|
||||||
|
|
11
src/auth/auth.module.ts
Normal file
11
src/auth/auth.module.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { AuthService } from './auth.service';
|
||||||
|
import { UsersModule } from '../users/users.module';
|
||||||
|
import { PassportModule } from '@nestjs/passport';
|
||||||
|
import { TokenStrategy } from './token.strategy';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
imports: [UsersModule, PassportModule],
|
||||||
|
providers: [AuthService, TokenStrategy],
|
||||||
|
})
|
||||||
|
export class AuthModule {}
|
31
src/auth/auth.service.spec.ts
Normal file
31
src/auth/auth.service.spec.ts
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
import { AuthService } from './auth.service';
|
||||||
|
import { UsersModule } from '../users/users.module';
|
||||||
|
import { getRepositoryToken } from '@nestjs/typeorm';
|
||||||
|
import { User } from '../users/user.entity';
|
||||||
|
|
||||||
|
describe('AuthService', () => {
|
||||||
|
let service: AuthService;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
providers: [
|
||||||
|
AuthService,
|
||||||
|
{
|
||||||
|
provide: getRepositoryToken(User),
|
||||||
|
useValue: {},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
imports: [UsersModule],
|
||||||
|
})
|
||||||
|
.overrideProvider(getRepositoryToken(User))
|
||||||
|
.useValue({})
|
||||||
|
.compile();
|
||||||
|
|
||||||
|
service = module.get<AuthService>(AuthService);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be defined', () => {
|
||||||
|
expect(service).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
16
src/auth/auth.service.ts
Normal file
16
src/auth/auth.service.ts
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { UsersService } from '../users/users.service';
|
||||||
|
import { User } from '../users/user.entity';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class AuthService {
|
||||||
|
constructor(private usersService: UsersService) {}
|
||||||
|
|
||||||
|
async validateToken(token: string): Promise<User> {
|
||||||
|
const user = await this.usersService.getUserByAuthToken(token);
|
||||||
|
if (user) {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
11
src/auth/token-auth.guard.ts
Normal file
11
src/auth/token-auth.guard.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
/*
|
||||||
|
* 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') {}
|
26
src/auth/token.strategy.ts
Normal file
26
src/auth/token.strategy.ts
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { Strategy } from 'passport-http-bearer';
|
||||||
|
import { PassportStrategy } from '@nestjs/passport';
|
||||||
|
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||||||
|
import { AuthService } from './auth.service';
|
||||||
|
import { User } from '../users/user.entity';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class TokenStrategy extends PassportStrategy(Strategy, 'token') {
|
||||||
|
constructor(private authService: AuthService) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
async validate(token: string): Promise<User> {
|
||||||
|
const user = await this.authService.validateToken(token);
|
||||||
|
if (!user) {
|
||||||
|
throw new UnauthorizedException();
|
||||||
|
}
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue