Skip to content

Commit

Permalink
Merge pull request #313 from WalletConnect/develop
Browse files Browse the repository at this point in the history
v0.8.1-beta.102
  • Loading branch information
flypaper0 authored Jul 7, 2022
2 parents 4507a82 + f36fe6b commit 6e89bf7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
5 changes: 3 additions & 2 deletions Sources/WalletConnectRelay/ClientAuth/JWT/JWT+Claims.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ extension JWT {
let iss: String
let sub: String
let aud: String
let iat: Date
let exp: Date
let iat: Int
let exp: Int

func encode() throws -> String {
let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = .withoutEscapingSlashes
jsonEncoder.dateEncodingStrategy = .secondsSince1970
let data = try jsonEncoder.encode(self)
return JWTEncoder.base64urlEncodedString(data: data)
Expand Down
1 change: 1 addition & 0 deletions Sources/WalletConnectRelay/ClientAuth/JWT/JWT+Header.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extension JWT {
func encode() throws -> String {
let jsonEncoder = JSONEncoder()
jsonEncoder.dateEncodingStrategy = .secondsSince1970
jsonEncoder.outputFormatting = .withoutEscapingSlashes
let data = try jsonEncoder.encode(self)
return JWTEncoder.base64urlEncodedString(data: data)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ struct SocketAuthenticator: SocketAuthenticating {

private func createAndSignJWT(subject: String, keyPair: SigningPrivateKey) throws -> String {
let issuer = didKeyFactory.make(pubKey: keyPair.publicKey.rawRepresentation, prefix: true)
let claims = JWT.Claims(iss: issuer, sub: subject, aud: getAudience(), iat: Date(), exp: getExpiry())
let now = Int(Date().timeIntervalSince1970)
let claims = JWT.Claims(iss: issuer, sub: subject, aud: getAudience(), iat: now, exp: getExpiry())
var jwt = JWT(claims: claims)
try jwt.sign(using: EdDSASigner(keyPair))
return try jwt.encoded()
Expand All @@ -34,11 +35,13 @@ struct SocketAuthenticator: SocketAuthenticating {
return Data.randomBytes(count: 32).toHexString()
}

private func getExpiry() -> Date {
private func getExpiry() -> Int {

var components = DateComponents()
components.setValue(1, for: .day)
// safe to unwrap as the date must be calculated
return Calendar.current.date(byAdding: components, to: Date())!
let date = Calendar.current.date(byAdding: components, to: Date())!
return Int(date.timeIntervalSince1970)
}

private func getAudience() -> String {
Expand Down
8 changes: 5 additions & 3 deletions Tests/RelayerTests/AuthTests/JWTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import XCTest
@testable import WalletConnectRelay

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

func testJWTEncoding() {
let claims = JWT.Claims.stub()
Expand All @@ -20,11 +20,13 @@ extension JWT.Claims {
static func stub() -> JWT.Claims {
let iss = "did:key:z6MkodHZwneVRShtaLf8JKYkxpDGp1vGZnpGmdBpX8M2exxH"
let sub = "c479fe5dc464e771e78b193d239a65b58d278cad1c34bfb0b5716e5bb514928e"
let iat = Date(timeIntervalSince1970: 1656910097)
let iatDate = Date(timeIntervalSince1970: 1656910097)
let iat = Int(iatDate.timeIntervalSince1970)
var components = DateComponents()
components.setValue(1, for: .day)
let aud = "wss://relay.walletconnect.com"
let exp = Calendar.current.date(byAdding: components, to: iat)!
let expDate = Calendar.current.date(byAdding: components, to: iatDate)!
let exp = Int(expDate.timeIntervalSince1970)
return JWT.Claims(iss: iss, sub: sub, aud: aud, iat: iat, exp: exp)
}
}

0 comments on commit 6e89bf7

Please sign in to comment.