Skip to content

feat(fac): Add custom TTL options for App Check - #1363

Merged
lahirumaramba merged 5 commits into
masterfrom
lm-fac-config-ttl
Jul 14, 2021
Merged

feat(fac): Add custom TTL options for App Check#1363
lahirumaramba merged 5 commits into
masterfrom
lm-fac-config-ttl

Conversation

@lahirumaramba

@lahirumarambalahirumaramba commented Jul 8, 2021

Copy link
Copy Markdown
Member
  • Add new AppCheckTokenOptions type
  • Change the public API createToken(...) to accept optional AppCheckTokenOptions
  • Add validations for custom ttl
  • Move transformMillisecondsToSecondsString() to utils
  • Add unit tests. (Integration tests will follow in a separate PR)
  • Add integration tests.

RELEASE NOTE: The createToken() API now supports configuring the TTL of the returned Firebase App Check Token.

@hiranya911hiranya911 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM with some suggestions.

Comment threadsrc/app-check/index.ts Outdated
}

/**
* Interface representing an App Check token options.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remove "an"?

Comment threadsrc/app-check/token-generator.ts Outdated
'AppCheckTokenOptions must be a non-null object.');
}
if (typeof options.ttlMillis !== 'undefined') {
if (!validator.isNumber(options.ttlMillis) || options.ttlMillis < 0) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Negative check is redundant due to the following check.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

I added the negative check here to specify that the ttl should be a non-negative duration in the error message. I agree though that it makes more sense to include/check that as part of the range validation below. Will update the code.

[THIRTY_MIN_IN_MS, THIRTY_MIN_IN_MS + 1, SEVEN_DAYS_IN_MS / 2, SEVEN_DAYS_IN_MS - 1, SEVEN_DAYS_IN_MS]
.forEach((ttlMillis) => {
it('should be fulfilled with a Firebase Custom JWT with a valid custom ttl' + JSON.stringify(ttlMillis), () => {
return tokenGenerator.createCustomToken(APP_ID, { ttlMillis })

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should probably decode the token and verify the expected TTL is set.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Good call. Thanks!

aud: FIREBASE_APP_CHECK_AUDIENCE,
exp: iat + ONE_HOUR_IN_SECONDS,
iat,
...customOptions,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just curious. Why is ttl separate from exp?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

According to go/fac-configurable-ttls we are not exposing the option to use exp at all to keep the interface simple. So in this case a custom ttl will override exp.

@lahirumaramba

Copy link
Copy Markdown
MemberAuthor

Thanks! Updated the code with PR fixes.

Adding @kevinthecheung to review the reference docs. Thank you!

@hiranya911hiranya911 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks. LGTM with a comment.

it('should be fulfilled with a Firebase Custom JWT with a valid custom ttl' + JSON.stringify(ttlMillis), () => {
return tokenGenerator.createCustomToken(APP_ID, { ttlMillis })
.should.eventually.be.a('string').and.not.be.empty;
[[THIRTY_MIN_IN_MS, '1800s'], [THIRTY_MIN_IN_MS + 1, '1800.001000000s'],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One entry per line for clarity:

[
[],
[],
...
]

@hiranya911hiranya911 removed their assignment Jul 9, 2021
Comment threadsrc/app-check/index.ts Outdated
Comment on lines +104 to +106
* The length of time measured in milliseconds starting from when the server
* mints the token for which the returned FAC token will be valid.
* This value must be in milliseconds and between 30 minutes and 7 days, inclusive.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
*Thelengthoftimemeasuredinmillisecondsstartingfromwhentheserver
*mintsthetokenforwhichthereturnedFACtokenwillbevalid.
*Thisvaluemustbeinmillisecondsandbetween30minutesand7days,inclusive.
*Thelengthoftime,inmilliseconds,forwhichtheAppChecktokenwill
*bevalid.Thisvaluemustbebetween30minutesand7days,inclusive.

@lahirumarambalahirumaramba changed the title Add custom TTL options for App Checkfeat(fac): Add custom TTL options for App CheckJul 14, 2021
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:stageStage a release candidaterelease-note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@lahirumaramba@hiranya911@kevinthecheung@weixifan