generated from amosproj/amos202Xss0Y-projname
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrated access token code to token module #44
- Loading branch information
1 parent
34a90ad
commit 2c277a2
Showing
7 changed files
with
142 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Get, | ||
HttpException, | ||
HttpStatus, | ||
Post, | ||
Query, | ||
Res, | ||
} from '@nestjs/common'; | ||
import { GrantBody } from 'openid-client'; | ||
import { Response } from 'express'; | ||
|
||
import { TokenService } from './token.service'; | ||
|
||
@Controller('token') | ||
export class TokenController { | ||
constructor(private readonly tokenService: TokenService) {} | ||
|
||
@Get('gettoken') | ||
async requestToken( | ||
@Query('issuer') | ||
issuer_s: string, | ||
@Res() | ||
res: Response, | ||
): Promise<any> { | ||
const issuer = await this.tokenService.get_issuer(issuer_s).catch(() => { | ||
throw new HttpException( | ||
{ | ||
status: HttpStatus.BAD_REQUEST, | ||
error: 'invalid issuer', | ||
}, | ||
HttpStatus.BAD_REQUEST, | ||
); | ||
}); | ||
const result = await this.tokenService.requestToken(issuer); | ||
res.json(result.data).send(); | ||
} | ||
|
||
@Post('gettoken') | ||
async requestTokenWithClientInformation( | ||
@Query('issuer') | ||
issuer_s: string, | ||
@Body() | ||
grantBody: GrantBody, | ||
@Res() | ||
res: Response, | ||
): Promise<any> { | ||
const issuer = await this.tokenService.get_issuer(issuer_s).catch(() => { | ||
throw new HttpException( | ||
{ | ||
status: HttpStatus.BAD_REQUEST, | ||
error: 'invalid issuer', | ||
}, | ||
HttpStatus.BAD_REQUEST, | ||
); | ||
}); | ||
const result = await this.tokenService.getToken( | ||
String(issuer.token_endpoint), | ||
grantBody, | ||
); | ||
res.json(result.data).send(); | ||
} | ||
} |
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,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TokenController } from './token.controller'; | ||
import { TokenService } from './token.service'; | ||
import { DiscoveryModule } from 'src/discovery/discovery.module'; | ||
import {ConfigModule} from "@nestjs/config"; | ||
|
||
@Module({ | ||
imports: [ConfigModule.forRoot(), DiscoveryModule], | ||
controllers: [TokenController], | ||
providers: [TokenService], | ||
}) | ||
export class TokenModule {} |
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,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { TokenService } from './token.service'; | ||
|
||
describe('TokenService', () => { | ||
let service: TokenService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [TokenService], | ||
}).compile(); | ||
|
||
service = module.get<TokenService>(TokenService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
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,44 @@ | ||
import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common'; | ||
import { GrantBody, Issuer } from 'openid-client'; | ||
import axios from 'axios'; | ||
import * as qs from 'qs'; | ||
import { DiscoveryService } from '../discovery/discovery.service'; | ||
|
||
@Injectable() | ||
export class TokenService { | ||
@Inject(DiscoveryService) | ||
private readonly discoveryService: DiscoveryService; | ||
|
||
async get_issuer(issuer_s: string) { | ||
return await this.discoveryService.get_issuer(issuer_s); | ||
} | ||
|
||
async getToken(token_endpoint: string, grantBody: GrantBody): Promise<any> { | ||
return await axios | ||
.post(token_endpoint, qs.stringify(grantBody), { | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
}) | ||
.catch(() => { | ||
throw new HttpException( | ||
{ | ||
status: HttpStatus.UNAUTHORIZED, | ||
error: 'Access denied', | ||
}, | ||
HttpStatus.UNAUTHORIZED, | ||
); | ||
}); | ||
} | ||
|
||
async requestToken(issuer: Issuer): Promise<any> { | ||
const grantBody: GrantBody = { | ||
grant_type: process.env.CLIENT_CREDENTIALS_STRING, | ||
client_id: process.env.CLIENT_ID, | ||
client_secret: process.env.CLIENT_SECRET, | ||
audience: process.env.AUDIENCE, | ||
}; | ||
|
||
return await this.getToken(String(issuer.token_endpoint), grantBody); | ||
} | ||
} |