Skip to content

Commit f6f3c3a

Browse files
Implement Firebase Auth UseUserAccessGroup for C++ SDK
This commit adds the UseUserAccessGroup method to the Firebase C++ Auth SDK. This method is only functional on iOS and acts as a no-op stub on other platforms (Android, desktop). It wraps the [FIRAuth useUserAccessGroup:error:] Objective-C method on iOS, allowing C++ developers to specify a keychain access group for sharing authentication data between apps. Key changes: - Added UseUserAccessGroup to the public Auth class in auth.h. - Implemented the iOS-specific logic in auth_ios.mm. - Provided stub implementations in auth_stub.cc (for desktop) and auth_android.cc (for Android).
1 parent e9ab71d commit f6f3c3a

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

auth/src/android/auth_android.cc

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

679+
// Stub implementation for UseUserAccessGroupInternal on Android.
680+
AuthError UseUserAccessGroupInternal(AuthData* auth_data,
681+
const char* group_id) {
682+
// This function is a no-op on Android.
683+
(void)auth_data;
684+
(void)group_id;
685+
return kAuthErrorNone;
686+
}
687+
679688
} // namespace auth
680689
} // namespace firebase

auth/src/auth.cc

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

374374
AUTH_RESULT_FN(Auth, CreateUserWithEmailAndPassword, AuthResult)
375375

376+
// Platform-specific implementation of UseUserAccessGroup.
377+
// This is defined in auth_ios.mm for iOS, and auth_stub.cc for other
378+
// platforms.
379+
AuthError UseUserAccessGroupInternal(AuthData* auth_data,
380+
const char* group_id);
381+
382+
AuthError Auth::UseUserAccessGroup(const char* group_id) {
383+
if (!auth_data_) {
384+
return kAuthErrorUninitialized;
385+
}
386+
return UseUserAccessGroupInternal(auth_data_, group_id);
387+
}
388+
376389
} // namespace auth
377390
} // namespace firebase

auth/src/include/firebase/auth.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,23 @@ class Auth {
517517
/// not available on the current device.
518518
static Auth* GetAuth(App* app, InitResult* init_result_out = nullptr);
519519

520+
/// @brief Sets the user access group to use for data sharing for the app.
521+
///
522+
/// This method is only applicable to iOS. On other platforms, it's a no-op
523+
/// and will return `kAuthErrorNone`.
524+
///
525+
/// Sets the user access group to use for data sharing for the app. This
526+
/// should be used to share authentication data between apps that have the
527+
/// same access group.
528+
///
529+
/// @param[in] group_id The user access group to use. For example,
530+
/// `com.example.accessgroup`. Set to `nullptr` or an empty string to clear
531+
/// a previously set value and use the default keychain group.
532+
///
533+
/// @return Returns `kAuthErrorNone` on success, or an `AuthError` code if an
534+
/// error occurred.
535+
AuthError UseUserAccessGroup(const char* group_id);
536+
520537
private:
521538
/// @cond FIREBASE_APP_INTERNAL
522539
friend class ::firebase::App;

auth/src/ios/auth_ios.mm

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

611+
AuthError UseUserAccessGroupInternal(AuthData* auth_data,
612+
const char* group_id) {
613+
NSString* group_id_nsstring = nil;
614+
if (group_id != nullptr && strlen(group_id) > 0) {
615+
group_id_nsstring = [NSString stringWithUTF8String:group_id];
616+
}
617+
618+
NSError* ns_error = nil;
619+
[AuthImpl(auth_data) useUserAccessGroup:group_id_nsstring error:&ns_error];
620+
return AuthErrorFromNSError(ns_error);
621+
}
622+
611623
} // namespace auth
612624
} // namespace firebase

auth/src/stub/auth_stub.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "auth/src/include/firebase/auth_error.h"
16+
#include "auth/src/common.h" // For AuthData
17+
18+
namespace firebase {
19+
namespace auth {
20+
21+
// Stub implementation for UseUserAccessGroupInternal on non-iOS platforms.
22+
AuthError UseUserAccessGroupInternal(AuthData* auth_data,
23+
const char* group_id) {
24+
// This function is a no-op on non-iOS platforms.
25+
(void)auth_data;
26+
(void)group_id;
27+
return kAuthErrorNone;
28+
}
29+
30+
// Other stub functions that are not implemented on desktop/non-mobile
31+
// should also go here. For example, if there were other iOS or Android
32+
// specific internal functions called from auth.cc
33+
34+
} // namespace auth
35+
} // namespace firebase

0 commit comments

Comments
 (0)