Skip to content

Commit

Permalink
fix: remove the requirement to check API key type when tokenizing (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcschy authored Feb 14, 2024
1 parent 2ac4a84 commit 536c43a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import AnyCodable
import Combine

public enum TokenizingError: Error {
case applicationTypeNotPublic
case invalidApiKey
case invalidInput
}

Expand Down Expand Up @@ -95,35 +95,10 @@ final public class BasisTheoryElements {
completion(nil, TokenizingError.invalidInput) // error logged with more detail in replaceElementRefs
return
}

BasisTheoryAPI.basePath = basePath
// TODO: should this BT-TRACE-ID and the tokenize one be the same???
getApplicationKey(apiKey: getApiKey(apiKey), btTraceId: btTraceId) { data, error in
guard error == nil else {
completion(nil, error)
TelemetryLogging.error("Failed to get Application by key", attributes: [
"endpoint": "GET /applications/key",
"BT-TRACE-ID": btTraceId,
"apiSuccess": false
])

return
}

guard data?.type == "public" else {
completion(nil, TokenizingError.applicationTypeNotPublic)
TelemetryLogging.warn("Tried to tokenize with a non-public API key with tokenize function", attributes: [
"endpoint": "POST /tokenize",
"BT-TRACE-ID": btTraceId,
"apiSuccess": false
])

return
}

TokenizeAPI.tokenizeWithRequestBuilder(body: AnyCodable(mutableBody)).addBasisTheoryElementHeaders(apiKey: getApiKey(apiKey), btTraceId: btTraceId).execute { result in
completeApiRequest(endpoint: endpoint, btTraceId: btTraceId, result: result, completion: completion)
}
TokenizeAPI.tokenizeWithRequestBuilder(body: AnyCodable(mutableBody)).addBasisTheoryElementHeaders(apiKey: getApiKey(apiKey), btTraceId: btTraceId).execute { result in
completeApiRequest(endpoint: endpoint, btTraceId: btTraceId, result: result, completion: completion)
}
}

Expand All @@ -140,37 +115,15 @@ final public class BasisTheoryElements {
completion(nil, TokenizingError.invalidInput) // error logged with more detail in replaceElementRefs
return
}

mutableBody.data = mutableData



BasisTheoryAPI.basePath = basePath
getApplicationKey(apiKey: getApiKey(apiKey), btTraceId: btTraceId) {data, error in
guard error == nil else {
completion(nil, error)
TelemetryLogging.error("Failed to get Application by key", attributes: [
"endpoint": "GET /applications/key",
"BT-TRACE-ID": btTraceId,
"apiSuccess": false
])

return
}

guard data?.type == "public" else {
completion(nil, TokenizingError.applicationTypeNotPublic)
TelemetryLogging.warn("Tried to tokenize with a non-public API key", attributes: [
"endpoint": "/tokens",
"BT-TRACE-ID": btTraceId
])

return
}

let createTokenRequest = mutableBody.toCreateTokenRequest()

TokensAPI.createWithRequestBuilder(createTokenRequest: createTokenRequest).addBasisTheoryElementHeaders(apiKey: getApiKey(apiKey), btTraceId: btTraceId).execute { result in
completeApiRequest(endpoint: endpoint, btTraceId: btTraceId, result: result, completion: completion)
}
let createTokenRequest = mutableBody.toCreateTokenRequest()

TokensAPI.createWithRequestBuilder(createTokenRequest: createTokenRequest).addBasisTheoryElementHeaders(apiKey: getApiKey(apiKey), btTraceId: btTraceId).execute { result in
completeApiRequest(endpoint: endpoint, btTraceId: btTraceId, result: result, completion: completion)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,7 @@ final class TokenizeAndCreateTokenServiceTests: XCTestCase {
}

override func tearDownWithError() throws { }

func testTokenizeChecksForApplicationTypeOfPublic() throws {
let body: [String: Any] = [
"myProp": "myValue"
]

let privateApiKey = Configuration.getConfiguration().privateBtApiKey!
let tokenizeExpectation = self.expectation(description: "Tokenize")
BasisTheoryElements.tokenize(body: body, apiKey: privateApiKey) { data, error in
XCTAssertNil(data)
XCTAssertEqual(error as! TokenizingError, TokenizingError.applicationTypeNotPublic)

tokenizeExpectation.fulfill()
}

waitForExpectations(timeout: TIMEOUT_EXPECTATION)
}


func testTokenizeReturnsErrorFromApplicationCheck() throws {
let body: [String: Any] = [
"myProp": "myValue"
Expand All @@ -50,24 +33,7 @@ final class TokenizeAndCreateTokenServiceTests: XCTestCase {

waitForExpectations(timeout: TIMEOUT_EXPECTATION)
}

func testCreateTokenChecksForApplicationTypeOfPublic() throws {
let body = CreateToken(type: "token", data: [
"myProp": "myValue"
])

let privateApiKey = Configuration.getConfiguration().privateBtApiKey!
let tokenizeExpectation = self.expectation(description: "Tokenize")
BasisTheoryElements.createToken(body: body, apiKey: privateApiKey) { data, error in
XCTAssertNil(data)
XCTAssertEqual(error as! TokenizingError, TokenizingError.applicationTypeNotPublic)

tokenizeExpectation.fulfill()
}

waitForExpectations(timeout: TIMEOUT_EXPECTATION)
}


func testCreateReturnsErrorFromApplicationCheck() throws {
let body = CreateToken(type: "token", data: [
"myProp": "myValue"
Expand Down

0 comments on commit 536c43a

Please sign in to comment.