Skip to content

Commit

Permalink
adding getLimitedUseToken function
Browse files Browse the repository at this point in the history
  • Loading branch information
apetta committed Oct 26, 2023
1 parent b85f5a4 commit 1f53199
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
34 changes: 34 additions & 0 deletions packages/app-check/ios/RNFBAppCheck/RNFBAppCheckModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions packages/app-check/ios/RNFBAppCheck/RNFBAppCheckProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions packages/app-check/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import version from './version';
export {
addTokenListener,
getToken,
getLimitedUseToken,
initializeAppCheck,
setTokenAutoRefreshEnabled,
} from './modular/index';
Expand Down Expand Up @@ -141,6 +142,10 @@ class FirebaseAppCheckModule extends FirebaseModule {
}
}

getLimitedUseToken() {
return this.native.getLimitedUseToken();
}

_parseListener(listenerOrObserver) {
return typeof listenerOrObserver === 'object'
? listenerOrObserver.next.bind(listenerOrObserver)
Expand Down
10 changes: 10 additions & 0 deletions packages/app-check/lib/modular/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppCheckTokenResult>}
*/
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.
Expand Down

0 comments on commit 1f53199

Please sign in to comment.