mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 10:14:54 +00:00
Merge pull request #116 from SyncrowIOT/SP-450-be-lack-of-defined-ap-is
Sp 450 be lack of defined ap is
This commit is contained in:
@ -2,7 +2,6 @@ import { Module } from '@nestjs/common';
|
|||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
import config from './config';
|
import config from './config';
|
||||||
import { AuthenticationModule } from './auth/auth.module';
|
import { AuthenticationModule } from './auth/auth.module';
|
||||||
import { AuthenticationController } from './auth/controllers/authentication.controller';
|
|
||||||
import { UserModule } from './users/user.module';
|
import { UserModule } from './users/user.module';
|
||||||
import { RoomModule } from './room/room.module';
|
import { RoomModule } from './room/room.module';
|
||||||
import { GroupModule } from './group/group.module';
|
import { GroupModule } from './group/group.module';
|
||||||
@ -53,7 +52,6 @@ import { ScheduleModule } from './schedule/schedule.module';
|
|||||||
VisitorPasswordModule,
|
VisitorPasswordModule,
|
||||||
ScheduleModule,
|
ScheduleModule,
|
||||||
],
|
],
|
||||||
controllers: [AuthenticationController],
|
|
||||||
providers: [
|
providers: [
|
||||||
{
|
{
|
||||||
provide: APP_INTERCEPTOR,
|
provide: APP_INTERCEPTOR,
|
||||||
|
|||||||
@ -1,6 +1,4 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { AuthenticationController } from './controllers/authentication.controller';
|
|
||||||
import { AuthenticationService } from './services/authentication.service';
|
|
||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
import { UserRepositoryModule } from '@app/common/modules/user/user.repository.module';
|
import { UserRepositoryModule } from '@app/common/modules/user/user.repository.module';
|
||||||
import { CommonModule } from '../../libs/common/src';
|
import { CommonModule } from '../../libs/common/src';
|
||||||
@ -16,9 +14,8 @@ import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
|||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, UserRepositoryModule, CommonModule],
|
imports: [ConfigModule, UserRepositoryModule, CommonModule],
|
||||||
controllers: [AuthenticationController, UserAuthController],
|
controllers: [UserAuthController],
|
||||||
providers: [
|
providers: [
|
||||||
AuthenticationService,
|
|
||||||
UserAuthService,
|
UserAuthService,
|
||||||
UserRepository,
|
UserRepository,
|
||||||
UserSessionRepository,
|
UserSessionRepository,
|
||||||
@ -26,6 +23,6 @@ import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
|||||||
UserRoleRepository,
|
UserRoleRepository,
|
||||||
RoleTypeRepository,
|
RoleTypeRepository,
|
||||||
],
|
],
|
||||||
exports: [AuthenticationService, UserAuthService],
|
exports: [UserAuthService],
|
||||||
})
|
})
|
||||||
export class AuthenticationModule {}
|
export class AuthenticationModule {}
|
||||||
|
|||||||
@ -1,16 +0,0 @@
|
|||||||
import { Controller, Post } from '@nestjs/common';
|
|
||||||
import { AuthenticationService } from '../services/authentication.service';
|
|
||||||
import { ApiTags } from '@nestjs/swagger';
|
|
||||||
|
|
||||||
@Controller({
|
|
||||||
version: '1',
|
|
||||||
path: 'authentication',
|
|
||||||
})
|
|
||||||
@ApiTags('Tuya Auth')
|
|
||||||
export class AuthenticationController {
|
|
||||||
constructor(private readonly authenticationService: AuthenticationService) {}
|
|
||||||
@Post('auth2')
|
|
||||||
async Authentication() {
|
|
||||||
return await this.authenticationService.main();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,2 +1 @@
|
|||||||
export * from './authentication.controller';
|
|
||||||
export * from './user-auth.controller';
|
export * from './user-auth.controller';
|
||||||
|
|||||||
@ -83,7 +83,7 @@ export class UserAuthController {
|
|||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(SuperAdminRoleGuard)
|
@UseGuards(SuperAdminRoleGuard)
|
||||||
@Get('user/list')
|
@Get('user')
|
||||||
async userList() {
|
async userList() {
|
||||||
const userList = await this.userAuthService.userList();
|
const userList = await this.userAuthService.userList();
|
||||||
return {
|
return {
|
||||||
|
|||||||
@ -1,120 +0,0 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
|
||||||
import * as qs from 'qs';
|
|
||||||
import * as crypto from 'crypto';
|
|
||||||
import { ConfigService } from '@nestjs/config';
|
|
||||||
import axios from 'axios';
|
|
||||||
@Injectable()
|
|
||||||
export class AuthenticationService {
|
|
||||||
private token: string;
|
|
||||||
private deviceId: string;
|
|
||||||
private accessKey: string;
|
|
||||||
private secretKey: string;
|
|
||||||
|
|
||||||
constructor(private readonly configService: ConfigService) {
|
|
||||||
(this.deviceId = this.configService.get<string>('auth-config.DEVICE_ID')),
|
|
||||||
(this.accessKey = this.configService.get<string>(
|
|
||||||
'auth-config.ACCESS_KEY',
|
|
||||||
)),
|
|
||||||
(this.secretKey = this.configService.get<string>(
|
|
||||||
'auth-config.SECRET_KEY',
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
async main() {
|
|
||||||
await this.getToken();
|
|
||||||
const data = await this.getDeviceInfo(this.deviceId);
|
|
||||||
console.log('fetch success: ', JSON.stringify(data));
|
|
||||||
return JSON.stringify(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
async getToken() {
|
|
||||||
const method = 'GET';
|
|
||||||
const timestamp = Date.now().toString();
|
|
||||||
const signUrl = 'https://openapi.tuyaeu.com/v1.0/token?grant_type=1';
|
|
||||||
const contentHash = crypto.createHash('sha256').update('').digest('hex');
|
|
||||||
const stringToSign = [method, contentHash, '', signUrl].join('\n');
|
|
||||||
const signStr = this.accessKey + timestamp + stringToSign;
|
|
||||||
|
|
||||||
const headers = {
|
|
||||||
t: timestamp,
|
|
||||||
sign_method: 'HMAC-SHA256',
|
|
||||||
client_id: this.accessKey,
|
|
||||||
sign: await this.encryptStr(signStr, this.secretKey),
|
|
||||||
};
|
|
||||||
|
|
||||||
const { data: login } = await axios.get(
|
|
||||||
'https://openapi.tuyaeu.com/v1.0/token',
|
|
||||||
{
|
|
||||||
params: {
|
|
||||||
grant_type: 1,
|
|
||||||
},
|
|
||||||
headers,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!login || !login.success) {
|
|
||||||
throw new Error(`fetch failed: ${login.msg}`);
|
|
||||||
}
|
|
||||||
this.token = login.result.access_token;
|
|
||||||
}
|
|
||||||
|
|
||||||
async getDeviceInfo(deviceId: string) {
|
|
||||||
const query = {};
|
|
||||||
const method = 'POST';
|
|
||||||
const url = `https://openapi.tuyaeu.com/v1.0/devices/${deviceId}/commands`;
|
|
||||||
const reqHeaders: { [k: string]: string } = await this.getRequestSign(
|
|
||||||
url,
|
|
||||||
method,
|
|
||||||
{},
|
|
||||||
query,
|
|
||||||
);
|
|
||||||
|
|
||||||
const { data } = await axios.post(url, {}, reqHeaders);
|
|
||||||
|
|
||||||
if (!data || !data.success) {
|
|
||||||
throw new Error(`request api failed: ${data.msg}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
async encryptStr(str: string, secret: string): Promise<string> {
|
|
||||||
return crypto
|
|
||||||
.createHmac('sha256', secret)
|
|
||||||
.update(str, 'utf8')
|
|
||||||
.digest('hex')
|
|
||||||
.toUpperCase();
|
|
||||||
}
|
|
||||||
|
|
||||||
async getRequestSign(
|
|
||||||
path: string,
|
|
||||||
method: string,
|
|
||||||
query: { [k: string]: any } = {},
|
|
||||||
body: { [k: string]: any } = {},
|
|
||||||
) {
|
|
||||||
const t = Date.now().toString();
|
|
||||||
const [uri, pathQuery] = path.split('?');
|
|
||||||
const queryMerged = Object.assign(query, qs.parse(pathQuery));
|
|
||||||
const sortedQuery: { [k: string]: string } = {};
|
|
||||||
Object.keys(queryMerged)
|
|
||||||
.sort()
|
|
||||||
.forEach((i) => (sortedQuery[i] = query[i]));
|
|
||||||
|
|
||||||
const querystring = decodeURIComponent(qs.stringify(sortedQuery));
|
|
||||||
const url = querystring ? `${uri}?${querystring}` : uri;
|
|
||||||
const contentHash = crypto
|
|
||||||
.createHash('sha256')
|
|
||||||
.update(JSON.stringify(body))
|
|
||||||
.digest('hex');
|
|
||||||
const stringToSign = [method, contentHash, '', url].join('\n');
|
|
||||||
const signStr = this.accessKey + this.token + t + stringToSign;
|
|
||||||
return {
|
|
||||||
t,
|
|
||||||
path: url,
|
|
||||||
client_id: 'this.accessKey',
|
|
||||||
sign: await this.encryptStr(signStr, this.secretKey),
|
|
||||||
sign_method: 'HMAC-SHA256',
|
|
||||||
access_token: this.token,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,2 +1 @@
|
|||||||
export * from './authentication.service';
|
|
||||||
export * from './user-auth.service';
|
export * from './user-auth.service';
|
||||||
|
|||||||
@ -134,7 +134,7 @@ export class BuildingController {
|
|||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard, BuildingPermissionGuard)
|
@UseGuards(JwtAuthGuard, BuildingPermissionGuard)
|
||||||
@Put('rename/:buildingUuid')
|
@Put(':buildingUuid')
|
||||||
async renameBuildingByUuid(
|
async renameBuildingByUuid(
|
||||||
@Param('buildingUuid') buildingUuid: string,
|
@Param('buildingUuid') buildingUuid: string,
|
||||||
@Body() updateBuildingDto: UpdateBuildingNameDto,
|
@Body() updateBuildingDto: UpdateBuildingNameDto,
|
||||||
|
|||||||
@ -133,7 +133,7 @@ export class CommunityController {
|
|||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Put('rename/:communityUuid')
|
@Put(':communityUuid')
|
||||||
async renameCommunityByUuid(
|
async renameCommunityByUuid(
|
||||||
@Param('communityUuid') communityUuid: string,
|
@Param('communityUuid') communityUuid: string,
|
||||||
@Body() updateCommunityDto: UpdateCommunityNameDto,
|
@Body() updateCommunityDto: UpdateCommunityNameDto,
|
||||||
|
|||||||
@ -134,7 +134,7 @@ export class FloorController {
|
|||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard, FloorPermissionGuard)
|
@UseGuards(JwtAuthGuard, FloorPermissionGuard)
|
||||||
@Put('rename/:floorUuid')
|
@Put(':floorUuid')
|
||||||
async renameFloorByUuid(
|
async renameFloorByUuid(
|
||||||
@Param('floorUuid') floorUuid: string,
|
@Param('floorUuid') floorUuid: string,
|
||||||
@Body() updateFloorNameDto: UpdateFloorNameDto,
|
@Body() updateFloorNameDto: UpdateFloorNameDto,
|
||||||
|
|||||||
@ -110,7 +110,7 @@ export class RoomController {
|
|||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard, RoomPermissionGuard)
|
@UseGuards(JwtAuthGuard, RoomPermissionGuard)
|
||||||
@Put('rename/:roomUuid')
|
@Put(':roomUuid')
|
||||||
async renameRoomByUuid(
|
async renameRoomByUuid(
|
||||||
@Param('roomUuid') roomUuid: string,
|
@Param('roomUuid') roomUuid: string,
|
||||||
@Body() updateRoomNameDto: UpdateRoomNameDto,
|
@Body() updateRoomNameDto: UpdateRoomNameDto,
|
||||||
|
|||||||
@ -132,7 +132,7 @@ export class UnitController {
|
|||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
|
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
|
||||||
@Put('rename/:unitUuid')
|
@Put(':unitUuid')
|
||||||
async renameUnitByUuid(
|
async renameUnitByUuid(
|
||||||
@Param('unitUuid') unitUuid: string,
|
@Param('unitUuid') unitUuid: string,
|
||||||
@Body() updateUnitNameDto: UpdateUnitNameDto,
|
@Body() updateUnitNameDto: UpdateUnitNameDto,
|
||||||
|
|||||||
@ -28,7 +28,7 @@ export class UserDevicePermissionController {
|
|||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminRoleGuard)
|
@UseGuards(AdminRoleGuard)
|
||||||
@Post('add')
|
@Post()
|
||||||
async addDevicePermission(
|
async addDevicePermission(
|
||||||
@Body() userDevicePermissionDto: UserDevicePermissionAddDto,
|
@Body() userDevicePermissionDto: UserDevicePermissionAddDto,
|
||||||
) {
|
) {
|
||||||
@ -52,7 +52,7 @@ export class UserDevicePermissionController {
|
|||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminRoleGuard)
|
@UseGuards(AdminRoleGuard)
|
||||||
@Put('edit/:devicePermissionUuid')
|
@Put(':devicePermissionUuid')
|
||||||
async editDevicePermission(
|
async editDevicePermission(
|
||||||
@Param('devicePermissionUuid') devicePermissionUuid: string,
|
@Param('devicePermissionUuid') devicePermissionUuid: string,
|
||||||
@Body() userDevicePermissionEditDto: UserDevicePermissionEditDto,
|
@Body() userDevicePermissionEditDto: UserDevicePermissionEditDto,
|
||||||
@ -76,7 +76,7 @@ export class UserDevicePermissionController {
|
|||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminRoleGuard)
|
@UseGuards(AdminRoleGuard)
|
||||||
@Get(':deviceUuid/list')
|
@Get(':deviceUuid')
|
||||||
async fetchDevicePermission(@Param('deviceUuid') deviceUuid: string) {
|
async fetchDevicePermission(@Param('deviceUuid') deviceUuid: string) {
|
||||||
try {
|
try {
|
||||||
const deviceDetails =
|
const deviceDetails =
|
||||||
|
|||||||
Reference in New Issue
Block a user