diff --git a/src/app.module.ts b/src/app.module.ts index fa2b50cc..a42cb3e6 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -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], }) diff --git a/src/discovery/discovery.controller.spec.ts b/src/discovery/discovery.controller.spec.ts deleted file mode 100644 index be71e9c6..00000000 --- a/src/discovery/discovery.controller.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { DiscoveryController } from './discovery.controller'; - -describe('DiscoveryController', () => { - let controller: DiscoveryController; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - controllers: [DiscoveryController], - }).compile(); - - controller = module.get(DiscoveryController); - }); - - it('should be defined', () => { - expect(controller).toBeDefined(); - }); -}); diff --git a/src/flows/flows.controller.ts b/src/flows/flows.controller.ts new file mode 100644 index 00000000..3eca6ebf --- /dev/null +++ b/src/flows/flows.controller.ts @@ -0,0 +1,4 @@ +import { Controller } from '@nestjs/common'; + +@Controller('flows') +export class FlowsController {} diff --git a/src/flows/flows.module.ts b/src/flows/flows.module.ts new file mode 100644 index 00000000..f4e54a49 --- /dev/null +++ b/src/flows/flows.module.ts @@ -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 {} diff --git a/src/flows/flows.service.spec.ts b/src/flows/flows.service.spec.ts new file mode 100644 index 00000000..08d77bb7 --- /dev/null +++ b/src/flows/flows.service.spec.ts @@ -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); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/src/flows/flows.service.ts b/src/flows/flows.service.ts new file mode 100644 index 00000000..0999ebb5 --- /dev/null +++ b/src/flows/flows.service.ts @@ -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; + } +} diff --git a/src/token/token.controller.ts b/src/token/token.controller.ts index 62ca379e..933450dc 100644 --- a/src/token/token.controller.ts +++ b/src/token/token.controller.ts @@ -24,16 +24,7 @@ export class TokenController { @Res() res: Response, ): Promise { - 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(); } diff --git a/src/token/token.module.ts b/src/token/token.module.ts index 2768ba9f..246e0a4e 100644 --- a/src/token/token.module.ts +++ b/src/token/token.module.ts @@ -8,5 +8,6 @@ import { ConfigModule } from '@nestjs/config'; imports: [ConfigModule.forRoot(), DiscoveryModule], controllers: [TokenController], providers: [TokenService], + exports: [TokenService], }) export class TokenModule {} diff --git a/src/token/token.service.spec.ts b/src/token/token.service.spec.ts index 583b9127..d68524ce 100644 --- a/src/token/token.service.spec.ts +++ b/src/token/token.service.spec.ts @@ -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', ); }); }); diff --git a/src/token/token.service.ts b/src/token/token.service.ts index c699af6a..96377a68 100644 --- a/src/token/token.service.ts +++ b/src/token/token.service.ts @@ -49,13 +49,24 @@ export class TokenService { }); } - async requestToken(issuer: Issuer): Promise { - if (issuer === undefined) { + async requestToken(issuer_s: string): Promise { + 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, diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts index 74e3fbdc..f61cbd5d 100644 --- a/src/user/user.controller.ts +++ b/src/user/user.controller.ts @@ -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'; @@ -16,5 +16,4 @@ export class UserController { root() { return; } - } diff --git a/src/user/user.module.ts b/src/user/user.module.ts index 1ddbb615..703ae4f6 100644 --- a/src/user/user.module.ts +++ b/src/user/user.module.ts @@ -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], }) diff --git a/src/user/user.service.ts b/src/user/user.service.ts index 7d6ea935..ccfdd065 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -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 { + return await this.flowsService.clientCredentialsFlow( + process.env.ISSUER_STRING, + ); } }