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

[V7] Make BTClientToken internal + drop Obj-C exposure #1445

Merged
merged 2 commits into from
Oct 28, 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
60 changes: 8 additions & 52 deletions Sources/BraintreeCore/Authorization/BTClientToken.swift
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
import Foundation

/// An authorization string used to initialize the Braintree SDK
@_documentation(visibility: private)
@objcMembers public class BTClientToken: NSObject, NSCoding, NSCopying, ClientAuthorization {
class BTClientToken: ClientAuthorization {

// NEXT_MAJOR_VERSION (v7): properties exposed for Objective-C interoperability + Drop-in access.
// Once the entire SDK is in Swift, determine if we want public properties to be internal and
// what we can make internal without breaking the Drop-in.
// MARK: - Public Properties
// MARK: - Internal Properties

/// :nodoc: This property is exposed for internal Braintree use only. Do not use. It is not covered by Semantic Versioning and may change or be removed at any time.
/// The client token as a BTJSON object
public let json: BTJSON
let json: BTJSON

/// :nodoc: This property is exposed for internal Braintree use only. Do not use. It is not covered by Semantic Versioning and may change or be removed at any time.
/// The extracted authorization fingerprint
public let bearer: String
let bearer: String

/// :nodoc: This property is exposed for internal Braintree use only. Do not use. It is not covered by Semantic Versioning and may change or be removed at any time.
/// The extracted configURL
public let configURL: URL
let configURL: URL

/// :nodoc:
public let type = AuthorizationType.clientToken
let type = AuthorizationType.clientToken

/// :nodoc: This property is exposed for internal Braintree use only. Do not use. It is not covered by Semantic Versioning and may change or be removed at any time.
/// The original string used to initialize this instance
public let originalValue: String
let originalValue: String

// MARK: - Initializers

/// :nodoc: This method is exposed for internal Braintree use only. Do not use. It is not covered by Semantic Versioning and may change or be removed at any time.
/// Initialize a client token with a client token string generated by a Braintree Server Library.
/// - Parameter clientToken: A client token string generated by a Braintree Server Library
@objc(initWithClientToken:error:)
public init(clientToken: String) throws {
init(clientToken: String) throws {
// Client token must be decoded first because the other values are retrieved from it
self.json = try Self.decodeClientToken(clientToken)

Expand Down Expand Up @@ -85,37 +74,4 @@ import Foundation

return BTJSON(value: clientTokenJSON)
}

// MARK: - NSCoding conformance

public func encode(with coder: NSCoder) {
coder.encode(originalValue, forKey: "originalValue")
}

public required convenience init?(coder: NSCoder) {
try? self.init(
clientToken: coder.decodeObject(forKey: "originalValue") as? String ?? ""
)
}

// MARK: - NSCopying conformance

@objc(copyWithZone:)
public func copy(with zone: NSZone? = nil) -> Any {
do {
return try BTClientToken(clientToken: self.originalValue)
} catch {
return error
}
}

// MARK: - NSObject override

public override func isEqual(_ object: Any?) -> Bool {
guard object is BTClientToken, let otherToken = object as? BTClientToken else {
return false
}

return self.json.asDictionary() == otherToken.json.asDictionary()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,73 +126,4 @@ class BTClientToken_Tests: XCTestCase {
XCTAssertEqual(error.localizedDescription, BTClientTokenError.invalidAuthorizationFingerprint.localizedDescription)
}
}

// MARK: - NSCoding test

func testNSCoding_afterEncodingAndDecodingClientToken_preservesClientTokenDataIntegrity() throws {
let authFingerprintString = "an_authorization_fingerprint|created_at=2014-02-12T18:02:30+0000&customer_id=1234567&public_key=integration_public_key"
let configURLString = "https://api.example.com/client_api/v1/configuration"
let overrides: [String: Any] = [
"configUrl": configURLString,
"authorizationFingerprint": authFingerprintString
]

let clientTokenEncodedJSON = TestClientTokenFactory.token(withVersion: 2, overrides: overrides)
let clientToken = try XCTUnwrap(BTClientToken(clientToken: clientTokenEncodedJSON))

let coder = NSKeyedArchiver(requiringSecureCoding: true)
clientToken.encode(with: coder)

let decoder = try XCTUnwrap(NSKeyedUnarchiver(forReadingFrom: coder.encodedData))
let returnedClientToken = BTClientToken(coder: decoder)
decoder.finishDecoding()

XCTAssertEqual(returnedClientToken?.configURL, URL(string: configURLString))
XCTAssertEqual(returnedClientToken?.bearer, authFingerprintString)
}

// MARK: - isEqual tests

func testIsEqual_whenTokensContainTheSameValues_returnsTrue() throws {
let clientTokenEncodedJSON = TestClientTokenFactory.token(withVersion: 2, overrides: ["authorizationFingerprint": "abcd"])
let clientToken = try XCTUnwrap(BTClientToken(clientToken: clientTokenEncodedJSON))
let clientToken2 = try XCTUnwrap(BTClientToken(clientToken: clientTokenEncodedJSON))

XCTAssertNotNil(clientToken)
XCTAssertNotNil(clientToken2)
XCTAssertEqual(clientToken, clientToken2)
}

func testIsEqual_whenTokensDoNotContainTheSameValues_returnsFalse() throws {
let clientTokenString1 = TestClientTokenFactory.token(withVersion: 2, overrides: ["authorizationFingerprint": "one_auth_fingerprint"])
let clientTokenString2 = TestClientTokenFactory.token(withVersion: 2, overrides: ["authorizationFingerprint": "different_auth_fingerprint"])

let clientToken = try XCTUnwrap(BTClientToken(clientToken: clientTokenString1))
let clientToken2 = try XCTUnwrap(BTClientToken(clientToken: clientTokenString2))

XCTAssertNotNil(clientToken)
XCTAssertNotNil(clientToken2)
XCTAssertNotEqual(clientToken, clientToken2)
}

// MARK: - NSCopying tests

func testCopy_returnsAnEquivalentInstance() throws {
let clientTokenRawJSON = TestClientTokenFactory.token(withVersion: 2)
let clientToken = try XCTUnwrap(BTClientToken(clientToken: clientTokenRawJSON))
let copiedClientToken = clientToken.copy() as? BTClientToken

XCTAssertEqual(copiedClientToken, clientToken)
}

func testCopy_returnsAnInstanceWithEqualValues() throws {
let clientTokenRawJSON = TestClientTokenFactory.token(withVersion: 2)
let clientToken = try XCTUnwrap(BTClientToken(clientToken: clientTokenRawJSON))
let copiedClientToken = clientToken.copy() as? BTClientToken

XCTAssertEqual(copiedClientToken?.configURL, clientToken.configURL)
XCTAssertEqual(copiedClientToken?.json.asDictionary(), clientToken.json.asDictionary())
XCTAssertEqual(copiedClientToken?.bearer, clientToken.bearer)
XCTAssertEqual(copiedClientToken?.originalValue, clientToken.originalValue)
}
}