This repository has been archived by the owner on Oct 19, 2021. It is now read-only.
Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
add api token in rest api
add apiBeareAuth add token in .env-sample
- Loading branch information
1 parent
10c3715
commit 37fd8f9
Showing
13 changed files
with
146 additions
and
13 deletions.
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { AuthService } from './auth.service'; | ||
| import { HttpStrategy } from './http.strategy'; | ||
| import { PassportModule } from '@nestjs/passport'; | ||
|
|
||
| @Module({ | ||
| imports: [ | ||
| PassportModule.register({ defaultStrategy: 'bearer' }), | ||
| ], | ||
| providers: [AuthService, HttpStrategy], | ||
| exports: [PassportModule, AuthService], | ||
| }) | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { ConfigService } from 'nestjs-config'; | ||
|
|
||
| @Injectable() | ||
| export class AuthService { | ||
|
|
||
| constructor() { } | ||
|
|
||
| async validateApiToken(token: string): Promise<any> { | ||
| const apiToken = ConfigService.get('ptarmigan.apiToken'); | ||
| if (token === apiToken) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { Strategy } from 'passport-http-bearer'; | ||
| import { PassportStrategy } from '@nestjs/passport'; | ||
| import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
| import { AuthService } from './auth.service'; | ||
|
|
||
| @Injectable() | ||
| export class HttpStrategy extends PassportStrategy(Strategy) { | ||
| constructor(private readonly authService: AuthService) { | ||
| super(); | ||
| } | ||
|
|
||
| async validate(token: string) { | ||
| const apiToken = await this.authService.validateApiToken(token); | ||
| if (!apiToken) { | ||
| throw new UnauthorizedException(); | ||
| } | ||
| return apiToken; | ||
| } | ||
| } |
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
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
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
Oops, something went wrong.