Skip to content

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

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 5 commits into from
Jul 14, 2021

Conversation

lahirumaramba
Copy link
Member

@lahirumaramba lahirumaramba commented Jul 8, 2021

  • 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.

@lahirumaramba lahirumaramba force-pushed the lm-fac-config-ttl branch 3 times, most recently from 8d6197a to 89b759c Compare July 8, 2021 19:49
@lahirumaramba lahirumaramba requested a review from hiranya911 July 8, 2021 19:52
@lahirumaramba lahirumaramba requested a review from weixifan July 8, 2021 19:56
Copy link
Contributor

@hiranya911 hiranya911 left a comment

Choose a reason for hiding this comment

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

LGTM with some suggestions.

@@ -95,6 +96,18 @@ export namespace appCheck {
ttlMillis: number;
}

/**
* Interface representing an App Check token options.
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove "an"?

'AppCheckTokenOptions must be a non-null object.');
}
if (typeof options.ttlMillis !== 'undefined') {
if (!validator.isNumber(options.ttlMillis) || options.ttlMillis < 0) {
Copy link
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
Member Author

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
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
Member Author

Choose a reason for hiding this comment

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

Good call. Thanks!

@@ -83,6 +93,7 @@ export class AppCheckTokenGenerator {
aud: FIREBASE_APP_CHECK_AUDIENCE,
exp: iat + ONE_HOUR_IN_SECONDS,
iat,
...customOptions,
Copy link
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
Member Author

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
Member Author

Thanks! Updated the code with PR fixes.

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

@lahirumaramba lahirumaramba added the release:stage Stage a release candidate label Jul 9, 2021
Copy link
Contributor

@hiranya911 hiranya911 left a comment

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
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:

[
  [],
  [],
  ...
]

@hiranya911 hiranya911 removed their assignment Jul 9, 2021
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
Contributor

Choose a reason for hiding this comment

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

Suggested change
* 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.
* The length of time, in milliseconds, for which the App Check token will
* be valid. This value must be between 30 minutes and 7 days, inclusive.

@lahirumaramba lahirumaramba changed the title Add custom TTL options for App Check feat(fac): Add custom TTL options for App Check Jul 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:stage Stage a release candidate release-note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants