Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue 72 - added cryptographic authentication to analytics entries #91

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions Sources/Server/Controllers/AnalyticsEntryController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}