Skip to content

chore: Update FAC to beta #1263

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 1 commit into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/app-check/app-check-api-client-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import * as validator from '../utils/validator';
import AppCheckToken = appCheck.AppCheckToken;

// App Check backend constants
const FIREBASE_APP_CHECK_V1_API_URL_FORMAT = 'https://firebaseappcheck.googleapis.com/v1alpha/projects/{projectId}/apps/{appId}:exchangeCustomToken';
const FIREBASE_APP_CHECK_V1_API_URL_FORMAT = 'https://firebaseappcheck.googleapis.com/v1beta/projects/{projectId}/apps/{appId}:exchangeCustomToken';

const FIREBASE_APP_CHECK_CONFIG_HEADERS = {
'X-Firebase-Client': `fire-admin-node/${utils.getSdkVersion()}`
Expand Down Expand Up @@ -147,9 +147,9 @@ export class AppCheckApiClient {
*/
private toAppCheckToken(resp: HttpResponse): AppCheckToken {
const token = resp.data.attestationToken;
// `timeToLive` is a string with the suffix "s" preceded by the number of seconds,
// `ttl` is a string with the suffix "s" preceded by the number of seconds,

Choose a reason for hiding this comment

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

Interesting -- unrelated to this review, the ttl is actually a textual representation of a Duration proto as per AIP-142 and the Duration documentation. The only downside is that parsing it via Duration might cost us an additional dependency.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah. I think for Javascript (Node.js) we can parse the JSON by referring to the JSON Mapping section in the proto. Can confirm that the JS SDK follows the same approach.

// with nanoseconds expressed as fractional seconds.
const ttlMillis = this.stringToMilliseconds(resp.data.timeToLive);
const ttlMillis = this.stringToMilliseconds(resp.data.ttl);
return {
token,
ttlMillis
Expand All @@ -169,7 +169,7 @@ export class AppCheckApiClient {
private stringToMilliseconds(duration: string): number {
if (!validator.isNonEmptyString(duration) || !duration.endsWith('s')) {
throw new FirebaseAppCheckError(
'invalid-argument', '`timeToLive` must be a valid duration string with the suffix `s`.');
'invalid-argument', '`ttl` must be a valid duration string with the suffix `s`.');
}
const seconds = duration.slice(0, -1);
return Math.floor(Number(seconds) * 1000);
Expand Down
2 changes: 1 addition & 1 deletion src/app-check/token-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { HttpError } from '../utils/api-request';
const ONE_HOUR_IN_SECONDS = 60 * 60;

// Audience to use for Firebase App Check Custom tokens
const FIREBASE_APP_CHECK_AUDIENCE = 'https://firebaseappcheck.googleapis.com/google.firebase.appcheck.v1alpha.TokenExchangeService';
const FIREBASE_APP_CHECK_AUDIENCE = 'https://firebaseappcheck.googleapis.com/google.firebase.appcheck.v1beta.TokenExchangeService';

/**
* Class for generating Firebase App Check tokens.
Expand Down
2 changes: 1 addition & 1 deletion src/app-check/token-verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
import DecodedAppCheckToken = appCheck.DecodedAppCheckToken;

const APP_CHECK_ISSUER = 'https://firebaseappcheck.googleapis.com/';
const JWKS_URL = 'https://firebaseappcheck.googleapis.com/v1alpha/jwks';
const JWKS_URL = 'https://firebaseappcheck.googleapis.com/v1beta/jwks';

/**
* Class for verifying Firebase App Check tokens.
Expand Down
30 changes: 30 additions & 0 deletions test/integration/app-check.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,34 @@ describe('admin.appCheck', () => {
});
});
});

describe('verifyToken', () => {
let validToken: admin.appCheck.AppCheckToken;

before(async () => {
if (!appId) {
return;
}
// obtain a valid app check token
validToken = await admin.appCheck().createToken(appId as string);
});

it('should succeed with a decoded verifed token response', function() {
if (!appId) {
this.skip();
}
return admin.appCheck().verifyToken(validToken.token)
.then((verifedToken) => {
expect(verifedToken).to.have.keys(['token', 'appId']);
expect(verifedToken.token).to.have.keys(['iss', 'sub', 'aud', 'exp', 'iat', 'app_id']);
expect(verifedToken.token.app_id).to.be.a('string').and.equals(appId);
});
});

it('should propagate API errors', () => {
// rejects with invalid-argument when the token is invalid
return admin.appCheck().verifyToken('invalid-token')
.should.eventually.be.rejected.and.have.property('code', 'app-check/invalid-argument');
});
});
});
14 changes: 7 additions & 7 deletions test/unit/app-check/app-check-api-client-internal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('AppCheckApiClient', () => {

const TEST_RESPONSE = {
attestationToken: 'token',
timeToLive: '3s'
ttl: '3s'
};

const mockOptions = {
Expand Down Expand Up @@ -182,15 +182,15 @@ describe('AppCheckApiClient', () => {

['', 'abc', '3s2', 'sssa', '3.000000001', '3.2', null, NaN, true, [], {}, 100, 1.2, -200, -2.4]
.forEach((invalidDuration) => {
it(`should throw if the returned timeToLive duration is: ${invalidDuration}`, () => {
it(`should throw if the returned ttl duration is: ${invalidDuration}`, () => {
const response = deepCopy(TEST_RESPONSE);
(response as any).timeToLive = invalidDuration;
(response as any).ttl = invalidDuration;
const stub = sinon
.stub(HttpClient.prototype, 'send')
.resolves(utils.responseFrom(response, 200));
stubs.push(stub);
const expected = new FirebaseAppCheckError(
'invalid-argument', '`timeToLive` must be a valid duration string with the suffix `s`.');
'invalid-argument', '`ttl` must be a valid duration string with the suffix `s`.');
return apiClient.exchangeToken(TEST_TOKEN_TO_EXCHANGE, APP_ID)
.should.eventually.be.rejected.and.deep.include(expected);
});
Expand All @@ -207,7 +207,7 @@ describe('AppCheckApiClient', () => {
expect(resp.ttlMillis).to.deep.equal(3000);
expect(stub).to.have.been.calledOnce.and.calledWith({
method: 'POST',
url: `https://firebaseappcheck.googleapis.com/v1alpha/projects/test-project/apps/${APP_ID}:exchangeCustomToken`,
url: `https://firebaseappcheck.googleapis.com/v1beta/projects/test-project/apps/${APP_ID}:exchangeCustomToken`,
headers: EXPECTED_HEADERS,
data: { customToken: TEST_TOKEN_TO_EXCHANGE }
});
Expand All @@ -219,10 +219,10 @@ describe('AppCheckApiClient', () => {
// 3 seconds with 0 nanoseconds expressed as "3s"
// 3 seconds and 1 nanosecond expressed as "3.000000001s"
// 3 seconds and 1 microsecond expressed as "3.000001s"
it(`should resolve with ttlMillis as ${ttlMillis} when timeToLive
it(`should resolve with ttlMillis as ${ttlMillis} when ttl
from server is: ${ttlString}`, () => {
const response = deepCopy(TEST_RESPONSE);
(response as any).timeToLive = ttlString;
(response as any).ttl = ttlString;
const stub = sinon
.stub(HttpClient.prototype, 'send')
.resolves(utils.responseFrom(response, 200));
Expand Down
2 changes: 1 addition & 1 deletion test/unit/app-check/token-generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const expect = chai.expect;

const ALGORITHM = 'RS256';
const ONE_HOUR_IN_SECONDS = 60 * 60;
const FIREBASE_APP_CHECK_AUDIENCE = 'https://firebaseappcheck.googleapis.com/google.firebase.appcheck.v1alpha.TokenExchangeService';
const FIREBASE_APP_CHECK_AUDIENCE = 'https://firebaseappcheck.googleapis.com/google.firebase.appcheck.v1beta.TokenExchangeService';

/**
* Verifies a token is signed with the private key corresponding to the provided public key.
Expand Down