Skip to content

Commit

Permalink
Migrated access token code to token module #44
Browse files Browse the repository at this point in the history
  • Loading branch information
raghunandanarava committed Jun 5, 2022
1 parent 34a90ad commit 2c277a2
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 31 deletions.
3 changes: 2 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { UserModule } from './user/user.module';
import { AppController } from './app.controller';
import { ConfigModule } from '@nestjs/config';
import { DiscoveryModule } from './discovery/discovery.module';
import { TokenModule } from './token/token.module';

@Module({
imports: [ConfigModule.forRoot(), UserModule, DiscoveryModule],
imports: [ConfigModule.forRoot(), UserModule, DiscoveryModule, TokenModule],
providers: [AppService],
controllers: [AppController],
})
Expand Down
29 changes: 0 additions & 29 deletions src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,4 @@ export class AppService {
getHello(): string {
return 'Hello World!';
}

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);
}
}
3 changes: 2 additions & 1 deletion src/discovery/discovery.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DiscoveryService } from './discovery.service';

@Module({
controllers: [DiscoveryController],
providers: [DiscoveryService]
providers: [DiscoveryService],
exports: [DiscoveryService],
})
export class DiscoveryModule {}
64 changes: 64 additions & 0 deletions src/token/token.controller.ts
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();
}
}
12 changes: 12 additions & 0 deletions src/token/token.module.ts
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 {}
18 changes: 18 additions & 0 deletions src/token/token.service.spec.ts
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();
});
});
44 changes: 44 additions & 0 deletions src/token/token.service.ts
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);
}
}

0 comments on commit 2c277a2

Please sign in to comment.