-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from getlarge/34-feat-implement-oauth2-client-…
…credentials-flow-to-access-api feat: implement oauth2 client credentials flow to access api
- Loading branch information
Showing
93 changed files
with
1,896 additions
and
1,755 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,115 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Get, | ||
HttpCode, | ||
HttpStatus, | ||
Post, | ||
UseGuards, | ||
UsePipes, | ||
ValidationPipe, | ||
} from '@nestjs/common'; | ||
import { | ||
ApiBearerAuth, | ||
ApiBody, | ||
ApiOperation, | ||
ApiResponse, | ||
ApiTags, | ||
} from '@nestjs/swagger'; | ||
import { SecurityRequirements } from '@ticketing/microservices/shared/constants'; | ||
import { | ||
CurrentClient, | ||
CurrentUser, | ||
} from '@ticketing/microservices/shared/decorators'; | ||
import { | ||
OryActionAuthGuard, | ||
OryAuthenticationGuard, | ||
OryOAuth2AuthenticationGuard, | ||
} from '@ticketing/microservices/shared/guards'; | ||
import { Actions, Resources } from '@ticketing/shared/constants'; | ||
import { requestValidationErrorFactory } from '@ticketing/shared/errors'; | ||
|
||
import { User } from '../users/models'; | ||
import { ClientsService } from './clients.service'; | ||
import { | ||
Client, | ||
ClientDto, | ||
CreateClientDto, | ||
CreatedClientDto, | ||
OryOAuth2WebhookPayloadDto, | ||
OryOAuth2WebhookResponseDto, | ||
} from './models'; | ||
|
||
@Controller(Resources.CLIENTS) | ||
@ApiTags(Resources.CLIENTS) | ||
export class ClientsController { | ||
constructor(private readonly clientsService: ClientsService) {} | ||
|
||
@UseGuards(OryAuthenticationGuard()) | ||
@UsePipes( | ||
new ValidationPipe({ | ||
transform: true, | ||
exceptionFactory: requestValidationErrorFactory, | ||
forbidUnknownValues: true, | ||
}), | ||
) | ||
@ApiOperation({ | ||
description: 'Register a new client', | ||
summary: `Register a new client - Scope : ${Resources.CLIENTS}:${Actions.CREATE_ONE}`, | ||
}) | ||
@ApiBody({ type: CreateClientDto }) | ||
@ApiResponse({ | ||
status: HttpStatus.OK, | ||
description: 'Client created', | ||
type: CreatedClientDto, | ||
}) | ||
@Post('') | ||
@HttpCode(HttpStatus.CREATED) | ||
create( | ||
@CurrentUser() user: User, | ||
@Body() body: CreateClientDto, | ||
): Promise<CreatedClientDto> { | ||
return this.clientsService.create(body, user); | ||
} | ||
|
||
@UseGuards(OryActionAuthGuard) | ||
@UsePipes( | ||
new ValidationPipe({ | ||
transform: true, | ||
exceptionFactory: requestValidationErrorFactory, | ||
forbidUnknownValues: true, | ||
}), | ||
) | ||
@ApiOperation({ | ||
description: 'Triggered when a client request an OAuth2 token', | ||
}) | ||
@ApiBody({ type: OryOAuth2WebhookPayloadDto }) | ||
@ApiResponse({ | ||
status: HttpStatus.OK, | ||
description: 'Token session update', | ||
type: OryOAuth2WebhookResponseDto, | ||
}) | ||
@Post('on-token-request') | ||
@HttpCode(HttpStatus.OK) | ||
onSignUp( | ||
@Body() body: OryOAuth2WebhookPayloadDto, | ||
): Promise<OryOAuth2WebhookResponseDto> { | ||
return this.clientsService.onTokenRequest(body); | ||
} | ||
|
||
@UseGuards(OryOAuth2AuthenticationGuard()) | ||
@ApiOperation({ | ||
description: 'Get details about currently authenticated client', | ||
summary: `Get current client - Scope : ${Resources.CLIENTS}:${Actions.READ_ONE}`, | ||
}) | ||
@ApiBearerAuth(SecurityRequirements.Bearer) | ||
@ApiResponse({ | ||
status: HttpStatus.CREATED, | ||
description: 'Current client authenticated', | ||
type: ClientDto, | ||
}) | ||
@Get('current-client') | ||
getCurrentClient(@CurrentClient() client: Client): Client { | ||
return client; | ||
} | ||
} |
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,41 @@ | ||
import { OryOAuth2Module } from '@getlarge/hydra-client-wrapper'; | ||
import { OryFrontendModule } from '@getlarge/kratos-client-wrapper'; | ||
import { Module } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
|
||
import { EnvironmentVariables } from '../env'; | ||
import { ClientsController } from './clients.controller'; | ||
import { ClientsService } from './clients.service'; | ||
import { Client, ClientSchema } from './schemas/client.schema'; | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([ | ||
{ | ||
name: Client.name, | ||
schema: ClientSchema, | ||
}, | ||
]), | ||
OryFrontendModule.forRootAsync({ | ||
inject: [ConfigService], | ||
useFactory: ( | ||
configService: ConfigService<EnvironmentVariables, true>, | ||
) => ({ | ||
basePath: configService.get('ORY_KRATOS_PUBLIC_URL'), | ||
}), | ||
}), | ||
OryOAuth2Module.forRootAsync({ | ||
inject: [ConfigService], | ||
useFactory: ( | ||
configService: ConfigService<EnvironmentVariables, true>, | ||
) => ({ | ||
basePath: configService.get('ORY_HYDRA_ADMIN_URL'), | ||
accessToken: configService.get('ORY_HYDRA_API_KEY'), | ||
}), | ||
}), | ||
], | ||
controllers: [ClientsController], | ||
providers: [ClientsService], | ||
}) | ||
export class ClientsModule {} |
Oops, something went wrong.