Skip to content

Commit b999ccf

Browse files
committed
Merge remote-tracking branch 'origin/sagonzal/device-code' into msal-node-logging
2 parents 11336f3 + bf0e3d8 commit b999ccf

31 files changed

+13672
-469
lines changed

lib/msal-common/package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/msal-common/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
"sinon": "^7.5.0",
8585
"tslib": "^1.10.0",
8686
"tslint": "^5.20.0",
87-
"typescript": "^3.6.4"
87+
"typescript": "^3.7.5"
8888
},
8989
"dependencies": {
9090
"debug": "^4.1.1"

lib/msal-common/src/authority/Authority.ts

-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ export abstract class Authority {
122122

123123
constructor(authority: string, networkInterface: INetworkModule) {
124124
this.canonicalAuthority = authority;
125-
console.log("Authority constructor: ", authority);
126125

127126
this._canonicalAuthority.validateAsUri();
128127
this.networkInterface = networkInterface;

lib/msal-common/src/client/AuthorizationCodeClient.ts

+42-37
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { BaseClient } from "./BaseClient";
77
import { AuthorizationCodeUrlRequest } from "../request/AuthorizationCodeUrlRequest";
88
import { AuthorizationCodeRequest } from "../request/AuthorizationCodeRequest";
99
import { Authority } from "../authority/Authority";
10-
import { ServerParamsGenerator } from "../server/ServerParamsGenerator";
10+
import { RequestUtils } from "../utils/RequestUtils";
1111
import { RequestValidator } from "../request/RequestValidator";
1212
import { GrantType } from "../utils/Constants";
1313
import { Configuration } from "../config/Configuration";
@@ -30,16 +30,20 @@ export class AuthorizationCodeClient extends BaseClient {
3030
}
3131

3232
/**
33-
* Creates a url for logging in a user.
34-
* - scopes added by default: openid, profile and offline_access.
35-
* - performs validation of the request parameters.
33+
* Creates the URL of the authorization request letting the user input credentials and consent to the
34+
* application. The URL target the /authorize endpoint of the authority configured in the
35+
* application object.
36+
*
37+
* Once the user inputs their credentials and consents, the authority will send a response to the redirect URI
38+
* sent in the request and should contain an authorization code, which can then be used to acquire tokens via
39+
* acquireToken(AuthorizationCodeRequest)
3640
* @param request
3741
*/
3842
async getAuthCodeUrl(request: AuthorizationCodeUrlRequest): Promise<string> {
3943

4044
const authority: Authority = await this.createAuthority(request && request.authority);
4145
const queryParams: Map<string, string> = this.generateAuthCodeUrlParams(request);
42-
const queryString = ServerParamsGenerator.createQueryString(queryParams);
46+
const queryString = RequestUtils.createQueryString(queryParams);
4347
return authority.authorizationEndpoint + "?" + queryString;
4448
}
4549

@@ -59,27 +63,25 @@ export class AuthorizationCodeClient extends BaseClient {
5963

6064
/**
6165
* Executes POST request to token endpoint
62-
* @param tokenEndPoint
63-
* @param body
64-
* @param headers
66+
* @param authority
67+
* @param request
6568
*/
66-
private async executeTokenRequest(authority: Authority, requestParameters: AuthorizationCodeRequest): Promise<string> {
69+
private async executeTokenRequest(authority: Authority, request: AuthorizationCodeRequest): Promise<string> {
6770

68-
const tokenParameters: Map<string, string> = this.generateAuthCodeParams(requestParameters);
69-
const headers: Map<string, string> = new Map<string, string>();
71+
const tokenParameters: Map<string, string> = this.generateAuthCodeParams(request);
72+
const requestBody = RequestUtils.createQueryString(tokenParameters);
73+
const headers: Map<string, string> = this.createDefaultTokenRequestHeaders();
7074

7175
let acquiredTokenResponse;
7276
try {
73-
acquiredTokenResponse = this.networkClient.sendPostRequestAsync<string>(
77+
acquiredTokenResponse = this.executePostToTokenEndpoint(
7478
authority.tokenEndpoint,
75-
{
76-
body: ServerParamsGenerator.createQueryString(tokenParameters),
77-
headers: this.createDefaultTokenRequestHeaders()
78-
}
79-
);
79+
requestBody,
80+
headers);
81+
8082
return acquiredTokenResponse;
8183
} catch (error) {
82-
console.log(error.response.data);
84+
console.log(error.response.data); // TODO use logger
8385
return error.response.data;
8486
}
8587
}
@@ -91,89 +93,92 @@ export class AuthorizationCodeClient extends BaseClient {
9193
private generateAuthCodeParams(request: AuthorizationCodeRequest) : Map<string, string> {
9294
const paramsMap: Map<string, string> = new Map<string, string>();
9395

94-
ServerParamsGenerator.addClientId(paramsMap, this.config.authOptions.clientId);
96+
RequestUtils.addClientId(paramsMap, this.config.authOptions.clientId);
9597

9698
// validate and add scopes
9799
const scopes = RequestValidator.validateAndGenerateScopes(
98100
request.scopes,
99101
this.config.authOptions.clientId
100102
);
101-
ServerParamsGenerator.addScopes(paramsMap, scopes);
103+
RequestUtils.addScopes(paramsMap, scopes);
102104

103105
// validate the redirectUri (to be a non null value)
104106
RequestValidator.validateRedirectUri(request.redirectUri);
105-
ServerParamsGenerator.addRedirectUri(paramsMap, request.redirectUri);
107+
RequestUtils.addRedirectUri(paramsMap, request.redirectUri);
106108

107109
// add code: user set, not validated
108-
ServerParamsGenerator.addAuthorizationCode(paramsMap, request.code);
110+
RequestUtils.addAuthorizationCode(paramsMap, request.code);
109111

110112
// add code_verifier if passed
111113
if (request.codeVerifier) {
112-
ServerParamsGenerator.addCodeVerifier(paramsMap, request.codeVerifier);
114+
RequestUtils.addCodeVerifier(paramsMap, request.codeVerifier);
113115
}
114116

115-
ServerParamsGenerator.addGrantType(paramsMap, GrantType.AUTHORIZATION_CODE_GRANT);
117+
RequestUtils.addGrantType(paramsMap, GrantType.AUTHORIZATION_CODE_GRANT);
116118

117119
return paramsMap;
118120
}
119121

120122
/**
121123
* This API validates the `AuthorizationCodeUrlRequest` and creates a URL
122124
* @param request
123-
* @param config
124125
*/
125126
private generateAuthCodeUrlParams(request: AuthorizationCodeUrlRequest): Map<string, string>{
126127
const paramsMap = new Map<string, string>();
127128

128-
ServerParamsGenerator.addClientId(paramsMap, this.config.authOptions.clientId);
129+
RequestUtils.addClientId(paramsMap, this.config.authOptions.clientId);
129130

130131
// validate and add scopes
131132
const scopes = RequestValidator.validateAndGenerateScopes(
132133
request.scopes,
133134
this.config.authOptions.clientId
134135
);
135-
ServerParamsGenerator.addScopes(paramsMap, scopes);
136+
RequestUtils.addScopes(paramsMap, scopes);
136137

137138
// validate the redirectUri (to be a non null value)
138139
RequestValidator.validateRedirectUri(request.redirectUri);
139-
ServerParamsGenerator.addRedirectUri(paramsMap, request.redirectUri);
140+
RequestUtils.addRedirectUri(paramsMap, request.redirectUri);
140141

141142
// generate the correlationId if not set by the user and add
142143
const correlationId = request.correlationId
143144
? request.correlationId
144145
: this.config.cryptoInterface.createNewGuid();
145-
ServerParamsGenerator.addCorrelationId(paramsMap, correlationId);
146+
RequestUtils.addCorrelationId(paramsMap, correlationId);
146147

147148
// add response_mode = fragment (currently hardcoded, have a future option to pass 'query' if the user chooses to)
148-
ServerParamsGenerator.addResponseMode(paramsMap);
149+
RequestUtils.addResponseMode(paramsMap, request.responseMode);
149150

150151
// add response_type = code
151-
ServerParamsGenerator.addResponseTypeCode(paramsMap);
152+
RequestUtils.addResponseTypeCode(paramsMap);
152153

153154
if (request.codeChallenge) {
154155
RequestValidator.validateCodeChallengeParams(request.codeChallenge, request.codeChallengeMethod);
155-
ServerParamsGenerator.addCodeChallengeParams(paramsMap, request.codeChallenge, request.codeChallengeMethod);
156+
RequestUtils.addCodeChallengeParams(paramsMap, request.codeChallenge, request.codeChallengeMethod);
156157
}
157158

158159
if (request.state) {
159-
ServerParamsGenerator.addState(paramsMap, request.state);
160+
RequestUtils.addState(paramsMap, request.state);
160161
}
161162

162163
if (request.prompt) {
163164
RequestValidator.validatePrompt(request.prompt);
164-
ServerParamsGenerator.addPrompt(paramsMap, request.prompt);
165+
RequestUtils.addPrompt(paramsMap, request.prompt);
165166
}
166167

167168
if (request.loginHint) {
168-
ServerParamsGenerator.addLoginHint(paramsMap, request.loginHint);
169+
RequestUtils.addLoginHint(paramsMap, request.loginHint);
169170
}
170171

171172
if (request.domainHint) {
172-
ServerParamsGenerator.addDomainHint(paramsMap, request.domainHint);
173+
RequestUtils.addDomainHint(paramsMap, request.domainHint);
173174
}
174175

175176
if (request.nonce) {
176-
ServerParamsGenerator.addNonce(paramsMap, request.nonce);
177+
RequestUtils.addNonce(paramsMap, request.nonce);
178+
}
179+
180+
if(request.claims) {
181+
RequestUtils.addClaims(paramsMap, request.claims);
177182
}
178183

179184
return paramsMap;

lib/msal-common/src/client/AuthorizationCodeFlow.ts

-131
This file was deleted.

lib/msal-common/src/client/BaseClient.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Logger } from "../logger/Logger";
1313
import { AuthorityFactory } from "../authority/AuthorityFactory";
1414
import { Constants } from "../utils/Constants";
1515
import {ClientAuthError} from "../error/ClientAuthError";
16-
import {ServerParamsGenerator} from "../server/ServerParamsGenerator";
16+
import {RequestUtils} from "../utils/RequestUtils";
1717

1818
/**
1919
* @hidden
@@ -108,8 +108,27 @@ export abstract class BaseClient {
108108
*/
109109
protected createDefaultTokenRequestHeaders(): Map<string, string> {
110110
const headers = new Map<string, string>();
111-
ServerParamsGenerator.addContentTypeHeader(headers);
112-
ServerParamsGenerator.addLibrarydataHeaders(headers);
111+
RequestUtils.addContentTypeHeader(headers);
112+
RequestUtils.addLibrarydataHeaders(headers);
113113
return headers;
114114
}
115+
116+
/**
117+
* Http post to token endpoint
118+
* @param tokenEndpoint
119+
* @param queryString
120+
* @param headers
121+
*/
122+
protected async executePostToTokenEndpoint(
123+
tokenEndpoint: string,
124+
queryString: string,
125+
headers: Map<string, string> ): Promise<string> {
126+
127+
return this.networkClient.sendPostRequestAsync<string>(
128+
tokenEndpoint,
129+
{
130+
body: queryString,
131+
headers: headers,
132+
});
133+
}
115134
}

0 commit comments

Comments
 (0)