Skip to content

Commit c8c5ca2

Browse files
authored
Merge pull request #1657 from AzureAD/sagonzal/msal-node-improve
[msal-node] Clean up public API
2 parents fc973cf + d953726 commit c8c5ca2

10 files changed

+88
-62
lines changed

lib/msal-common/src/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ export { SPAClient } from "./client/SPAClient";
33
export { AuthorizationCodeClient } from "./client/AuthorizationCodeClient";
44
export { DeviceCodeClient } from "./client/DeviceCodeClient";
55
export { RefreshTokenClient } from "./client/RefreshTokenClient";
6-
export { AuthOptions, SystemOptions, LoggerOptions, TelemetryOptions, DEFAULT_SYSTEM_OPTIONS } from "./config/ClientConfiguration";
6+
export {
7+
AuthOptions, SystemOptions, LoggerOptions, TelemetryOptions, DEFAULT_SYSTEM_OPTIONS
8+
} from "./config/ClientConfiguration";
79
export { ClientConfiguration } from "./config/ClientConfiguration";
810
// Account
911
export { Account } from "./account/Account";
@@ -42,5 +44,7 @@ export { ServerError } from "./error/ServerError";
4244
export { ClientAuthError, ClientAuthErrorMessage } from "./error/ClientAuthError";
4345
export { ClientConfigurationError, ClientConfigurationErrorMessage } from "./error/ClientConfigurationError";
4446
// Constants and Utils
45-
export { Constants, PromptValue, TemporaryCacheKeys, PersistentCacheKeys } from "./utils/Constants";
47+
export {
48+
Constants, PromptValue, TemporaryCacheKeys, PersistentCacheKeys, Prompt, ResponseMode
49+
} from "./utils/Constants";
4650
export { StringUtils } from "./utils/StringUtils";

lib/msal-common/src/request/AuthorizationCodeUrlRequest.ts

+22-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
* Licensed under the MIT License.
44
*/
55

6+
import { Prompt, ResponseMode } from "../utils/Constants";
7+
68
/**
7-
* @type AuthorizationCodeUrlRequest: Request object passed by user to retrieve a Code from the server (first leg of authorization code grant flow)
9+
* @type AuthorizationCodeUrlRequest: Request object passed by user to retrieve a Code from the
10+
* server (first leg of authorization code grant flow)
811
*/
912
export type AuthorizationCodeUrlRequest = {
1013

@@ -20,15 +23,17 @@ export type AuthorizationCodeUrlRequest = {
2023
scopes: Array<string>;
2124

2225
/**
23-
* Url of the authority which the application acquires tokens from
26+
* Url of the authority which the application acquires tokens from. Defaults to
27+
* https://login.microsoftonline.com/common. If using the same authority for all request, authority should set
28+
* on client application object and not request, to avoid resolving authority endpoints multiple times.
2429
*/
2530
authority?: string;
2631

2732
/**
2833
* Specifies the method that should be used to send the authentication result to your app.
2934
* Can be query, form_post, or fragment. If no value is passed in, it defaults to query.
3035
*/
31-
responseMode?: string;
36+
responseMode?: ResponseMode;
3237

3338
/**
3439
* Used to secure authorization code grant via Proof of Key for Code Exchange (PKCE).
@@ -37,8 +42,8 @@ export type AuthorizationCodeUrlRequest = {
3742
codeChallenge?: string;
3843

3944
/**
40-
* The method used to encode the code verifier for the code challenge parameter. Can be one
41-
* of plain or S256. If excluded, code challenge is assumed to be plaintext. For more
45+
* The method used to encode the code verifier for the code challenge parameter. Can be
46+
* "plain" or "S256". If excluded, code challenge is assumed to be plaintext. For more
4247
* information, see the PKCE RCF: https://tools.ietf.org/html/rfc7636
4348
*/
4449
codeChallengeMethod?: string;
@@ -53,8 +58,17 @@ export type AuthorizationCodeUrlRequest = {
5358

5459
/**
5560
* Indicates the type of user interaction that is required.
61+
*
62+
* login: will force the user to enter their credentials on that request, negating single-sign on
63+
*
64+
* none: will ensure that the user isn't presented with any interactive prompt. if request can't be completed via
65+
* single-sign on, the endpoint will return an interaction_required error
66+
* consent: will the trigger the OAuth consent dialog after the user signs in, asking the user to grant permissions
67+
* to the app
68+
* select_account: will interrupt single sign-=on providing account selection experience listing all the accounts in
69+
* session or any remembered accounts or an option to choose to use a different account
5670
*/
57-
prompt?: string;
71+
prompt?: Prompt;
5872

5973
/**
6074
* Can be used to pre-fill the username/email address field of the sign-in page for the user,
@@ -77,8 +91,8 @@ export type AuthorizationCodeUrlRequest = {
7791
claims?: string;
7892

7993
/**
80-
* A value included in the request that is also returned in the token response. A randomly
81-
* generated unique value is typically used for preventing cross site request forgery attacks.
94+
* A value included in the request that is returned in the id token. A randomly
95+
* generated unique value is typically used to mitigate replay attacks.
8296
*/
8397
nonce?: string;
8498

lib/msal-common/src/request/DeviceCodeRequest.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ export type DeviceCodeRequest = {
3030
cancel?: boolean;
3131

3232
/**
33-
* URI of the authority from which MSAL will acquire the tokens from. If this value is not set, MSAL defaults
34-
* to the authority used when configuring the PublicClientApplication.
33+
* Url of the authority which the application acquires tokens from. Defaults to
34+
* https://login.microsoftonline.com/common. If using the same authority for all request, authority should set
35+
* on client application object and not request, to avoid resolving authority endpoints multiple times.
3536
*/
3637
authority?: string;
3738
};

lib/msal-common/src/server/RequestParameterBuilder.ts

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

6-
import { AADServerParamKeys, SSOTypes } from "../utils/Constants";
7-
import { Constants } from "../utils/Constants";
6+
import { AADServerParamKeys, Constants, Prompt, ResponseMode, SSOTypes } from "../utils/Constants";
87
import { ScopeSet } from "../request/ScopeSet";
98
import { ClientConfigurationError } from "../error/ClientConfigurationError";
109

@@ -29,10 +28,10 @@ export class RequestParameterBuilder {
2928
* add response_mode. defaults to query.
3029
* @param responseMode
3130
*/
32-
addResponseMode(responseMode?: string): void {
31+
addResponseMode(responseMode?: ResponseMode): void {
3332
this.parameters.set(
3433
AADServerParamKeys.RESPONSE_MODE,
35-
encodeURIComponent((responseMode) ? responseMode : Constants.QUERY_RESPONSE_MODE)
34+
encodeURIComponent((responseMode) ? responseMode : ResponseMode.QUERY)
3635
);
3736
}
3837

@@ -100,7 +99,7 @@ export class RequestParameterBuilder {
10099
* add prompt
101100
* @param prompt
102101
*/
103-
addPrompt(prompt: string): void {
102+
addPrompt(prompt: Prompt): void {
104103
this.parameters.set(`${AADServerParamKeys.PROMPT}`, encodeURIComponent(prompt));
105104
}
106105

lib/msal-common/src/utils/Constants.ts

+21-10
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export const Constants = {
3434
CODE_GRANT_TYPE: "authorization_code",
3535
RT_GRANT_TYPE: "refresh_token",
3636
FRAGMENT_RESPONSE_MODE: "fragment",
37-
QUERY_RESPONSE_MODE: "query",
3837
S256_CODE_CHALLENGE_METHOD: "S256",
3938
URL_FORM_CONTENT_TYPE: "application/x-www-form-urlencoded;charset=utf-8",
4039
AUTHORIZATION_PENDING: "authorization_pending"
@@ -45,7 +44,7 @@ export const Constants = {
4544
*/
4645
export enum HeaderNames {
4746
CONTENT_TYPE = "Content-Type"
48-
};
47+
}
4948

5049
/**
5150
* Temporary cache keys for MSAL, deleted after any request.
@@ -61,7 +60,7 @@ export enum TemporaryCacheKeys {
6160
URL_HASH = "urlHash",
6261
REQUEST_PARAMS = "request.params",
6362
SCOPES = "scopes"
64-
};
63+
}
6564

6665
/**
6766
* Persistent cache keys MSAL which stay while user is logged in.
@@ -72,7 +71,7 @@ export enum PersistentCacheKeys {
7271
ADAL_ID_TOKEN = "adal.idtoken",
7372
ERROR = "error",
7473
ERROR_DESC = "error.description"
75-
};
74+
}
7675

7776
/**
7877
* List of pre-established trusted host URLs.
@@ -93,7 +92,7 @@ export enum AADAuthorityConstants {
9392
COMMON = "common",
9493
ORGANIZATIONS = "organizations",
9594
CONSUMERS = "consumers"
96-
};
95+
}
9796

9897
/**
9998
* Keys in the hashParams sent by AAD Server
@@ -129,7 +128,7 @@ export enum AADServerParamKeys {
129128
X_CLIENT_CPU = "x-client-CPU",
130129
POST_LOGOUT_URI = "post_logout_redirect_uri",
131130
DEVICE_CODE = "device_code"
132-
};
131+
}
133132

134133
/**
135134
* IdToken claim string constants
@@ -168,13 +167,13 @@ export enum SSOTypes {
168167
ACCOUNT = "account",
169168
SID = "sid",
170169
LOGIN_HINT = "login_hint",
171-
ID_TOKEN ="id_token",
170+
ID_TOKEN = "id_token",
172171
DOMAIN_HINT = "domain_hint",
173172
ORGANIZATIONS = "organizations",
174173
CONSUMERS = "consumers",
175174
ACCOUNT_ID = "accountIdentifier",
176175
HOMEACCOUNT_ID = "homeAccountIdentifier"
177-
};
176+
}
178177

179178
/**
180179
* Disallowed extra query parameters.
@@ -193,7 +192,9 @@ export const CodeChallengeMethodValues = {
193192
};
194193

195194
/**
196-
*
195+
* The method used to encode the code verifier for the code challenge parameter. can be one
196+
* of plain or s256. if excluded, code challenge is assumed to be plaintext. for more
197+
* information, see the pkce rcf: https://tools.ietf.org/html/rfc7636
197198
*/
198199
export const CodeChallengeMethodValuesArray: string[] = [
199200
CodeChallengeMethodValues.PLAIN,
@@ -209,6 +210,16 @@ export enum ResponseMode {
209210
FORM_POST = "form_post"
210211
}
211212

213+
/**
214+
* Allowed values for prompt
215+
*/
216+
export enum Prompt {
217+
LOGIN = "login",
218+
NONE = "none",
219+
CONSENT = "consent",
220+
SELECT_ACCOUNT = "select_account"
221+
}
222+
212223
/**
213224
* allowed grant_type
214225
*/
@@ -219,5 +230,5 @@ export enum GrantType {
219230
RESOURCE_OWNER_PASSWORD_GRANT = "password",
220231
REFRESH_TOKEN_GRANT = "refresh_token",
221232
DEVICE_CODE_GRANT = "device_code"
222-
};
233+
}
223234

lib/msal-common/test/client/AuthorizationCodeClient.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe("AuthorizationCodeClient unit tests", () => {
5858
expect(loginUrl).to.contain(`${AADServerParamKeys.RESPONSE_TYPE}=${Constants.CODE_RESPONSE_TYPE}`);
5959
expect(loginUrl).to.contain(`${AADServerParamKeys.CLIENT_ID}=${TEST_CONFIG.MSAL_CLIENT_ID}`);
6060
expect(loginUrl).to.contain(`${AADServerParamKeys.REDIRECT_URI}=${encodeURIComponent(TEST_URIS.TEST_REDIRECT_URI_LOCALHOST)}`);
61-
expect(loginUrl).to.contain(`${AADServerParamKeys.RESPONSE_MODE}=${encodeURIComponent(Constants.QUERY_RESPONSE_MODE)}`);
61+
expect(loginUrl).to.contain(`${AADServerParamKeys.RESPONSE_MODE}=${encodeURIComponent(ResponseMode.QUERY)}`);
6262
});
6363

6464
it("Creates an authorization url passing in a default scope", async () => {
@@ -78,7 +78,7 @@ describe("AuthorizationCodeClient unit tests", () => {
7878
expect(loginUrl).to.contain(`${AADServerParamKeys.RESPONSE_TYPE}=${Constants.CODE_RESPONSE_TYPE}`);
7979
expect(loginUrl).to.contain(`${AADServerParamKeys.CLIENT_ID}=${TEST_CONFIG.MSAL_CLIENT_ID}`);
8080
expect(loginUrl).to.contain(`${AADServerParamKeys.REDIRECT_URI}=${encodeURIComponent(TEST_URIS.TEST_REDIRECT_URI_LOCALHOST)}`);
81-
expect(loginUrl).to.contain(`${AADServerParamKeys.RESPONSE_MODE}=${encodeURIComponent(Constants.QUERY_RESPONSE_MODE)}`)
81+
expect(loginUrl).to.contain(`${AADServerParamKeys.RESPONSE_MODE}=${encodeURIComponent(ResponseMode.QUERY)}`);
8282
});
8383

8484
it("Creates an authorization url passing in optional parameters", async () => {

lib/msal-node/src/client/ClientApplication.ts

+10-22
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,20 @@ import { Constants as NodeConstants } from "./../utils/Constants";
2424

2525
export abstract class ClientApplication {
2626

27-
protected config: Configuration;
28-
protected _authority: Authority;
27+
private config: Configuration;
28+
private _authority: Authority;
29+
private readonly cryptoProvider: CryptoProvider;
30+
private readonly storage: Storage;
2931

3032
/**
3133
* @constructor
32-
* Constructor for the ClientApplication to instantiate the PublicClientApplication object
33-
*
34-
* Important attributes in the Configuration object for auth are:
35-
* - clientID: the application ID of your application. You can obtain one by registering your application with our Application registration portal
36-
* - authority: the authority URL for your application.
37-
* - redirect_uri: the uri of your application registered in the portal.
38-
*
39-
* In Azure AD, authority is a URL indicating the Azure active directory that MSAL uses to obtain tokens.
40-
* It is of the form https://login.microsoftonline.com/{Enter_the_Tenant_Info_Here}
41-
* If your application supports Accounts in one organizational directory, replace "Enter_the_Tenant_Info_Here" value with the Tenant Id or Tenant name (for example, contoso.microsoft.com).
42-
* If your application supports Accounts in any organizational directory, replace "Enter_the_Tenant_Info_Here" value with organizations.
43-
* If your application supports Accounts in any organizational directory and personal Microsoft accounts, replace "Enter_the_Tenant_Info_Here" value with common.
44-
* To restrict support to Personal Microsoft accounts only, replace "Enter_the_Tenant_Info_Here" value with consumers.
45-
*
46-
* In Azure B2C, authority is of the form https://{instance}/tfp/{tenant}/{policyName}/ls
47-
*
48-
* @param {@link (Configuration:type)} configuration object for the MSAL PublicClientApplication instance
49-
*/
34+
* Constructor for the ClientApplication
35+
*/
5036
protected constructor(configuration: Configuration) {
5137
this.config = buildAppConfiguration(configuration);
5238

39+
this.cryptoProvider = new CryptoProvider();
40+
this.storage = new Storage(this.config.auth.clientId, this.config.cache!);
5341
B2cAuthority.setKnownAuthorities(this.config.auth.knownAuthorities!);
5442
}
5543

@@ -114,9 +102,9 @@ export abstract class ClientApplication {
114102
loggerCallback: this.config.system!.loggerOptions!.loggerCallback,
115103
piiLoggingEnabled: this.config.system!.loggerOptions!.piiLoggingEnabled,
116104
},
117-
cryptoInterface: new CryptoProvider(),
105+
cryptoInterface: this.cryptoProvider,
118106
networkInterface: this.config.system!.networkClient,
119-
storageInterface: new Storage(this.config.auth!.clientId, this.config.cache!),
107+
storageInterface: this.storage,
120108
libraryInfo: {
121109
sku: NodeConstants.MSAL_SKU,
122110
version: version,

lib/msal-node/src/client/PublicClientApplication.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,25 @@ import { ClientApplication } from './ClientApplication';
1212
* are not trusted to safely store application secrets, and therefore can only request tokens in the name of an user.
1313
*/
1414
export class PublicClientApplication extends ClientApplication {
15+
1516
/**
1617
* @constructor
17-
* Constructor for the PublicClientApplication
1818
*
19-
* Required attributes in the Configuration object are:
20-
* - clientID: the application ID of your application. You can obtain one by registering your application with our Application registration portal
19+
* Important attributes in the Configuration object for auth are:
20+
* - clientID: the application ID of your application. ou can obtain one by registering your application with our Application registration portal
2121
* - authority: the authority URL for your application.
2222
*
23-
* In Azure AD, authority is a URL indicating of the form https://login.microsoftonline.com/{Enter_the_Tenant_Info_Here}.
23+
* AAD authorities are of the form https://login.microsoftonline.com/{Enter_the_Tenant_Info_Here}
2424
* If your application supports Accounts in one organizational directory, replace "Enter_the_Tenant_Info_Here" value with the Tenant Id or Tenant name (for example, contoso.microsoft.com).
2525
* If your application supports Accounts in any organizational directory, replace "Enter_the_Tenant_Info_Here" value with organizations.
2626
* If your application supports Accounts in any organizational directory and personal Microsoft accounts, replace "Enter_the_Tenant_Info_Here" value with common.
2727
* To restrict support to Personal Microsoft accounts only, replace "Enter_the_Tenant_Info_Here" value with consumers.
2828
*
29-
* In Azure B2C, authority is of the form https://{instance}/tfp/{tenant}/{policyName}/
30-
* Full B2C functionality will be available in this library in future versions.
29+
* Azure B2C authorities are of the form https://{instance}/{tenant}/{policy}. Each policy is considered
30+
* it's own authority. You will have to set the all of the knownAuthorities at the time of the client application
31+
* construction
32+
*
33+
* ADFS authorities are of the form https://{instance}/adfs
3134
*
3235
* @param {@link (Configuration:type)} configuration object for the MSAL PublicClientApplication instance
3336
*/

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import { NetworkUtils } from '../utils/NetworkUtils';
1111
import { CACHE } from '../utils/Constants';
1212
import debug from "debug";
1313

14+
/**
15+
* - clientId - Client id of the application.
16+
* - authority - Url of the authority. If no value is set, defaults to https://login.microsoftonline.com/common.
17+
* - knownAuthorities - Needed for Azure B2C. All authorities that will be used in the client application.
18+
*/
1419
export type NodeAuthOptions = {
1520
clientId: string;
1621
authority?: string;
@@ -33,7 +38,7 @@ export type CacheOptions = {
3338
* Type for configuring logger and http client options
3439
*
3540
* - logger - Used to initialize the Logger object; TODO: Expand on logger details or link to the documentation on logger
36-
* - networkClient -
41+
* - networkClient - Http client used for all http get and post calls. Defaults to using MSAL's default http client.
3742
*/
3843
export type NodeSystemOptions = {
3944
loggerOptions?: LoggerOptions;
@@ -43,10 +48,9 @@ export type NodeSystemOptions = {
4348
/**
4449
* Use the configuration object to configure MSAL and initialize the client application object
4550
*
46-
* This object allows you to configure important elements of MSAL functionality:
4751
* - auth: this is where you configure auth elements like clientID, authority used for authenticating against the Microsoft Identity Platform
48-
* - cache: this is where you configure cache location and whether to store cache in cookies
49-
* - system: this is where you can configure the network client, logger, token renewal offset, and telemetry
52+
* - cache: this is where you configure cache location
53+
* - system: this is where you can configure the network client, logger
5054
*/
5155
export type Configuration = {
5256
auth: NodeAuthOptions;

lib/msal-node/src/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ export {
1111
// Request
1212
AuthorizationCodeRequest,
1313
DeviceCodeRequest,
14-
TokenExchangeParameters,
14+
RefreshTokenRequest,
15+
Prompt,
16+
ResponseMode,
1517
// Response
1618
AuthResponse,
1719
// Error

0 commit comments

Comments
 (0)