From 07d27705b62329ce37a2a564123efa06f85875c7 Mon Sep 17 00:00:00 2001 From: guru-web3 <105355858+guru-web3@users.noreply.github.com> Date: Tue, 14 May 2024 11:54:25 +0800 Subject: [PATCH 1/2] feat: parse token util function --- Sources/mpc-core-kit-swift/Helper.swift | 30 ++++++++++++++++++- .../mpc_kit_swiftTests.swift | 8 +++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Sources/mpc-core-kit-swift/Helper.swift b/Sources/mpc-core-kit-swift/Helper.swift index 2eb5ff4..8097b79 100644 --- a/Sources/mpc-core-kit-swift/Helper.swift +++ b/Sources/mpc-core-kit-swift/Helper.swift @@ -11,7 +11,6 @@ import tkey import BigInt import curveSecp256k1 import FetchNodeDetails - import SingleFactorAuth @@ -79,3 +78,32 @@ public extension Web3AuthNetwork { return convertWeb3AuthNetworkToTorusNetWork(network: self) } } + +public func parseToken(jwtToken jwt: String) -> [String: Any] { + let segments = jwt.components(separatedBy: ".") + return decodeJWTPart(segments[1]) ?? [:] +} + +func base64UrlDecode(_ value: String) -> Data? { + var base64 = value + .replacingOccurrences(of: "-", with: "+") + .replacingOccurrences(of: "_", with: "/") + + let length = Double(base64.lengthOfBytes(using: String.Encoding.utf8)) + let requiredLength = 4 * ceil(length / 4.0) + let paddingLength = requiredLength - length + if paddingLength > 0 { + let padding = "".padding(toLength: Int(paddingLength), withPad: "=", startingAt: 0) + base64 = base64 + padding + } + return Data(base64Encoded: base64, options: .ignoreUnknownCharacters) +} + +func decodeJWTPart(_ value: String) -> [String: Any]? { + guard let bodyData = base64UrlDecode(value), + let json = try? JSONSerialization.jsonObject(with: bodyData, options: []), let payload = json as? [String: Any] else { + return nil + } + + return payload +} diff --git a/Tests/mpc-kit-swiftTests/mpc_kit_swiftTests.swift b/Tests/mpc-kit-swiftTests/mpc_kit_swiftTests.swift index aa8a301..81d0bbe 100644 --- a/Tests/mpc-kit-swiftTests/mpc_kit_swiftTests.swift +++ b/Tests/mpc-kit-swiftTests/mpc_kit_swiftTests.swift @@ -3,6 +3,7 @@ import XCTest import JWTKit import curveSecp256k1 import SingleFactorAuth +import Foundation // JWT payload structure. struct TestPayload: JWTPayload, Equatable { @@ -83,6 +84,13 @@ func mockLogin2 (email:String) throws -> String { let payload = TestPayload(subject: SubjectClaim(stringLiteral: subject), expiration: ExpirationClaim(value: modifiedDate), audience: "torus-key-test", isAdmin: false, emailVerified: true, issuer: "torus-key-test", iat: IssuedAtClaim(value: Date()), email: email) let jwt = try signers.sign(payload) + let parsedToken = parseToken(jwtToken: jwt); + if let parsedEmail = parsedToken["email"] as? String { + XCTAssertEqual(parsedEmail, email); + print(parsedEmail); + } else { + XCTFail("Verifier ID not matching.") + } return jwt } catch { throw error From 0a2c55997fb25cd6d5793005d67f58c7e9749852 Mon Sep 17 00:00:00 2001 From: guru-web3 <105355858+guru-web3@users.noreply.github.com> Date: Thu, 30 May 2024 23:26:33 +0530 Subject: [PATCH 2/2] fix: review comments --- .../mpc_kit_swiftTests.swift | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/Tests/mpc-kit-swiftTests/mpc_kit_swiftTests.swift b/Tests/mpc-kit-swiftTests/mpc_kit_swiftTests.swift index 81d0bbe..de9b7af 100644 --- a/Tests/mpc-kit-swiftTests/mpc_kit_swiftTests.swift +++ b/Tests/mpc-kit-swiftTests/mpc_kit_swiftTests.swift @@ -84,13 +84,7 @@ func mockLogin2 (email:String) throws -> String { let payload = TestPayload(subject: SubjectClaim(stringLiteral: subject), expiration: ExpirationClaim(value: modifiedDate), audience: "torus-key-test", isAdmin: false, emailVerified: true, issuer: "torus-key-test", iat: IssuedAtClaim(value: Date()), email: email) let jwt = try signers.sign(payload) - let parsedToken = parseToken(jwtToken: jwt); - if let parsedEmail = parsedToken["email"] as? String { - XCTAssertEqual(parsedEmail, email); - print(parsedEmail); - } else { - XCTFail("Verifier ID not matching.") - } + return jwt } catch { throw error @@ -195,4 +189,18 @@ final class mpc_kit_swiftTests: XCTestCase { let hash2 = try Data(hex: "010203040506").sha3(varient: Variants.KECCAK256) let signatures2 = try await coreKitInstance2.tssSign(message: hash2) } + + func testparseToken() async throws { + let email = "testiosEmail004" + let data = try mockLogin2(email: email) + let token = data + let parsedToken = parseToken(jwtToken: token); + print("Parsed Token: \(parsedToken)"); + if let parsedEmail = parsedToken["email"] as? String { + XCTAssertEqual(parsedEmail, email); + print(parsedEmail); + } else { + XCTFail("Verifier ID not matching.") + } + } }