Browse Source

add functions to login the radar scope

Sven Czarnian 2 years ago
parent
commit
822b39f4fd
3 changed files with 61 additions and 1 deletions
  1. 34 1
      src/auth/auth.controller.ts
  2. 9 0
      src/auth/auth.service.ts
  3. 18 0
      src/auth/dto/radarscope.dto.ts

+ 34 - 1
src/auth/auth.controller.ts

@@ -1,18 +1,21 @@
 import {
+  Body,
   Controller,
   Get,
   HttpException,
   HttpStatus,
   Patch,
+  Post,
   Query,
   Redirect,
   Req,
   UseGuards,
 } from '@nestjs/common';
-import { ApiQuery } from '@nestjs/swagger';
+import { ApiBody, ApiQuery, ApiResponse } from '@nestjs/swagger';
 import { ConfigService } from '@nestjs/config';
 import { AuthService } from './auth.service';
 import { JwtGuard } from './guards/jwt.guard';
+import { RadarScopeDto } from './dto/radarscope.dto';
 import { UserDto } from './dto/user.dto';
 import { Request } from 'express';
 
@@ -57,6 +60,36 @@ export class AuthController {
     }
   }
 
+  @Post('/radarScope')
+  @ApiBody({
+    description: 'The airport definition',
+    type: RadarScopeDto,
+  })
+  @ApiResponse({
+    status: 200,
+    description: 'The created Bearer token to use endpoints',
+    type: String,
+  })
+  @ApiResponse({
+    status: 404,
+    description: 'The VATSIM ID and key combination is invalid',
+  })
+  async radarScope(
+    @Body('scopeData') scopeData: RadarScopeDto,
+  ): Promise<string> {
+    return this.authService
+      .loginRadarScope(scopeData.vatsimId, scopeData.key)
+      .then((token) => {
+        if (token === undefined) {
+          throw new HttpException(
+            'Unknown VATSIM ID or invalid key',
+            HttpStatus.NOT_FOUND,
+          );
+        }
+        return token;
+      });
+  }
+
   @UseGuards(JwtGuard)
   @Get('/user')
   async user(@Req() request: Request): Promise<UserDto> {

+ 9 - 0
src/auth/auth.service.ts

@@ -113,6 +113,15 @@ export class AuthService {
       });
   }
 
+  async loginRadarScope(vatsimId: string, key: string): Promise<string> {
+    return this.userModel.findOne({ vatsimId }).then((user) => {
+      if (!user || user.radarScopeKey !== key) return undefined;
+
+      const payload = { vatsimId: vatsimId, sub: key };
+      return this.jwtService.sign(payload);
+    });
+  }
+
   async resetRadarScopeKey(token: string): Promise<void> {
     const payload = this.jwtService.verify(token, {
       secret: this.config.get<string>('server.jwt-secret'),

+ 18 - 0
src/auth/dto/radarscope.dto.ts

@@ -0,0 +1,18 @@
+import { IsNotEmpty } from 'class-validator';
+import { ApiProperty } from '@nestjs/swagger';
+
+export class RadarScopeDto {
+  @IsNotEmpty()
+  @ApiProperty({
+    description: 'The VATSIM ID of the controller',
+    example: '10000001',
+  })
+  vatsimId: string;
+
+  @IsNotEmpty()
+  @ApiProperty({
+    description: 'The unique key to logon the RADAR scope',
+    example: 'SECRET',
+  })
+  key: string;
+}