Skip to content

Commit d29d242

Browse files
feat: Implement UseUserAccessGroup for iOS
Implement the Firebase Authentication method UseUserAccessGroup in the C++ SDK. This method allows specifying a user access group for keychain data sharing on iOS. It calls the underlying Objective-C method `[FIRAuth useUserAccessGroup:error:]`. On other platforms (Android, Desktop), this method is a no-op stub, as the functionality is iOS-specific.
1 parent e48c5ce commit d29d242

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed

auth/src/android/auth_android.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,5 +676,13 @@ void DisableTokenAutoRefresh(AuthData* auth_data) {}
676676
void InitializeTokenRefresher(AuthData* auth_data) {}
677677
void DestroyTokenRefresher(AuthData* auth_data) {}
678678

679+
#if !FIREBASE_PLATFORM_IOS
680+
// Stub for non-iOS platforms.
681+
void Auth::UseUserAccessGroup(const char* user_access_group) {
682+
// This function is only implemented on iOS.
683+
(void)user_access_group; // Mark as used to avoid compiler warnings.
684+
}
685+
#endif // !FIREBASE_PLATFORM_IOS
686+
679687
} // namespace auth
680688
} // namespace firebase

auth/src/auth.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,5 +373,13 @@ AUTH_RESULT_FN(Auth, SignInWithEmailAndPassword, AuthResult)
373373

374374
AUTH_RESULT_FN(Auth, CreateUserWithEmailAndPassword, AuthResult)
375375

376+
#if !FIREBASE_PLATFORM_IOS
377+
// Stub for non-iOS platforms.
378+
void Auth::UseUserAccessGroup(const char* user_access_group) {
379+
// This function is only implemented on iOS.
380+
(void)user_access_group; // Mark as used to avoid compiler warnings.
381+
}
382+
#endif // !FIREBASE_PLATFORM_IOS
383+
376384
} // namespace auth
377385
} // namespace firebase

auth/src/desktop/auth_desktop.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,5 +768,13 @@ void IdTokenRefreshThread::DisableAuthRefresh() {
768768
ref_count_--;
769769
}
770770

771+
#if !FIREBASE_PLATFORM_IOS
772+
// Stub for non-iOS platforms.
773+
void Auth::UseUserAccessGroup(const char* user_access_group) {
774+
// This function is only implemented on iOS.
775+
(void)user_access_group; // Mark as used to avoid compiler warnings.
776+
}
777+
#endif // !FIREBASE_PLATFORM_IOS
778+
771779
} // namespace auth
772780
} // namespace firebase

auth/src/include/firebase/auth.h

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

431+
/// @brief Specifies a user access group for keychain data sharing for the
432+
/// current app.
433+
///
434+
/// @details This method is only functional on iOS. On other platforms, it is
435+
/// a no-op.
436+
///
437+
/// Setting this will allow the application to share user credentials with
438+
/// other applications that are members of the same access group.
439+
///
440+
/// After this is called, all future get and set keychain operations will use
441+
/// the new user access group. By default, this is nil and credentials are
442+
/// only accessible by the current application.
443+
///
444+
/// @param[in] user_access_group The user access group string to use.
445+
/// Pass `nullptr` or an empty string to reset to the default (app-only)
446+
/// access group.
447+
void UseUserAccessGroup(const char* user_access_group);
448+
431449
#ifndef SWIG
432450
/// @brief Registers a listener to changes in the authentication state.
433451
///

auth/src/ios/auth_ios.mm

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

611+
void Auth::UseUserAccessGroup(const char* user_access_group) {
612+
if (!auth_data_) return;
613+
NSString* access_group_nsstring = nil;
614+
if (user_access_group != nullptr && strlen(user_access_group) > 0) {
615+
access_group_nsstring = [NSString stringWithUTF8String:user_access_group];
616+
}
617+
618+
NSError* error = nil;
619+
BOOL success = [AuthImpl(auth_data_) useUserAccessGroup:access_group_nsstring error:&error];
620+
if (!success || error) {
621+
LogWarning("Error setting user access group: %s",
622+
[[error localizedDescription] UTF8String]);
623+
// Note: The C++ method is void, so we're not propagating the error further up.
624+
// If specific error handling is needed in C++, the method signature would need to change.
625+
}
626+
}
627+
611628
} // namespace auth
612629
} // namespace firebase

0 commit comments

Comments
 (0)