Skip to content

Commit 64decbc

Browse files
authored
Merge pull request #4106 from AzureAD/cloudinstance-authority-enum
Add AzureCloudInstance to JS libraries
2 parents d0366a7 + 00dbde2 commit 64decbc

36 files changed

+506
-157
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "Add AzureCloudInstance to JS libraries",
4+
"packageName": "@azure/msal-browser",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Add AzureCloudInstance to JS libraries",
4+
"packageName": "@azure/msal-common",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "Add AzureCloudInstance to JS libraries",
4+
"packageName": "@azure/msal-node",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

lib/msal-browser/FAQ.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ For MSAL.js 2.x, please review [this document](https://github.com/AzureAD/micros
239239

240240
The `authority` string that you need to supplant to MSAL app configuration is not explicitly listed among the **Endpoint** links on `Azure Portal/AzureAD/App Registration/Overview` page. It is simply the domain part of a `/token` or `/authorize` endpoint, followed by the tenant name or ID e.g. `https://login.microsoftonline.com/common`.
241241

242+
## What does authority string default to if I provide "authority" and "azureCloudOptions"?
243+
244+
If the developer provides `azureCloudOptions`, MSAL.js will overwrite any value provided in the `authority`. MSAL.js will also give preference to the parameters provided in a `request` over `configuration`. Please note that if `azureCloudOptions` are set in the configuration, they will take precedence over `authority` in the `request`. If the developer needs to overwrite this, they need to set `azureCloudOptions` in the `request`.
245+
242246
## What should I set my `redirectUri` to?
243247

244248
When you attempt to authenticate MSAL will navigate to your IDP's sign in page either in the current window, a popup window or a hidden iframe depending on whether you used a redirect, popup or silent API respectively. When authentication is complete the IDP will redirect the window to the `redirectUri` specified in the request with the authentication response in the url hash. You can use any page in your application as your `redirectUri` but there are some additional considerations you should be aware of depending on which API you are using. All pages used as a `redirectUri` **must** be registered as a Reply Url of type "SPA" on your app registration.
@@ -262,7 +266,7 @@ The library is built to specifically use the fragment response mode. This is a s
262266

263267
## How do I configure the position and dimensions of popups?
264268

265-
A popup window's position and dimension can be configured by passing the height, width, top position, and left position in the request. If no configurations are passed, MSAL defaults will be used. See the request documentation for [PopupRequest](https://azuread.github.io/microsoft-authentication-library-for-js/ref/modules/_azure_msal_browser.html#popuprequest) and [EndSessionPopupRequest](https://azuread.github.io/microsoft-authentication-library-for-js/ref/modules/_azure_msal_browser.html#endsessionpopuprequest) for more details.
269+
A popup window's position and dimension can be configured by passing the height, width, top position, and left position in the request. If no configurations are passed, MSAL defaults will be used. See the request documentation for [PopupRequest](https://azuread.github.io/microsoft-authentication-library-for-js/ref/modules/_azure_msal_browser.html#popuprequest) and [EndSessionPopupRequest](https://azuread.github.io/microsoft-authentication-library-for-js/ref/modules/_azure_msal_browser.html#endsessionpopuprequest) for more details.
266270

267271
Note that popup dimensions should be positioned on screen and sized smaller than the parent window. Popups that are positioned off-screen or larger than the parent window will use MSAL defaults instead.
268272

@@ -312,7 +316,7 @@ Please refer to our performance guide [here](https://github.com/AzureAD/microsof
312316

313317
## I'm seeing scopes `openid`, `profile`, `email`, `offline_access` and `User.Read` in my tokens, even though I haven't requested them. What are they?
314318

315-
The first four (`openid`, `profile`, `email` and `offline_access`) are called **default scopes**. They are added to Azure AD as part of Azure AD - OAuth 2.0/OpenID Connect compliance. They are **not** part of any particular API. You can read more about them [here](https://openid.net/specs/openid-connect-core-1_0.html).
319+
The first four (`openid`, `profile`, `email` and `offline_access`) are called **default scopes**. They are added to Azure AD as part of Azure AD - OAuth 2.0/OpenID Connect compliance. They are **not** part of any particular API. You can read more about them [here](https://openid.net/specs/openid-connect-core-1_0.html).
316320

317321
The scope `User.Read`, on the other hand, is an MS Graph API scope. It is also added by default to every app registration. However if your application is not calling MS Graph API, you can simply ignore it.
318322

@@ -366,7 +370,7 @@ Our recommendation is to move to the new password reset experience since it simp
366370
pca.loginPopup()
367371
.then((response) => {
368372
// do something with auth response
369-
}).catch(error => {
373+
}).catch(error => {
370374
// Error handling
371375
if (error.errorMessage) {
372376
// Check for forgot password error

lib/msal-browser/docs/configuration.md

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ const msalInstance = new PublicClientApplication(msalConfig);
7373
| `navigateToLoginRequestUrl` | If `true`, will navigate back to the original request location before processing the authorization code response. If the `redirectUri` is the same as the original request location, this flag should be set to false. | boolean | `true` |
7474
| `clientCapabilities` | Array of capabilities to be added to all network requests as part of the `xms_cc` claims request | Array of strings | [] |
7575
| `protocolMode` | Enum representing the protocol mode to use. If `"AAD"`, will function on the OIDC-compliant AAD v2 endpoints; if `"OIDC"`, will function on other OIDC-compliant endpoints. | string | `"AAD"` |
76+
| `azureCloudOptions` | A defined set of azure cloud options for developers to default to their specific cloud authorities, for specific clouds supported please refer to the [AzureCloudInstance](aka.ms/msaljs/azure_cloud_instance) | [AzureCloudOptions](https://azuread.github.io/microsoft-authentication-library-for-js/ref/modules/_azure_msal_common.html#azurecloudoptions) | [AzureCloudInstance.None](msaljs/azure_cloud_instance)
7677

7778
### Cache Config Options
7879

lib/msal-browser/src/cache/TokenCache.ts

+19-18
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ export class TokenCache implements ITokenCache {
3737
this.logger = logger;
3838
this.cryptoObj = cryptoObj;
3939
}
40-
40+
4141
// Move getAllAccounts here and cache utility APIs
4242

4343
/**
44-
* API to load tokens to msal-browser cache.
45-
* @param request
44+
* API to load tokens to msal-browser cache.
45+
* @param request
4646
* @param response
4747
* @param options
4848
*/
@@ -58,13 +58,14 @@ export class TokenCache implements ITokenCache {
5858
this.loadAccessToken(request, response, request.account.homeAccountId, request.account.environment, request.account.tenantId, options);
5959
} else if (request.authority) {
6060

61+
const authorityUrl = Authority.generateAuthority(request.authority, request.azureCloudOptions);
6162
const authorityOptions: AuthorityOptions = {
6263
protocolMode: this.config.auth.protocolMode,
6364
knownAuthorities: this.config.auth.knownAuthorities,
6465
cloudDiscoveryMetadata: this.config.auth.cloudDiscoveryMetadata,
65-
authorityMetadata: this.config.auth.authorityMetadata
66+
authorityMetadata: this.config.auth.authorityMetadata,
6667
};
67-
const authority = new Authority(request.authority, this.config.system.networkClient, this.storage, authorityOptions);
68+
const authority = new Authority(authorityUrl, this.config.system.networkClient, this.storage, authorityOptions);
6869

6970
// "clientInfo" from options takes precedence over "clientInfo" in response
7071
if (options.clientInfo) {
@@ -85,11 +86,11 @@ export class TokenCache implements ITokenCache {
8586

8687
/**
8788
* Helper function to load id tokens to msal-browser cache
88-
* @param idToken
89-
* @param homeAccountId
90-
* @param environment
91-
* @param tenantId
92-
* @param options
89+
* @param idToken
90+
* @param homeAccountId
91+
* @param environment
92+
* @param tenantId
93+
* @param options
9394
*/
9495
private loadIdToken(idToken: string, homeAccountId: string, environment: string, tenantId: string, options: LoadTokenOptions): void {
9596

@@ -110,13 +111,13 @@ export class TokenCache implements ITokenCache {
110111

111112
/**
112113
* Helper function to load access tokens to msal-browser cache
113-
* @param request
114-
* @param response
115-
* @param options
116-
* @param homeAccountId
117-
* @param environment
118-
* @param tenantId
119-
* @returns
114+
* @param request
115+
* @param response
116+
* @param options
117+
* @param homeAccountId
118+
* @param environment
119+
* @param tenantId
120+
* @returns
120121
*/
121122
private loadAccessToken(request: SilentRequest, response: ExternalTokenResponse, homeAccountId: string, environment: string, tenantId: string, options: LoadTokenOptions): void {
122123

@@ -132,7 +133,7 @@ export class TokenCache implements ITokenCache {
132133
if (!options.extendedExpiresOn) {
133134
throw BrowserAuthError.createUnableToLoadTokenError("Please provide an extendedExpiresOn value in the options.");
134135
}
135-
136+
136137
const scopes = new ScopeSet(request.scopes).printScopes();
137138
const expiresOn = response.expires_in;
138139
const extendedExpiresOn = options.extendedExpiresOn;

lib/msal-browser/src/config/Configuration.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License.
44
*/
55

6-
import { SystemOptions, LoggerOptions, INetworkModule, DEFAULT_SYSTEM_OPTIONS, Constants, ProtocolMode, LogLevel, StubbedNetworkModule } from "@azure/msal-common";
6+
import { SystemOptions, LoggerOptions, INetworkModule, DEFAULT_SYSTEM_OPTIONS, Constants, ProtocolMode, LogLevel, StubbedNetworkModule, AzureCloudInstance, AzureCloudOptions } from "@azure/msal-common";
77
import { BrowserUtils } from "../utils/BrowserUtils";
88
import { BrowserCacheLocation } from "../utils/BrowserConstants";
99
import { INavigationClient } from "../navigation/INavigationClient";
@@ -21,11 +21,11 @@ export const DEFAULT_REDIRECT_TIMEOUT_MS = 30000;
2121
* - authority - You can configure a specific authority, defaults to " " or "https://login.microsoftonline.com/common"
2222
* - knownAuthorities - An array of URIs that are known to be valid. Used in B2C scenarios.
2323
* - cloudDiscoveryMetadata - A string containing the cloud discovery response. Used in AAD scenarios.
24-
* - redirectUri - The redirect URI where authentication responses can be received by your application. It must exactly match one of the redirect URIs registered in the Azure portal.
25-
* - postLogoutRedirectUri - The redirect URI where the window navigates after a successful logout.
26-
* - navigateToLoginRequestUrl - Boolean indicating whether to navigate to the original request URL after the auth server navigates to the redirect URL.
27-
* - clientCapabilities - Array of capabilities which will be added to the claims.access_token.xms_cc request property on every network request.
28-
* - protocolMode - Enum that represents the protocol that msal follows. Used for configuring proper endpoints.
24+
* - redirectUri - The redirect URI where authentication responses can be received by your application. It must exactly match one of the redirect URIs registered in the Azure portal.
25+
* - postLogoutRedirectUri - The redirect URI where the window navigates after a successful logout.
26+
* - navigateToLoginRequestUrl - Boolean indicating whether to navigate to the original request URL after the auth server navigates to the redirect URL.
27+
* - clientCapabilities - Array of capabilities which will be added to the claims.access_token.xms_cc request property on every network request.
28+
* - protocolMode - Enum that represents the protocol that msal follows. Used for configuring proper endpoints.
2929
*/
3030
export type BrowserAuthOptions = {
3131
clientId: string;
@@ -38,14 +38,15 @@ export type BrowserAuthOptions = {
3838
navigateToLoginRequestUrl?: boolean;
3939
clientCapabilities?: Array<string>;
4040
protocolMode?: ProtocolMode;
41+
azureCloudOptions?: AzureCloudOptions;
4142
};
4243

4344
/**
4445
* Use this to configure the below cache configuration options:
4546
*
46-
* - cacheLocation - Used to specify the cacheLocation user wants to set. Valid values are "localStorage" and "sessionStorage"
47-
* - storeAuthStateInCookie - If set, MSAL stores the auth request state required for validation of the auth flows in the browser cookies. By default this flag is set to false.
48-
* - secureCookies - If set, MSAL sets the "Secure" flag on cookies so they can only be sent over HTTPS. By default this flag is set to false.
47+
* - cacheLocation - Used to specify the cacheLocation user wants to set. Valid values are "localStorage" and "sessionStorage"
48+
* - storeAuthStateInCookie - If set, MSAL stores the auth request state required for validation of the auth flows in the browser cookies. By default this flag is set to false.
49+
* - secureCookies - If set, MSAL sets the "Secure" flag on cookies so they can only be sent over HTTPS. By default this flag is set to false.
4950
*/
5051
export type CacheOptions = {
5152
cacheLocation?: BrowserCacheLocation | string;
@@ -122,7 +123,11 @@ export function buildConfiguration({ auth: userInputAuth, cache: userInputCache,
122123
postLogoutRedirectUri: "",
123124
navigateToLoginRequestUrl: true,
124125
clientCapabilities: [],
125-
protocolMode: ProtocolMode.AAD
126+
protocolMode: ProtocolMode.AAD,
127+
azureCloudOptions: {
128+
azureCloudInstance: AzureCloudInstance.None,
129+
tenant: ""
130+
},
126131
};
127132

128133
// Default cache options for browser

lib/msal-browser/src/index.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ export { PublicClientApplication } from "./app/PublicClientApplication";
1212
export { Configuration, BrowserAuthOptions, CacheOptions, BrowserSystemOptions, BrowserConfiguration, DEFAULT_IFRAME_TIMEOUT_MS } from "./config/Configuration";
1313
export { InteractionType, InteractionStatus, BrowserCacheLocation, WrapperSKU, ApiId } from "./utils/BrowserConstants";
1414
export { BrowserUtils } from "./utils/BrowserUtils";
15-
15+
1616
// Browser Errors
1717
export { BrowserAuthError, BrowserAuthErrorMessage } from "./error/BrowserAuthError";
1818
export { BrowserConfigurationAuthError, BrowserConfigurationAuthErrorMessage } from "./error/BrowserConfigurationAuthError";
19-
19+
2020
// Interfaces
2121
export { IPublicClientApplication, stubbedPublicClientApplication } from "./app/IPublicClientApplication";
2222
export { INavigationClient } from "./navigation/INavigationClient";
@@ -30,34 +30,34 @@ export { EndSessionRequest } from "./request/EndSessionRequest";
3030
export { EndSessionPopupRequest } from "./request/EndSessionPopupRequest";
3131
export { AuthorizationUrlRequest } from "./request/AuthorizationUrlRequest";
3232
export { AuthorizationCodeRequest } from "./request/AuthorizationCodeRequest";
33-
33+
3434
// Cache
3535
export { LoadTokenOptions } from "./cache/TokenCache";
3636
export { BrowserCacheManager } from "./cache/BrowserCacheManager";
37-
37+
3838
// Clients
3939
export { StandardInteractionClient } from "./interaction_client/StandardInteractionClient";
4040
export { RedirectClient } from "./interaction_client/RedirectClient";
4141
export { PopupClient } from "./interaction_client/PopupClient";
4242
export { SilentIframeClient } from "./interaction_client/SilentIframeClient";
4343
export { SilentCacheClient } from "./interaction_client/SilentCacheClient";
4444
export { SilentRefreshClient } from "./interaction_client/SilentRefreshClient";
45-
45+
4646
// Handlers
4747
export { RedirectHandler } from "./interaction_handler/RedirectHandler";
48-
48+
4949
// Events
5050
export { EventMessage, EventPayload, EventError, EventCallbackFunction, EventMessageUtils, PopupEvent } from "./event/EventMessage";
5151
export { EventType } from "./event/EventType";
5252
export { EventHandler } from "./event/EventHandler";
53-
53+
5454
export { SignedHttpRequest, SignedHttpRequestOptions } from "./crypto/SignedHttpRequest";
55-
55+
5656
// Utilities
5757
export { BrowserStateObject } from "./utils/BrowserProtocolUtils";
5858
export { BrowserConstants, TemporaryCacheKeys } from "./utils/BrowserConstants";
5959
export { PopupUtils } from "./utils/PopupUtils";
60-
60+
6161
// Common Object Formats
6262
export {
6363
AuthenticationScheme,
@@ -91,8 +91,11 @@ export {
9191
// Utils
9292
StringUtils,
9393
UrlString,
94+
// AzureCloudInstance enum
95+
AzureCloudInstance,
96+
AzureCloudOptions,
9497
AuthenticationHeaderParser,
9598
OIDC_DEFAULT_SCOPES
9699
} from "@azure/msal-common";
97-
100+
98101
export { version } from "./packageMetadata";

lib/msal-browser/src/interaction_client/PopupClient.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { PopupRequest } from "../request/PopupRequest";
1717
export class PopupClient extends StandardInteractionClient {
1818
/**
1919
* Acquires tokens by opening a popup window to the /authorize endpoint of the authority
20-
* @param request
20+
* @param request
2121
*/
2222
acquireToken(request: PopupRequest): Promise<AuthenticationResult> {
2323
try {
@@ -42,7 +42,7 @@ export class PopupClient extends StandardInteractionClient {
4242

4343
/**
4444
* Clears local cache for the current user then opens a popup window prompting the user to sign-out of the server
45-
* @param logoutRequest
45+
* @param logoutRequest
4646
*/
4747
logout(logoutRequest?: EndSessionPopupRequest): Promise<void> {
4848
try {
@@ -91,7 +91,7 @@ export class PopupClient extends StandardInteractionClient {
9191
const authCodeRequest: CommonAuthorizationCodeRequest = await this.initializeAuthorizationCodeRequest(validRequest);
9292

9393
// Initialize the client
94-
const authClient: AuthorizationCodeClient = await this.createAuthCodeClient(serverTelemetryManager, validRequest.authority);
94+
const authClient: AuthorizationCodeClient = await this.createAuthCodeClient(serverTelemetryManager, validRequest.authority, validRequest.azureCloudOptions);
9595
this.logger.verbose("Auth code client created");
9696

9797
// Create acquire token url.
@@ -120,7 +120,7 @@ export class PopupClient extends StandardInteractionClient {
120120
const result = await interactionHandler.handleCodeResponseFromHash(hash, state, authClient.authority, this.networkClient);
121121

122122
return result;
123-
} catch (e) {
123+
} catch (e) {
124124
if (popup) {
125125
// Close the synchronous popup if an error is thrown before the window unload event is registered
126126
popup.close();
@@ -137,20 +137,20 @@ export class PopupClient extends StandardInteractionClient {
137137
}
138138

139139
/**
140-
*
141-
* @param validRequest
142-
* @param popupName
140+
*
141+
* @param validRequest
142+
* @param popupName
143143
* @param requestAuthority
144-
* @param popup
145-
* @param mainWindowRedirectUri
146-
* @param popupWindowAttributes
144+
* @param popup
145+
* @param mainWindowRedirectUri
146+
* @param popupWindowAttributes
147147
*/
148148
protected async logoutPopupAsync(validRequest: CommonEndSessionRequest, popupName: string, popupWindowAttributes: PopupWindowAttributes, requestAuthority?: string, popup?: Window|null, mainWindowRedirectUri?: string): Promise<void> {
149149
this.logger.verbose("logoutPopupAsync called");
150150
this.eventHandler.emitEvent(EventType.LOGOUT_START, InteractionType.Popup, validRequest);
151151

152152
const serverTelemetryManager = this.initializeServerTelemetryManager(ApiId.logoutPopup);
153-
153+
154154
try {
155155
// Clear cache on logout
156156
await this.clearCacheOnLogout(validRequest.account);
@@ -202,7 +202,7 @@ export class PopupClient extends StandardInteractionClient {
202202
if (e instanceof AuthError) {
203203
e.setCorrelationId(this.correlationId);
204204
}
205-
205+
206206
this.browserStorage.setInteractionInProgress(false);
207207
this.eventHandler.emitEvent(EventType.LOGOUT_FAILURE, InteractionType.Popup, null, e);
208208
this.eventHandler.emitEvent(EventType.LOGOUT_END, InteractionType.Popup);

0 commit comments

Comments
 (0)