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

[msal-browser][msal-common] Instance Aware Authentication (updated) #1804

Merged
merged 27 commits into from
Dec 3, 2020

Conversation

pkanher617
Copy link
Contributor

This PR implements the instance_aware feature for msal-browser. Specifically, it adds the authority object to the response and handles all parameters which come back in the fragment.

It also adds TokenResponse to the browser exports (as per PR #1551).

Replaces PR #1584.

@github-actions github-actions bot added msal-browser Related to msal-browser package msal-common Related to msal-common package samples Related to the samples apps for the library. labels Jun 19, 2020
@pkanher617 pkanher617 changed the base branch from logout-request to dev June 24, 2020 17:25
@github-actions
Copy link
Contributor

github-actions bot commented Jul 9, 2020

This PR has not seen activity in 14 days. It may be closed if it remains stale.

@github-actions github-actions bot added the no-pr-activity PR has been inactive for 14 days label Jul 9, 2020
@jasonnutter
Copy link
Contributor

@pkanher617 Status on this PR?

@github-actions github-actions bot removed the no-pr-activity PR has been inactive for 14 days label Aug 25, 2020
@pkanher617
Copy link
Contributor Author

@pkanher617 Status on this PR?

Need to update this to latest dev and follow up with proper caching schema. Will update in the next week.

@github-actions
Copy link
Contributor

This PR has not seen activity in 14 days. It may be closed if it remains stale.

@github-actions github-actions bot added the no-pr-activity PR has been inactive for 14 days label Sep 11, 2020
@github-actions github-actions bot closed this Sep 18, 2020
@tnorling tnorling reopened this Sep 18, 2020
@tnorling tnorling added work-in-progress Issue or PR is not finished. and removed no-pr-activity PR has been inactive for 14 days labels Sep 18, 2020
const loginRequest = {
scopes: ["User.Read"],
extraQueryParameters: {
"instance_aware": "true"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this need its own sample? Can we write a short doc instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would prefer to have a sample that shows how to use the flow and the graph endpoints correctly. Happy to revisit if we have a lot of pushback on this.

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 we should write a doc (or update an existing one) for this regardless. The sample may or may not become redundant at that point

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will definitely update the docs to add the instance_aware flow

Copy link
Collaborator

@tnorling tnorling left a comment

Choose a reason for hiding this comment

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

Generally implementation looks ok, a few comments and would like to see unit tests. Also curious if the msal-common additions are type safe, given stricter type checking is imminent.

@pkanher617
Copy link
Contributor Author

pkanher617 commented Nov 25, 2020

Generally implementation looks ok, a few comments and would like to see unit tests.

Unit tests are updated, and I am adding more for the scenarios around instance_aware. Also going to add e2e tests if I can.

Also curious if the msal-common additions are type safe, given stricter type checking is imminent.

Would like to merge the strictNullChecks PR before I fully confirm this, but I think we should be fine.

@github-actions github-actions bot added the msal-react Related to @azure/msal-react label Nov 25, 2020
@coveralls
Copy link

coveralls commented Nov 25, 2020

Coverage Status

Coverage increased (+0.8%) to 82.312% when pulling f6e5e47 on instance-aware-2.0 into 7f3a24f on dev.


// Get cached items
const nonceKey = this.browserStorage.generateNonceKey(requestState);
const cachedNonce = this.browserStorage.getTemporaryCache(nonceKey);

// Assign code to request
this.authCodeRequest.code = authCode;
this.authCodeRequest.code = authCodeResponse.code;
Copy link
Member

Choose a reason for hiding this comment

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

nit: Isn't the name confusing? I am assuming here authCodeResponse is from the fetch-auth-code request and authcodeRequest is the auth call for token?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes that's correct. If you have suggestions please suggest, but I think it is clear enough. The authCodeRequest is a field and the response is not.


protected async updateTokenEndpointAuthority(cloudInstanceHostname: string, authority: Authority, networkModule: INetworkModule): Promise<void> {
const cloudInstanceAuthorityUri = `https://${cloudInstanceHostname}/${authority.tenant}/`;
if (cloudInstanceAuthorityUri !== authority.canonicalAuthority) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we check aliases here? eg: login.windows.net is the same as login.microsoftonline.com and should not reinstantiate the authority instance in that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

this.browserCrypto = browserCrypto;
}

/**
* Redirects window to given URL.
* @param urlNavigate
*/
initiateAuthRequest(requestUrl: string, authCodeRequest: AuthorizationCodeRequest, redirectTimeout: number, redirectStartPage?: string): Promise<void> {
initiateAuthRequest(requestUrl: string, authCodeRequest: AuthorizationCodeRequest, redirectTimeout?: number, redirectStartPage?: string): Promise<void> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is redirectTimeout set to optional? FYI in #2669, I also made redirectStartPage required, since it will always be passed: https://github.com/AzureAD/microsoft-authentication-library-for-js/pull/2669/files#diff-d592fb8a2a52b3d36d8625f2210c27b8360c6d1f0cff4fe06494d1baf77ac593

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, is it because it now implements InteractionHandler?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that's correct. The interface implementation does not include redirectTimeout.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we remove the initiateAuthRequest abstract from InteractionHandler so that we dont have to make these optional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would like to keep the abstract function as a constraint for subclasses implementing this. I understand that there needs to be a compiler level way to ensure that the redirectTimeout and redirectStartPage are passed, but for now can we ensure this by adding code errors?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I personally don't like making a param optional but then throwing if it's not provided. Would prefer either making this its own function that doesn't implement the abstract or getting rid of the abstract altogether.

Copy link
Contributor Author

@pkanher617 pkanher617 Dec 2, 2020

Choose a reason for hiding this comment

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

I personally think it's worse design to take out the abstract functionality than to check if an optional parameter is provided or not. Let me see if I can find an alternative, but in the meantime @tnorling can you approve the PR so I can merge as soon as it is ready?

Copy link
Contributor

Choose a reason for hiding this comment

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

I also don't like the idea of throwing an error if the params are not passed. I would rather the code just handle it gracefully.

Copy link
Contributor

Choose a reason for hiding this comment

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

So you can also just leave this as you have it, and I can deal with adjustments that need to be made for my PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

made an update to the initiateAuthRequest function, please check and see if it works for you guys.

Copy link
Contributor

@jasonnutter jasonnutter left a comment

Choose a reason for hiding this comment

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

One question otherwise looks good!

Copy link
Collaborator

@tnorling tnorling left a comment

Choose a reason for hiding this comment

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

Looks good, thanks for addressing feedback

@@ -303,7 +303,10 @@ export abstract class ClientApplication {

const redirectStartPage = (request && request.redirectStartPage) || window.location.href;
// Show the UI once the url has been created. Response will come back in the hash, which will be handled in the handleRedirectCallback function.
return interactionHandler.initiateAuthRequest(navigateUrl, authCodeRequest, this.config.system.redirectNavigationTimeout, redirectStartPage);
return interactionHandler.initiateAuthRequest(navigateUrl, authCodeRequest, {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Can we define the params above and make this a one liner?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

@@ -379,7 +382,9 @@ export abstract class ClientApplication {
const interactionHandler = new PopupHandler(authClient, this.browserStorage);

// Show the UI once the url has been created. Get the window handle for the popup.
const popupWindow: Window = interactionHandler.initiateAuthRequest(navigateUrl, authCodeRequest, popup);
const popupWindow: Window = interactionHandler.initiateAuthRequest(navigateUrl, authCodeRequest, {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

Copy link
Contributor

@jmckennon jmckennon left a comment

Choose a reason for hiding this comment

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

LGTM!

@pkanher617 pkanher617 merged commit c6a7aad into dev Dec 3, 2020
@pkanher617 pkanher617 deleted the instance-aware-2.0 branch December 3, 2020 00:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
msal-browser Related to msal-browser package msal-common Related to msal-common package msal-react Related to @azure/msal-react samples Related to the samples apps for the library. work-in-progress Issue or PR is not finished.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants