From ab187006345250666388096f6e08e58adfa647fc Mon Sep 17 00:00:00 2001 From: sydneymorton Date: Tue, 6 Oct 2020 12:57:44 -0700 Subject: [PATCH] Fix Nits --- VCEntities/VCEntities.xcodeproj/project.pbxproj | 4 ++++ .../VCEntities/formatters/FormatterHelpers.swift | 4 ++-- .../formatters/IssuanceResponseFormatter.swift | 7 +++---- .../formatters/PresentationResponseFormatter.swift | 7 +++---- .../VCEntities/formatters/TokenTimeConstraints.swift | 10 ++++++++++ .../formatters/VerifiablePresentationFormatter.swift | 6 +++--- .../presentation/PresentationResponseContainer.swift | 2 -- VcJwt/VcJwt/algorithms/ECPublicJwk.swift | 3 +-- 8 files changed, 26 insertions(+), 17 deletions(-) create mode 100644 VCEntities/VCEntities/formatters/TokenTimeConstraints.swift diff --git a/VCEntities/VCEntities.xcodeproj/project.pbxproj b/VCEntities/VCEntities.xcodeproj/project.pbxproj index 1264728..ca6ddbb 100644 --- a/VCEntities/VCEntities.xcodeproj/project.pbxproj +++ b/VCEntities/VCEntities.xcodeproj/project.pbxproj @@ -14,6 +14,7 @@ 5518CC7425264CAD00C7A21B /* PresentationResponseContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5518CC7325264CAD00C7A21B /* PresentationResponseContainer.swift */; }; 5518CC7625264D5700C7A21B /* ResponseMappings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5518CC7525264D5700C7A21B /* ResponseMappings.swift */; }; 551F30432527DC050081D5E7 /* FormatterHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 551F30422527DC050081D5E7 /* FormatterHelpers.swift */; }; + 551F3057252D02AB0081D5E7 /* TokenTimeConstraints.swift in Sources */ = {isa = PBXBuildFile; fileRef = 551F3056252D02AB0081D5E7 /* TokenTimeConstraints.swift */; }; 55575738251BC575009979AB /* VCEntities.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5557572E251BC575009979AB /* VCEntities.framework */; }; 5557573D251BC575009979AB /* VCEntitiesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5557573C251BC575009979AB /* VCEntitiesTests.swift */; }; 5557573F251BC575009979AB /* VCEntities.h in Headers */ = {isa = PBXBuildFile; fileRef = 55575731251BC575009979AB /* VCEntities.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -82,6 +83,7 @@ 5518CC7325264CAD00C7A21B /* PresentationResponseContainer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PresentationResponseContainer.swift; sourceTree = ""; }; 5518CC7525264D5700C7A21B /* ResponseMappings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResponseMappings.swift; sourceTree = ""; }; 551F30422527DC050081D5E7 /* FormatterHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FormatterHelpers.swift; sourceTree = ""; }; + 551F3056252D02AB0081D5E7 /* TokenTimeConstraints.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TokenTimeConstraints.swift; sourceTree = ""; }; 5557572E251BC575009979AB /* VCEntities.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = VCEntities.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 55575731251BC575009979AB /* VCEntities.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = VCEntities.h; sourceTree = ""; }; 55575732251BC575009979AB /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -319,6 +321,7 @@ 5518CC7125264C6F00C7A21B /* PresentationResponseFormatter.swift */, 555CE08925267FE500C1C938 /* VerifiablePresentationFormatter.swift */, 551F30422527DC050081D5E7 /* FormatterHelpers.swift */, + 551F3056252D02AB0081D5E7 /* TokenTimeConstraints.swift */, ); path = formatters; sourceTree = ""; @@ -486,6 +489,7 @@ 5584E4A02525656500A9DE58 /* SchemaDescriptor.swift in Sources */, 55575766251BC6CF009979AB /* LogoDisplayDescriptor.swift in Sources */, 5557576F251BC6CF009979AB /* IdTokenDescriptor.swift in Sources */, + 551F3057252D02AB0081D5E7 /* TokenTimeConstraints.swift in Sources */, 555CE08A25267FE500C1C938 /* VerifiablePresentationFormatter.swift in Sources */, 55575772251BC6CF009979AB /* ServiceDescriptor.swift in Sources */, 5557576E251BC6CF009979AB /* ClaimDescriptor.swift in Sources */, diff --git a/VCEntities/VCEntities/formatters/FormatterHelpers.swift b/VCEntities/VCEntities/formatters/FormatterHelpers.swift index 56307bd..5499086 100644 --- a/VCEntities/VCEntities/formatters/FormatterHelpers.swift +++ b/VCEntities/VCEntities/formatters/FormatterHelpers.swift @@ -5,10 +5,10 @@ import VCJwt -func createIatAndExp(expiryInSeconds: Int) -> (Double, Double) { +func createTokenTimeConstraints(expiryInSeconds: Int) -> TokenTimeConstraints { let iat = (Date().timeIntervalSince1970).rounded(.down) let exp = iat + Double(expiryInSeconds) - return (iat, exp) + return TokenTimeConstraints(issuedAt: iat, expiration: exp) } func formatHeaders(usingIdentifier identifier: MockIdentifier) -> Header { diff --git a/VCEntities/VCEntities/formatters/IssuanceResponseFormatter.swift b/VCEntities/VCEntities/formatters/IssuanceResponseFormatter.swift index 4372e7a..2dd8c31 100644 --- a/VCEntities/VCEntities/formatters/IssuanceResponseFormatter.swift +++ b/VCEntities/VCEntities/formatters/IssuanceResponseFormatter.swift @@ -4,7 +4,6 @@ *--------------------------------------------------------------------------------------------*/ import VCJwt -import VCCrypto public protocol IssuanceResponseFormatting { func format(response: IssuanceResponseContainer, usingIdentifier identifier: MockIdentifier) throws -> IssuanceResponse @@ -33,7 +32,7 @@ public class IssuanceResponseFormatter: IssuanceResponseFormatting { private func formatClaims(response: IssuanceResponseContainer, usingIdentifier identifier: MockIdentifier) throws -> IssuanceResponseClaims { let publicKey = try signer.getPublicJwk(from: identifier.keyId, withKeyId: identifier.keyReference) - let (iat, exp) = createIatAndExp(expiryInSeconds: response.expiryInSeconds) + let timeConstraints = createTokenTimeConstraints(expiryInSeconds: response.expiryInSeconds) return IssuanceResponseClaims(publicKeyThumbprint: try publicKey.getThumbprint(), audience: response.audience, @@ -42,8 +41,8 @@ public class IssuanceResponseFormatter: IssuanceResponseFormatting { contract: response.contractUri, jti: UUID().uuidString, attestations: self.formatAttestations(response: response), - iat: iat, - exp: exp) + iat: timeConstraints.issuedAt, + exp: timeConstraints.expiration) } private func formatAttestations(response: IssuanceResponseContainer) -> AttestationResponseDescriptor? { diff --git a/VCEntities/VCEntities/formatters/PresentationResponseFormatter.swift b/VCEntities/VCEntities/formatters/PresentationResponseFormatter.swift index 93207bc..26fb4f4 100644 --- a/VCEntities/VCEntities/formatters/PresentationResponseFormatter.swift +++ b/VCEntities/VCEntities/formatters/PresentationResponseFormatter.swift @@ -4,7 +4,6 @@ *--------------------------------------------------------------------------------------------*/ import VCJwt -import VCCrypto let CREDENTIAL_PATH = "$.attestations.presentations." let CREDENTIAL_ENCODING = "base64Url" @@ -38,7 +37,7 @@ public class PresentationResponseFormatter: PresentationResponseFormatting { private func formatClaims(response: PresentationResponseContainer, usingIdentifier identifier: MockIdentifier) throws -> PresentationResponseClaims { let publicKey = try signer.getPublicJwk(from: identifier.keyId, withKeyId: identifier.keyReference) - let (iat, exp) = createIatAndExp(expiryInSeconds: response.expiryInSeconds) + let timeConstraints = createTokenTimeConstraints(expiryInSeconds: response.expiryInSeconds) var presentationSubmission: PresentationSubmission? = nil var attestations: AttestationResponseDescriptor? = nil @@ -56,8 +55,8 @@ public class PresentationResponseFormatter: PresentationResponseFormatting { attestations: attestations, state: response.request.content.state, nonce: response.request.content.nonce, - iat: iat, - exp: exp) + iat: timeConstraints.issuedAt, + exp: timeConstraints.expiration) } private func formatPresentationSubmission(response: PresentationResponseContainer, keyType: String) -> PresentationSubmission { diff --git a/VCEntities/VCEntities/formatters/TokenTimeConstraints.swift b/VCEntities/VCEntities/formatters/TokenTimeConstraints.swift new file mode 100644 index 0000000..eeae2ab --- /dev/null +++ b/VCEntities/VCEntities/formatters/TokenTimeConstraints.swift @@ -0,0 +1,10 @@ +/*--------------------------------------------------------------------------------------------- +* Copyright (c) Microsoft Corporation. All rights reserved. +* Licensed under the MIT License. See License.txt in the project root for license information. +*--------------------------------------------------------------------------------------------*/ + + +struct TokenTimeConstraints { + let issuedAt: Double + let expiration: Double +} diff --git a/VCEntities/VCEntities/formatters/VerifiablePresentationFormatter.swift b/VCEntities/VCEntities/formatters/VerifiablePresentationFormatter.swift index e0711c1..9da4a84 100644 --- a/VCEntities/VCEntities/formatters/VerifiablePresentationFormatter.swift +++ b/VCEntities/VCEntities/formatters/VerifiablePresentationFormatter.swift @@ -23,7 +23,7 @@ class VerifiablePresentationFormatter { usingIdentifier identifier: MockIdentifier) throws -> VerifiablePresentation { let headers = formatHeaders(usingIdentifier: identifier) - let (iat, exp) = createIatAndExp(expiryInSeconds: exp) + let timeConstraints = createTokenTimeConstraints(expiryInSeconds: exp) let verifiablePresentationDescriptor = try self.createVerifiablePresentationDescriptor(toWrap: vc) let vpClaims = VerifiablePresentationClaims(vpId: UUID().uuidString, @@ -31,8 +31,8 @@ class VerifiablePresentationFormatter { verifiablePresentation: verifiablePresentationDescriptor, issuerOfVp: identifier.id, audience: audience, - iat: iat, - exp: exp) + iat: timeConstraints.issuedAt, + exp: timeConstraints.expiration) var token = JwsToken(headers: headers, content: vpClaims) try token.sign(using: self.signer, withSecret: identifier.keyId) diff --git a/VCEntities/VCEntities/presentation/PresentationResponseContainer.swift b/VCEntities/VCEntities/presentation/PresentationResponseContainer.swift index e764e50..d7d4e57 100644 --- a/VCEntities/VCEntities/presentation/PresentationResponseContainer.swift +++ b/VCEntities/VCEntities/presentation/PresentationResponseContainer.swift @@ -20,7 +20,5 @@ public struct PresentationResponseContainer { self.audience = presentationRequest.content.redirectURI self.request = presentationRequest self.expiryInSeconds = exp - - print(request.content.redirectURI) } } diff --git a/VcJwt/VcJwt/algorithms/ECPublicJwk.swift b/VcJwt/VcJwt/algorithms/ECPublicJwk.swift index 88a9191..31a8abc 100644 --- a/VcJwt/VcJwt/algorithms/ECPublicJwk.swift +++ b/VcJwt/VcJwt/algorithms/ECPublicJwk.swift @@ -46,15 +46,14 @@ public struct ECPublicJwk: Codable { } public func getThumbprint() throws -> String { + let hashAlgorithm = Sha256() guard let encodedJwk = self.getMinimumAlphabeticJwk().data(using: .utf8) else { throw VCJwtError.unableToParseString } - print(String(data: encodedJwk, encoding: .utf8)!) let hash = hashAlgorithm.hash(data: encodedJwk) - // print(String(data: hash, encoding: .utf8)!) return hash.base64URLEncodedString() } }