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

New VC unit tests #36

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions Sources/Issuer/SDJWTIssuer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,12 @@ public class SDJWTIssuer {
kbJWT: nil
)
return try createSignedSDJWT(sdJwt: ungsingedSDJWT, issuersPrivateKey: signingKey)
// ..........
case .presentation(let signedJWT, let selectedDisclosures, let KBJWT):
let signedJWT = signedJWT.disclosuresToPresent(disclosures: selectedDisclosures)
if let KBJWT {
return try createKeyBondedSDJWT(signedSDJWT: signedJWT, kbJWT: KBJWT, holdersPrivateKey: signingKey)
}
return signedJWT
// ..........
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
* limitations under the License.
*/
import Foundation
import SwiftyJSON

import XCTest

@testable import eudi_lib_sdjwt_swift

class NetworkingMock: Networking {
class NetworkingBundleMock: Networking {

let path: String
let `extension`: String
Expand Down Expand Up @@ -56,3 +58,36 @@ class NetworkingMock: Networking {
return try await data(from: URL(string: "https://www.example.com")!)
}
}

class NetworkingJSONMock: Networking {

let json: JSON
let statusCode: Int

init(
json: JSON,
statusCode: Int = 200
) {
self.json = json
self.statusCode = statusCode
}

func data(
from url: URL
) async throws -> (Data, URLResponse) {
let result = Result<Data, Error>.success(try self.json.rawData())
let response = HTTPURLResponse(
url: .stub(),
statusCode: statusCode,
httpVersion: nil,
headerFields: [:]
)
return try (result.get(), response!)
}

func data(
for request: URLRequest
) async throws -> (Data, URLResponse) {
return try await data(from: URL(string: "https://www.example.com")!)
}
}
123 changes: 107 additions & 16 deletions Tests/Verification/VcVerifierTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ final class VcVerifierTest: XCTestCase {
// When
let result = try await SDJWTVCVerifier(
trust: X509CertificateChainVerifier()
)
.verifyIssuance(
).verifyIssuance(
unverifiedSdJwt: sdJwtString
)

Expand All @@ -56,13 +55,12 @@ final class VcVerifierTest: XCTestCase {
// When
let result = try await SDJWTVCVerifier(
fetcher: SdJwtVcIssuerMetaDataFetcher(
session: NetworkingMock(
session: NetworkingBundleMock(
path: "issuer_meta_data",
extension: "json"
)
)
)
.verifyIssuance(
).verifyIssuance(
unverifiedSdJwt: sdJwtString
)

Expand All @@ -78,8 +76,7 @@ final class VcVerifierTest: XCTestCase {
// When
let result = try await SDJWTVCVerifier(
lookup: LookupPublicKeysFromDIDDocumentMock()
)
.verifyIssuance(
).verifyIssuance(
unverifiedSdJwt: sdJwtString
)

Expand All @@ -103,8 +100,7 @@ final class VcVerifierTest: XCTestCase {

let result = try await SDJWTVCVerifier(
trust: X509CertificateChainVerifier()
)
.verifyIssuance(
).verifyIssuance(
unverifiedSdJwt: json
)

Expand All @@ -128,8 +124,7 @@ final class VcVerifierTest: XCTestCase {

let result = try await SDJWTVCVerifier(
trust: X509CertificateChainVerifier()
)
.verifyIssuance(
).verifyIssuance(
unverifiedSdJwt: json
)

Expand All @@ -153,13 +148,12 @@ final class VcVerifierTest: XCTestCase {

let result = try await SDJWTVCVerifier(
fetcher: SdJwtVcIssuerMetaDataFetcher(
session: NetworkingMock(
session: NetworkingBundleMock(
path: "issuer_meta_data",
extension: "json"
)
)
)
.verifyIssuance(
).verifyIssuance(
unverifiedSdJwt: json
)

Expand All @@ -175,7 +169,7 @@ final class VcVerifierTest: XCTestCase {
// When
let result = try await SDJWTVCVerifier(
fetcher: SdJwtVcIssuerMetaDataFetcher(
session: NetworkingMock(
session: NetworkingBundleMock(
path: "issuer_meta_data",
extension: "json"
)
Expand Down Expand Up @@ -206,7 +200,7 @@ final class VcVerifierTest: XCTestCase {

let result = try await SDJWTVCVerifier(
fetcher: SdJwtVcIssuerMetaDataFetcher(
session: NetworkingMock(
session: NetworkingBundleMock(
path: "issuer_meta_data",
extension: "json"
)
Expand All @@ -220,4 +214,101 @@ final class VcVerifierTest: XCTestCase {
// Then
XCTAssertNoThrow(try result.get())
}

func testVerifyPresentation_WithDSLBuiltValidSDJWT_WithIssuerMetaData_Presentation_ShouldSucceed() async throws {

let issuersKey = issuersKeyPair.public
let issuerJwk = try issuersKey.jwk

let holdersKey = holdersKeyPair.public
let holdersJwk = try holdersKey.jwk

let jsonObject: JSON = [
"issuer": "https://example.com/issuer",
"jwks": [
"keys": [
[
"crv": "P-256",
"kid": "Ao50Swzv_uWu805LcuaTTysu_6GwoqnvJh9rnc44U48",
"kty": "EC",
"x": issuerJwk.x?.base64URLEncode(),
"y": issuerJwk.y?.base64URLEncode()
]
]
]
]

let issuerSignedSDJWT = try SDJWTIssuer.issue(
issuersPrivateKey: issuersKeyPair.private,
header: DefaultJWSHeaderImpl(
algorithm: .ES256,
keyID: "Ao50Swzv_uWu805LcuaTTysu_6GwoqnvJh9rnc44U48"
)
) {
ConstantClaims.iat(time: Date())
ConstantClaims.exp(time: Date() + 3600)
ConstantClaims.iss(domain: "https://example.com/issuer")
FlatDisclosedClaim("sub", "6c5c0a49-b589-431d-bae7-219122a9ec2c")
FlatDisclosedClaim("given_name", "太郎")
FlatDisclosedClaim("family_name", "山田")
FlatDisclosedClaim("email", "\"unusual email address\"@example.jp")
FlatDisclosedClaim("phone_number", "+81-80-1234-5678")
ObjectClaim("address") {
FlatDisclosedClaim("street_address", "東京都港区芝公園4丁目2−8")
FlatDisclosedClaim("locality", "東京都")
FlatDisclosedClaim("region", "港区")
FlatDisclosedClaim("country", "JP")
}
FlatDisclosedClaim("birthdate", "1940-01-01")
ObjectClaim("cnf") {
ObjectClaim("jwk") {
PlainClaim("kid", "Ao50Swzv_uWu805LcuaTTysu_6GwoqnvJh9rnc44U48")
PlainClaim("kty", "EC")
PlainClaim("y", holdersJwk.y!.base64URLEncode())
PlainClaim("x", holdersJwk.x!.base64URLEncode())
PlainClaim("crv", "P-256")
}
}
}

let sdHash = DigestCreator()
.hashAndBase64Encode(
input: CompactSerialiser(
signedSDJWT: issuerSignedSDJWT
).serialised
)!

let holder = try SDJWTIssuer
.presentation(
holdersPrivateKey: holdersKeyPair.private,
signedSDJWT: issuerSignedSDJWT,
disclosuresToPresent: issuerSignedSDJWT.disclosures,
keyBindingJWT: KBJWT(
header: DefaultJWSHeaderImpl(algorithm: .ES256),
kbJwtPayload: .init([
Keys.nonce.rawValue: "123456789",
Keys.aud.rawValue: "example.com",
Keys.iat.rawValue: 1694600000,
Keys.sdHash.rawValue: sdHash
])
)
)

let serialized: String = CompactSerialiser(signedSDJWT: holder).serialised

let result = try await SDJWTVCVerifier(
fetcher: SdJwtVcIssuerMetaDataFetcher(
session: NetworkingJSONMock(
json: jsonObject
)
)
).verifyPresentation(
unverifiedSdJwt: serialized,
claimsVerifier: ClaimsVerifier(),
keyBindingVerifier: KeyBindingVerifier()
)

XCTAssertEqual(sdHash, holder.delineatedCompactSerialisation)
XCTAssertNoThrow(try result.get())
}
}
Loading