From 1f5319978e2ae63e7e315fe0d298b01d2ad0c24a Mon Sep 17 00:00:00 2001 From: Akshay Petta Date: Thu, 26 Oct 2023 20:49:28 +0100 Subject: [PATCH] adding getLimitedUseToken function --- .../ReactNativeFirebaseAppCheckModule.java | 28 +++++++++++++++ .../ios/RNFBAppCheck/RNFBAppCheckModule.m | 34 +++++++++++++++++++ .../ios/RNFBAppCheck/RNFBAppCheckProvider.m | 6 ++++ packages/app-check/lib/index.js | 5 +++ packages/app-check/lib/modular/index.js | 10 ++++++ 5 files changed, 83 insertions(+) diff --git a/packages/app-check/android/src/main/java/io/invertase/firebase/appcheck/ReactNativeFirebaseAppCheckModule.java b/packages/app-check/android/src/main/java/io/invertase/firebase/appcheck/ReactNativeFirebaseAppCheckModule.java index 15e918e7c58..37ebb2aa5f7 100644 --- a/packages/app-check/android/src/main/java/io/invertase/firebase/appcheck/ReactNativeFirebaseAppCheckModule.java +++ b/packages/app-check/android/src/main/java/io/invertase/firebase/appcheck/ReactNativeFirebaseAppCheckModule.java @@ -206,6 +206,34 @@ public void getToken(String appName, boolean forceRefresh, Promise promise) { }); } + @ReactMethod + public void getLimitedUseToken(String appName, Promise promise) { + Log.d(LOGTAG, "getLimitedUseToken appName: " + appName); + FirebaseApp firebaseApp = FirebaseApp.getInstance(appName); + + Tasks.call( + getExecutor(), + () -> { + return Tasks.await(FirebaseAppCheck.getInstance(firebaseApp).getLimitedUseAppCheckToken()); + }) + .addOnCompleteListener( + getExecutor(), + (task) -> { + if (task.isSuccessful()) { + WritableMap tokenResultMap = Arguments.createMap(); + tokenResultMap.putString("token", task.getResult().getToken()); + promise.resolve(tokenResultMap); + } else { + Log.e( + LOGTAG, + "Unknown error while fetching limited-use AppCheck token " + + task.getException().getMessage()); + rejectPromiseWithCodeAndMessage( + promise, "token-error", task.getException().getMessage()); + } + }); + } + /** Add a new token change listener - if one doesn't exist already */ @ReactMethod public void addAppCheckListener(final String appName) { diff --git a/packages/app-check/ios/RNFBAppCheck/RNFBAppCheckModule.m b/packages/app-check/ios/RNFBAppCheck/RNFBAppCheckModule.m index f962a7cb697..766327fdb87 100644 --- a/packages/app-check/ios/RNFBAppCheck/RNFBAppCheckModule.m +++ b/packages/app-check/ios/RNFBAppCheck/RNFBAppCheckModule.m @@ -134,4 +134,38 @@ + (instancetype)sharedInstance { }]; } +RCT_EXPORT_METHOD(getLimitedUseToken + : (FIRApp *)firebaseApp + : (RCTPromiseResolveBlock)resolve + : (RCTPromiseRejectBlock)reject) { + FIRAppCheck *appCheck = [FIRAppCheck appCheckWithApp:firebaseApp]; + DLog(@"appName %@", firebaseApp.name); + [appCheck + limitedUseTokenWithCompletion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) { + if (error != nil) { + // Handle any errors if the token was not retrieved. + DLog(@"RNFBAppCheck - getLimitedUseToken - Unable to retrieve App Check token: %@", error); + [RNFBSharedUtils rejectPromiseWithUserInfo:reject + userInfo:(NSMutableDictionary *)@{ + @"code" : @"token-error", + @"message" : [error localizedDescription], + }]; + return; + } + if (token == nil) { + DLog(@"RNFBAppCheck - getLimitedUseToken - Unable to retrieve App Check token."); + [RNFBSharedUtils rejectPromiseWithUserInfo:reject + userInfo:(NSMutableDictionary *)@{ + @"code" : @"token-null", + @"message" : @"no token fetched", + }]; + return; + } + + NSMutableDictionary *tokenResultDictionary = [NSMutableDictionary new]; + tokenResultDictionary[@"token"] = token.token; + resolve(tokenResultDictionary); + }]; +} + @end diff --git a/packages/app-check/ios/RNFBAppCheck/RNFBAppCheckProvider.m b/packages/app-check/ios/RNFBAppCheck/RNFBAppCheckProvider.m index f68f4ca1316..0f0a77b6ee6 100644 --- a/packages/app-check/ios/RNFBAppCheck/RNFBAppCheckProvider.m +++ b/packages/app-check/ios/RNFBAppCheck/RNFBAppCheckProvider.m @@ -80,4 +80,10 @@ - (void)getTokenWithCompletion:(nonnull void (^)(FIRAppCheckToken *_Nullable, [self.delegateProvider getTokenWithCompletion:handler]; } +- (void)getLimitedUseTokenWithCompletion: + (nonnull void (^)(FIRAppCheckToken *_Nullable, NSError *_Nullable))handler { + DLog(@"proxying directly to FIRAppCheck for limitedUse token..."); + [[FIRAppCheck appCheck] limitedUseTokenWithCompletion:handler]; +} + @end diff --git a/packages/app-check/lib/index.js b/packages/app-check/lib/index.js index b68734ad6a0..cecb4153f80 100644 --- a/packages/app-check/lib/index.js +++ b/packages/app-check/lib/index.js @@ -29,6 +29,7 @@ import version from './version'; export { addTokenListener, getToken, + getLimitedUseToken, initializeAppCheck, setTokenAutoRefreshEnabled, } from './modular/index'; @@ -141,6 +142,10 @@ class FirebaseAppCheckModule extends FirebaseModule { } } + getLimitedUseToken() { + return this.native.getLimitedUseToken(); + } + _parseListener(listenerOrObserver) { return typeof listenerOrObserver === 'object' ? listenerOrObserver.next.bind(listenerOrObserver) diff --git a/packages/app-check/lib/modular/index.js b/packages/app-check/lib/modular/index.js index 3da292967f3..55ffd141a79 100644 --- a/packages/app-check/lib/modular/index.js +++ b/packages/app-check/lib/modular/index.js @@ -45,6 +45,16 @@ export function getToken(appCheckInstance, forceRefresh) { return appCheckInstance.app.appCheck().getToken(forceRefresh); } +/** + * Get a limited-use (consumable) App Check token. + * For use with server calls to firebase functions or custom backends using firebase admin SDK. + * @param appCheckInstance - AppCheck + * @returns {Promise} + */ +export function getLimitedUseToken(appCheckInstance) { + return appCheckInstance.app.appCheck().getLimitedUseToken(); +} + /** * Registers a listener to changes in the token state. * There can be more than one listener registered at the same time for one or more App Check instances.