-
Notifications
You must be signed in to change notification settings - Fork 938
Add public token API to AppCheck #5033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
44b8c46
Add public token API
hsubox76 b95bc35
Fix lint
hsubox76 3b0ce02
Restore interop type
hsubox76 b8caa9d
Update packages/app-check-types/index.d.ts
hsubox76 d7f0635
Fix onErrors
hsubox76 d687bda
Bind next/error functions
hsubox76 077240f
Merge branch 'ch-appcheck-tokenapi' of https://github.com/firebase/fi…
hsubox76 df666f0
Address PR comments
hsubox76 99f0293
Add changeset
hsubox76 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,16 +16,26 @@ | |
*/ | ||
import '../test/setup'; | ||
import { expect } from 'chai'; | ||
import { stub } from 'sinon'; | ||
import { activate, setTokenAutoRefreshEnabled } from './api'; | ||
import { stub, spy } from 'sinon'; | ||
import { | ||
activate, | ||
setTokenAutoRefreshEnabled, | ||
getToken, | ||
onTokenChanged | ||
} from './api'; | ||
import { | ||
FAKE_SITE_KEY, | ||
getFakeApp, | ||
getFakeCustomTokenProvider | ||
getFakeCustomTokenProvider, | ||
getFakePlatformLoggingProvider, | ||
removegreCAPTCHAScriptsOnPage | ||
} from '../test/util'; | ||
import { getState } from './state'; | ||
import { clearState, getState } from './state'; | ||
import * as reCAPTCHA from './recaptcha'; | ||
import { FirebaseApp } from '@firebase/app-types'; | ||
import * as internalApi from './internal-api'; | ||
import * as client from './client'; | ||
import * as storage from './storage'; | ||
|
||
describe('api', () => { | ||
describe('activate()', () => { | ||
|
@@ -86,4 +96,137 @@ describe('api', () => { | |
expect(getState(app).isTokenAutoRefreshEnabled).to.equal(true); | ||
}); | ||
}); | ||
describe('getToken()', () => { | ||
it('getToken() calls the internal getToken() function', async () => { | ||
const app = getFakeApp({ automaticDataCollectionEnabled: true }); | ||
const fakePlatformLoggingProvider = getFakePlatformLoggingProvider(); | ||
const internalGetToken = stub(internalApi, 'getToken').resolves({ | ||
token: 'a-token-string' | ||
}); | ||
await getToken(app, fakePlatformLoggingProvider, true); | ||
expect(internalGetToken).to.be.calledWith( | ||
app, | ||
fakePlatformLoggingProvider, | ||
true | ||
); | ||
}); | ||
it('getToken() throws errors returned with token', async () => { | ||
const app = getFakeApp({ automaticDataCollectionEnabled: true }); | ||
const fakePlatformLoggingProvider = getFakePlatformLoggingProvider(); | ||
stub(internalApi, 'getToken').resolves({ | ||
token: 'a-token-string', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be the dummy token in this case, right? Can you add a comment here to clarify? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clarified. |
||
error: Error('there was an error') | ||
}); | ||
await expect( | ||
getToken(app, fakePlatformLoggingProvider, true) | ||
).to.be.rejectedWith('there was an error'); | ||
}); | ||
}); | ||
describe('onTokenChanged()', () => { | ||
afterEach(() => { | ||
clearState(); | ||
removegreCAPTCHAScriptsOnPage(); | ||
}); | ||
it('Listeners work when using top-level parameters pattern', async () => { | ||
const app = getFakeApp({ automaticDataCollectionEnabled: true }); | ||
activate(app, FAKE_SITE_KEY, true); | ||
const fakePlatformLoggingProvider = getFakePlatformLoggingProvider(); | ||
const fakeRecaptchaToken = 'fake-recaptcha-token'; | ||
const fakeRecaptchaAppCheckToken = { | ||
token: 'fake-recaptcha-app-check-token', | ||
expireTimeMillis: 123, | ||
issuedAtTimeMillis: 0 | ||
}; | ||
stub(reCAPTCHA, 'getToken').returns(Promise.resolve(fakeRecaptchaToken)); | ||
stub(client, 'exchangeToken').returns( | ||
Promise.resolve(fakeRecaptchaAppCheckToken) | ||
); | ||
stub(storage, 'writeTokenToStorage').returns(Promise.resolve(undefined)); | ||
|
||
const listener1 = (): void => { | ||
throw new Error(); | ||
}; | ||
const listener2 = spy(); | ||
|
||
const errorFn1 = spy(); | ||
const errorFn2 = spy(); | ||
|
||
const unSubscribe1 = onTokenChanged( | ||
app, | ||
fakePlatformLoggingProvider, | ||
listener1, | ||
errorFn1 | ||
); | ||
const unSubscribe2 = onTokenChanged( | ||
app, | ||
fakePlatformLoggingProvider, | ||
listener2, | ||
errorFn2 | ||
); | ||
|
||
expect(getState(app).tokenListeners.length).to.equal(2); | ||
|
||
await getToken(app, fakePlatformLoggingProvider); | ||
|
||
expect(listener2).to.be.calledWith({ | ||
token: fakeRecaptchaAppCheckToken.token | ||
}); | ||
expect(errorFn1).to.be.calledOnce; | ||
Feiyang1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(errorFn2).to.not.be.called; | ||
unSubscribe1(); | ||
unSubscribe2(); | ||
expect(getState(app).tokenListeners.length).to.equal(0); | ||
}); | ||
|
||
it('Listeners work when using Observer pattern', async () => { | ||
const app = getFakeApp({ automaticDataCollectionEnabled: true }); | ||
activate(app, FAKE_SITE_KEY, true); | ||
const fakePlatformLoggingProvider = getFakePlatformLoggingProvider(); | ||
const fakeRecaptchaToken = 'fake-recaptcha-token'; | ||
const fakeRecaptchaAppCheckToken = { | ||
token: 'fake-recaptcha-app-check-token', | ||
expireTimeMillis: 123, | ||
issuedAtTimeMillis: 0 | ||
}; | ||
stub(reCAPTCHA, 'getToken').returns(Promise.resolve(fakeRecaptchaToken)); | ||
stub(client, 'exchangeToken').returns( | ||
Promise.resolve(fakeRecaptchaAppCheckToken) | ||
); | ||
stub(storage, 'writeTokenToStorage').returns(Promise.resolve(undefined)); | ||
|
||
const listener1 = (): void => { | ||
throw new Error(); | ||
}; | ||
const listener2 = spy(); | ||
|
||
const errorFn1 = spy(); | ||
const errorFn2 = spy(); | ||
|
||
/** | ||
* Reverse the order of adding the failed and successful handler, for extra | ||
* testing. | ||
*/ | ||
const unSubscribe2 = onTokenChanged(app, fakePlatformLoggingProvider, { | ||
next: listener2, | ||
error: errorFn2 | ||
}); | ||
const unSubscribe1 = onTokenChanged(app, fakePlatformLoggingProvider, { | ||
next: listener1, | ||
error: errorFn1 | ||
}); | ||
|
||
expect(getState(app).tokenListeners.length).to.equal(2); | ||
|
||
await getToken(app, fakePlatformLoggingProvider); | ||
|
||
expect(listener2).to.be.calledWith({ | ||
token: fakeRecaptchaAppCheckToken.token | ||
}); | ||
expect(errorFn1).to.be.calledOnce; | ||
expect(errorFn2).to.not.be.called; | ||
unSubscribe1(); | ||
unSubscribe2(); | ||
expect(getState(app).tokenListeners.length).to.equal(0); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.