Skip to content

Commit

Permalink
change jwt date to int
Browse files Browse the repository at this point in the history
  • Loading branch information
llbartekll committed Jul 7, 2022
1 parent 0e5f018 commit f36fe6b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Sources/WalletConnectRelay/ClientAuth/JWT/JWT+Claims.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ 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()
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
6 changes: 4 additions & 2 deletions Tests/RelayerTests/AuthTests/JWTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 f36fe6b

Please sign in to comment.