Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: parse token util function #27

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion Sources/mpc-core-kit-swift/Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import tkey
import BigInt
import curveSecp256k1
import FetchNodeDetails

import SingleFactorAuth


Expand Down Expand Up @@ -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
}
Comment on lines +102 to +109
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should throw if the jwt is not decodable, not return nil.

func decodeJWTPart(_ value: String) throws -> [String: Any] {
...
}

Could you not just use the jwtDecode package instead?

i.e

 return try decode(jwt: idToken)

16 changes: 16 additions & 0 deletions Tests/mpc-kit-swiftTests/mpc_kit_swiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import XCTest
import JWTKit
import curveSecp256k1
import SingleFactorAuth
import Foundation

// JWT payload structure.
struct TestPayload: JWTPayload, Equatable {
Expand Down Expand Up @@ -83,6 +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)

return jwt
} catch {
throw error
Expand Down Expand Up @@ -187,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.")
}
}
}
Loading