mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 07:07:21 +00:00
add register client api
This commit is contained in:
@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { ClientEntity } from './entities';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([ClientEntity])],
|
||||
exports: [TypeOrmModule],
|
||||
})
|
||||
export class ClientRepositoryModule {}
|
@ -29,11 +29,13 @@ import { RoleModule } from './role/role.module';
|
||||
import { TermsConditionsModule } from './terms-conditions/terms-conditions.module';
|
||||
import { PrivacyPolicyModule } from './privacy-policy/privacy-policy.module';
|
||||
import { TagModule } from './tags/tags.module';
|
||||
import { ClientModule } from './client/client.module';
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
load: config,
|
||||
}),
|
||||
ClientModule,
|
||||
AuthenticationModule,
|
||||
UserModule,
|
||||
InviteUserModule,
|
||||
|
13
src/client/client.module.ts
Normal file
13
src/client/client.module.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
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';
|
||||
|
||||
@Module({
|
||||
imports: [ClientRepositoryModule],
|
||||
controllers: [ClientController],
|
||||
providers: [ClientService, ClientRepository],
|
||||
exports: [ClientService],
|
||||
})
|
||||
export class ClientModule {}
|
15
src/client/controllers/client.controller.ts
Normal file
15
src/client/controllers/client.controller.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { Controller, Post, Body } from '@nestjs/common';
|
||||
import { ApiTags } from '@nestjs/swagger';
|
||||
import { RegisterClientDto } from '../dtos/register-client.dto';
|
||||
import { ClientService } from '../services';
|
||||
|
||||
@ApiTags('OAuth Clients')
|
||||
@Controller('clients')
|
||||
export class ClientController {
|
||||
constructor(private readonly clientService: ClientService) {}
|
||||
|
||||
@Post('register')
|
||||
async registerClient(@Body() dto: RegisterClientDto) {
|
||||
return this.clientService.registerClient(dto);
|
||||
}
|
||||
}
|
1
src/client/controllers/index.ts
Normal file
1
src/client/controllers/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './client.controller';
|
3
src/client/dtos/index.ts
Normal file
3
src/client/dtos/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from './add.community.dto';
|
||||
export * from './project.param.dto';
|
||||
export * from './get.community.dto';
|
31
src/client/dtos/register-client.dto.ts
Normal file
31
src/client/dtos/register-client.dto.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class RegisterClientDto {
|
||||
@ApiProperty({
|
||||
example: 'SmartHomeApp',
|
||||
description: 'The name of the client',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
@ApiProperty({
|
||||
example: 'https://client-app.com/callback',
|
||||
description: 'The redirect URI of the client',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
redirectUri: string;
|
||||
|
||||
@ApiProperty({
|
||||
example: ['DEVICE_SINGLE_CONTROL', 'DEVICE_VIEW'],
|
||||
description: 'The scopes of the client',
|
||||
required: true,
|
||||
})
|
||||
@IsString({ each: true })
|
||||
@IsNotEmpty({ each: true })
|
||||
scopes: string[];
|
||||
}
|
45
src/client/services/client.service.ts
Normal file
45
src/client/services/client.service.ts
Normal file
@ -0,0 +1,45 @@
|
||||
import { 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';
|
||||
|
||||
@Injectable()
|
||||
export class ClientService {
|
||||
constructor(
|
||||
@InjectRepository(ClientEntity)
|
||||
private clientRepository: Repository<ClientEntity>,
|
||||
) {}
|
||||
|
||||
async registerClient(dto: RegisterClientDto) {
|
||||
const clientId = crypto.randomBytes(16).toString('hex');
|
||||
const clientSecret = crypto.randomBytes(32).toString('hex');
|
||||
|
||||
const client = this.clientRepository.create({
|
||||
name: dto.name,
|
||||
clientId,
|
||||
clientSecret,
|
||||
redirectUri: dto.redirectUri,
|
||||
scopes: dto.scopes,
|
||||
});
|
||||
|
||||
await this.clientRepository.save(client);
|
||||
|
||||
return { clientId, clientSecret };
|
||||
}
|
||||
|
||||
async validateClient(
|
||||
clientId: string,
|
||||
clientSecret: string,
|
||||
): Promise<ClientEntity | null> {
|
||||
const client = await this.clientRepository.findOne({
|
||||
where: { clientId, clientSecret },
|
||||
});
|
||||
if (!client) {
|
||||
throw new NotFoundException('Invalid client credentials');
|
||||
}
|
||||
return client;
|
||||
}
|
||||
}
|
1
src/client/services/index.ts
Normal file
1
src/client/services/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './client.service';
|
Reference in New Issue
Block a user