Skip to content

Commit

Permalink
fix(app-check): getToken(false) fix, listener unsubscribe is a function
Browse files Browse the repository at this point in the history
- previously sending false through would crash android
- forceRefresh currently ignored on iOS because of upstream issue causing native crash
- fix docs
  • Loading branch information
mikehardy committed Aug 18, 2021
1 parent 3ef3410 commit 8b42e20
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
14 changes: 9 additions & 5 deletions packages/app-check/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@

---

AppCheck description.
App Check works alongside other Firebase services to help protect your backend resources from abuse, such as billing fraud or phishing. With App Check, devices running your app will use an app or device attestation provider that attests to one or both of the following:

[> Learn More](https://firebase.google.com/products/app-check/)
- Requests originate from your authentic app
- Requests originate from an authentic, untampered device

This attestation is attached to every request your app makes to your Firebase backend resources.

[> Learn More](https://firebase.google.com/docs/app-check/)

## Installation

Expand All @@ -35,9 +40,8 @@ yarn add @react-native-firebase/app-check

## Documentation

- [Guides](#TODO)
- [Installation](#TODO)
- [Reference](#TODO)
- [Guides](https://rnfirebase.io/app-check/usage/)
- [Reference](https://rnfirebase.io/reference/app-check)

## License

Expand Down
15 changes: 14 additions & 1 deletion packages/app-check/e2e/appcheck.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,26 @@ describe('appCheck()', function () {
describe('getToken())', function () {
it('token fetch attempt should work', async function () {
// Our tests configure a debug provider with shared secret so we should get a valid token
const token = await firebase.appCheck().getToken(true);
const token = await firebase.appCheck().getToken();
token.should.not.equal('');
const decodedToken = jwt.decode(token);
decodedToken.aud[1].should.equal('projects/react-native-firebase-testing');
if (decodedToken.exp < Date.now()) {
Promise.reject('Token already expired');
}

// Force refresh should get a different token?
// TODO iOS tokens are stale because of https://github.com/firebase/firebase-ios-sdk/issues/8544
if (device.getPlatform() === 'android') {
const token2 = await firebase.appCheck().getToken(true);
token2.should.not.equal('');
const decodedToken2 = jwt.decode(token2);
decodedToken2.aud[1].should.equal('projects/react-native-firebase-testing');
if (decodedToken2.exp < Date.now()) {
Promise.reject('Token already expired');
}
(token === token2).should.be.false();
}
});
});
describe('activate())', function () {
Expand Down
3 changes: 2 additions & 1 deletion packages/app-check/ios/RNFBAppcheck/RNFBAppCheckModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ - (dispatch_queue_t)methodQueue {
: (RCTPromiseResolveBlock)resolve
: (RCTPromiseRejectBlock)reject) {
FIRAppCheck *appCheck = [FIRAppCheck appCheckWithApp:firebaseApp];
[appCheck tokenForcingRefresh:NO
[appCheck tokenForcingRefresh:NO // TODO Cannot use forceRefresh argument, if we send 'YES' in
// https://github.com/firebase/firebase-ios-sdk/issues/8544
completion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
if (error != nil) {
// Handle any errors if the token was not retrieved.
Expand Down
12 changes: 8 additions & 4 deletions packages/app-check/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,20 @@ class FirebaseAppCheckModule extends FirebaseModule {
}

getToken(forceRefresh) {
return this.native.getToken(forceRefresh);
if (!forceRefresh) {
return this.native.getToken(false);
} else {
return this.native.getToken(true);
}
}

onTokenChanged() {
// iOS does not provide any native listening feature
if (isIOS) {
return;
return () => {};
}
// TODO unimplemented on Android
return;
return () => {};
}
}

Expand All @@ -63,7 +67,7 @@ export default createModuleNamespace({
version,
namespace,
nativeModuleName,
nativeEvents: false, // TODO verify if this is interesting - token refresh listener perhaps?
nativeEvents: false, // TODO implement ['appcheck-token-changed'],
hasMultiAppSupport: true,
hasCustomUrlOrRegionSupport: false,
ModuleClass: FirebaseAppCheckModule,
Expand Down

1 comment on commit 8b42e20

@vercel
Copy link

@vercel vercel bot commented on 8b42e20 Aug 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.