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] Add SilentRequest.ts #1964

Merged
merged 4 commits into from Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 9 additions & 7 deletions lib/msal-browser/src/app/IPublicClientApplication.ts
@@ -1,19 +1,21 @@
import { AuthenticationResult, AuthorizationUrlRequest } from "@azure/msal-common";
import { SilentFlowRequest, EndSessionRequest, AccountInfo } from "../";
import { AuthenticationResult, AuthorizationUrlRequest, SilentFlowRequest, AccountInfo, EndSessionRequest } from "@azure/msal-common";
import { RedirectRequest } from "../request/RedirectRequest";
import { PopupRequest } from "../request/PopupRequest";
import { SilentRequest } from "../request/SilentRequest";

/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
export interface IPublicClientApplication {
acquireTokenPopup(request: AuthorizationUrlRequest): Promise<AuthenticationResult>;
acquireTokenRedirect(request: AuthorizationUrlRequest): Promise<void>;
acquireTokenSilent(silentRequest: SilentFlowRequest): Promise<AuthenticationResult>;
acquireTokenPopup(request: PopupRequest): Promise<AuthenticationResult>;
acquireTokenRedirect(request: RedirectRequest): Promise<void>;
acquireTokenSilent(silentRequest: SilentRequest): Promise<AuthenticationResult>;
getAccountByUsername(userName: string): AccountInfo;
getAllAccounts(): AccountInfo[];
handleRedirectPromise(): Promise<AuthenticationResult | null>;
loginPopup(request: AuthorizationUrlRequest): Promise<AuthenticationResult>;
loginRedirect(request: AuthorizationUrlRequest): Promise<void>;
loginPopup(request: PopupRequest): Promise<AuthenticationResult>;
loginRedirect(request: RedirectRequest): Promise<void>;
logout(logoutRequest?: EndSessionRequest): Promise<void>;
ssoSilent(request: AuthorizationUrlRequest): Promise<AuthenticationResult>;
jasonnutter marked this conversation as resolved.
Show resolved Hide resolved
}
5 changes: 4 additions & 1 deletion lib/msal-browser/src/app/PublicClientApplication.ts
Expand Up @@ -43,6 +43,7 @@ import { version } from "../../package.json";
import { IPublicClientApplication } from "./IPublicClientApplication";
import { RedirectRequest } from "../request/RedirectRequest";
import { PopupRequest } from "../request/PopupRequest";
import { SilentRequest } from "../request/SilentRequest";

/**
* The PublicClientApplication class is the object exposed by the library to perform authentication and authorization functions in Single Page Applications
Expand Down Expand Up @@ -361,13 +362,14 @@ export class PublicClientApplication implements IPublicClientApplication {
* @returns {Promise.<AuthenticationResult>} - a promise that is fulfilled when this function has completed, or rejected if an error was raised. Returns the {@link AuthResponse} object
*
*/
async acquireTokenSilent(request: SilentFlowRequest): Promise<AuthenticationResult> {
async acquireTokenSilent(request: SilentRequest): Promise<AuthenticationResult> {
// block the reload if it occurred inside a hidden iframe
BrowserUtils.blockReloadInHiddenIframes();
const silentRequest: SilentFlowRequest = {
...request,
...this.initializeBaseRequest(request)
};

try {
const silentAuthClient = await this.createSilentFlowClient(silentRequest.authority);
// Send request to renew token. Auth module will throw errors if token cannot be renewed.
Expand All @@ -379,6 +381,7 @@ export class PublicClientApplication implements IPublicClientApplication {
if (isServerError && isInvalidGrantError && !isInteractionRequiredError) {
const silentAuthUrlRequest: AuthorizationUrlRequest = this.initializeAuthorizationRequest({
...silentRequest,
redirectUri: request.redirectUri,
prompt: PromptValue.NONE
});

Expand Down
2 changes: 1 addition & 1 deletion lib/msal-browser/src/index.ts
Expand Up @@ -10,14 +10,14 @@ export { BrowserConfigurationAuthError, BrowserConfigurationAuthErrorMessage } f
export { IPublicClientApplication } from "./app/IPublicClientApplication";
export { PopupRequest } from "./request/PopupRequest";
export { RedirectRequest } from "./request/RedirectRequest";
export { SilentRequest } from "./request/SilentRequest";

// Common Object Formats
export {
// Account
AccountInfo,
// Request
AuthorizationUrlRequest,
SilentFlowRequest,
EndSessionRequest,
// Response
AuthenticationResult,
Expand Down
22 changes: 21 additions & 1 deletion lib/msal-browser/src/request/PopupRequest.ts
Expand Up @@ -7,6 +7,26 @@ import { AuthorizationUrlRequest } from "@azure/msal-common";

/**
* @type PopupRequest: Request object passed by user to retrieve a Code from the
* server (first leg of authorization code grant flow)
* server (first leg of authorization code grant flow) with a popup window.
*
* - scopes - Array of scopes the application is requesting access to.
* - authority - Url of the authority which the application acquires tokens from.
* - correlationId - Unique GUID set per request to trace a request end-to-end for telemetry purposes.
* - 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.
* - extraScopesToConsent - Scopes for a different resource when the user needs consent upfront.
* - responseMode - Specifies the method that should be used to send the authentication result to your app. Can be query, form_post, or fragment. If no value is passed in, it defaults to query.
* - codeChallenge - Used to secure authorization code grant via Proof of Key for Code Exchange (PKCE). For more information, see the PKCE RCF:https://tools.ietf.org/html/rfc7636
* - codeChallengeMethod - The method used to encode the code verifier for the code challenge parameter. Can be "plain" or "S256". If excluded, code challenge is assumed to be plaintext. For more information, see the PKCE RCF: https://tools.ietf.org/html/rfc7636
* - state - A value included in the request that is also returned in the token response. A randomly generated unique value is typically used for preventing cross site request forgery attacks. The state is also used to encode information about the user's state in the app before the authentication request occurred.
* - prompt - Indicates the type of user interaction that is required.
* login: will force the user to enter their credentials on that request, negating single-sign on
* none: will ensure that the user isn't presented with any interactive prompt. if request can't be completed via single-sign on, the endpoint will return an interaction_required error
* consent: will the trigger the OAuth consent dialog after the user signs in, asking the user to grant permissions to the app
* select_account: will interrupt single sign-=on providing account selection experience listing all the accounts in session or any remembered accounts or an option to choose to use a different account
* - loginHint - Can be used to pre-fill the username/email address field of the sign-in page for the user, if you know the username/email address ahead of time. Often apps use this parameter during re-authentication, having already extracted the username from a previous sign-in using the preferred_username claim.
* - domainHint - Provides a hint about the tenant or domain that the user should use to sign in. The value of the domain hint is a registered domain for the tenant.
* - extraQueryParameters - String to string map of custom query parameters.
* - claims - In cases where Azure AD tenant admin has enabled conditional access policies, and the policy has not been met, exceptions will contain claims that need to be consented to.
* - nonce - A value included in the request that is returned in the id token. A randomly generated unique value is typically used to mitigate replay attacks.
*/
export type PopupRequest = AuthorizationUrlRequest;
28 changes: 22 additions & 6 deletions lib/msal-browser/src/request/RedirectRequest.ts
Expand Up @@ -7,13 +7,29 @@ import { AuthorizationUrlRequest } from "@azure/msal-common";

/**
* @type RedirectRequest: Request object passed by user to retrieve a Code from the
* server (first leg of authorization code grant flow)
* server (first leg of authorization code grant flow) with a full page redirect.
*
* - scopes - Array of scopes the application is requesting access to.
* - authority - Url of the authority which the application acquires tokens from.
* - correlationId - Unique GUID set per request to trace a request end-to-end for telemetry purposes.
* - 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.
* - extraScopesToConsent - Scopes for a different resource when the user needs consent upfront.
* - responseMode - Specifies the method that should be used to send the authentication result to your app. Can be query, form_post, or fragment. If no value is passed in, it defaults to query.
* - codeChallenge - Used to secure authorization code grant via Proof of Key for Code Exchange (PKCE). For more information, see the PKCE RCF:https://tools.ietf.org/html/rfc7636
* - codeChallengeMethod - The method used to encode the code verifier for the code challenge parameter. Can be "plain" or "S256". If excluded, code challenge is assumed to be plaintext. For more information, see the PKCE RCF: https://tools.ietf.org/html/rfc7636
* - state - A value included in the request that is also returned in the token response. A randomly generated unique value is typically used for preventing cross site request forgery attacks. The state is also used to encode information about the user's state in the app before the authentication request occurred.
* - prompt - Indicates the type of user interaction that is required.
* login: will force the user to enter their credentials on that request, negating single-sign on
* none: will ensure that the user isn't presented with any interactive prompt. if request can't be completed via single-sign on, the endpoint will return an interaction_required error
* consent: will the trigger the OAuth consent dialog after the user signs in, asking the user to grant permissions to the app
* select_account: will interrupt single sign-=on providing account selection experience listing all the accounts in session or any remembered accounts or an option to choose to use a different account
* - loginHint - Can be used to pre-fill the username/email address field of the sign-in page for the user, if you know the username/email address ahead of time. Often apps use this parameter during re-authentication, having already extracted the username from a previous sign-in using the preferred_username claim.
* - domainHint - Provides a hint about the tenant or domain that the user should use to sign in. The value of the domain hint is a registered domain for the tenant.
* - extraQueryParameters - String to string map of custom query parameters.
* - claims - In cases where Azure AD tenant admin has enabled conditional access policies, and the policy has not been met, exceptions will contain claims that need to be consented to.
* - nonce - A value included in the request that is returned in the id token. A randomly generated unique value is typically used to mitigate replay attacks.
* - redirectStartPage - The page that should be returned to after loginRedirect or acquireTokenRedirect. This should only be used if this is different from the redirectUri and will default to the page that initiates the request. When the navigateToLoginRequestUrl config option is set to false this parameter will be ignored.
*/
export type RedirectRequest = AuthorizationUrlRequest & {
/**
* The page that should be returned to after loginRedirect or acquireTokenRedirect. This should only be used
* if this is different from the redirectUri and will default to the page that initiates the request.
* When the navigateToLoginRequestUrl config option is set to false this parameter will be ignored.
*/
redirectStartPage?: string;
};
22 changes: 22 additions & 0 deletions lib/msal-browser/src/request/SilentRequest.ts
@@ -0,0 +1,22 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

import { SilentFlowRequest } from "@azure/msal-common";

/**
* @type SilentRequest: Request object passed by user to retrieve tokens from the
* cache, renew an expired token with a refresh token, or retrieve a code (first leg of authorization code grant flow)
* in a hidden iframe.
*
* - scopes - Array of scopes the application is requesting access to.
* - authority - Url of the authority which the application acquires tokens from.
* - correlationId - Unique GUID set per request to trace a request end-to-end for telemetry purposes.
* - account - Account entity to lookup the credentials.
* - forceRefresh - Forces silent requests to make network calls if true.
* - 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.
*/
export type SilentRequest = SilentFlowRequest & {
redirectUri?: string;
};
7 changes: 7 additions & 0 deletions lib/msal-common/src/request/EndSessionRequest.ts
Expand Up @@ -5,6 +5,13 @@

import { AccountInfo } from "../account/AccountInfo";

/**
* EndSessionRequest
* - account - Account object that will be logged out of. All tokens tied to this account will be cleared.
* - postLogoutRedirectUri - URI to navigate to after logout page.
* - authority - Authority to send logout request to.
* - correlationId - Unique GUID set per request to trace a request end-to-end for telemetry purposes.
*/
export type EndSessionRequest = {
account?: AccountInfo,
postLogoutRedirectUri?: string,
Expand Down
1 change: 0 additions & 1 deletion lib/msal-common/src/request/RefreshTokenRequest.ts
Expand Up @@ -11,7 +11,6 @@ import { BaseAuthRequest } from "./BaseAuthRequest";
* - authority - URL of the authority, the security token service (STS) from which MSAL will acquire tokens.
* - correlationId - Unique GUID set per request to trace a request end-to-end for telemetry purposes.
* - refreshToken - A refresh token returned from a previous request to the Identity provider.
* - 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.
*/
export type RefreshTokenRequest = BaseAuthRequest & {
refreshToken: string;
Expand Down
1 change: 0 additions & 1 deletion lib/msal-common/src/request/SilentFlowRequest.ts
Expand Up @@ -13,7 +13,6 @@ import { BaseAuthRequest } from "./BaseAuthRequest";
* - correlationId - Unique GUID set per request to trace a request end-to-end for telemetry purposes.
* - account - Account entity to lookup the credentials.
* - forceRefresh - Forces silent requests to make network calls if true.
* - 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.
*/
export type SilentFlowRequest = BaseAuthRequest & {
account: AccountInfo;
Expand Down