fix: integrate with signature
This commit is contained in:
@@ -1,42 +1,22 @@
|
||||
import { verify } from '~/helpers/signature'
|
||||
import { DeviceCommandBody } from '../schema'
|
||||
import { sendCommandToThirdParty } from './command'
|
||||
|
||||
export default abstract class CommandService {
|
||||
static async sendCommand(body: DeviceCommandBody, headers: Record<string, string | undefined>): Promise<{
|
||||
static async sendCommand(body: DeviceCommandBody): Promise<{
|
||||
status: 'success'
|
||||
message: 'Command accepted'
|
||||
receivedAt: string
|
||||
}> {
|
||||
const apiKey = headers['x-api-key']
|
||||
|
||||
// For now, use apiKey as secretKey since DB validation is skipped
|
||||
const secretKey = apiKey
|
||||
|
||||
// Remove signature from body for verification
|
||||
const { signature, ...bodyWithoutSignature } = body
|
||||
|
||||
const isValid = verify(bodyWithoutSignature, signature, secretKey)
|
||||
if (!isValid) {
|
||||
throw new Error('invalid_signature')
|
||||
}
|
||||
|
||||
// TODO: Check nonce for replay attack when DB is implemented
|
||||
|
||||
const result = {
|
||||
status: 'success' as const,
|
||||
message: 'Command accepted' as const,
|
||||
receivedAt: new Date().toISOString(),
|
||||
}
|
||||
|
||||
// Generate topicData and send to third-party service
|
||||
try {
|
||||
const topicData = `${body.merchantName}/${body.floorName}_${body.unitNumber}-${body.deviceName}-${body.roomName}-${body.deviceType}-${body.commandType}-${body.towerNumber}`
|
||||
await sendCommandToThirdParty(topicData, body.payload.action)
|
||||
} catch {
|
||||
throw new Error('internal_error')
|
||||
}
|
||||
|
||||
return result
|
||||
return {
|
||||
status: 'success' as const,
|
||||
message: 'Command accepted' as const,
|
||||
receivedAt: new Date().toISOString(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user