mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-25 05:57:55 +00:00
finished client endpoints
This commit is contained in:
@@ -3,11 +3,22 @@ import { ClientController } from './controllers';
|
||||
import { ClientService } from './services';
|
||||
import { ClientRepositoryModule } from '@app/common/modules/client/client.repository.module';
|
||||
import { ClientRepository } from '@app/common/modules/client/repositories';
|
||||
import { AuthService } from '@app/common/auth/services/auth.service';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
import { UserSessionRepository } from '@app/common/modules/session/repositories/session.repository';
|
||||
|
||||
@Module({
|
||||
imports: [ClientRepositoryModule],
|
||||
controllers: [ClientController],
|
||||
providers: [ClientService, ClientRepository],
|
||||
providers: [
|
||||
ClientService,
|
||||
ClientRepository,
|
||||
AuthService,
|
||||
JwtService,
|
||||
UserRepository,
|
||||
UserSessionRepository,
|
||||
],
|
||||
exports: [ClientService],
|
||||
})
|
||||
export class ClientModule {}
|
||||
|
||||
@@ -1,15 +1,33 @@
|
||||
import { Controller, Post, Body } from '@nestjs/common';
|
||||
import { ApiTags } from '@nestjs/swagger';
|
||||
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { RegisterClientDto } from '../dtos/register-client.dto';
|
||||
import { ClientService } from '../services';
|
||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||
import { ClientTokenDto } from '../dtos/token-client.dto';
|
||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||
|
||||
@ApiTags('OAuth Clients')
|
||||
@Controller('clients')
|
||||
@Controller({
|
||||
version: EnableDisableStatusEnum.ENABLED,
|
||||
path: ControllerRoute.CLIENT.ROUTE,
|
||||
})
|
||||
export class ClientController {
|
||||
constructor(private readonly clientService: ClientService) {}
|
||||
|
||||
@Post('register')
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.CLIENT.ACTIONS.REGISTER_NEW_CLIENT_SUMMARY,
|
||||
description: ControllerRoute.CLIENT.ACTIONS.REGISTER_NEW_CLIENT_DESCRIPTION,
|
||||
})
|
||||
async registerClient(@Body() dto: RegisterClientDto) {
|
||||
return this.clientService.registerClient(dto);
|
||||
}
|
||||
@Post('token')
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.CLIENT.ACTIONS.LOGIN_CLIENT_SUMMARY,
|
||||
description: ControllerRoute.CLIENT.ACTIONS.LOGIN_CLIENT_DESCRIPTION,
|
||||
})
|
||||
async login(@Body() dto: ClientTokenDto) {
|
||||
return this.clientService.loginWithClientCredentials(dto);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1 @@
|
||||
export * from './add.community.dto';
|
||||
export * from './project.param.dto';
|
||||
export * from './get.community.dto';
|
||||
export * from './register-client.dto';
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class ClientTokenDto {
|
||||
@ApiProperty({
|
||||
example: 'abcd1234xyz',
|
||||
description: 'The client ID of the client',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
clientId: string;
|
||||
|
||||
@ApiProperty({
|
||||
example: 'secureSecret123',
|
||||
description: 'The client secret of the client',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
clientSecret: string;
|
||||
}
|
||||
@@ -1,18 +1,37 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { HttpStatus, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import * as crypto from 'crypto';
|
||||
import { RegisterClientDto } from '../dtos/register-client.dto';
|
||||
import { ClientEntity } from '@app/common/modules/client/entities';
|
||||
import { ClientTokenDto } from '../dtos/token-client.dto';
|
||||
import { AuthService } from '@app/common/auth/services/auth.service';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
|
||||
@Injectable()
|
||||
export class ClientService {
|
||||
constructor(
|
||||
@InjectRepository(ClientEntity)
|
||||
private clientRepository: Repository<ClientEntity>,
|
||||
private readonly authService: AuthService,
|
||||
) {}
|
||||
|
||||
async loginWithClientCredentials(dto: ClientTokenDto) {
|
||||
const client = await this.validateClient(dto.clientId, dto.clientSecret);
|
||||
const payload = {
|
||||
client: {
|
||||
clientId: client.clientId,
|
||||
uuid: client.uuid,
|
||||
scopes: client.scopes,
|
||||
},
|
||||
};
|
||||
const tokens = await this.authService.getTokens(payload, false, '5m');
|
||||
return new SuccessResponseDto({
|
||||
message: `Client logged in successfully`,
|
||||
data: tokens,
|
||||
statusCode: HttpStatus.CREATED,
|
||||
});
|
||||
}
|
||||
async registerClient(dto: RegisterClientDto) {
|
||||
const clientId = crypto.randomBytes(16).toString('hex');
|
||||
const clientSecret = crypto.randomBytes(32).toString('hex');
|
||||
@@ -27,7 +46,11 @@ export class ClientService {
|
||||
|
||||
await this.clientRepository.save(client);
|
||||
|
||||
return { clientId, clientSecret };
|
||||
return new SuccessResponseDto({
|
||||
message: `Client registered successfully`,
|
||||
data: { clientId, clientSecret },
|
||||
statusCode: HttpStatus.CREATED,
|
||||
});
|
||||
}
|
||||
|
||||
async validateClient(
|
||||
|
||||
Reference in New Issue
Block a user