Skip to content

Commit

Permalink
#125: update parser so it is able to parse deeplink and also JWT in Q…
Browse files Browse the repository at this point in the history
…R code
  • Loading branch information
Hopsaheysa committed Nov 28, 2023
1 parent 0e865ad commit bb6975d
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions WultraMobileTokenSDK/Operations/Utils/WMTPACUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ public class WMTPACUtils {
}

if let operationId = queryItems.first(where: { $0.name == "oid" })?.value {
let totp = queryItems.first(where: { $0.name == "totp" })?.value
let totp = queryItems.first(where: { $0.name == "potp" })?.value
return WMTPACData(operationId: operationId, totp: totp)
} else if let code = queryItems.first?.value {
return parseJWT(code: code)
} else {
D.error("Failed to get operationId from query items")
D.error("Failed to get Query Items values for parsing")
return nil
}
}
Expand All @@ -46,14 +48,36 @@ public class WMTPACUtils {
if let encodedURLString = code.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), let url = URL(string: encodedURLString) {
return parseDeeplink(url: url)
} else {
D.error("Failed to created URL from QR code String.")
return nil
return parseJWT(code: code)
}
}

private static func parseJWT(code: String) -> WMTPACData? {
let jwtParts = code.split(separator: ".")

// At this moment we dont care about header, we want only payload which is the second part of JWT
let jwtBase64String = jwtParts.count > 1 ? String(jwtParts[1]) : ""

if let base64EncodedData = jwtBase64String.data(using: .utf8),
let dataPayload = Data(base64Encoded: base64EncodedData) {
do {
return try JSONDecoder().decode(WMTPACData.self, from: dataPayload)
} catch {
D.error("Failed to decode JWT from: \(code)")
D.error("With error: \(error)")
return nil
}
}

D.error("Failed to decode QR JWT from: \(jwtBase64String)")
return nil
}


}

/// Data which is return after parsing PAC code
public struct WMTPACData {
public struct WMTPACData: Decodable {

/// The ID of the operation associated with the PAC
public let operationId: String
Expand Down

0 comments on commit bb6975d

Please sign in to comment.