From 2b53b4c7377eea848d2d952f5889008a4706b590 Mon Sep 17 00:00:00 2001 From: chi Date: Fri, 29 Mar 2024 16:48:55 -0400 Subject: [PATCH] issue 72 - added cryptographic authentication to analytics enteries --- .../AnalyticsEntryController.swift | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/Sources/Server/Controllers/AnalyticsEntryController.swift b/Sources/Server/Controllers/AnalyticsEntryController.swift index 6e6925c..bab19a4 100644 --- a/Sources/Server/Controllers/AnalyticsEntryController.swift +++ b/Sources/Server/Controllers/AnalyticsEntryController.swift @@ -18,14 +18,34 @@ struct AnalyticsEntryController: RouteCollection { } private func read(_ request: Request) async throws -> AnalyticsEntry { - let entry = try await AnalyticsEntry.find( - request.parameters.get("id"), - on: request.db(.psql) - ) - guard let entry else { - throw Abort(.notFound) + + // decode retrieval request + + let retrievalRequest = try request.query.decode(AnalyticsEntry.RetrievalRequest.self) + + guard let idString = request.parameters.get("id"), let id = UUID(uuidString: idString) else { + throw Abort(.badRequest) + } + + // cryptogrpahic verification + guard let data = id.uuidString.data(using: .utf8) else { + throw Abort(.internalServerError) + } + + // crytographic signature + if try CryptographyUtilities.verify(signature: retrievalRequest.signature, of: data) { + let entry = try await AnalyticsEntry.find( + id, + on: request.db(.psql) + ) + guard let entry else { + throw Abort(.notFound) + } + return entry + } else { + throw Abort(.forbidden) } - return entry } } +