Skip to content

Commit

Permalink
JWT tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
flypaper0 committed Oct 4, 2023
1 parent 9dc84f1 commit 77121b1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
11 changes: 11 additions & 0 deletions Sources/WalletConnectJWT/JSONEncoder+JWT.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation

extension JSONEncoder {

public static var jwt: JSONEncoder {
let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = .withoutEscapingSlashes
jsonEncoder.dateEncodingStrategy = .secondsSince1970
return jsonEncoder
}
}
6 changes: 3 additions & 3 deletions Sources/WalletConnectJWT/JWT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ struct JWT<JWTClaims: JWTEncodable>: Codable, Equatable {
let signature: String
let string: String

init(claims: JWTClaims, signer: JWTSigning) throws {
init(claims: JWTClaims, signer: JWTSigning, jsonEncoder: JSONEncoder = .jwt) throws {
self.header = JWTHeader(alg: signer.alg)
self.claims = claims

let headerString = try header.encode()
let claimsString = try claims.encode()
let headerString = try header.encode(jsonEncoder: jsonEncoder)
let claimsString = try claims.encode(jsonEncoder: jsonEncoder)
let signature = try signer.sign(header: headerString, claims: claimsString)

self.signature = signature
Expand Down
7 changes: 2 additions & 5 deletions Sources/WalletConnectJWT/JWTEncodable.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import Foundation

public protocol JWTEncodable: Codable, Equatable {
func encode() throws -> String
func encode(jsonEncoder: JSONEncoder) throws -> String

static func decode(from string: String) throws -> Self
}

extension JWTEncodable {

public func encode() throws -> String {
let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = .withoutEscapingSlashes
jsonEncoder.dateEncodingStrategy = .secondsSince1970
public func encode(jsonEncoder: JSONEncoder) throws -> String {
let data = try jsonEncoder.encode(self)
return JWTEncoder.base64urlEncodedString(data: data)
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/RelayerTests/AuthTests/EdDSASignerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class EdDSASignerTests: XCTestCase {
let keyRaw = Data(hex: "58e0254c211b858ef7896b00e3f36beeb13d568d47c6031c4218b87718061295")
let signingKey = try! SigningPrivateKey(rawRepresentation: keyRaw)
sut = EdDSASigner(signingKey)
let header = try! JWTHeader(alg: "EdDSA").encode()
let claims = try! RelayAuthPayload.Claims.stub().encode()
let header = try! JWTHeader(alg: "EdDSA").encode(jsonEncoder: .jwt)
let claims = try! RelayAuthPayload.Claims.stub().encode(jsonEncoder: .jwt)
let signature = try! sut.sign(header: header, claims: claims)
XCTAssertNotNil(signature)
}
Expand Down
7 changes: 5 additions & 2 deletions Tests/RelayerTests/AuthTests/JWTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import XCTest
@testable import WalletConnectJWT

final class JWTTests: XCTestCase {
let expectedJWT = "eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJjNDc5ZmU1ZGM0NjRlNzcxZTc4YjE5M2QyMzlhNjViNThkMjc4Y2FkMWMzNGJmYjBiNTcxNmU1YmI1MTQ5MjhlIiwiYXVkIjoid3NzOi8vcmVsYXkud2FsbGV0Y29ubmVjdC5jb20iLCJpYXQiOjE2NTY5MTAwOTcsImlzcyI6ImRpZDprZXk6ejZNa29kSFp3bmVWUlNodGFMZjhKS1lreHBER3AxdkdabnBHbWRCcFg4TTJleHhIIiwiZXhwIjoxNjU2OTk2NDk3fQ.0JkxOM-FV21U7Hk-xycargj_qNRaYV2H5HYtE4GzAeVQYiKWj7YySY5AdSqtCgGzX4Gt98XWXn2kSr9rE1qvCA"
let expectedJWT = "eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ3c3M6Ly9yZWxheS53YWxsZXRjb25uZWN0LmNvbSIsImV4cCI6MTY1Njk5NjQ5NywiaWF0IjoxNjU2OTEwMDk3LCJpc3MiOiJkaWQ6a2V5Ono2TWtvZEhad25lVlJTaHRhTGY4SktZa3hwREdwMXZHWm5wR21kQnBYOE0yZXh4SCIsInN1YiI6ImM0NzlmZTVkYzQ2NGU3NzFlNzhiMTkzZDIzOWE2NWI1OGQyNzhjYWQxYzM0YmZiMGI1NzE2ZTViYjUxNDkyOGUifQ.0JkxOM-FV21U7Hk-xycargj_qNRaYV2H5HYtE4GzAeVQYiKWj7YySY5AdSqtCgGzX4Gt98XWXn2kSr9rE1qvCA"

func testJWTEncoding() throws {
let signer = EdDSASignerMock()
signer.signature = "0JkxOM-FV21U7Hk-xycargj_qNRaYV2H5HYtE4GzAeVQYiKWj7YySY5AdSqtCgGzX4Gt98XWXn2kSr9rE1qvCA"
let jwt = try JWT(claims: RelayAuthPayload.Claims.stub(), signer: signer)
let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = [.sortedKeys, .withoutEscapingSlashes]
jsonEncoder.dateEncodingStrategy = .secondsSince1970
let jwt = try JWT(claims: RelayAuthPayload.Claims.stub(), signer: signer, jsonEncoder: jsonEncoder)
XCTAssertEqual(expectedJWT, jwt.string)
}

Expand Down

0 comments on commit 77121b1

Please sign in to comment.