Add /auth endpoint

This commit is contained in:
Lea 2023-07-03 20:00:23 +02:00
parent 92ac80f4fa
commit fc7a87a7c7
3 changed files with 53 additions and 26 deletions

View file

@ -1,6 +1,30 @@
import { Controller, Get } from '@nestjs/common'; import { Controller, Get, UseGuards } from '@nestjs/common';
import { ApiInfo, AppService } from './app.service'; import { AppService } from './app.service';
import { ApiOperation } from '@nestjs/swagger'; import {
ApiOperation,
ApiProperty,
ApiResponse,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';
import { META } from './meta';
import { AuthData } from './auth/authdata.decorator';
import { AuthGuard, AuthenticationData } from './auth/auth.guard';
export class ApiInfo {
@ApiProperty()
version: string;
}
export class AuthInfo {
@ApiProperty()
authenticated: boolean;
@ApiProperty()
user: string;
@ApiProperty({ default: false })
god: boolean;
}
@Controller() @Controller()
export class AppController { export class AppController {
@ -9,6 +33,24 @@ export class AppController {
@Get() @Get()
@ApiOperation({ summary: 'Fetch API configuration' }) @ApiOperation({ summary: 'Fetch API configuration' })
getHello(): ApiInfo { getHello(): ApiInfo {
return this.appService.getRoot(); return {
version: META.version,
};
}
@Get('/auth')
@UseGuards(AuthGuard)
@ApiOperation({ summary: 'Check authentication status' })
@ApiResponse({ type: AuthInfo, status: 200 })
@ApiUnauthorizedResponse({
description: 'Request will fail if the user is not authenticated',
})
getAuth(@AuthData() auth: AuthenticationData): AuthInfo {
return {
// Since the AuthGuard stops the request otherwise, we can assume the user is authenticated
authenticated: true,
user: auth.user,
god: auth.god || false,
};
} }
} }

View file

@ -1,15 +1,4 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { META } from './meta';
export interface ApiInfo {
version: string;
}
@Injectable() @Injectable()
export class AppService { export class AppService {}
getRoot(): ApiInfo {
return {
version: META.version,
};
}
}

View file

@ -1,17 +1,13 @@
import { import { createParamDecorator, ExecutionContext } from '@nestjs/common';
createParamDecorator,
ExecutionContext,
InternalServerErrorException,
} from '@nestjs/common';
import { AuthenticationData } from './auth.guard'; import { AuthenticationData } from './auth.guard';
export const AuthData = createParamDecorator( export const AuthData = createParamDecorator(
(data: unknown, ctx: ExecutionContext) => { (data: unknown, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest(); const request = ctx.switchToHttp().getRequest();
if (!request.auth_data) //if (!request.auth_data)
throw new InternalServerErrorException( // throw new InternalServerErrorException(
'Authentication data not present in request object', // 'Authentication data not present in request object',
); // );
return request.auth_data as AuthenticationData; return request.auth_data as AuthenticationData | undefined;
}, },
); );