Skip to content
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

Managed Identity: ResponseHandler can now accept an ISO 8601 date in the ServerAuthorizationTokenResponse's "expires_in" value #7529

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from

Conversation

Robbie-Microsoft
Copy link
Collaborator

Fixes #7393

@github-actions github-actions bot added documentation Related to documentation. msal-common Related to msal-common package labels Jan 24, 2025
* @param returnValueIfUndefined - The value to return if the tokenExpirationTimestamp is undefined.
* @returns The number of seconds until the token expires, or the returnValueIfUndefined if the tokenExpirationTimestamp is undefined.
*/
private getSecondsUntilTokenExpires(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you move this to TimeUtils as a pure function?

private getSecondsUntilTokenExpires(
reqTimestamp: number,
tokenExpirationTimestamp: number | string | undefined,
returnValueIfUndefined: number | undefined
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need this as a param if we only ever pass 0?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added that in there because I originally coded this PR to also handle (future-proof?) ext_expires_in, refresh_in, and refresh_token_expires_in. However, I'm not sure if those are necessary, and if they are, maybe it should be another PR. I've posed this question to Bogdan and Gladwin and am awaiting feedback.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think even for those the fallback would be 0, no?

Copy link
Collaborator Author

@Robbie-Microsoft Robbie-Microsoft Jan 25, 2025

Choose a reason for hiding this comment

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

refreshOnSeconds (derived from refresh_in) can be passed as undefined to CacheHelpers.createAccessTokenEntity, and rtExpiresOn (derived from refresh_token_expires_in) can be passed as undefined to CacheHelpers.createRefreshTokenEntity.

reqTimestamp: number,
tokenExpirationTimestamp: number | string | undefined,
returnValueIfUndefined: number | undefined
): number | undefined {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
): number | undefined {
): number {

? Math.floor(
new Date(tokenExpirationTimestamp).getTime() / 1000
) - reqTimestamp
: parseInt(tokenExpirationTimestamp, 10);
Copy link
Collaborator

@tnorling tnorling Jan 24, 2025

Choose a reason for hiding this comment

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

minus reqTimestamp?

Copy link
Collaborator Author

@Robbie-Microsoft Robbie-Microsoft Jan 24, 2025

Choose a reason for hiding this comment

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

Sorry, I don't understand what you're asking.

But to explain this: If the expires_in value is an ISO 8601 string, it's in the future - say 3599 seconds (an hour minus 1 second). This equation will subtract the current time, as of the moment of the request, from the ISO 8601 string and end up with what's left (3599 seconds).

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm confused by the fact that we seem to be mixing and matching expiresAT with expiresIN values. It's not clear what these variables represent. We have a variable called tokenExpirationTimestamp which suggests it represents expiresAT and would need a subtraction of reqTimestamp in order to obtain expiresIN but we're only doing that subtraction if the value is ISO8601 format otherwise treating it as if it's already expiresIN.

This might be poor API design on the server side if they're sending back values that represent different things on the same parameter but if that's the case we should write our logic in way that clears up this confusion. e.g.

const expiresIn = isIso8601(serverResponse.expires_in) ? getExpiresInFromExpiresAt(serverResponse.expires_in, reqTimestamp) : serverResponse.expires_in || 0;
const expiresAt = isIso8601(serverResponse.expires_in) ? serverResponse.expiresIn : getExpiresAtFromExpiresIn(serverResponse.expires_in, reqTimestamp);

) - reqTimestamp
: parseInt(tokenExpirationTimestamp, 10);
} else {
return tokenExpirationTimestamp || returnValueIfUndefined;
Copy link
Collaborator

Choose a reason for hiding this comment

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

minus reqTimestamp?

@github-actions github-actions bot added the msal-browser Related to msal-browser package label Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Related to documentation. msal-browser Related to msal-browser package msal-common Related to msal-common package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Fix Date-Time Parsing in Token Expiration Calculation for Multiple Formats
2 participants