-
Notifications
You must be signed in to change notification settings - Fork 14
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
DBP: Integrate subscription account authentication to DBP #1995
Changes from all commits
53e4afe
e0f90c7
136c77c
cda7456
49fd1bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// DataBrokerProtectionSubscriptionEventHandler.swift | ||
// | ||
// Copyright © 2023 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
#if DBP && SUBSCRIPTION | ||
|
||
import Foundation | ||
import Account | ||
import DataBrokerProtection | ||
|
||
final class DataBrokerProtectionSubscriptionEventHandler { | ||
samsymons marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private let accountManager: Account.AccountManaging | ||
private let authRepository: AuthenticationRepository | ||
|
||
init(accountManager: Account.AccountManaging = Account.AccountManager(), | ||
authRepository: AuthenticationRepository = KeychainAuthenticationData()) { | ||
self.accountManager = accountManager | ||
self.authRepository = authRepository | ||
} | ||
|
||
func registerForSubscriptionAccountManagerEvents() { | ||
NotificationCenter.default.addObserver(self, selector: #selector(handleAccountDidSignIn), name: .accountDidSignIn, object: nil) | ||
NotificationCenter.default.addObserver(self, selector: #selector(handleAccountDidSignOut), name: .accountDidSignOut, object: nil) | ||
} | ||
|
||
@objc private func handleAccountDidSignIn() { | ||
guard let token = accountManager.token else { | ||
Pixel.fire(.dataBrokerProtectionErrorWhenFetchingSubscriptionAuthTokenAfterSignIn) | ||
assertionFailure("[DBP Subscription] AccountManager signed in but token could not be retrieved") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @samsymons I know this is unlikely, but I wonder if I should add a safeguard somewhere else for this to happen. For example, fetching the token again before using it (if no token is present). What are your thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think in this case we're good since we're reacting to a sign in event. But, we can talk with Michał about it to be sure – I am of the opinion that we should only need to access the token, and the underlying subscription logic should ensure that we always get the latest. |
||
return | ||
} | ||
|
||
authRepository.save(accessToken: token) | ||
} | ||
|
||
@objc private func handleAccountDidSignOut() { | ||
// Going to be defined here: https://app.asana.com/0/1204006570077678/1206206961074916/f | ||
} | ||
} | ||
|
||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I've been adding some of these checks myself as a part of the remote messaging change. Once we enable these features in the App Store target, we should be good to remove the flags.