-
Notifications
You must be signed in to change notification settings - Fork 2.7k
[msal-common][msal-node] Authority changes #1424
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
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
076ecb3
Adding knownAuthorities() and B2CAuthority
sameerag 679425b
Merge branch 'msal-node-logging' into msal-node-authority
sameerag c4fe6c0
addressing feedback and in sync with PR #1416
sameerag 4d31901
Merge branch 'sagonzal/device-code' into msal-node-authority
sameerag 7a9cefc
Update package.json
sameerag b846ebc
Merge branch 'sagonzal/device-code' into msal-node-authority
sameerag ae43cea
addressing feedback
sameerag a66906f
Fix a bug in AuthorityFactory.ts
sameerag cb2cf62
Merge remote-tracking branch 'origin/sagonzal/msal-node-tests' into m…
sameerag 5a680a6
merge msal-node-tests
sameerag 80b2e1d
remove log stmt
sameerag 890ed57
Merge branch 'msal-node-logging' into msal-node-authority
sameerag 735ace6
Merge branch 'msal-node-logging' into msal-node-authority
sameerag ca9bc66
Merge branch 'msal-node-logging' into msal-node-authority
sameerag e93edb0
Add unit tests fpr authority changes
sameerag 2a23b57
Addressing feedback and fixing tests
sameerag fb4c5cf
Initiate knownAuthorities in msal-node
sameerag f58fa41
git merge msal-node-logging
sameerag 67311ff
Add client_info for tokenResponse
sameerag c004275
Merge branch 'msal-node-logging' into msal-node-authority
sameerag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
import { Authority } from "./Authority"; | ||
import { AuthorityType } from "./AuthorityType"; | ||
import { ClientConfigurationError } from "../error/ClientConfigurationError"; | ||
import { INetworkModule } from "../network/INetworkModule"; | ||
|
||
export const B2CTrustedHostList: string[] = []; | ||
|
||
/** | ||
* The AadAuthority class extends the Authority class and adds functionality specific to the Azure AD OAuth Authority. | ||
sameerag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
export class B2cAuthority extends Authority { | ||
// Set authority type to AAD | ||
public get authorityType(): AuthorityType { | ||
return AuthorityType.B2C; | ||
} | ||
|
||
public constructor(authority: string, networkInterface: INetworkModule) { | ||
super(authority, networkInterface); | ||
} | ||
|
||
/** | ||
* Returns a promise which resolves to the OIDC endpoint | ||
* Only responds with the endpoint | ||
*/ | ||
public async getOpenIdConfigurationEndpointAsync(): Promise<string> { | ||
if (this.isInTrustedHostList(this.canonicalAuthorityUrlComponents.HostNameAndPort)) { | ||
return this.defaultOpenIdConfigurationEndpoint; | ||
} | ||
|
||
throw ClientConfigurationError.createUntrustedAuthorityError(); | ||
} | ||
|
||
/** | ||
* Checks to see if the host is in a list of trusted hosts | ||
* @param {string} The host to look up | ||
*/ | ||
private isInTrustedHostList(host: string): boolean { | ||
return B2CTrustedHostList.includes(host); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -215,4 +215,3 @@ export enum GrantType { | |
REFRESH_TOKEN_GRANT = "refresh_token", | ||
DEVICE_CODE_GRANT = "device_code" | ||
}; | ||
|
111 changes: 94 additions & 17 deletions
111
lib/msal-common/test/authority/AuthorityFactory.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,116 @@ | ||
import { expect } from "chai"; | ||
import { AuthorityFactory } from "../../src/authority/AuthorityFactory"; | ||
import { INetworkModule, NetworkRequestOptions } from "../../src/network/INetworkModule"; | ||
import { ClientConfigurationErrorMessage, ClientAuthErrorMessage, Constants, Authority } from "../../src"; | ||
import { | ||
INetworkModule, | ||
NetworkRequestOptions | ||
} from "../../src/network/INetworkModule"; | ||
import { | ||
ClientConfigurationErrorMessage, | ||
Constants, | ||
Authority, | ||
ClientAuthError, | ||
ClientAuthErrorMessage | ||
} from "../../src"; | ||
import { AadAuthority } from "../../src/authority/AadAuthority"; | ||
import { B2cAuthority, B2CTrustedHostList} from "../../src/authority/B2cAuthority"; | ||
import { TEST_CONFIG } from "../utils/StringConstants"; | ||
|
||
describe("AuthorityFactory.ts Class Unit Tests", () => { | ||
|
||
const networkInterface: INetworkModule = { | ||
sendGetRequestAsync<T>(url: string, options?: NetworkRequestOptions): T { | ||
sendGetRequestAsync<T>( | ||
url: string, | ||
options?: NetworkRequestOptions | ||
): T { | ||
return null; | ||
}, | ||
sendPostRequestAsync<T>(url: string, options?: NetworkRequestOptions): T { | ||
sendPostRequestAsync<T>( | ||
url: string, | ||
options?: NetworkRequestOptions | ||
): T { | ||
return null; | ||
} | ||
}; | ||
|
||
it("AuthorityFactory returns null if given url is null or empty", () => { | ||
expect(() => AuthorityFactory.createInstance("", networkInterface)).to.throw(ClientConfigurationErrorMessage.urlEmptyError.desc); | ||
expect(() => AuthorityFactory.createInstance(null, networkInterface)).to.throw(ClientConfigurationErrorMessage.urlEmptyError.desc); | ||
}); | ||
|
||
it("Throws error for B2C url strings that contain tfp", () => { | ||
expect(() => AuthorityFactory.createInstance("https://contoso.b2clogin.com/tfp/contoso.onmicrosoft.com/B2C_1_signupsignin1", networkInterface)).to.throw(ClientAuthErrorMessage.invalidAuthorityType.desc); | ||
expect(() => | ||
AuthorityFactory.createInstance("", networkInterface) | ||
).to.throw(ClientConfigurationErrorMessage.urlEmptyError.desc); | ||
expect(() => | ||
AuthorityFactory.createInstance(null, networkInterface) | ||
).to.throw(ClientConfigurationErrorMessage.urlEmptyError.desc); | ||
}); | ||
|
||
it("Throws error for malformed url strings", () => { | ||
expect(() => AuthorityFactory.createInstance(`http://login.microsoftonline.com/common`, networkInterface)).to.throw(ClientConfigurationErrorMessage.authorityUriInsecure.desc); | ||
expect(() => AuthorityFactory.createInstance(`https://login.microsoftonline.com/`, networkInterface)).to.throw(ClientConfigurationErrorMessage.urlParseError.desc); | ||
expect(() => AuthorityFactory.createInstance("This is not a URI", networkInterface)).to.throw(ClientConfigurationErrorMessage.urlParseError.desc); | ||
expect(() => AuthorityFactory.createInstance("", networkInterface)).to.throw(ClientConfigurationErrorMessage.urlEmptyError.desc); | ||
expect(() => | ||
AuthorityFactory.createInstance( | ||
`http://login.microsoftonline.com/common`, | ||
networkInterface | ||
) | ||
).to.throw(ClientConfigurationErrorMessage.authorityUriInsecure.desc); | ||
expect(() => | ||
AuthorityFactory.createInstance( | ||
`https://login.microsoftonline.com/`, | ||
networkInterface | ||
) | ||
).to.throw(ClientConfigurationErrorMessage.urlParseError.desc); | ||
expect(() => | ||
AuthorityFactory.createInstance( | ||
"This is not a URI", | ||
networkInterface | ||
) | ||
).to.throw(ClientConfigurationErrorMessage.urlParseError.desc); | ||
expect(() => | ||
AuthorityFactory.createInstance("", networkInterface) | ||
).to.throw(ClientConfigurationErrorMessage.urlEmptyError.desc); | ||
}); | ||
|
||
it("createInstance returns an AAD instance for any valid url string that does not contain a tfp", () => { | ||
const authorityInstance = AuthorityFactory.createInstance(Constants.DEFAULT_AUTHORITY, networkInterface); | ||
it("createInstance returns an AAD instance if knownAuthorities not provided", () => { | ||
const authorityInstance = AuthorityFactory.createInstance( | ||
Constants.DEFAULT_AUTHORITY, | ||
networkInterface | ||
); | ||
expect(authorityInstance instanceof AadAuthority); | ||
expect(authorityInstance instanceof Authority); | ||
}); | ||
|
||
it("createInstance returns B2C instance if knownAuthorities is provided", () => { | ||
AuthorityFactory.setKnownAuthorities(["fabrikamb2c.b2clogin.com"]); | ||
const authorityInstance = AuthorityFactory.createInstance( | ||
TEST_CONFIG.b2cValidAuthority, | ||
networkInterface | ||
); | ||
expect(authorityInstance instanceof B2cAuthority); | ||
expect(authorityInstance instanceof Authority); | ||
}); | ||
|
||
it("Do not add additional authorities to trusted host list if it has already been populated", () => { | ||
AuthorityFactory.setKnownAuthorities(["fabrikamb2c.b2clogin.com"]); | ||
AuthorityFactory.setKnownAuthorities(["fake.b2clogin.com"]); | ||
|
||
console.log(B2CTrustedHostList); | ||
|
||
expect(B2CTrustedHostList).to.include("fabrikamb2c.b2clogin.com"); | ||
expect(B2CTrustedHostList).not.to.include("fake.b2clogin.com"); | ||
expect(B2CTrustedHostList.length).to.equal(1); | ||
}); | ||
|
||
it("Throws error if AuthorityType is not AAD or B2C", done => { | ||
//Right now only way to throw this is to send adfs authority. This will need to change when we implement ADFS | ||
const errorAuthority = "https://login.microsoftonline.com/adfs"; | ||
try { | ||
const authorityInstance = AuthorityFactory.createInstance( | ||
errorAuthority, | ||
networkInterface | ||
); | ||
} catch (e) { | ||
expect(e).to.be.instanceOf(ClientAuthError); | ||
expect(e.errorCode).to.be.eql( | ||
ClientAuthErrorMessage.invalidAuthorityType.code | ||
); | ||
expect(e.errorMessage).to.be.eql( | ||
`${ClientAuthErrorMessage.invalidAuthorityType.desc} Given Url: ${errorAuthority}` | ||
); | ||
done(); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.