Skip to content

PR-4962: Error cases changes #11

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

Merged
merged 4 commits into from
Feb 27, 2024
Merged
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
2 changes: 1 addition & 1 deletion PayrailsCSE.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'PayrailsCSE'
s.version = '0.2.0'
s.version = '1.0.0'
s.summary = 'Payrails client-side encryption SDK'

# This description is used to generate tags and improve search results.
Expand Down
36 changes: 30 additions & 6 deletions Sources/PayrailsCSE/PayrailsCSE.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ public struct TokenizeResponse: Codable {
public let errors: [PayrailsError]?
}



public struct PayrailsCSE {
var cseConfig: CSEConfiguration?

public init(data: String, version: String) {
let config = parseConfig(data: data)
public init(data: String, version: String) throws {
let config = try parseConfig(data: data)
cseConfig = config
}

Expand Down Expand Up @@ -117,13 +119,27 @@ public struct PayrailsCSE {
task.resume()
}

private func parseConfig(data: String) -> CSEConfiguration {
private func parseConfig(data: String) throws -> CSEConfiguration {
guard let decodedData = Data(base64Encoded: data) else {
fatalError("Failed to decode Base64 data")
throw NSError(
domain: "payrails:client.cse",
code: 0,
userInfo: [
"code": "payrails:client.cse:configuration.malformed",
"detail": "The provided configuration string is invalid. Please ensure it matches the required format and structure as defined in the documentation",
"docUrl": "https://docs.payrails.com/docs/sdk#initializing-the-sdk"
])
}

guard let config = try? JSONDecoder().decode(CSEConfiguration.self, from: decodedData) else {
fatalError("Failed to parse CSEConfiguration")
throw NSError(
domain: "payrails:client.cse",
code: 0,
userInfo: [
"code": "payrails:client.cse:configuration.malformed",
"detail": "The provided configuration string is invalid. Please ensure it matches the required format and structure as defined in the documentation",
"docUrl": "https://docs.payrails.com/docs/sdk#initializing-the-sdk"
])
}

return config
Expand All @@ -146,7 +162,15 @@ public struct PayrailsCSE {
]

guard let publicKeyRef = SecKeyCreateWithData(publicKeyData as CFData, options as CFDictionary, &error) else {
fatalError("Failed to create public key: \(error!)")
debugPrint("Failed to create public key: \(error!)")
throw NSError(
domain: "payrails:client.cse",
code: 0,
userInfo: [
"code": "payrails:client.cse:configuration.invalid",
"detail": "The provided configuration is missing the public key required for encryption",
"docUrl": "https://docs.payrails.com/docs/tokenize-cards-with-client-side-encryption",
])
}

return publicKeyRef
Expand Down