Skip to content

Commit 0eff982

Browse files
feat: Implement UseUserAccessGroup for iOS
This commit introduces the `UseUserAccessGroup` method in the Firebase C++ Authentication SDK. This method allows developers to specify a user access group for keychain operations on iOS, enabling keychain sharing between apps from the same developer. It wraps the `[FIRAuth useUserAccessGroup:error:]` Objective-C method. - Added `Auth::UseUserAccessGroup(const char* access_group)` to the public API in `firebase/auth.h`. - Implemented the method for iOS in `auth_ios.mm`, calling the corresponding FIRAuth method and handling potential errors. - Provided no-op stub implementations for Android and desktop platforms, as the functionality is iOS-specific. The method returns `kAuthErrorNone` on success or if it's a no-op on non-iOS platforms. On iOS, it returns an appropriate `AuthError` if the underlying Objective-C method reports an error.
1 parent df805b2 commit 0eff982

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

auth/src/android/auth_android.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,11 @@ void Auth::UseEmulator(std::string host, uint32_t port) {
670670
SetEmulatorJni(auth_data_, host.c_str(), port);
671671
}
672672

673+
AuthError Auth::UseUserAccessGroup(const char* access_group) {
674+
// No-op on Android.
675+
return kAuthErrorNone;
676+
}
677+
673678
// Not implemented for Android.
674679
void EnableTokenAutoRefresh(AuthData* auth_data) {}
675680
void DisableTokenAutoRefresh(AuthData* auth_data) {}

auth/src/desktop/auth_desktop.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,11 @@ void Auth::UseEmulator(std::string host, uint32_t port) {
575575
auth_impl->assigned_emulator_url.append(std::to_string(port));
576576
}
577577

578+
AuthError Auth::UseUserAccessGroup(const char* access_group) {
579+
// No-op on desktop.
580+
return kAuthErrorNone;
581+
}
582+
578583
void InitializeTokenRefresher(AuthData* auth_data) {
579584
auto auth_impl = static_cast<AuthImpl*>(auth_data->auth_impl);
580585
auth_impl->token_refresh_thread.Initialize(auth_data);

auth/src/include/firebase/auth.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,28 @@ class Auth {
173173
/// set_language_code().
174174
void UseAppLanguage();
175175

176+
/// @brief Uses the specified user access group for keychain operations on
177+
/// iOS.
178+
///
179+
/// This method should be called before any other Firebase Auth operations that
180+
/// might interact with the keychain, such as sign-in or sign-out.
181+
///
182+
/// On iOS, this method corresponds to `[FIRAuth useUserAccessGroup:]`.
183+
/// If a value is provided, it will be used to set the user's access group,
184+
/// which will be used to share credentials across apps from the same
185+
/// developer. If `nullptr` is provided, it will clear any previously set
186+
/// access group.
187+
///
188+
/// On other platforms (Android, desktop), this method is a no-op and will
189+
/// always return `kAuthErrorNone`.
190+
///
191+
/// @param[in] access_group The access group to use, or `nullptr` to clear
192+
/// the access group.
193+
///
194+
/// @return `kAuthErrorNone` on success, or an `AuthError` code if an error
195+
/// occurred on iOS (e.g., keychain error).
196+
AuthError UseUserAccessGroup(const char* access_group);
197+
176198
// ----- Providers -------------------------------------------------------
177199
/// Asynchronously requests the IDPs (identity providers) that can be used
178200
/// for the given email address.

auth/src/ios/auth_ios.mm

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,26 @@ void SignInCallback(FIRUser *_Nullable user, NSError *_Nullable error,
590590
SetEmulatorJni(auth_data_, host.c_str(), port);
591591
}
592592

593+
AuthError Auth::UseUserAccessGroup(const char* access_group) {
594+
if (!auth_data_) {
595+
return kAuthErrorNone; // Or appropriate error if auth_data_ is unexpectedly null
596+
}
597+
FIRAuth* fir_auth = AuthImpl(auth_data_);
598+
NSString* ns_access_group = nil;
599+
if (access_group) {
600+
ns_access_group = [NSString stringWithUTF8String:access_group];
601+
}
602+
603+
NSError* ns_error = nil;
604+
BOOL success = [fir_auth useUserAccessGroup:ns_access_group error:&ns_error];
605+
606+
if (success) {
607+
return kAuthErrorNone;
608+
} else {
609+
return AuthErrorFromNSError(ns_error);
610+
}
611+
}
612+
593613
// Remap iOS SDK errors reported by the UIDelegate. While these errors seem like
594614
// user interaction errors, they are actually caused by bad provider ids.
595615
NSError *RemapBadProviderIDErrors(NSError *_Nonnull error) {

0 commit comments

Comments
 (0)