V0.6.1 Arroyo Toad
Pushing Private Keys (Obviously these are test only)
This commit is contained in:
@@ -19,7 +19,7 @@ public func privateP256_2_pem(privateKey: P256.Signing.PrivateKey) -> String {
|
||||
}
|
||||
|
||||
// UGLY: Refactor to make it easier to comprehend
|
||||
public func fetchPrivateP256Key(deviceID: UInt128) async throws -> P256.Signing.PrivateKey {
|
||||
public func fetchPrivateP256Key(deviceID: UInt) async throws -> P256.Signing.PrivateKey {
|
||||
|
||||
// UGLY: but fast
|
||||
let privateKeyFolder = ProcessInfo.processInfo.environment["PRIVATE_KEY_FOLDER"] ?? "./Private/PrivateKeysP256"
|
||||
@@ -29,21 +29,7 @@ public func fetchPrivateP256Key(deviceID: UInt128) async throws -> P256.Signing.
|
||||
do {
|
||||
let key = try pem2_P265_PrivateKey(filePath: keyFilePath)
|
||||
// TODO: send public key to another server
|
||||
let publicKey = key.publicKey.pemRepresentation
|
||||
|
||||
// UGLY: hardcoded
|
||||
var httpRequest = URLRequest(url: URL(string: "http://publick-key-db.internal/key")!)
|
||||
httpRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
httpRequest.httpMethod = "POST"
|
||||
|
||||
let message: [String : Encodable] = [
|
||||
"deviceID": deviceID,
|
||||
"publicKey": publicKey
|
||||
]
|
||||
let data = try JSONSerialization.data(withJSONObject: message)
|
||||
httpRequest.httpBody = data
|
||||
|
||||
let _ = try await URLSession.shared.upload(for: httpRequest, from: data)
|
||||
|
||||
|
||||
return key
|
||||
} catch {
|
||||
@@ -52,7 +38,23 @@ public func fetchPrivateP256Key(deviceID: UInt128) async throws -> P256.Signing.
|
||||
|
||||
do {
|
||||
let key = createPrivateP256Key()
|
||||
|
||||
let publicKey = key.publicKey.pemRepresentation
|
||||
|
||||
try privateP256_2_pem(privateKey: key).write(to: URL(filePath: keyFilePath), atomically: true, encoding: String.Encoding.utf8)
|
||||
|
||||
|
||||
// UGLY: hardcoded
|
||||
var httpRequest = URLRequest(url: URL(string: "http://public-key-db.internal/key")!)
|
||||
httpRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
httpRequest.httpMethod = "POST"
|
||||
|
||||
let message = PublicKeyMessage(deviceID: deviceID, publicKey: publicKey)
|
||||
let encoder = JSONEncoder()
|
||||
let data = try encoder.encode(message)
|
||||
httpRequest.httpBody = data
|
||||
|
||||
let _ = try await URLSession.shared.upload(for: httpRequest, from: data)
|
||||
return key
|
||||
} catch {
|
||||
throw ParsingError.ImpossibleToWriteKeyToFileSystem
|
||||
|
||||
4
Sources/App/CustomCode/Utils/PublicKeyMessage.swift
Normal file
4
Sources/App/CustomCode/Utils/PublicKeyMessage.swift
Normal file
@@ -0,0 +1,4 @@
|
||||
public struct PublicKeyMessage : Codable {
|
||||
public let deviceID: UInt
|
||||
public let publicKey: String
|
||||
}
|
||||
Reference in New Issue
Block a user