-
Notifications
You must be signed in to change notification settings - Fork 99
[PM-33569] feat: Add notification service extension to update auth request notifications #2444
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
Open
matt-livefront
wants to merge
3
commits into
main
Choose a base branch
from
matt/PM-33569-push-notification-extension
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...rdenNotificationExtension/Application/Support/BitwardenNotificationExtension.entitlements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
| <plist version="1.0"> | ||
| <dict> | ||
| <key>com.apple.security.application-groups</key> | ||
| <array> | ||
| <string>group.$(BASE_BUNDLE_ID)</string> | ||
| </array> | ||
| </dict> | ||
| </plist> |
107 changes: 107 additions & 0 deletions
107
BitwardenNotificationExtension/Application/Support/Info.plist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
| <plist version="1.0"> | ||
| <dict> | ||
| <key>BitwardenAppIdentifier</key> | ||
| <string>$(BASE_BUNDLE_ID)</string> | ||
| <key>CFBundleDevelopmentRegion</key> | ||
| <string>en</string> | ||
| <key>CFBundleDisplayName</key> | ||
| <string>Bitwarden</string> | ||
| <key>CFBundleExecutable</key> | ||
| <string>$(EXECUTABLE_NAME)</string> | ||
| <key>CFBundleIdentifier</key> | ||
| <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> | ||
| <key>CFBundleInfoDictionaryVersion</key> | ||
| <string>6.0</string> | ||
| <key>CFBundleLocalizations</key> | ||
| <array> | ||
| <string>af</string> | ||
| <string>ar</string> | ||
| <string>az</string> | ||
| <string>be</string> | ||
| <string>bg</string> | ||
| <string>bn</string> | ||
| <string>bs</string> | ||
| <string>ca</string> | ||
| <string>cs</string> | ||
| <string>cy</string> | ||
| <string>da</string> | ||
| <string>de</string> | ||
| <string>el</string> | ||
| <string>en</string> | ||
| <string>en-GB</string> | ||
| <string>en-IN</string> | ||
| <string>es</string> | ||
| <string>et</string> | ||
| <string>eu</string> | ||
| <string>fa</string> | ||
| <string>fi</string> | ||
| <string>fil</string> | ||
| <string>fr</string> | ||
| <string>gl</string> | ||
| <string>he</string> | ||
| <string>hi</string> | ||
| <string>hr</string> | ||
| <string>hu</string> | ||
| <string>id</string> | ||
| <string>it</string> | ||
| <string>ja</string> | ||
| <string>ka</string> | ||
| <string>kn</string> | ||
| <string>ko</string> | ||
| <string>lt</string> | ||
| <string>lv</string> | ||
| <string>ml</string> | ||
| <string>mr</string> | ||
| <string>my</string> | ||
| <string>nb</string> | ||
| <string>ne</string> | ||
| <string>nl</string> | ||
| <string>nn-NO</string> | ||
| <string>or</string> | ||
| <string>pl</string> | ||
| <string>pt</string> | ||
| <string>pt-BR</string> | ||
| <string>ro</string> | ||
| <string>ru</string> | ||
| <string>si</string> | ||
| <string>sk</string> | ||
| <string>sl</string> | ||
| <string>sr</string> | ||
| <string>sv</string> | ||
| <string>ta</string> | ||
| <string>te</string> | ||
| <string>th</string> | ||
| <string>tr</string> | ||
| <string>uk</string> | ||
| <string>vi</string> | ||
| <string>zh-Hans</string> | ||
| <string>zh-Hant</string> | ||
| </array> | ||
| <key>CFBundleName</key> | ||
| <string>Bitwarden Notification</string> | ||
| <key>CFBundlePackageType</key> | ||
| <string>XPC!</string> | ||
| <key>CFBundleShortVersionString</key> | ||
| <string>$(MARKETING_VERSION)</string> | ||
| <key>CFBundleVersion</key> | ||
| <string>$(CURRENT_PROJECT_VERSION)</string> | ||
| <key>ITSAppUsesNonExemptEncryption</key> | ||
| <true/> | ||
| <key>ITSEncryptionExportComplianceCode</key> | ||
| <string>ecf076d3-4824-4d7b-b716-2a9a47d7d296</string> | ||
| <key>NSExtension</key> | ||
| <dict> | ||
| <key>NSExtensionPointIdentifier</key> | ||
| <string>com.apple.usernotifications.service</string> | ||
| <key>NSExtensionPrincipalClass</key> | ||
| <string>$(PRODUCT_MODULE_NAME).NotificationServiceExtension</string> | ||
| </dict> | ||
| <key>UIRequiredDeviceCapabilities</key> | ||
| <dict> | ||
| <key>arm64</key> | ||
| <true/> | ||
| </dict> | ||
| </dict> | ||
| </plist> |
31 changes: 31 additions & 0 deletions
31
BitwardenNotificationExtension/NotificationServiceExtension.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import BitwardenShared | ||
| import OSLog | ||
| import UserNotifications | ||
|
|
||
| // MARK: - NotificationServiceExtension | ||
|
|
||
| /// The notification service extension entry point, responsible for intercepting incoming alert | ||
| /// push notifications and updating their content before delivery. | ||
| /// | ||
| /// This class is intentionally thin β all decoding and state-lookup logic lives in | ||
| /// `DefaultNotificationExtensionHelper` (in `BitwardenShared`) where it is fully testable. | ||
| /// | ||
| class NotificationServiceExtension: UNNotificationServiceExtension { | ||
| // MARK: UNNotificationServiceExtension | ||
|
|
||
| override func didReceive( | ||
| _ request: UNNotificationRequest, | ||
| withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void, | ||
| ) { | ||
| guard let content = request.content.mutableCopy() as? UNMutableNotificationContent else { | ||
| Logger.appExtension.error("Failed to cast UNNotificationContent to UNMutableNotificationContent") | ||
| contentHandler(request.content) | ||
| return | ||
| } | ||
|
|
||
| Task { | ||
| let updatedContent = await DefaultNotificationExtensionHelper().processNotification(content: content) | ||
| contentHandler(updatedContent) | ||
| } | ||
| } | ||
| } |
121 changes: 121 additions & 0 deletions
121
BitwardenShared/Core/Platform/Services/NotificationExtensionHelper.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import BitwardenKit | ||
| import BitwardenResources | ||
| import Foundation | ||
| import UserNotifications | ||
|
|
||
| // MARK: - NotificationExtensionHelper | ||
|
|
||
| /// A protocol for a helper that processes notification content within a notification service extension. | ||
| /// | ||
| public protocol NotificationExtensionHelper { // sourcery: AutoMockable | ||
| /// Processes a notification, updating the content as appropriate based on its type. | ||
| /// | ||
| /// - Parameter content: The mutable notification content to update. | ||
| /// - Returns: The updated notification content, or the original content unchanged if the | ||
| /// payload cannot be decoded or no update applies. | ||
| /// | ||
| func processNotification( | ||
| content: UNMutableNotificationContent, | ||
| ) async -> UNMutableNotificationContent | ||
| } | ||
|
|
||
| // MARK: - DefaultNotificationExtensionHelper | ||
|
|
||
| /// The default implementation of `NotificationExtensionHelper`. | ||
| /// | ||
| public class DefaultNotificationExtensionHelper: NotificationExtensionHelper { | ||
| // MARK: Properties | ||
|
|
||
| /// The store used to read persisted app settings. | ||
| private let appSettingsStore: AppSettingsStore | ||
|
|
||
| /// The reporter used to log non-fatal errors. | ||
| private let errorReporter: ErrorReporter | ||
|
|
||
| // MARK: Initialization | ||
|
|
||
| /// Initializes a `DefaultNotificationExtensionHelper` with an explicit settings store | ||
| /// and error reporter. | ||
| /// | ||
| /// - Parameters: | ||
| /// - appSettingsStore: The store used to read persisted app settings. | ||
| /// - errorReporter: The reporter used to log non-fatal errors. | ||
| /// | ||
| init(appSettingsStore: AppSettingsStore, errorReporter: ErrorReporter) { | ||
| self.appSettingsStore = appSettingsStore | ||
| self.errorReporter = errorReporter | ||
|
|
||
| Resources.initialLanguageCode = appSettingsStore.appLocale ?? Bundle.main.preferredLocalizations.first | ||
| } | ||
|
|
||
| /// Initializes a `DefaultNotificationExtensionHelper` using the shared app group `UserDefaults` | ||
| /// and an `OSLogErrorReporter`. | ||
| /// | ||
| public convenience init() { | ||
| let userDefaults = UserDefaults(suiteName: Bundle.main.groupIdentifier)! | ||
| self.init( | ||
| appSettingsStore: DefaultAppSettingsStore(userDefaults: userDefaults), | ||
| errorReporter: OSLogErrorReporter(), | ||
fedemkr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| } | ||
|
|
||
| // MARK: NotificationExtensionHelper | ||
|
|
||
| public func processNotification(content: UNMutableNotificationContent) async -> UNMutableNotificationContent { | ||
| do { | ||
| guard let notificationData = try decodePushNotificationData(from: content.userInfo), | ||
| let type = notificationData.type | ||
| else { | ||
| return content | ||
| } | ||
|
|
||
| switch type { | ||
| case .authRequest: | ||
| return try handleAuthRequest(notificationData, content: content) | ||
| default: | ||
| return content | ||
| } | ||
| } catch { | ||
| errorReporter.log(error: error) | ||
| return content | ||
| } | ||
| } | ||
|
|
||
| // MARK: Private | ||
|
|
||
| /// Decodes a `PushNotificationData` from a notification's `userInfo` dictionary. | ||
| /// | ||
| /// - Parameter userInfo: The notification's `userInfo` dictionary. | ||
| /// - Returns: The decoded `PushNotificationData`, or `nil` if the `"data"` key is absent. | ||
| /// - Throws: An error if JSON serialization or decoding fails. | ||
| /// | ||
| private func decodePushNotificationData(from userInfo: [AnyHashable: Any]) throws -> PushNotificationData? { | ||
| guard let messageContent = userInfo["data"] as? [AnyHashable: Any] else { return nil } | ||
| let jsonData = try JSONSerialization.data(withJSONObject: messageContent) | ||
| return try JSONDecoder().decode(PushNotificationData.self, from: jsonData) | ||
| } | ||
fedemkr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /// Handles an auth request notification, updating the body with the requesting user's email | ||
| /// if found in the accounts store. | ||
| /// | ||
| /// - Parameters: | ||
| /// - notificationData: The decoded push notification data. | ||
| /// - content: The mutable notification content to update. | ||
| /// - Returns: The updated content, or the original content unchanged if the user is not found. | ||
| /// - Throws: An error if the login request payload cannot be decoded. | ||
| /// | ||
| private func handleAuthRequest( | ||
| _ notificationData: PushNotificationData, | ||
| content: UNMutableNotificationContent, | ||
| ) throws -> UNMutableNotificationContent { | ||
| let loginRequest: LoginRequestNotification = try notificationData.data() | ||
| guard let email = appSettingsStore.state?.accounts[loginRequest.userId]?.profile.email else { | ||
| return content | ||
| } | ||
|
|
||
| content.title = Localizations.logInRequested | ||
| content.body = Localizations.confimLogInAttempForX(email) | ||
|
|
||
| return content | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.