Skip to content

Add instance discovery metadata to token cache#149

Closed
sangonzal wants to merge 4 commits intodevfrom
sagonzal/instanceDiscoveryMetadataCaching
Closed

Add instance discovery metadata to token cache#149
sangonzal wants to merge 4 commits intodevfrom
sagonzal/instanceDiscoveryMetadataCaching

Conversation

@sangonzal
Copy link
Copy Markdown
Contributor

In the case where a client application object is create in a new process, and they are using a token cache that has been previously written to disk, then they can use the instance discovery metadata from the token cache instead of having to make a network call.

if (instanceDiscoveryResponse.metadata() != null) {
for (InstanceDiscoveryMetadataEntry entry : instanceDiscoveryResponse.metadata()) {

// expiry time of 96 hours
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What should expiry date be?

sendInstanceDiscoveryRequest(authorityUrl, msalRequest, serviceBundle);

if (validateAuthority) {
validate(instanceDiscoveryResponse);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[Aside] The way you do validate is different from how .net does it so it might be a good idea to align here... Can you give me an example where the instance discovery endpoint would fail validation ? I manually tested using a "bogus authority" and the instance discovery endpoint fails with a 500 error I believe.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We are not validating instance discovery endpoint here, only validating that the response contains "tenant_discovery_endpoint". Authority validation happens here: https://github.com/AzureAD/microsoft-authentication-library-for-java/blob/dev/src/main/java/com/microsoft/aad/msal4j/Authority.java#L88

What validation happens in .net? Agreed that we should align here.

protected static final int MIN_ACCESS_TOKEN_EXPIRE_IN_SEC = 5 * 60;

transient private ReadWriteLock lock = new ReentrantReadWriteLock();
transient ReadWriteLock lock = new ReentrantReadWriteLock();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Expose a getter instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, will fix.


URL authority = new URL(app.authority());

PowerMock.mockStaticPartial(AadInstanceDiscovery.class, "sendInstanceDiscoveryRequest");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[Aside] If you use an interface and either make your service a singleton or just pass the instance around, the code would be easier to test without tools for "untestable code"

"client_id": "my_client_id"
}
},
"InstanceDiscoveryMetadata": {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

MSAL .net should be ok with this, it is similar to the "unknown entity" above. Otherwise, the cache structure looks good to me, but please open an API review so that this is documented for other teams / new MSALs to come.

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.

+1 : to get this documented

Copy link
Copy Markdown
Member

@bgavrilMS bgavrilMS left a comment

Choose a reason for hiding this comment

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

Generally looks good, please document the cache changes and consider refactoring the instance metadata service to be non-static.

@sangonzal sangonzal closed this Jan 9, 2020
@sangonzal sangonzal deleted the sagonzal/instanceDiscoveryMetadataCaching branch February 12, 2021 02:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants