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-node][msal-common] Add device code response in DeviceCodeClient.ts #1947

Merged
merged 9 commits into from
Jul 20, 2020
22 changes: 19 additions & 3 deletions lib/msal-common/src/client/DeviceCodeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { ClientConfiguration } from "../config/ClientConfiguration";
import { TimeUtils } from "../utils/TimeUtils";
import { ServerAuthorizationTokenResponse } from "../server/ServerAuthorizationTokenResponse";
import { ScopeSet } from "../request/ScopeSet";
import { ResponseHandler } from "../response/ResponseHandler";
import { AuthenticationResult } from '../response/AuthenticationResult';

/**
* OAuth2.0 Device code client
Expand All @@ -28,16 +30,29 @@ export class DeviceCodeClient extends BaseClient {
* polls token endpoint to exchange device code for tokens
* @param request
*/
public async acquireToken(request: DeviceCodeRequest): Promise<string> {
public async acquireToken(request: DeviceCodeRequest): Promise<AuthenticationResult> {

const deviceCodeResponse: DeviceCodeResponse = await this.getDeviceCode(request);
request.deviceCodeCallback(deviceCodeResponse);
const response: ServerAuthorizationTokenResponse = await this.acquireTokenWithDeviceCode(
request,
deviceCodeResponse);

// TODO handle response
return JSON.stringify(response);
const responseHandler = new ResponseHandler(
this.config.authOptions.clientId,
this.cacheManager,
this.cryptoUtils,
this.logger
);

// Validate response. This function throws a server error if an error is returned by the server.
responseHandler.validateTokenResponse(response);
const tokenResponse = responseHandler.handleServerTokenResponse(
response,
this.authority
);

return tokenResponse;
}

/**
Expand Down Expand Up @@ -174,6 +189,7 @@ export class DeviceCodeClient extends BaseClient {
requestParameters.addDeviceCode(deviceCodeResponse.deviceCode);
const correlationId = request.correlationId || this.config.cryptoInterface.createNewGuid();
requestParameters.addCorrelationId(correlationId);
requestParameters.addClientInfo();
return requestParameters.createQueryString();
}
}
4 changes: 2 additions & 2 deletions lib/msal-node/src/client/PublicClientApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License.
*/

import { DeviceCodeClient, DeviceCodeRequest } from '@azure/msal-common';
import { DeviceCodeClient, DeviceCodeRequest, AuthenticationResult } from '@azure/msal-common';
import { Configuration } from '../config/Configuration';
import { ClientApplication } from './ClientApplication';

Expand Down Expand Up @@ -42,7 +42,7 @@ export class PublicClientApplication extends ClientApplication {
* Since the client cannot receive incoming requests, it polls the authorization server repeatedly
* until the end-user completes input of credentials.
*/
public async acquireTokenByDeviceCode(request: DeviceCodeRequest): Promise<string> {
public async acquireTokenByDeviceCode(request: DeviceCodeRequest): Promise<AuthenticationResult> {
this.logger.info("acquireTokenByDeviceCode called");
const deviceCodeConfig = await this.buildOauthClientConfiguration(
request.authority
Expand Down
2 changes: 1 addition & 1 deletion samples/msal-node-extensions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"author": "Microsoft",
"license": "MIT",
"dependencies": {
"@azure/msal-node": "^0.1.0",
"@azure/msal-node": "^1.0.0-alpha.0",
"express": "^4.17.1",
"@azure/msal-node-extensions": "0.1.0"
}
Expand Down