2022-01-23 20:49:06 +01:00
|
|
|
import Vapor
|
|
|
|
|
2023-12-08 12:39:10 +01:00
|
|
|
func routes(_ app: Application) {
|
2022-01-24 17:17:06 +01:00
|
|
|
|
|
|
|
/**
|
2022-04-08 13:33:09 +02:00
|
|
|
Post a message to the device for unlocking.
|
2022-05-01 13:12:16 +02:00
|
|
|
|
2023-12-08 12:39:10 +01:00
|
|
|
The expects a `Message` in the body data of the POST request, containing the message to send to the device.
|
|
|
|
Expects a header ``RouteAPI.authenticationHeader`` with the hexencoded authentication token with binary length ``ServerMessage.authTokenSize``.
|
2022-01-24 17:17:06 +01:00
|
|
|
|
2023-12-08 12:39:10 +01:00
|
|
|
The request returns the ``ServerMessage.messageSize`` bytes of data constituting the device response,
|
|
|
|
or a status code corresponding to a ``MessageResult``.
|
|
|
|
This request does not complete until either the device responds or the request times out.
|
|
|
|
The timeout is specified by the configuration parameter `deviceTimeout`.
|
2022-01-24 17:17:06 +01:00
|
|
|
*/
|
2023-12-08 12:39:10 +01:00
|
|
|
app.post(SesameRoute.postMessage.path) { request async throws in
|
2023-11-10 13:45:42 +01:00
|
|
|
do {
|
2023-12-08 12:39:10 +01:00
|
|
|
guard let authString = request.headers.first(name: SesameHeader.authenticationHeader),
|
|
|
|
let authToken = Data(fromHexEncodedString: authString),
|
|
|
|
authToken.count == SesameHeader.serverAuthenticationTokenSize else {
|
2023-12-08 15:43:29 +01:00
|
|
|
throw MessageResult.missingOrInvalidAuthenticationHeaderFromRemote
|
2023-12-08 12:39:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
guard let body = request.body.data,
|
|
|
|
let message = body.getData(at: 0, length: body.readableBytes) else {
|
2023-12-08 15:43:29 +01:00
|
|
|
throw MessageResult.noOrInvalidBodyDataFromRemote
|
2023-12-08 12:39:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let responseMessage = try await deviceManager.sendMessageToDevice(message, authToken: authToken, on: request.eventLoop)
|
|
|
|
return Response(status: .ok, body: .init(data: responseMessage))
|
2023-11-10 13:45:42 +01:00
|
|
|
} catch let error as MessageResult {
|
2023-12-08 12:39:10 +01:00
|
|
|
return Response(status: .init(statusCode: error.statusCode))
|
2022-04-07 23:53:25 +02:00
|
|
|
}
|
2022-01-23 20:49:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-08 12:39:10 +01:00
|
|
|
Start a new websocket connection for the device to receive messages from the server.
|
|
|
|
|
|
|
|
The request must contain a header ``RouteAPI.socketAuthenticationHeader`` with a valid authentication token.
|
2022-01-23 20:49:06 +01:00
|
|
|
*/
|
2023-12-08 12:39:10 +01:00
|
|
|
app.webSocket(SesameRoute.socket.path) { request, socket async in
|
|
|
|
guard let authToken = request.headers.first(name: SesameHeader.authenticationHeader) else {
|
2023-12-06 09:05:41 +01:00
|
|
|
try? await socket.close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
await deviceManager.createNewDeviceConnection(socket: socket, auth: authToken)
|
2022-01-23 20:49:06 +01:00
|
|
|
}
|
|
|
|
}
|