Skip to content

Commit

Permalink
Created flows module and defined a flow until getting an access token #…
Browse files Browse the repository at this point in the history
  • Loading branch information
raghunandanarava committed Jun 6, 2022
1 parent a4a8c38 commit 32f8882
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 39 deletions.
3 changes: 2 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { AppController } from './app.controller';
import { ConfigModule } from '@nestjs/config';
import { DiscoveryModule } from './discovery/discovery.module';
import { TokenModule } from './token/token.module';
import { FlowsModule } from './flows/flows.module';

@Module({
imports: [ConfigModule.forRoot(), UserModule, DiscoveryModule, TokenModule],
imports: [ConfigModule.forRoot(), UserModule, DiscoveryModule, TokenModule, FlowsModule],
providers: [AppService],
controllers: [AppController],
})
Expand Down
18 changes: 0 additions & 18 deletions src/discovery/discovery.controller.spec.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/flows/flows.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Controller } from '@nestjs/common';

@Controller('flows')
export class FlowsController {}
12 changes: 12 additions & 0 deletions src/flows/flows.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { FlowsController } from './flows.controller';
import { FlowsService } from './flows.service';
import { TokenModule } from '../token/token.module';

@Module({
imports: [TokenModule],
controllers: [FlowsController],
providers: [FlowsService],
exports: [FlowsService],
})
export class FlowsModule {}
18 changes: 18 additions & 0 deletions src/flows/flows.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { FlowsService } from './flows.service';

describe('FlowsService', () => {
let service: FlowsService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [FlowsService],
}).compile();

service = module.get<FlowsService>(FlowsService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
13 changes: 13 additions & 0 deletions src/flows/flows.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Injectable, Inject } from '@nestjs/common';
import { TokenService } from '../token/token.service';

@Injectable()
export class FlowsService {
@Inject(TokenService)
private readonly tokenService: TokenService;

async clientCredentialsFlow(issuer_s: string) {
const accessToken = await this.tokenService.requestToken(issuer_s);
return accessToken;
}
}
11 changes: 1 addition & 10 deletions src/token/token.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,7 @@ export class TokenController {
@Res()
res: Response,
): Promise<any> {
const issuer = await this.tokenService.getIssuer(issuer_s).catch(() => {
throw new HttpException(
{
status: HttpStatus.BAD_REQUEST,
error: 'invalid issuer',
},
HttpStatus.BAD_REQUEST,
);
});
const result = await this.tokenService.requestToken(issuer);
const result = await this.tokenService.requestToken(issuer_s);
res.json(result.data).send();
}

Expand Down
1 change: 1 addition & 0 deletions src/token/token.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ import { ConfigModule } from '@nestjs/config';
imports: [ConfigModule.forRoot(), DiscoveryModule],
controllers: [TokenController],
providers: [TokenService],
exports: [TokenService],
})
export class TokenModule {}
10 changes: 8 additions & 2 deletions src/token/token.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,15 @@ describe('TokenService', () => {
});

describe('requestToken', () => {
it('should fail if empty issuer is provided', async () => {
it('should fail if no issuer is provided', async () => {
await expect(service.requestToken(undefined)).rejects.toThrow(
'Received issuer is empty',
'There was no issuer string passed to get the issuer',
);
});

it('should fail if empty issuer is provided', async () => {
await expect(service.requestToken('')).rejects.toThrow(
'There was no issuer string passed to get the issuer',
);
});
});
Expand Down
17 changes: 14 additions & 3 deletions src/token/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,24 @@ export class TokenService {
});
}

async requestToken(issuer: Issuer): Promise<any> {
if (issuer === undefined) {
async requestToken(issuer_s: string): Promise<any> {
if (issuer_s === undefined || issuer_s === '') {
throw new HttpException(
'Received issuer is empty',
'There was no issuer string passed to get the issuer',
HttpStatus.BAD_REQUEST,
);
}

const issuer = await this.getIssuer(issuer_s).catch(() => {
throw new HttpException(
{
status: HttpStatus.BAD_REQUEST,
error: 'invalid issuer',
},
HttpStatus.BAD_REQUEST,
);
});

const grantBody: GrantBody = {
grant_type: process.env.CLIENT_CREDENTIALS_STRING,
client_id: process.env.CLIENT_ID,
Expand Down
3 changes: 1 addition & 2 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Body, Controller, Get, Post, Query, Render} from '@nestjs/common';
import { Body, Controller, Get, Post, Render } from '@nestjs/common';
import { UserService } from './user.service';
import { LoginUserDto } from './Dto/LoginUser.dto';

Expand All @@ -16,5 +16,4 @@ export class UserController {
root() {
return;
}

}
2 changes: 2 additions & 0 deletions src/user/user.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Module } from '@nestjs/common';
import { UserController } from './user.controller';
import { UserService } from './user.service';
import { FlowsModule } from '../flows/flows.module';

@Module({
imports: [FlowsModule],
controllers: [UserController],
providers: [UserService],
})
Expand Down
12 changes: 9 additions & 3 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Injectable } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import { LoginUserDto } from './Dto/LoginUser.dto';
import { FlowsService } from '../flows/flows.service';

@Injectable()
export class UserService {
login(loginUserDto: LoginUserDto): { result: string } {
return { result:'Login Successful' };
@Inject(FlowsService)
private readonly flowsService: FlowsService;

async login(loginUserDto: LoginUserDto): Promise<any> {
return await this.flowsService.clientCredentialsFlow(
process.env.ISSUER_STRING,
);
}
}

0 comments on commit 32f8882

Please sign in to comment.