Last active
February 7, 2019 12:07
-
-
Save abhyuditjain/e1f0bb9c0a54b1d4aca0410e04b8e247 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PassportStrategy } from '@nestjs/passport'; | |
import { Injectable, UnauthorizedException, HttpStatus, Logger } from '@nestjs/common'; | |
import { Strategy } from 'passport-localapikey-update'; | |
import { size } from 'lodash'; | |
import { AuthService } from './auth.service'; | |
@Injectable() | |
export class ApiKeyStrategy extends PassportStrategy(Strategy, 'localapikey') { | |
constructor(private readonly authService: AuthService) { | |
super(); | |
} | |
async validate(token: string) { | |
Logger.log('HERE!!!!!!!!!!!!!!', 'ApiKeyStrategy'); // Not printed | |
const data = await this.authService.authenticateClient(token); | |
if (!size(data)) { | |
throw new UnauthorizedException('Unauthorized'); | |
} | |
return data; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Module } from '@nestjs/common'; | |
import { AuthService } from './auth.service'; | |
import { ConfigModule } from 'src/config/config.module'; | |
import { ApiKeyStrategy } from './apiKey.strategy'; | |
@Module({ | |
imports: [ConfigModule], | |
providers: [AuthService, ApiKeyStrategy], | |
}) | |
export class AuthModule { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, Logger } from '@nestjs/common'; | |
import { ClientRO } from 'src/client/client.dto'; | |
import { ConfigService } from 'src/config/config.service'; | |
import axios from 'axios'; | |
@Injectable() | |
export class AuthService { | |
private authBaseUrl: string; | |
private logger = new Logger('AuthService'); | |
private clientVerifyEndpoint = '/api/client/verify'; | |
constructor(config: ConfigService) { | |
this.authBaseUrl = config.authBaseUrl; | |
} | |
async authenticateClient(apiKey: string): Promise<ClientRO> { | |
this.logger.log('Here'); // doesn't reach here | |
try { | |
const data = await axios({ | |
method: 'GET', | |
baseURL: this.authBaseUrl, | |
url: this.clientVerifyEndpoint, | |
headers: { | |
authorization: apiKey, | |
}, | |
}); | |
this.logger.log(`Auth data: ${data}`); | |
return new ClientRO(); | |
} catch (e) { | |
this.logger.error(`Auth error: ${e}`); | |
} | |
return new ClientRO(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Controller, Get, Post, Put, Delete, Body, Param, UseGuards } from '@nestjs/common'; | |
import { AuthGuard } from '@nestjs/passport'; | |
import { DriverService } from './driver.service'; | |
import { DriverDTO } from './driver.dto'; | |
@Controller('drivers') | |
export class DriverController { | |
constructor(private driverService: DriverService) { } | |
@Get() | |
@UseGuards(AuthGuard('localapikey')) | |
listDrivers() { | |
return this.driverService.list(); | |
} | |
@Get(':id') | |
showDriver(@Param() id: string) { | |
return this.driverService.show(id); | |
} | |
@Post() | |
createDriver(@Body() data: DriverDTO) { | |
return this.driverService.create(data); | |
} | |
@Put(':id') | |
updateDriver(@Param() id: string, @Body() data: Partial<DriverDTO>) { | |
return this.driverService.update(id, data); | |
} | |
@Delete(':id') | |
deleteDriver(@Param() id: string) { | |
return this.driverService.delete(id); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Module } from '@nestjs/common'; | |
import { TypeOrmModule } from '@nestjs/typeorm'; | |
import { DriverEntity } from './driver.entity'; | |
import { DriverService } from './driver.service'; | |
import { DriverController } from './driver.controller'; | |
@Module({ | |
imports: [TypeOrmModule.forFeature([DriverEntity])], | |
providers: [DriverService], | |
controllers: [DriverController], | |
}) | |
export class DriverModule { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment