From d5c5e020281275504fa22bbfd7f46825e4925ec6 Mon Sep 17 00:00:00 2001 From: Mahdi Bahrami Date: Fri, 14 Feb 2025 20:31:07 +0330 Subject: [PATCH] Update benchmarking + Support Android + add .swift-format + other refinements (#227) --- .github/workflows/benchmark.yml | 17 ++-- .github/workflows/test.yml | 11 ++- .gitignore | 1 + .swift-format | 70 +++++++++++++ Benchmarks/.gitignore | 1 + Benchmarks/.swiftformat | 1 + Benchmarks/Package.swift | 6 +- .../{Benchmarks => }/Signing/Signing.swift | 0 .../TokenLifecycle/TokenLifecycle.swift | 0 .../Verifying/Verifying.swift | 0 Snippets/JWKExamples.swift | 4 +- Snippets/JWTKitExamples.swift | 4 +- Sources/JWTKit/Claims/ExpirationClaim.swift | 4 +- Sources/JWTKit/Claims/IssuedAtClaim.swift | 4 +- .../JWTKit/Claims/JWTMultiValueClaim.swift | 4 +- Sources/JWTKit/Claims/JWTUnixEpochClaim.swift | 4 +- Sources/JWTKit/Claims/LocaleClaim.swift | 4 +- Sources/JWTKit/Claims/NotBeforeClaim.swift | 4 +- Sources/JWTKit/ECDSA/ECDSA.swift | 4 +- Sources/JWTKit/ECDSA/ECDSAKeyTypes.swift | 4 +- Sources/JWTKit/ECDSA/ECDSASigner.swift | 4 +- Sources/JWTKit/ECDSA/P256+CurveType.swift | 4 +- Sources/JWTKit/ECDSA/P384+CurveType.swift | 4 +- Sources/JWTKit/ECDSA/P521+CurveType.swift | 4 +- Sources/JWTKit/EdDSA/EdDSA.swift | 4 +- Sources/JWTKit/EdDSA/EdDSASigner.swift | 4 +- .../JWTKit/EdDSA/JWTKeyCollection+EdDSA.swift | 4 +- Sources/JWTKit/HMAC/HMAC.swift | 4 +- Sources/JWTKit/HMAC/HMACSigner.swift | 4 +- .../JWTKit/HMAC/JWTKeyCollection+HMAC.swift | 4 +- Sources/JWTKit/JWTAlgorithm.swift | 4 +- Sources/JWTKit/JWTError.swift | 4 +- Sources/JWTKit/JWTHeaderField.swift | 4 +- Sources/JWTKit/JWTKeyCollection.swift | 4 +- Sources/JWTKit/JWTParser.swift | 4 +- Sources/JWTKit/JWTSerializer.swift | 4 +- Sources/JWTKit/JWTSigner.swift | 4 +- Sources/JWTKit/None/UnsecuredNoneSigner.swift | 4 +- Sources/JWTKit/RSA/RSA.swift | 4 +- Sources/JWTKit/RSA/RSASigner.swift | 4 +- Sources/JWTKit/Utilities/Base64URL.swift | 4 +- Sources/JWTKit/Utilities/CryptoSigner.swift | 4 +- .../Utilities/CustomizedJSONCoders.swift | 4 +- Sources/JWTKit/Utilities/Utilities.swift | 4 +- .../Vendor/FirebaseAuthIdentityToken.swift | 4 +- .../JWTKit/X5C/ValidationTimePayload.swift | 4 +- Sources/JWTKit/X5C/X5CVerifier.swift | 4 +- Tests/JWTKitTests/ClaimTests.swift | 11 ++- Tests/JWTKitTests/ECDSATests.swift | 11 ++- Tests/JWTKitTests/EdDSATests.swift | 4 +- Tests/JWTKitTests/JWTKitTests.swift | 99 ++++++++++++------- Tests/JWTKitTests/PSSTests.swift | 25 +++-- Tests/JWTKitTests/RSATests.swift | 40 ++++++-- Tests/JWTKitTests/Types/LocalePayload.swift | 4 +- Tests/JWTKitTests/VendorTokenTests.swift | 52 +++++++--- Tests/JWTKitTests/X5CTests.swift | 35 ++++--- 56 files changed, 355 insertions(+), 181 deletions(-) create mode 100644 .swift-format create mode 120000 Benchmarks/.gitignore create mode 120000 Benchmarks/.swiftformat rename Benchmarks/{Benchmarks => }/Signing/Signing.swift (100%) rename Benchmarks/{Benchmarks => }/TokenLifecycle/TokenLifecycle.swift (100%) rename Benchmarks/{Benchmarks => }/Verifying/Verifying.swift (100%) diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index 3731e9bc..20f87acf 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -1,20 +1,17 @@ name: benchmark on: workflow_dispatch: + inputs: + sha: + type: string + required: true + description: "The commit SHA to run the benchmarks against." push: branches: [main] - pull_request_review: - types: [submitted] - pull_request: - branches: [main] - types: [synchronize] - paths: - - Sources/*.swift - - Benchmarks/ - - .github/workflows/benchmark.yml jobs: benchmark: - if: github.run_attempt > 1 || github.event.review.state == 'approved' || startsWith(github.event_name, 'pull_request') != true uses: vapor/ci/.github/workflows/run-benchmark.yml@main + with: + sha: ${{ inputs.sha }} secrets: inherit diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1a2bc6a7..8cd1193f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,14 +4,14 @@ concurrency: cancel-in-progress: true on: pull_request: { types: [opened, reopened, synchronize, ready_for_review] } - push: { branches: [ main ] } + push: { branches: [main] } -jobs: +jobs: linux-integration: if: ${{ !(github.event.pull_request.draft || false) }} runs-on: ubuntu-latest - container: swift:jammy - steps: + container: swift:noble + steps: - name: Check out JWTKit uses: actions/checkout@v4 with: @@ -34,5 +34,6 @@ jobs: with_linting: true with_windows: true with_musl: true + with_android: true ios_scheme_name: jwt-kit - secrets: inherit \ No newline at end of file + secrets: inherit diff --git a/.gitignore b/.gitignore index f724f106..6504da49 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ Packages .build +.index-build .DS_Store *.xcodeproj Package.pins diff --git a/.swift-format b/.swift-format new file mode 100644 index 00000000..e9016f20 --- /dev/null +++ b/.swift-format @@ -0,0 +1,70 @@ +{ + "fileScopedDeclarationPrivacy": { + "accessLevel": "private" + }, + "indentation": { + "spaces": 4 + }, + "indentConditionalCompilationBlocks": false, + "indentSwitchCaseLabels": false, + "lineBreakAroundMultilineExpressionChainComponents": false, + "lineBreakBeforeControlFlowKeywords": false, + "lineBreakBeforeEachArgument": false, + "lineBreakBeforeEachGenericRequirement": false, + "lineLength": 140, + "maximumBlankLines": 1, + "multiElementCollectionTrailingCommas": true, + "noAssignmentInExpressions": { + "allowedFunctions": [ + "XCTAssertNoThrow" + ] + }, + "prioritizeKeepingFunctionOutputTogether": false, + "respectsExistingLineBreaks": true, + "rules": { + "AllPublicDeclarationsHaveDocumentation": false, + "AlwaysUseLiteralForEmptyCollectionInit": false, + "AlwaysUseLowerCamelCase": true, + "AmbiguousTrailingClosureOverload": true, + "BeginDocumentationCommentWithOneLineSummary": false, + "DoNotUseSemicolons": true, + "DontRepeatTypeInStaticProperties": true, + "FileScopedDeclarationPrivacy": true, + "FullyIndirectEnum": true, + "GroupNumericLiterals": true, + "IdentifiersMustBeASCII": true, + "NeverForceUnwrap": false, + "NeverUseForceTry": false, + "NeverUseImplicitlyUnwrappedOptionals": false, + "NoAccessLevelOnExtensionDeclaration": true, + "NoAssignmentInExpressions": true, + "NoBlockComments": true, + "NoCasesWithOnlyFallthrough": true, + "NoEmptyTrailingClosureParentheses": true, + "NoLabelsInCasePatterns": true, + "NoLeadingUnderscores": false, + "NoParensAroundConditions": true, + "NoPlaygroundLiterals": true, + "NoVoidReturnOnFunctionSignature": true, + "OmitExplicitReturns": false, + "OneCasePerLine": true, + "OneVariableDeclarationPerLine": true, + "OnlyOneTrailingClosureArgument": true, + "OrderedImports": true, + "ReplaceForEachWithForLoop": true, + "ReturnVoidInsteadOfEmptyTuple": true, + "TypeNamesShouldBeCapitalized": true, + "UseEarlyExits": false, + "UseExplicitNilCheckInConditions": true, + "UseLetInEveryBoundCaseVariable": true, + "UseShorthandTypeNames": true, + "UseSingleLinePropertyGetter": true, + "UseSynthesizedInitializer": true, + "UseTripleSlashForDocumentationComments": true, + "UseWhereClausesInForLoops": false, + "ValidateDocumentationComments": false + }, + "spacesAroundRangeFormationOperators": false, + "tabWidth": 4, + "version": 1 +} diff --git a/Benchmarks/.gitignore b/Benchmarks/.gitignore new file mode 120000 index 00000000..5a19b83f --- /dev/null +++ b/Benchmarks/.gitignore @@ -0,0 +1 @@ +../.gitignore \ No newline at end of file diff --git a/Benchmarks/.swiftformat b/Benchmarks/.swiftformat new file mode 120000 index 00000000..a0aefc22 --- /dev/null +++ b/Benchmarks/.swiftformat @@ -0,0 +1 @@ +../.swiftformat \ No newline at end of file diff --git a/Benchmarks/Package.swift b/Benchmarks/Package.swift index e2639f01..52173d31 100644 --- a/Benchmarks/Package.swift +++ b/Benchmarks/Package.swift @@ -18,7 +18,7 @@ let package = Package( .product(name: "Benchmark", package: "package-benchmark"), .product(name: "JWTKit", package: "jwt-kit"), ], - path: "Benchmarks/Signing", + path: "Signing", plugins: [ .plugin(name: "BenchmarkPlugin", package: "package-benchmark") ] @@ -29,7 +29,7 @@ let package = Package( .product(name: "Benchmark", package: "package-benchmark"), .product(name: "JWTKit", package: "jwt-kit"), ], - path: "Benchmarks/Verifying", + path: "Verifying", plugins: [ .plugin(name: "BenchmarkPlugin", package: "package-benchmark") ] @@ -40,7 +40,7 @@ let package = Package( .product(name: "Benchmark", package: "package-benchmark"), .product(name: "JWTKit", package: "jwt-kit"), ], - path: "Benchmarks/TokenLifecycle", + path: "TokenLifecycle", plugins: [ .plugin(name: "BenchmarkPlugin", package: "package-benchmark") ] diff --git a/Benchmarks/Benchmarks/Signing/Signing.swift b/Benchmarks/Signing/Signing.swift similarity index 100% rename from Benchmarks/Benchmarks/Signing/Signing.swift rename to Benchmarks/Signing/Signing.swift diff --git a/Benchmarks/Benchmarks/TokenLifecycle/TokenLifecycle.swift b/Benchmarks/TokenLifecycle/TokenLifecycle.swift similarity index 100% rename from Benchmarks/Benchmarks/TokenLifecycle/TokenLifecycle.swift rename to Benchmarks/TokenLifecycle/TokenLifecycle.swift diff --git a/Benchmarks/Benchmarks/Verifying/Verifying.swift b/Benchmarks/Verifying/Verifying.swift similarity index 100% rename from Benchmarks/Benchmarks/Verifying/Verifying.swift rename to Benchmarks/Verifying/Verifying.swift diff --git a/Snippets/JWKExamples.swift b/Snippets/JWKExamples.swift index a9a55484..bdc393ef 100644 --- a/Snippets/JWKExamples.swift +++ b/Snippets/JWKExamples.swift @@ -1,9 +1,9 @@ import JWTKit #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif let rsaModulus = "..." diff --git a/Snippets/JWTKitExamples.swift b/Snippets/JWTKitExamples.swift index 297a5268..422edecc 100644 --- a/Snippets/JWTKitExamples.swift +++ b/Snippets/JWTKitExamples.swift @@ -2,9 +2,9 @@ import JWTKit #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif // Signs and verifies JWTs diff --git a/Sources/JWTKit/Claims/ExpirationClaim.swift b/Sources/JWTKit/Claims/ExpirationClaim.swift index 17e57542..8a3514b5 100644 --- a/Sources/JWTKit/Claims/ExpirationClaim.swift +++ b/Sources/JWTKit/Claims/ExpirationClaim.swift @@ -1,7 +1,7 @@ #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif /// The "exp" (expiration time) claim identifies the expiration time on diff --git a/Sources/JWTKit/Claims/IssuedAtClaim.swift b/Sources/JWTKit/Claims/IssuedAtClaim.swift index 0cfc3938..511ea281 100644 --- a/Sources/JWTKit/Claims/IssuedAtClaim.swift +++ b/Sources/JWTKit/Claims/IssuedAtClaim.swift @@ -1,7 +1,7 @@ #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif /// The "iat" (issued at) claim identifies the time at which the JWT was diff --git a/Sources/JWTKit/Claims/JWTMultiValueClaim.swift b/Sources/JWTKit/Claims/JWTMultiValueClaim.swift index 657dc7b1..a8c2f1d9 100644 --- a/Sources/JWTKit/Claims/JWTMultiValueClaim.swift +++ b/Sources/JWTKit/Claims/JWTMultiValueClaim.swift @@ -1,7 +1,7 @@ #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif public protocol JWTMultiValueClaim: JWTClaim where Value: Collection, Value.Element: Codable { diff --git a/Sources/JWTKit/Claims/JWTUnixEpochClaim.swift b/Sources/JWTKit/Claims/JWTUnixEpochClaim.swift index 25186847..438e43d6 100644 --- a/Sources/JWTKit/Claims/JWTUnixEpochClaim.swift +++ b/Sources/JWTKit/Claims/JWTUnixEpochClaim.swift @@ -1,7 +1,7 @@ #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif public protocol JWTUnixEpochClaim: JWTClaim where Value == Date {} diff --git a/Sources/JWTKit/Claims/LocaleClaim.swift b/Sources/JWTKit/Claims/LocaleClaim.swift index 479d37cb..7668e675 100644 --- a/Sources/JWTKit/Claims/LocaleClaim.swift +++ b/Sources/JWTKit/Claims/LocaleClaim.swift @@ -1,7 +1,7 @@ #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif public struct LocaleClaim: JWTClaim, Equatable, ExpressibleByStringLiteral { diff --git a/Sources/JWTKit/Claims/NotBeforeClaim.swift b/Sources/JWTKit/Claims/NotBeforeClaim.swift index cd5977b9..165dc4fa 100644 --- a/Sources/JWTKit/Claims/NotBeforeClaim.swift +++ b/Sources/JWTKit/Claims/NotBeforeClaim.swift @@ -1,7 +1,7 @@ #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif /// The "nbf" (not before) claim identifies the time before which the JWT diff --git a/Sources/JWTKit/ECDSA/ECDSA.swift b/Sources/JWTKit/ECDSA/ECDSA.swift index 5f7124de..885a9a3a 100644 --- a/Sources/JWTKit/ECDSA/ECDSA.swift +++ b/Sources/JWTKit/ECDSA/ECDSA.swift @@ -2,9 +2,9 @@ import Crypto import X509 #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif public enum ECDSA: Sendable {} diff --git a/Sources/JWTKit/ECDSA/ECDSAKeyTypes.swift b/Sources/JWTKit/ECDSA/ECDSAKeyTypes.swift index 3626946e..abef0991 100644 --- a/Sources/JWTKit/ECDSA/ECDSAKeyTypes.swift +++ b/Sources/JWTKit/ECDSA/ECDSAKeyTypes.swift @@ -2,9 +2,9 @@ import Crypto import X509 #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif /// A typealias representing the parameters of an ECDSA (Elliptic Curve Digital Signature Algorithm) key. diff --git a/Sources/JWTKit/ECDSA/ECDSASigner.swift b/Sources/JWTKit/ECDSA/ECDSASigner.swift index 121573d5..259d9658 100644 --- a/Sources/JWTKit/ECDSA/ECDSASigner.swift +++ b/Sources/JWTKit/ECDSA/ECDSASigner.swift @@ -1,7 +1,7 @@ #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif struct ECDSASigner: JWTAlgorithm, CryptoSigner { diff --git a/Sources/JWTKit/ECDSA/P256+CurveType.swift b/Sources/JWTKit/ECDSA/P256+CurveType.swift index 1de76999..e778d498 100644 --- a/Sources/JWTKit/ECDSA/P256+CurveType.swift +++ b/Sources/JWTKit/ECDSA/P256+CurveType.swift @@ -1,9 +1,9 @@ import Crypto #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif // TODO: Remove @unchecked Sendable when Crypto is updated to use Sendable diff --git a/Sources/JWTKit/ECDSA/P384+CurveType.swift b/Sources/JWTKit/ECDSA/P384+CurveType.swift index e0092e9d..5133589d 100644 --- a/Sources/JWTKit/ECDSA/P384+CurveType.swift +++ b/Sources/JWTKit/ECDSA/P384+CurveType.swift @@ -1,9 +1,9 @@ import Crypto #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif // TODO: Remove @unchecked Sendable when Crypto is updated to use Sendable diff --git a/Sources/JWTKit/ECDSA/P521+CurveType.swift b/Sources/JWTKit/ECDSA/P521+CurveType.swift index 1f870144..e01cc8d7 100644 --- a/Sources/JWTKit/ECDSA/P521+CurveType.swift +++ b/Sources/JWTKit/ECDSA/P521+CurveType.swift @@ -1,9 +1,9 @@ import Crypto #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif // TODO: Remove @unchecked Sendable when Crypto is updated to use Sendable diff --git a/Sources/JWTKit/EdDSA/EdDSA.swift b/Sources/JWTKit/EdDSA/EdDSA.swift index 18e0f59d..82b28a9a 100644 --- a/Sources/JWTKit/EdDSA/EdDSA.swift +++ b/Sources/JWTKit/EdDSA/EdDSA.swift @@ -1,9 +1,9 @@ import Crypto #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif /// Namespace for the EdDSA (Edwards-curve Digital Signature Algorithm) signing algorithm. diff --git a/Sources/JWTKit/EdDSA/EdDSASigner.swift b/Sources/JWTKit/EdDSA/EdDSASigner.swift index c451c464..6c984336 100644 --- a/Sources/JWTKit/EdDSA/EdDSASigner.swift +++ b/Sources/JWTKit/EdDSA/EdDSASigner.swift @@ -1,9 +1,9 @@ import Crypto #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif struct EdDSASigner: JWTAlgorithm, Sendable { diff --git a/Sources/JWTKit/EdDSA/JWTKeyCollection+EdDSA.swift b/Sources/JWTKit/EdDSA/JWTKeyCollection+EdDSA.swift index 9c34b913..e9610284 100644 --- a/Sources/JWTKit/EdDSA/JWTKeyCollection+EdDSA.swift +++ b/Sources/JWTKit/EdDSA/JWTKeyCollection+EdDSA.swift @@ -1,9 +1,9 @@ import Crypto #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif extension JWTKeyCollection { diff --git a/Sources/JWTKit/HMAC/HMAC.swift b/Sources/JWTKit/HMAC/HMAC.swift index b9494d82..a3c99f12 100644 --- a/Sources/JWTKit/HMAC/HMAC.swift +++ b/Sources/JWTKit/HMAC/HMAC.swift @@ -1,9 +1,9 @@ @preconcurrency import Crypto #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif public struct HMACKey: Sendable { diff --git a/Sources/JWTKit/HMAC/HMACSigner.swift b/Sources/JWTKit/HMAC/HMACSigner.swift index ee317d7d..3f954f40 100644 --- a/Sources/JWTKit/HMAC/HMACSigner.swift +++ b/Sources/JWTKit/HMAC/HMACSigner.swift @@ -1,9 +1,9 @@ @preconcurrency import Crypto #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif struct HMACSigner: JWTAlgorithm where SHAType: HashFunction { diff --git a/Sources/JWTKit/HMAC/JWTKeyCollection+HMAC.swift b/Sources/JWTKit/HMAC/JWTKeyCollection+HMAC.swift index 3c950461..3945fb5b 100644 --- a/Sources/JWTKit/HMAC/JWTKeyCollection+HMAC.swift +++ b/Sources/JWTKit/HMAC/JWTKeyCollection+HMAC.swift @@ -1,9 +1,9 @@ import Crypto #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif extension JWTKeyCollection { diff --git a/Sources/JWTKit/JWTAlgorithm.swift b/Sources/JWTKit/JWTAlgorithm.swift index 515ef960..851b552a 100644 --- a/Sources/JWTKit/JWTAlgorithm.swift +++ b/Sources/JWTKit/JWTAlgorithm.swift @@ -1,7 +1,7 @@ #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif /// A protocol defining the necessary functionality for a JWT (JSON Web Token) algorithm. diff --git a/Sources/JWTKit/JWTError.swift b/Sources/JWTKit/JWTError.swift index b1a75b71..31cea7f9 100644 --- a/Sources/JWTKit/JWTError.swift +++ b/Sources/JWTKit/JWTError.swift @@ -1,7 +1,7 @@ #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif /// JWT error type. diff --git a/Sources/JWTKit/JWTHeaderField.swift b/Sources/JWTKit/JWTHeaderField.swift index 17f1eb19..8f29f110 100644 --- a/Sources/JWTKit/JWTHeaderField.swift +++ b/Sources/JWTKit/JWTHeaderField.swift @@ -1,7 +1,7 @@ #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif public indirect enum JWTHeaderField: Hashable, Sendable, Codable { diff --git a/Sources/JWTKit/JWTKeyCollection.swift b/Sources/JWTKit/JWTKeyCollection.swift index b407276a..35f9b2e2 100644 --- a/Sources/JWTKit/JWTKeyCollection.swift +++ b/Sources/JWTKit/JWTKeyCollection.swift @@ -1,9 +1,9 @@ import Logging #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif /// A collection of JWT and JWK signers for handling JSON Web Tokens (JWTs). diff --git a/Sources/JWTKit/JWTParser.swift b/Sources/JWTKit/JWTParser.swift index abc4179f..d683891b 100644 --- a/Sources/JWTKit/JWTParser.swift +++ b/Sources/JWTKit/JWTParser.swift @@ -1,7 +1,7 @@ #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif public protocol JWTParser: Sendable { diff --git a/Sources/JWTKit/JWTSerializer.swift b/Sources/JWTKit/JWTSerializer.swift index f3b20614..13994f89 100644 --- a/Sources/JWTKit/JWTSerializer.swift +++ b/Sources/JWTKit/JWTSerializer.swift @@ -1,9 +1,9 @@ import X509 #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif public protocol JWTSerializer: Sendable { diff --git a/Sources/JWTKit/JWTSigner.swift b/Sources/JWTKit/JWTSigner.swift index 97ee07af..4db4c330 100644 --- a/Sources/JWTKit/JWTSigner.swift +++ b/Sources/JWTKit/JWTSigner.swift @@ -1,7 +1,7 @@ #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif /// A JWT signer. diff --git a/Sources/JWTKit/None/UnsecuredNoneSigner.swift b/Sources/JWTKit/None/UnsecuredNoneSigner.swift index 4b7734a6..0aa92557 100644 --- a/Sources/JWTKit/None/UnsecuredNoneSigner.swift +++ b/Sources/JWTKit/None/UnsecuredNoneSigner.swift @@ -1,7 +1,7 @@ #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif struct UnsecuredNoneSigner: JWTAlgorithm { diff --git a/Sources/JWTKit/RSA/RSA.swift b/Sources/JWTKit/RSA/RSA.swift index cd9bf78b..6eafeb8e 100644 --- a/Sources/JWTKit/RSA/RSA.swift +++ b/Sources/JWTKit/RSA/RSA.swift @@ -3,9 +3,9 @@ import X509 import _CryptoExtras #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif extension Insecure { diff --git a/Sources/JWTKit/RSA/RSASigner.swift b/Sources/JWTKit/RSA/RSASigner.swift index 9ffb0356..f380af3b 100644 --- a/Sources/JWTKit/RSA/RSASigner.swift +++ b/Sources/JWTKit/RSA/RSASigner.swift @@ -1,9 +1,9 @@ import _CryptoExtras #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif struct RSASigner: JWTAlgorithm, CryptoSigner { diff --git a/Sources/JWTKit/Utilities/Base64URL.swift b/Sources/JWTKit/Utilities/Base64URL.swift index bd8ace01..ab40db51 100644 --- a/Sources/JWTKit/Utilities/Base64URL.swift +++ b/Sources/JWTKit/Utilities/Base64URL.swift @@ -1,7 +1,7 @@ #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif extension String { diff --git a/Sources/JWTKit/Utilities/CryptoSigner.swift b/Sources/JWTKit/Utilities/CryptoSigner.swift index abfc670e..8e3212d5 100644 --- a/Sources/JWTKit/Utilities/CryptoSigner.swift +++ b/Sources/JWTKit/Utilities/CryptoSigner.swift @@ -1,9 +1,9 @@ import Crypto #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif public struct DigestAlgorithm: Sendable, Equatable { diff --git a/Sources/JWTKit/Utilities/CustomizedJSONCoders.swift b/Sources/JWTKit/Utilities/CustomizedJSONCoders.swift index 5946d760..81209e03 100644 --- a/Sources/JWTKit/Utilities/CustomizedJSONCoders.swift +++ b/Sources/JWTKit/Utilities/CustomizedJSONCoders.swift @@ -1,7 +1,7 @@ #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif public protocol JWTJSONDecoder: Sendable { diff --git a/Sources/JWTKit/Utilities/Utilities.swift b/Sources/JWTKit/Utilities/Utilities.swift index 2e0b4dd5..1c7aa3a6 100644 --- a/Sources/JWTKit/Utilities/Utilities.swift +++ b/Sources/JWTKit/Utilities/Utilities.swift @@ -1,7 +1,7 @@ #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif extension DataProtocol { diff --git a/Sources/JWTKit/Vendor/FirebaseAuthIdentityToken.swift b/Sources/JWTKit/Vendor/FirebaseAuthIdentityToken.swift index efea3b54..7e61f427 100644 --- a/Sources/JWTKit/Vendor/FirebaseAuthIdentityToken.swift +++ b/Sources/JWTKit/Vendor/FirebaseAuthIdentityToken.swift @@ -1,7 +1,7 @@ #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif public struct FirebaseAuthIdentityToken: JWTPayload { diff --git a/Sources/JWTKit/X5C/ValidationTimePayload.swift b/Sources/JWTKit/X5C/ValidationTimePayload.swift index 99a38314..2d6a3932 100644 --- a/Sources/JWTKit/X5C/ValidationTimePayload.swift +++ b/Sources/JWTKit/X5C/ValidationTimePayload.swift @@ -1,7 +1,7 @@ #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif /// A protocol defining the requirements for payloads that include a validation time. diff --git a/Sources/JWTKit/X5C/X5CVerifier.swift b/Sources/JWTKit/X5C/X5CVerifier.swift index 31ce6e72..ddb65fef 100644 --- a/Sources/JWTKit/X5C/X5CVerifier.swift +++ b/Sources/JWTKit/X5C/X5CVerifier.swift @@ -1,9 +1,9 @@ import X509 #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif /// An object for verifying JWS tokens that contain the `x5c` header parameter diff --git a/Tests/JWTKitTests/ClaimTests.swift b/Tests/JWTKitTests/ClaimTests.swift index e9cd5ad4..bfc643c7 100644 --- a/Tests/JWTKitTests/ClaimTests.swift +++ b/Tests/JWTKitTests/ClaimTests.swift @@ -1,10 +1,11 @@ -import JWTKit +#if canImport(Testing) import Testing +import JWTKit #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif @Suite("Claim Tests") @@ -35,7 +36,8 @@ struct ClaimTests { let nadizaDialectSlovenia = try LocalePayload.from(#"{"locale":"sl-nedis"}"#) let germanSwissPost1996 = try LocalePayload.from(#"{"locale":"de-CH-1996"}"#) let chineseTraditionalTwoPrivate = try LocalePayload.from( - #"{"locale":"zh-Hant-CN-x-private1-private2"}"#) + #"{"locale":"zh-Hant-CN-x-private1-private2"}"# + ) #expect(plainEnglish.locale.value.identifier == "en") #expect(brazillianPortugese.locale.value.identifier == "pt-BR") @@ -114,3 +116,4 @@ struct ClaimTests { _ = try await keyCollection.verify(jwt, as: ExpirationPayload.self) } } +#endif // canImport(Testing) diff --git a/Tests/JWTKitTests/ECDSATests.swift b/Tests/JWTKitTests/ECDSATests.swift index abd94598..6a51c63d 100644 --- a/Tests/JWTKitTests/ECDSATests.swift +++ b/Tests/JWTKitTests/ECDSATests.swift @@ -1,11 +1,12 @@ +#if canImport(Testing) +import Testing import Crypto import JWTKit -import Testing #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif @Suite("ECDSA Tests") @@ -240,7 +241,8 @@ struct ECDSATests { return false } return error.foo == "qux" - }) + } + ) // Second test: "bar" payload should pass verification let token = try await keys.sign(Payload(foo: "bar")) @@ -343,3 +345,4 @@ let ecdsaPublicKey = """ C18ScRb4Z6poMBgJtYlVtd9ly63URv57ZW0Ncs1LiZB7WATb3svu+1c7HQ== -----END PUBLIC KEY----- """ +#endif // canImport(Testing) diff --git a/Tests/JWTKitTests/EdDSATests.swift b/Tests/JWTKitTests/EdDSATests.swift index db4578ed..5fb535c3 100644 --- a/Tests/JWTKitTests/EdDSATests.swift +++ b/Tests/JWTKitTests/EdDSATests.swift @@ -1,5 +1,6 @@ -import JWTKit +#if canImport(Testing) import Testing +import JWTKit @Suite("EdDSA Tests") struct EdDSATests { @@ -160,3 +161,4 @@ let eddsaPublicKeyBase64 = "0ZcEvMCSYqSwR8XIkxOoaYjRQSAO8frTMSCpNbUl4lE=" let eddsaPrivateKeyBase64 = "d1H3/dcg0V3XyAuZW2TE5Z3rhY20M+4YAfYu/HUQd8w=" let eddsaPublicKeyBase64Url = "0ZcEvMCSYqSwR8XIkxOoaYjRQSAO8frTMSCpNbUl4lE" let eddsaPrivateKeyBase64Url = "d1H3_dcg0V3XyAuZW2TE5Z3rhY20M-4YAfYu_HUQd8w" +#endif // canImport(Testing) diff --git a/Tests/JWTKitTests/JWTKitTests.swift b/Tests/JWTKitTests/JWTKitTests.swift index 0c3ffc78..260af760 100644 --- a/Tests/JWTKitTests/JWTKitTests.swift +++ b/Tests/JWTKitTests/JWTKitTests.swift @@ -1,11 +1,12 @@ -import JWTKit +#if canImport(Testing) import Testing +import JWTKit import X509 #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif @Suite("JWTKit Tests") @@ -127,9 +128,12 @@ struct JWTKitTests { func sign() async throws { let data = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImZvbyJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImV4cCI6OTk5OTk5OTk5OTk5OTl9.Gf7leJ8i30LmMI7GBTpWDMXV60y1wkTOCOBudP9v9ms" - let keyCollection = await JWTKeyCollection().add( - hmac: "bar", digestAlgorithm: .sha256, kid: "foo" - ) + let keyCollection = await JWTKeyCollection() + .add( + hmac: "bar", + digestAlgorithm: .sha256, + kid: "foo" + ) let payload = try await keyCollection.verify(data, as: TestPayload.self) #expect(payload.name == "John Doe") } @@ -309,15 +313,16 @@ struct JWTKitTests { "awDmF9aqLqokmXjiydda8mKboArWwP2Ih7K3Ad3Og_u9nUp2gZrXiCMxGGSQiN5Jg3yiW_ffNYaHfyfRWKyQ_g31n4UfPLmPtw6iL3V9GChV5ZDRE9HpxE88U8r1h__xFFrrdnBeWKW8NldI70jg7vY6uiRae4uuXCfSbs4iAUxmRVKWCnV7JE6sObQKUV_EJkBcyND5Y97xsmWD0nPmXCnloQ84gF-eTErJoZBvQhJ4BhmBeUlREHmDKssaxVOCK4l335DKHD1vbuPk9e49M71BK7r2y4Atqk3TEetnwzMs3u-L9RqHaGIBw5u324uGweY7QeD7HFdAUtpjOq_MQQ" // sign jwt - let keyCollection = try await JWTKeyCollection().add( - rsa: Insecure.RSA.PrivateKey( - modulus: modulus, - exponent: exponent, - privateExponent: privateExponent - ), - digestAlgorithm: .sha256, - kid: "vapor" - ) + let keyCollection = try await JWTKeyCollection() + .add( + rsa: Insecure.RSA.PrivateKey( + modulus: modulus, + exponent: exponent, + privateExponent: privateExponent + ), + digestAlgorithm: .sha256, + kid: "vapor" + ) struct Foo: JWTPayload { var bar: Int func verify(using _: some JWTAlgorithm) throws {} @@ -374,15 +379,19 @@ struct JWTKitTests { admin: false, exp: .init(value: .init(timeIntervalSince1970: 2_000_000_000)) ) - let keyCollection = await JWTKeyCollection().addUnsecuredNone( - parser: DefaultJWTParser(jsonDecoder: decoder), - serializer: DefaultJWTSerializer(jsonEncoder: encoder) - ) + let keyCollection = await JWTKeyCollection() + .addUnsecuredNone( + parser: DefaultJWTParser(jsonDecoder: decoder), + serializer: DefaultJWTSerializer(jsonEncoder: encoder) + ) let token = try await keyCollection.sign(payload) #expect( - (token.split(separator: ".").dropFirst(1).first.map { - String(decoding: Data($0.utf8).base64URLDecodedBytes(), as: UTF8.self) - } ?? "").contains(#""exp":""#)) + (token.split(separator: ".").dropFirst(1).first + .map { + String(decoding: Data($0.utf8).base64URLDecodedBytes(), as: UTF8.self) + } ?? "") + .contains(#""exp":""#) + ) try await #expect(keyCollection.verify(token.bytes, as: TestPayload.self) == payload) try await #expect(keyCollection.verify(data.bytes, as: TestPayload.self) == payload) #expect(token.hasSuffix(".")) @@ -425,13 +434,15 @@ struct JWTKitTests { let (encodedHeader, encodedPayload, encodedSignature) = try getTokenParts(token) let header = try jsonDecoder.decode( - JWTHeader.self, from: .init(encodedHeader.base64URLDecodedBytes()) + JWTHeader.self, + from: .init(encodedHeader.base64URLDecodedBytes()) ) let payload = if header.b64?.asBool ?? true { try self.jsonDecoder.decode( - Payload.self, from: .init(encodedPayload.base64URLDecodedBytes()) + Payload.self, + from: .init(encodedPayload.base64URLDecodedBytes()) ) } else { try self.jsonDecoder.decode(Payload.self, from: .init(encodedPayload)) @@ -445,12 +456,16 @@ struct JWTKitTests { let keyCollection = await JWTKeyCollection() .add( - hmac: "secret", digestAlgorithm: .sha256, parser: CustomParser(), + hmac: "secret", + digestAlgorithm: .sha256, + parser: CustomParser(), serializer: CustomSerializer() ) let payload = TestPayload( - sub: "vapor", name: "Foo", admin: false, + sub: "vapor", + name: "Foo", + admin: false, exp: .init(value: .init(timeIntervalSince1970: 2_000_000_000)) ) @@ -526,16 +541,19 @@ struct JWTKitTests { let jsonDecoder = JSONDecoder() let decodedFields = try jsonDecoder.decode([String: JWTHeaderField].self, from: encodedHeader) let decodedJsonFields = try jsonDecoder.decode( - [String: JWTHeaderField].self, from: jsonFields.data(using: .utf8)! + [String: JWTHeaderField].self, + from: jsonFields.data(using: .utf8)! ) #expect(decodedFields == decodedJsonFields) } @Test("Test Custom Header Fields") func customHeaderFields() async throws { - let keyCollection = await JWTKeyCollection().add( - hmac: .init(key: .init(size: .bits256)), digestAlgorithm: .sha384 - ) + let keyCollection = await JWTKeyCollection() + .add( + hmac: .init(key: .init(size: .bits256)), + digestAlgorithm: .sha384 + ) let payload = TestPayload( sub: "vapor", @@ -620,7 +638,8 @@ struct JWTKitTests { parsed.header.crit == [ "b64", "http://openbanking.org.uk/iat", "http://openbanking.org.uk/iss", "http://openbanking.org.uk/tan", - ]) + ] + ) #expect(parsed.header.kid == "90210ABAD") } @@ -691,7 +710,8 @@ struct JWTKitTests { OF/2NxApJCzGCEDdfSp6VQO30hyhRANCAAQRWz+jn65BtOMvdyHKcvjBeBSDZH2r 1RTwjmYSi9R/zpBnuQ4EiMnCqfMPWiZqB4QdbAd0E7oH50VpuZ1P087G -----END PRIVATE KEY----- - """) + """ + ) let keyCollection = await JWTKeyCollection() .add(hmac: "secret", digestAlgorithm: .sha256, kid: "hmac") @@ -708,12 +728,16 @@ struct JWTKitTests { } let hmacIteratinglyVerified = try await keyCollection.verify( - hmacToken, as: TestPayload.self, iteratingKeys: true + hmacToken, + as: TestPayload.self, + iteratingKeys: true ) #expect(hmacIteratinglyVerified.sub == "1234567890") let ecdsaIteratinglyVerified = try await keyCollection.verify( - ecdsaToken, as: TestPayload.self, iteratingKeys: true + ecdsaToken, + as: TestPayload.self, + iteratingKeys: true ) #expect(ecdsaIteratinglyVerified.sub == "1234567890") } @@ -760,8 +784,10 @@ struct JWTKitTests { func jwtErrorDescription() { #expect( JWTError.claimVerificationFailure( - failedClaim: ExpirationClaim(value: .init(timeIntervalSince1970: 1)), reason: "test" - ).description + failedClaim: ExpirationClaim(value: .init(timeIntervalSince1970: 1)), + reason: "test" + ) + .description == "JWTKitError(errorType: claimVerificationFailure, failedClaim: JWTKit.ExpirationClaim(value: 1970-01-01 00:00:01 +0000), reason: \"test\")" ) #expect( @@ -927,3 +953,4 @@ let firebaseCert = """ sb7NLsl7DkvjjxTz7I98xaGbfhofgYympeKT6UO+tmc= -----END CERTIFICATE----- """ +#endif // canImport(Testing) diff --git a/Tests/JWTKitTests/PSSTests.swift b/Tests/JWTKitTests/PSSTests.swift index 17518490..037112d9 100644 --- a/Tests/JWTKitTests/PSSTests.swift +++ b/Tests/JWTKitTests/PSSTests.swift @@ -1,10 +1,11 @@ -import JWTKit +#if canImport(Testing) import Testing +import JWTKit #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif @Suite("PSS Tests") @@ -13,8 +14,11 @@ struct PSSTests { @Test("Test PSS Docs") func pssDocs() async throws { await #expect(throws: Never.self) { - try await JWTKeyCollection().add( - pss: Insecure.RSA.PublicKey(pem: publicKey), digestAlgorithm: .sha256) + try await JWTKeyCollection() + .add( + pss: Insecure.RSA.PublicKey(pem: publicKey), + digestAlgorithm: .sha256 + ) } } @@ -74,7 +78,9 @@ struct PSSTests { await #expect( performing: { let token = try await keyCollection.sign( - Payload(foo: "qux"), header: ["kid": "private"]) + Payload(foo: "qux"), + header: ["kid": "private"] + ) _ = try await keyCollection.verify(token, as: Payload.self) }, throws: { error in @@ -82,7 +88,8 @@ struct PSSTests { return false } return notBarError.foo == "qux" - }) + } + ) // Case where foo is "bar" let token = try await keyCollection.sign(Payload(foo: "bar")) @@ -108,7 +115,8 @@ struct PSSTests { func exportPublicKeyWhenKeyIsPrivate() async throws { let privateKey = try Insecure.RSA.PrivateKey(pem: privateKey) let publicKeyFromPrivate = try Insecure.RSA.PublicKey( - pem: privateKey.publicKey.pemRepresentation) + pem: privateKey.publicKey.pemRepresentation + ) let publicKey = try Insecure.RSA.PublicKey(pem: publicKey) #expect(publicKeyFromPrivate == publicKey) } @@ -224,3 +232,4 @@ struct PSSTests { L4z0tz7QWE0aGuOA32YqCSnrSYKdBTPFDILCdfHonzfP7WMPibz4jWxu_FzNk9s4Dh-uN2lV3NGW10pAsnqffD89LtYanRjaIdHnLW_PFo5fEL2yltK7qMB9hO1JegppKCfoc79W4-dr-4qy1Op0B3npOP-DaUYlNamfDmIbQW32UKeJzdGIn-_ryrBT7hQW6_uHLS2VFPPk0rNkPPKZYoNaqGnJ0eaFFF-dFwiThXIpPz--dxTAL8xYf275rjG8C9lh6awOfJSIdXMVuQITWf62E0mSQPR2-219bShMKriDYcYLbT3BJEgOkRBBHGuHo9R5TN298anxZqV1u5jtUQ """ } +#endif // canImport(Testing) diff --git a/Tests/JWTKitTests/RSATests.swift b/Tests/JWTKitTests/RSATests.swift index 11dd4125..f1a95745 100644 --- a/Tests/JWTKitTests/RSATests.swift +++ b/Tests/JWTKitTests/RSATests.swift @@ -1,5 +1,6 @@ -import JWTKit +#if canImport(Testing) import Testing +import JWTKit import _CryptoExtras @Suite("RSA Tests") @@ -29,8 +30,11 @@ struct RSATests { func privateKeyInitFromPrimes() async throws { #expect(throws: Never.self) { try Insecure.RSA.PrivateKey( - modulus: modulus, exponent: publicExponent, - privateExponent: privateExponent, prime1: prime1, prime2: prime2 + modulus: modulus, + exponent: publicExponent, + privateExponent: privateExponent, + prime1: prime1, + prime2: prime2 ) } } @@ -88,11 +92,17 @@ struct RSATests { @Test("Test signing with raw built private key") func signWithRawPrivateKey() async throws { let privateKey = try Insecure.RSA.PrivateKey( - modulus: modulus, exponent: publicExponent, privateExponent: privateExponent + modulus: modulus, + exponent: publicExponent, + privateExponent: privateExponent ) let keyCollection = try await JWTKeyCollection() - .add(rsa: Insecure.RSA.PrivateKey(pem: privateKey.pemRepresentation), digestAlgorithm: .sha256, kid: "private") + .add( + rsa: Insecure.RSA.PrivateKey(pem: privateKey.pemRepresentation), + digestAlgorithm: .sha256, + kid: "private" + ) let payload = TestPayload( sub: "vapor", @@ -109,12 +119,19 @@ struct RSATests { @Test("Test signing with raw built private key with primes") func signWithRawPrivateKeyWithPrimes() async throws { let privateKey = try Insecure.RSA.PrivateKey( - modulus: modulus, exponent: publicExponent, privateExponent: privateExponent, - prime1: prime1, prime2: prime2 + modulus: modulus, + exponent: publicExponent, + privateExponent: privateExponent, + prime1: prime1, + prime2: prime2 ) let keyCollection = try await JWTKeyCollection() - .add(rsa: Insecure.RSA.PrivateKey(pem: privateKey.pemRepresentation), digestAlgorithm: .sha256, kid: "private") + .add( + rsa: Insecure.RSA.PrivateKey(pem: privateKey.pemRepresentation), + digestAlgorithm: .sha256, + kid: "private" + ) let payload = TestPayload( sub: "vapor", @@ -205,7 +222,11 @@ struct RSATests { @Test("Test exporting raw built private key as PEM") func exportKeyAsPEMWhenRawBuilt() async throws { - let key = try Insecure.RSA.PrivateKey(modulus: modulus, exponent: publicExponent, privateExponent: privateExponent) + let key = try Insecure.RSA.PrivateKey( + modulus: modulus, + exponent: publicExponent, + privateExponent: privateExponent + ) let key2 = try Insecure.RSA.PrivateKey(pem: key.pemRepresentation) #expect(key == key2) } @@ -377,3 +398,4 @@ let publicKey2 = """ mwIDAQAB -----END PUBLIC KEY----- """ +#endif // canImport(Testing) diff --git a/Tests/JWTKitTests/Types/LocalePayload.swift b/Tests/JWTKitTests/Types/LocalePayload.swift index 6701a96d..0bec254b 100644 --- a/Tests/JWTKitTests/Types/LocalePayload.swift +++ b/Tests/JWTKitTests/Types/LocalePayload.swift @@ -1,9 +1,9 @@ import JWTKit #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif struct LocalePayload: Codable { diff --git a/Tests/JWTKitTests/VendorTokenTests.swift b/Tests/JWTKitTests/VendorTokenTests.swift index 2d7c5d20..aa0dae04 100644 --- a/Tests/JWTKitTests/VendorTokenTests.swift +++ b/Tests/JWTKitTests/VendorTokenTests.swift @@ -1,10 +1,11 @@ -import JWTKit +#if canImport(Testing) import Testing +import JWTKit #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif @Suite("VendorTokenTests") @@ -66,7 +67,8 @@ struct VendorTokenTests { await #expect( throws: JWTError.claimVerificationFailure( - failedClaim: token.issuer, reason: "Token not provided by Google" + failedClaim: token.issuer, + reason: "Token not provided by Google" ) ) { try await collection.verify(jwt, as: GoogleIdentityToken.self) @@ -155,7 +157,8 @@ struct VendorTokenTests { await #expect( throws: JWTError.claimVerificationFailure( - failedClaim: token.issuer, reason: "Token not provided by Apple" + failedClaim: token.issuer, + reason: "Token not provided by Apple" ) ) { try await collection.verify(jwt, as: AppleIdentityToken.self) @@ -222,7 +225,10 @@ struct VendorTokenTests { let jwt = try await collection.sign(token) await #expect( - throws: JWTError.claimVerificationFailure(failedClaim: token.issuer, reason: "Token not provided by Microsoft") + throws: JWTError.claimVerificationFailure( + failedClaim: token.issuer, + reason: "Token not provided by Microsoft" + ) ) { try await collection.verify(jwt, as: MicrosoftIdentityToken.self) } @@ -256,7 +262,8 @@ struct VendorTokenTests { await #expect( throws: JWTError.claimVerificationFailure( - failedClaim: nil, reason: "Token must contain tenant Id" + failedClaim: nil, + reason: "Token must contain tenant Id" ) ) { try await collection.verify(jwt, as: MicrosoftIdentityToken.self) @@ -269,7 +276,8 @@ struct VendorTokenTests { issuer: "https://securetoken.google.com/firprojectname-12345", subject: "1234567890", audience: .init(value: ["firprojectname-12345"]), - issuedAt: .init(value: .now), expires: .init(value: .now + 3600), + issuedAt: .init(value: .now), + expires: .init(value: .now + 3600), authTime: .now, userID: "1234567890", email: "user@example.com", @@ -277,7 +285,11 @@ struct VendorTokenTests { phoneNumber: nil, name: "John Doe", picture: "https://example.com/johndoe.png", - firebase: .init(identities: ["google.com": ["9876543210"], "email": ["user@example.com"]], signInProvider: "google.com")) + firebase: .init( + identities: ["google.com": ["9876543210"], "email": ["user@example.com"]], + signInProvider: "google.com" + ) + ) let collection = await JWTKeyCollection().add(hmac: "secret", digestAlgorithm: .sha256) let jwt = try await collection.sign(token) @@ -293,7 +305,8 @@ struct VendorTokenTests { issuer: "https://example.com", subject: "1234567890", audience: .init(value: ["firprojectname-12345"]), - issuedAt: .init(value: .now), expires: .init(value: .now + 3600), + issuedAt: .init(value: .now), + expires: .init(value: .now + 3600), authTime: .now, userID: "1234567890", email: "user@example.com", @@ -301,14 +314,19 @@ struct VendorTokenTests { phoneNumber: nil, name: "John Doe", picture: "https://example.com/johndoe.png", - firebase: .init(identities: ["google.com": ["9876543210"], "email": ["user@example.com"]], signInProvider: "google.com")) + firebase: .init( + identities: ["google.com": ["9876543210"], "email": ["user@example.com"]], + signInProvider: "google.com" + ) + ) let collection = await JWTKeyCollection().add(hmac: "secret", digestAlgorithm: .sha256) let jwt = try await collection.sign(token) await #expect( throws: JWTError.claimVerificationFailure( - failedClaim: token.issuer, reason: "Token not provided by Google" + failedClaim: token.issuer, + reason: "Token not provided by Google" ) ) { try await collection.verify(jwt, as: FirebaseAuthIdentityToken.self) @@ -321,7 +339,8 @@ struct VendorTokenTests { issuer: "https://securetoken.google.com/firprojectname-12345", subject: .init(stringLiteral: String(repeating: "A", count: 1000)), audience: .init(value: ["firprojectname-12345"]), - issuedAt: .init(value: .now), expires: .init(value: .now + 3600), + issuedAt: .init(value: .now), + expires: .init(value: .now + 3600), authTime: .now, userID: "1234567890", email: "user@example.com", @@ -329,7 +348,11 @@ struct VendorTokenTests { phoneNumber: nil, name: "John Doe", picture: "https://example.com/johndoe.png", - firebase: .init(identities: ["google.com": ["9876543210"], "email": ["user@example.com"]], signInProvider: "google.com")) + firebase: .init( + identities: ["google.com": ["9876543210"], "email": ["user@example.com"]], + signInProvider: "google.com" + ) + ) let collection = await JWTKeyCollection().add(hmac: "secret", digestAlgorithm: .sha256) let jwt = try await collection.sign(token) @@ -344,3 +367,4 @@ struct VendorTokenTests { } } } +#endif // canImport(Testing) diff --git a/Tests/JWTKitTests/X5CTests.swift b/Tests/JWTKitTests/X5CTests.swift index f18fe580..35c2becf 100644 --- a/Tests/JWTKitTests/X5CTests.swift +++ b/Tests/JWTKitTests/X5CTests.swift @@ -1,11 +1,12 @@ -import JWTKit +#if canImport(Testing) import Testing +import JWTKit import X509 #if !canImport(Darwin) - import FoundationEssentials +import FoundationEssentials #else - import Foundation +import Foundation #endif /// Test the x5c verification abilities of JWTSigners. @@ -277,8 +278,10 @@ struct X5CTests { @Test("Test signing with x5c chain") func signWithX5CChain() async throws { - let keyCollection = try await JWTKeyCollection().add( - ecdsa: ES256PrivateKey(pem: x5cLeafCertKey)) + let keyCollection = try await JWTKeyCollection() + .add( + ecdsa: ES256PrivateKey(pem: x5cLeafCertKey) + ) let payload = TestPayload( sub: "vapor", @@ -301,8 +304,10 @@ struct X5CTests { @Test("Test signing with invalid x5c chain") func signWithInvalidX5CChain() async throws { - let keyCollection = try await JWTKeyCollection().add( - ecdsa: ES256PrivateKey(pem: x5cLeafCertKey)) + let keyCollection = try await JWTKeyCollection() + .add( + ecdsa: ES256PrivateKey(pem: x5cLeafCertKey) + ) let payload = TestPayload( sub: "vapor", @@ -338,7 +343,8 @@ struct X5CTests { pemLines.append("-----BEGIN CERTIFICATE-----") while encoded.count > 0 { - let prefixIndex = encoded.index(encoded.startIndex, offsetBy: 64, limitedBy: encoded.endIndex) ?? encoded.endIndex + let prefixIndex = + encoded.index(encoded.startIndex, offsetBy: 64, limitedBy: encoded.endIndex) ?? encoded.endIndex pemLines.append(encoded[..