-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
seperating out app oauth module from app module
- Loading branch information
Showing
9 changed files
with
270 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AppOauthController } from './app-oauth.controller'; | ||
|
||
describe('AppOauthController', () => { | ||
let controller: AppOauthController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AppOauthController], | ||
providers: [], | ||
}).compile(); | ||
|
||
controller = module.get<AppOauthController>(AppOauthController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).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,72 @@ | ||
import { | ||
Controller, | ||
ValidationPipe, | ||
Post, | ||
UsePipes, | ||
HttpCode, | ||
UseFilters, | ||
Headers, | ||
Logger, | ||
} from '@nestjs/common'; | ||
|
||
import { AppAuthService } from 'src/app-auth/services/app-auth.service'; | ||
import { | ||
ApiBadRequestResponse, | ||
ApiHeader, | ||
ApiResponse, | ||
ApiTags, | ||
ApiUnauthorizedResponse, | ||
} from '@nestjs/swagger'; | ||
import { AllExceptionsFilter } from 'src/utils/utils'; | ||
import { | ||
AppSecretHeader, | ||
AppSubdomainHeader, | ||
} from './dtos/app-sercret.decorator'; | ||
import { | ||
GenerateTokenError, | ||
GenerateTokenResponse, | ||
AppError, | ||
} from './dtos/generate-token.dto'; | ||
|
||
@UseFilters(AllExceptionsFilter) | ||
@ApiTags('App') | ||
@Controller('app') | ||
export class AppOauthController { | ||
constructor(private readonly appAuthService: AppAuthService) {} | ||
|
||
@ApiHeader({ | ||
name: 'X-Api-Secret-Key', | ||
description: 'Provide Api Secret key to get access token', | ||
required: true, | ||
}) | ||
@ApiHeader({ | ||
name: 'Origin', | ||
description: 'Origin as you set in application cors', | ||
required: false, | ||
}) | ||
@ApiBadRequestResponse({ | ||
status: 400, | ||
description: 'Error occured at the time of generating access token', | ||
type: AppError, | ||
}) | ||
@Post('oauth') | ||
@HttpCode(200) | ||
@ApiResponse({ | ||
status: 200, | ||
description: 'AccessToken generated', | ||
type: GenerateTokenResponse, | ||
}) | ||
@ApiUnauthorizedResponse({ | ||
description: 'Unauthorized', | ||
type: GenerateTokenError, | ||
}) | ||
@UsePipes(ValidationPipe) | ||
generateAccessToken( | ||
@Headers('X-Api-Secret-Key') apiSectretKey: string, | ||
@AppSecretHeader() appSecreatKey, | ||
@AppSubdomainHeader() appSubdomain, | ||
): Promise<{ access_token; expiresIn; tokenType }> { | ||
Logger.log('reGenerateAppSecretKey() method: starts', 'AppOAuthController'); | ||
return this.appAuthService.generateAccessToken(appSecreatKey, appSubdomain); | ||
} | ||
} |
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,14 @@ | ||
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; | ||
import { AppOauthController } from './app-oauth.controller'; | ||
import { AppAuthModule } from 'src/app-auth/app-auth.module'; | ||
import { WhitelistSSICorsMiddleware } from 'src/utils/middleware/cors.middleware'; | ||
@Module({ | ||
imports: [AppAuthModule], | ||
controllers: [AppOauthController], | ||
providers: [], | ||
}) | ||
export class AppOauthModule implements NestModule { | ||
configure(consumer: MiddlewareConsumer) { | ||
consumer.apply(WhitelistSSICorsMiddleware).forRoutes(AppOauthController); | ||
} | ||
} |
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,31 @@ | ||
import { | ||
createParamDecorator, | ||
ExecutionContext, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
|
||
export const AppSecretHeader = createParamDecorator( | ||
(_data: unknown, ctx: ExecutionContext) => { | ||
const request = ctx.switchToHttp().getRequest(); | ||
if ( | ||
!request.headers['x-api-secret-key'] || | ||
request.headers['x-api-secret-key'] == undefined | ||
) { | ||
throw new UnauthorizedException(['x-api-secret-key header is missing']); | ||
} | ||
return request.headers['x-api-secret-key']; | ||
}, | ||
); | ||
|
||
export const AppSubdomainHeader = createParamDecorator( | ||
(_data: unknown, ctx: ExecutionContext) => { | ||
const request = ctx.switchToHttp().getRequest(); | ||
if ( | ||
!request.headers['x-subdomain'] || | ||
request.headers['x-subdomain'] == undefined | ||
) { | ||
throw new UnauthorizedException(['x-subdomain header is missing']); | ||
} | ||
return request.headers['x-subdomain']; | ||
}, | ||
); |
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,82 @@ | ||
import { IsNotEmpty, IsString, IsNumber } from 'class-validator'; | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
|
||
export class GenerateTokenError { | ||
@ApiProperty({ | ||
description: 'statusCode', | ||
example: 401, | ||
}) | ||
@IsNumber() | ||
statusCode: number; | ||
|
||
@ApiProperty({ | ||
description: 'message', | ||
example: ['access_denied'], | ||
}) | ||
@IsString() | ||
message: string; | ||
|
||
@ApiProperty({ | ||
description: 'Unauthorized', | ||
example: 'Unauthorized', | ||
}) | ||
@IsString() | ||
error: 'Unauthorized'; | ||
} | ||
export class GenerateTokenResponse { | ||
@ApiProperty({ | ||
description: 'accessToken', | ||
example: | ||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6IjRkNjhmMjNmLTcwZjQtNDFhZC1hMGViLTU3MjA4YTZlOTcxMSIsImFwcFNlY3JldCI6IjNjN2NiNTY1LTZmNWQtNGY2MC1hMjQ2LTZhOGFjYWVhMmY0MyIsImdyYW50VHlwZSI6ImNsaWVudF9jcmVkZW50aWFscyIsImlhdCI6MTY3NDAyMDY3NCwiZXhwIjoxNjc0MDM1MDc0fQ.P-AbheTJMxQNGLTkGWOsnct4M0nKCd-7oUFGqMCpIDM', | ||
}) | ||
@IsNotEmpty() | ||
@IsString() | ||
access_token: string; | ||
|
||
@ApiProperty({ | ||
description: 'Type of token', | ||
example: 'Bearer', | ||
}) | ||
@IsNotEmpty() | ||
@IsString() | ||
tokenType: string; | ||
|
||
@ApiProperty({ | ||
description: 'Token expiry time', | ||
example: 14400, | ||
}) | ||
@IsNotEmpty() | ||
@IsNumber() | ||
expiresIn: number; | ||
} | ||
|
||
export class RegenrateAppApiSecretResponse { | ||
@ApiProperty({ | ||
description: 'apiSecretKey for getting access token', | ||
example: 'xyz.ert34nbhjf48959', | ||
}) | ||
apiSecretKey: string; | ||
} | ||
|
||
export class AppError { | ||
@ApiProperty({ | ||
description: 'statusCode', | ||
example: 400, | ||
}) | ||
@IsNumber() | ||
statusCode: number; | ||
|
||
@ApiProperty({ | ||
description: 'message', | ||
example: ['error message 1', 'error message 2'], | ||
}) | ||
@IsString() | ||
message: Array<string>; | ||
|
||
@ApiProperty({ | ||
description: 'error', | ||
example: 'Bad Request', | ||
}) | ||
@IsString() | ||
error: string; | ||
} |
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.