|  | 
|  | 1 | +// | 
|  | 2 | +//  JWTCryptoTests.swift | 
|  | 3 | +//  Supabase | 
|  | 4 | +// | 
|  | 5 | +//  Created by Coverage Tests | 
|  | 6 | +// | 
|  | 7 | + | 
|  | 8 | +import XCTest | 
|  | 9 | +@testable import Auth | 
|  | 10 | +@testable import Helpers | 
|  | 11 | + | 
|  | 12 | +#if canImport(Security) | 
|  | 13 | +final class JWTCryptoTests: XCTestCase { | 
|  | 14 | + | 
|  | 15 | +  // MARK: - JWK+RSA Tests | 
|  | 16 | + | 
|  | 17 | +  func testRSAPublishKeyGeneration() { | 
|  | 18 | +    // Test data from a real RS256 JWT (modulus and exponent) | 
|  | 19 | +    // This is a sample RSA256 public key | 
|  | 20 | +    let jwk = JWK( | 
|  | 21 | +      kty: "RSA", | 
|  | 22 | +      keyOps: ["verify"], | 
|  | 23 | +      alg: "RS256", | 
|  | 24 | +      kid: "test-key-1", | 
|  | 25 | +      n: "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw", | 
|  | 26 | +      e: "AQAB", | 
|  | 27 | +      crv: nil, | 
|  | 28 | +      x: nil, | 
|  | 29 | +      y: nil, | 
|  | 30 | +      k: nil | 
|  | 31 | +    ) | 
|  | 32 | + | 
|  | 33 | +    // Test valid RSA key generation | 
|  | 34 | +    let rsaKey = jwk.rsaPublishKey | 
|  | 35 | +    XCTAssertNotNil(rsaKey, "RSA public key should be generated successfully") | 
|  | 36 | +  } | 
|  | 37 | + | 
|  | 38 | +  func testRSAPublishKeyInvalidAlgorithm() { | 
|  | 39 | +    // Test with invalid algorithm | 
|  | 40 | +    let jwk = JWK( | 
|  | 41 | +      kty: "RSA", | 
|  | 42 | +      keyOps: nil, | 
|  | 43 | +      alg: "ES256", // Wrong algorithm - should be RS256 | 
|  | 44 | +      kid: "test-key-2", | 
|  | 45 | +      n: "test-modulus", | 
|  | 46 | +      e: "AQAB", | 
|  | 47 | +      crv: nil, | 
|  | 48 | +      x: nil, | 
|  | 49 | +      y: nil, | 
|  | 50 | +      k: nil | 
|  | 51 | +    ) | 
|  | 52 | + | 
|  | 53 | +    let rsaKey = jwk.rsaPublishKey | 
|  | 54 | +    XCTAssertNil(rsaKey, "RSA public key should be nil with wrong algorithm") | 
|  | 55 | +  } | 
|  | 56 | + | 
|  | 57 | +  func testRSAPublishKeyInvalidKeyType() { | 
|  | 58 | +    // Test with invalid key type | 
|  | 59 | +    let jwk = JWK( | 
|  | 60 | +      kty: "EC", // Wrong type - should be RSA | 
|  | 61 | +      keyOps: nil, | 
|  | 62 | +      alg: "RS256", | 
|  | 63 | +      kid: "test-key-3", | 
|  | 64 | +      n: "test-modulus", | 
|  | 65 | +      e: "AQAB", | 
|  | 66 | +      crv: nil, | 
|  | 67 | +      x: nil, | 
|  | 68 | +      y: nil, | 
|  | 69 | +      k: nil | 
|  | 70 | +    ) | 
|  | 71 | + | 
|  | 72 | +    let rsaKey = jwk.rsaPublishKey | 
|  | 73 | +    XCTAssertNil(rsaKey, "RSA public key should be nil with wrong key type") | 
|  | 74 | +  } | 
|  | 75 | + | 
|  | 76 | +  func testRSAPublishKeyMissingModulus() { | 
|  | 77 | +    // Test with missing modulus | 
|  | 78 | +    let jwk = JWK( | 
|  | 79 | +      kty: "RSA", | 
|  | 80 | +      keyOps: nil, | 
|  | 81 | +      alg: "RS256", | 
|  | 82 | +      kid: "test-key-4", | 
|  | 83 | +      n: nil, // Missing modulus | 
|  | 84 | +      e: "AQAB", | 
|  | 85 | +      crv: nil, | 
|  | 86 | +      x: nil, | 
|  | 87 | +      y: nil, | 
|  | 88 | +      k: nil | 
|  | 89 | +    ) | 
|  | 90 | + | 
|  | 91 | +    let rsaKey = jwk.rsaPublishKey | 
|  | 92 | +    XCTAssertNil(rsaKey, "RSA public key should be nil with missing modulus") | 
|  | 93 | +  } | 
|  | 94 | + | 
|  | 95 | +  func testRSAPublishKeyMissingExponent() { | 
|  | 96 | +    // Test with missing exponent | 
|  | 97 | +    let jwk = JWK( | 
|  | 98 | +      kty: "RSA", | 
|  | 99 | +      keyOps: nil, | 
|  | 100 | +      alg: "RS256", | 
|  | 101 | +      kid: "test-key-5", | 
|  | 102 | +      n: "test-modulus", | 
|  | 103 | +      e: nil, // Missing exponent | 
|  | 104 | +      crv: nil, | 
|  | 105 | +      x: nil, | 
|  | 106 | +      y: nil, | 
|  | 107 | +      k: nil | 
|  | 108 | +    ) | 
|  | 109 | + | 
|  | 110 | +    let rsaKey = jwk.rsaPublishKey | 
|  | 111 | +    XCTAssertNil(rsaKey, "RSA public key should be nil with missing exponent") | 
|  | 112 | +  } | 
|  | 113 | + | 
|  | 114 | +  func testRSAPublishKeyInvalidBase64() { | 
|  | 115 | +    // Test with invalid Base64URL data | 
|  | 116 | +    let jwk = JWK( | 
|  | 117 | +      kty: "RSA", | 
|  | 118 | +      keyOps: nil, | 
|  | 119 | +      alg: "RS256", | 
|  | 120 | +      kid: "test-key-6", | 
|  | 121 | +      n: "!!!invalid-base64!!!", | 
|  | 122 | +      e: "AQAB", | 
|  | 123 | +      crv: nil, | 
|  | 124 | +      x: nil, | 
|  | 125 | +      y: nil, | 
|  | 126 | +      k: nil | 
|  | 127 | +    ) | 
|  | 128 | + | 
|  | 129 | +    let rsaKey = jwk.rsaPublishKey | 
|  | 130 | +    XCTAssertNil(rsaKey, "RSA public key should be nil with invalid base64 modulus") | 
|  | 131 | +  } | 
|  | 132 | + | 
|  | 133 | +  // MARK: - JWTAlgorithm Tests | 
|  | 134 | + | 
|  | 135 | +  func testRS256VerificationWithValidSignature() { | 
|  | 136 | +    // Create a sample JWT token (this would normally come from a real auth server) | 
|  | 137 | +    // For testing, we'll use a known-good JWT | 
|  | 138 | +    let header = #"{"alg":"RS256","typ":"JWT"}"# | 
|  | 139 | +    let payload = #"{"sub":"1234567890","name":"Test User","iat":1516239022}"# | 
|  | 140 | + | 
|  | 141 | +    guard | 
|  | 142 | +      let headerData = header.data(using: .utf8), | 
|  | 143 | +      let payloadData = payload.data(using: .utf8) | 
|  | 144 | +    else { | 
|  | 145 | +      XCTFail("Failed to create test data") | 
|  | 146 | +      return | 
|  | 147 | +    } | 
|  | 148 | + | 
|  | 149 | +    let headerB64 = Base64URL.encode(headerData) | 
|  | 150 | +    let payloadB64 = Base64URL.encode(payloadData) | 
|  | 151 | + | 
|  | 152 | +    // Create a mock signature (in real scenario, this would be a proper RSA signature) | 
|  | 153 | +    let mockSignature = Data([0x00, 0x01, 0x02, 0x03]) | 
|  | 154 | +    let signatureB64 = Base64URL.encode(mockSignature) | 
|  | 155 | + | 
|  | 156 | +    let jwtString = "\(headerB64).\(payloadB64).\(signatureB64)" | 
|  | 157 | + | 
|  | 158 | +    // Decode the JWT | 
|  | 159 | +    guard let decoded = JWT.decode(jwtString) else { | 
|  | 160 | +      XCTFail("Failed to decode JWT") | 
|  | 161 | +      return | 
|  | 162 | +    } | 
|  | 163 | + | 
|  | 164 | +    XCTAssertEqual(decoded.raw.header, headerB64) | 
|  | 165 | +    XCTAssertEqual(decoded.raw.payload, payloadB64) | 
|  | 166 | +    XCTAssertEqual(decoded.signature, mockSignature) | 
|  | 167 | +  } | 
|  | 168 | + | 
|  | 169 | +  func testRS256AlgorithmType() { | 
|  | 170 | +    let algorithm = JWTAlgorithm.rs256 | 
|  | 171 | +    XCTAssertEqual(algorithm.rawValue, "RS256") | 
|  | 172 | +  } | 
|  | 173 | + | 
|  | 174 | +} | 
|  | 175 | +#endif | 
0 commit comments