Skip to content

Commit d8965fc

Browse files
feat(auth): Implement UseUserAccessGroup for C++ iOS SDK
This commit implements the `UseUserAccessGroup` method in the Firebase C++ Auth SDK. This method is iOS-only and allows developers to specify a keychain access group for sharing authentication state across multiple apps from the same publisher. - Added the public API `UseUserAccessGroup(const char* access_group)` to `firebase::auth::Auth`. - Implemented the Objective-C wrapper in `auth_ios.mm` to call `[FIRAuth useUserAccessGroup:error:]`. - Added stub implementations for Android and Desktop platforms that log a warning and return `kAuthErrorNone`. - Included comprehensive Doxygen comments for the new API in `auth.h`.
1 parent e48c5ce commit d8965fc

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

auth/src/android/auth_android.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,15 @@ 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+
// This is an iOS-only feature. No-op on Android.
675+
(void)access_group; // Mark as unused
676+
LogWarning(
677+
"UseUserAccessGroup is an iOS-only feature and has no effect on "
678+
"Android.");
679+
return kAuthErrorNone;
680+
}
681+
673682
// Not implemented for Android.
674683
void EnableTokenAutoRefresh(AuthData* auth_data) {}
675684
void DisableTokenAutoRefresh(AuthData* auth_data) {}

auth/src/desktop/auth_desktop.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,15 @@ 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+
// This is an iOS-only feature. No-op on Desktop.
580+
(void)access_group; // Mark as unused
581+
LogWarning(
582+
"UseUserAccessGroup is an iOS-only feature and has no effect on "
583+
"Desktop.");
584+
return kAuthErrorNone;
585+
}
586+
578587
void InitializeTokenRefresher(AuthData* auth_data) {
579588
auto auth_impl = static_cast<AuthImpl*>(auth_data->auth_impl);
580589
auth_impl->token_refresh_thread.Initialize(auth_data);

auth/src/include/firebase/auth.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,38 @@ class Auth {
428428
/// Get results of the most recent call to SendPasswordResetEmail.
429429
Future<void> SendPasswordResetEmailLastResult() const;
430430

431+
/// @brief Sets the user access group to use for keychain data sharing.
432+
///
433+
/// This method is only functional on iOS and tvOS. On other platforms, it is
434+
/// a no-op and will return `kAuthErrorNone`.
435+
///
436+
/// Allows you to specify a keychain access group to be used for sharing
437+
/// authentication state across multiple apps from the same publisher.
438+
/// Calling this method will switch the underlying data persistence for the
439+
/// current Auth instance to the specified access group.
440+
///
441+
/// If `access_group` is `nullptr` or an empty string, the access group will
442+
/// be cleared and the default persistence (isolated to the app) will be
443+
/// used.
444+
///
445+
/// @param[in] access_group The keychain access group string (e.g.,
446+
/// "com.example.mygroup"). Pass `nullptr` or an empty string to reset to
447+
/// the default app-specific keychain.
448+
///
449+
/// @return `kAuthErrorNone` on success.
450+
/// Returns `kAuthErrorKeychainError` if an error occurs while
451+
/// accessing the keychain (iOS/tvOS only).
452+
/// Returns `kAuthErrorUnimplemented` if called on a platform other
453+
/// than iOS or tvOS, though current stub implementations return
454+
/// `kAuthErrorNone`.
455+
///
456+
/// @note On tvOS, if `shareAuthStateAcrossDevices` is set to `true`,
457+
/// attempting to set an access group may have specific behaviors or
458+
/// limitations as outlined in the Firebase iOS SDK documentation. Refer to
459+
/// the official Firebase documentation for `[FIRAuth
460+
/// useUserAccessGroup:error:]` for more details.
461+
AuthError UseUserAccessGroup(const char* access_group);
462+
431463
#ifndef SWIG
432464
/// @brief Registers a listener to changes in the authentication state.
433465
///

auth/src/ios/auth_ios.mm

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,5 +608,25 @@ void DisableTokenAutoRefresh(AuthData *auth_data) {}
608608
void InitializeTokenRefresher(AuthData *auth_data) {}
609609
void DestroyTokenRefresher(AuthData *auth_data) {}
610610

611+
AuthError Auth::UseUserAccessGroup(const char *access_group) {
612+
if (!auth_data_ || !AuthImpl(auth_data_)) {
613+
return kAuthErrorUninitialized;
614+
}
615+
616+
NSString *ns_access_group = nil;
617+
if (access_group && strlen(access_group) > 0) {
618+
ns_access_group = [NSString stringWithUTF8String:access_group];
619+
}
620+
621+
NSError *error = nil;
622+
BOOL success = [AuthImpl(auth_data_) useUserAccessGroup:ns_access_group error:&error];
623+
624+
if (success) {
625+
return kAuthErrorNone;
626+
} else {
627+
return AuthErrorFromNSError(error);
628+
}
629+
}
630+
611631
} // namespace auth
612632
} // namespace firebase

0 commit comments

Comments
 (0)