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

[MBL-736] Upgrade PerimeterX #1839

Merged
merged 7 commits into from
Aug 8, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 7 additions & 20 deletions Kickstarter-iOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ internal final class AppDelegate: UIResponder, UIApplicationDelegate {
.messageBannerViewController?.showBanner(with: success ? .success : .error, message: message)
}

self.viewModel.outputs.configurePerimeterX
.observeValues {
AppEnvironment.current.apiService.perimeterXClient
Copy link
Contributor Author

@msadoon msadoon Aug 1, 2023

Choose a reason for hiding this comment

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

This is now where we call start on PerimeterX, not in Service. Thinking is more along the lines of their official instructions (Step 5). Although the SDK instructions explicitly say to "You should only call this function once" and appDidFinishLaunchingWithOptions is called on every relaunch.
Some ambiguity there.

Copy link
Contributor

Choose a reason for hiding this comment

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

The instructions also say "It's essential to call this function as early as possible in your application and before any URL request to your server" so this should be fine. I feel like they meant to say "only call this function once per app launch" 🤷🏻‍♂️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah ok makes sense

.start(policyDomains: [AppEnvironment.current.apiService.serverConfig.apiBaseUrl.host ?? ""])
}

NotificationCenter.default
.addObserver(forName: Notification.Name.ksr_sessionStarted, object: nil, queue: nil) { [weak self] _ in
self?.viewModel.inputs.userSessionStarted()
Expand Down Expand Up @@ -268,28 +274,13 @@ internal final class AppDelegate: UIResponder, UIApplicationDelegate {
self?.viewModel.inputs.configUpdatedNotificationObserved()
}

NotificationCenter.default
.addObserver(
forName: Notification.Name.ksr_perimeterXCaptcha,
object: nil,
queue: nil
) { [weak self] note in
self?.viewModel.inputs.perimeterXCaptchaTriggeredWithUserInfo(note.userInfo)
}

self.window?.tintColor = .ksr_create_700

self.viewModel.inputs.applicationDidFinishLaunching(
application: application,
launchOptions: launchOptions
)

self.viewModel.outputs.goToPerimeterXCaptcha
.observeForControllerAction()
.observeValues { response in
self.goToPerimeterXCaptcha(response)
}

UNUserNotificationCenter.current().delegate = self

return self.viewModel.outputs.applicationDidFinishLaunchingReturnValue
Expand Down Expand Up @@ -407,10 +398,6 @@ internal final class AppDelegate: UIResponder, UIApplicationDelegate {
self.rootTabBarController?.switchToMessageThread(messageThread)
}

private func goToPerimeterXCaptcha(_ response: PerimeterXBlockResponseType) {
response.displayCaptcha(on: self.window?.rootViewController)
}

private func goToCreatorMessageThread(_ projectId: Param, _ messageThread: MessageThread) {
self.rootTabBarController?
.switchToCreatorMessageThread(projectId: projectId, messageThread: messageThread)
Expand All @@ -436,7 +423,7 @@ internal final class AppDelegate: UIResponder, UIApplicationDelegate {
_ = AppEnvironment.current.remoteConfigClient?
.addOnConfigUpdateListener { configUpdate, error in
guard let realtimeUpdateError = error else {
print("🔮 Remote Config Keys Update: \(configUpdate?.updatedKeys)")
print("🔮 Remote Config Keys Update: \(String(describing: configUpdate?.updatedKeys))")

return
}
Expand Down
19 changes: 0 additions & 19 deletions Kickstarter-iOS/AppDelegateViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ public protocol AppDelegateViewModelInputs {
/// Call when Remote Config configuration has failed
func remoteConfigClientConfigurationFailed()

/// Call when Perimeter X Captcha is triggered
func perimeterXCaptchaTriggeredWithUserInfo(_ userInfo: [AnyHashable: Any]?)

/// Call when the contextual PushNotification dialog should be presented.
func showNotificationDialog(notification: Notification)

Expand Down Expand Up @@ -144,9 +141,6 @@ public protocol AppDelegateViewModelOutputs {
/// Emits a message thread when we should navigate to it.
var goToMessageThread: Signal<MessageThread, Never> { get }

/// Emits when we should display the Perimeter X Captcha view.
var goToPerimeterXCaptcha: Signal<PerimeterXBlockResponseType, Never> { get }

/// Emits when the root view controller should navigate to the user's profile.
var goToProfile: Signal<(), Never> { get }

Expand Down Expand Up @@ -763,13 +757,6 @@ public final class AppDelegateViewModel: AppDelegateViewModelType, AppDelegateVi
.filter(isTrue)
.mapConst(0)

self.goToPerimeterXCaptcha = self.perimeterXCaptchaTriggeredWithUserInfoProperty.signal.skipNil()
.map { userInfo -> PerimeterXBlockResponseType? in
guard let response = userInfo["response"] as? PerimeterXBlockResponseType else { return nil }
return response
}
.skipNil()

self.configureSegmentWithBraze = self.applicationLaunchOptionsProperty.signal
.skipNil()
.map { _ in
Expand Down Expand Up @@ -948,11 +935,6 @@ public final class AppDelegateViewModel: AppDelegateViewModelType, AppDelegateVi
self.remoteConfigClientConfigurationFailedProperty.value = ()
}

private let perimeterXCaptchaTriggeredWithUserInfoProperty = MutableProperty<[AnyHashable: Any]?>(nil)
public func perimeterXCaptchaTriggeredWithUserInfo(_ userInfo: [AnyHashable: Any]?) {
self.perimeterXCaptchaTriggeredWithUserInfoProperty.value = userInfo
}

public let applicationIconBadgeNumber: Signal<Int, Never>
public let configureAppCenterWithData: Signal<AppCenterConfigData, Never>
public let configureFirebase: Signal<(), Never>
Expand All @@ -968,7 +950,6 @@ public final class AppDelegateViewModel: AppDelegateViewModelType, AppDelegateVi
public let goToDiscovery: Signal<DiscoveryParams?, Never>
public let goToLoginWithIntent: Signal<LoginIntent, Never>
public let goToMessageThread: Signal<MessageThread, Never>
public let goToPerimeterXCaptcha: Signal<PerimeterXBlockResponseType, Never>
public let goToProfile: Signal<(), Never>
public let goToProjectActivities: Signal<Param, Never>
public let goToMobileSafari: Signal<URL, Never>
Expand Down
24 changes: 0 additions & 24 deletions Kickstarter-iOS/AppDelegateViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ final class AppDelegateViewModelTests: TestCase {
private let goToDiscovery = TestObserver<DiscoveryParams?, Never>()
private let goToProjectActivities = TestObserver<Param, Never>()
private let goToLoginWithIntent = TestObserver<LoginIntent, Never>()
private let goToPerimeterXCaptcha = TestObserver<PerimeterXBlockResponseType, Never>()
private let goToProfile = TestObserver<(), Never>()
private let goToMobileSafari = TestObserver<URL, Never>()
private let goToSearch = TestObserver<(), Never>()
Expand Down Expand Up @@ -75,7 +74,6 @@ final class AppDelegateViewModelTests: TestCase {
self.vm.outputs.goToDashboard.observe(self.goToDashboard.observer)
self.vm.outputs.goToDiscovery.observe(self.goToDiscovery.observer)
self.vm.outputs.goToLoginWithIntent.observe(self.goToLoginWithIntent.observer)
self.vm.outputs.goToPerimeterXCaptcha.observe(self.goToPerimeterXCaptcha.observer)
self.vm.outputs.goToProfile.observe(self.goToProfile.observer)
self.vm.outputs.goToMobileSafari.observe(self.goToMobileSafari.observer)
self.vm.outputs.goToProjectActivities.observe(self.goToProjectActivities.observer)
Expand Down Expand Up @@ -2222,28 +2220,6 @@ final class AppDelegateViewModelTests: TestCase {
}
}

func testGoToPerimeterXCaptcha_Captcha() {
self.goToPerimeterXCaptcha.assertDidNotEmitValue()

let response = MockPerimeterXBlockResponse(blockType: .Captcha)

self.vm.inputs.perimeterXCaptchaTriggeredWithUserInfo(["response": response])

self.goToPerimeterXCaptcha.assertValueCount(1)
XCTAssertEqual(self.goToPerimeterXCaptcha.values.last?.type, .Captcha)
}

func testGoToPerimeterXCaptcha_Blocked() {
self.goToPerimeterXCaptcha.assertDidNotEmitValue()

let response = MockPerimeterXBlockResponse(blockType: .Block)

self.vm.inputs.perimeterXCaptchaTriggeredWithUserInfo(["response": response])

self.goToPerimeterXCaptcha.assertValueCount(1)
XCTAssertEqual(self.goToPerimeterXCaptcha.values.last?.type, .Block)
}

func testFeatureFlagsRetainedInConfig_NotRelease() {
let mockBundle = MockBundle(
bundleIdentifier: KickstarterBundleIdentifier.beta.rawValue
Expand Down