Skip to content

Commit c47b2dd

Browse files
SantiagoSantiago
Santiago
authored and
Santiago
committed
PR feedback #2
1 parent f0f8c7a commit c47b2dd

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

lib/msal-common/src/error/ClientAuthError.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export const ClientAuthErrorMessage = {
153153
},
154154
invalidClientCredential: {
155155
code: "invalid_client_credential",
156-
desc: "Client credential (secret, certificate, or assertion) must not be empty when creating a confidential client"
156+
desc: "Client credential (secret, certificate, or assertion) must not be empty when creating a confidential client. An application should at most have one credential"
157157
}
158158
};
159159

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

+19-10
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,35 @@ export class ConfidentialClientApplication extends ClientApplication {
3636
}
3737

3838
private setClientCredential(configuration: Configuration): void {
39-
if (!StringUtils.isEmpty(configuration.auth!.clientSecret!)) {
39+
40+
const clientSecretNotEmpty = !StringUtils.isEmpty(configuration.auth.clientSecret!);
41+
const clientAssertionNotEmpty = !StringUtils.isEmpty(configuration.auth.clientAssertion!);
42+
const certificate = configuration.auth.clientCertificate!;
43+
const certificateNotEmpty = !StringUtils.isEmpty(certificate.thumbprint) || !StringUtils.isEmpty(certificate.privateKey);
44+
45+
// Check that at most one credential is set on the application
46+
if (
47+
clientSecretNotEmpty && clientAssertionNotEmpty ||
48+
clientAssertionNotEmpty && certificateNotEmpty ||
49+
clientSecretNotEmpty && certificateNotEmpty) {
50+
throw ClientAuthError.createInvalidCredentialError();
51+
}
52+
53+
if (clientSecretNotEmpty) {
4054
this.clientSecret = configuration.auth.clientSecret!;
4155
return;
4256
}
4357

44-
if (!StringUtils.isEmpty(configuration.auth.clientAssertion!)) {
58+
if (clientAssertionNotEmpty) {
4559
this.clientAssertion = ClientAssertion.fromAssertion(configuration.auth.clientAssertion!);
4660
return;
4761
}
4862

49-
if (configuration.auth.clientCertificate != null) {
50-
const certificate = configuration.auth.clientCertificate;
51-
if (StringUtils.isEmpty(certificate.thumbprint) || StringUtils.isEmpty(certificate.privateKey)) {
52-
throw ClientAuthError.createInvalidCredentialError();
53-
}
54-
63+
if (!certificateNotEmpty) {
64+
throw ClientAuthError.createInvalidCredentialError();
65+
} else {
5566
this.clientAssertion = ClientAssertion.fromCertificate(certificate.thumbprint, certificate.privateKey);
56-
return;
5767
}
58-
throw ClientAuthError.createInvalidCredentialError();
5968
}
6069
}
6170

lib/msal-node/test/client/ConfidentialClientApplication.spec.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { ConfidentialClientApplication } from './../../src/client/ConfidentialClientApplication';
2-
import { Authority, ClientConfiguration, AuthorizationCodeRequest, AuthorityFactory, AuthorizationCodeClient, RefreshTokenRequest, RefreshTokenClient } from '@azure/msal-common';
2+
import { Authority, ClientConfiguration, AuthorizationCodeRequest, AuthorityFactory, AuthorizationCodeClient, RefreshTokenRequest, RefreshTokenClient, StringUtils } from '@azure/msal-common';
33
import { TEST_CONSTANTS } from '../utils/TestConstants';
44
import { Configuration } from "../../src/config/Configuration";
55
import { mocked } from 'ts-jest/utils';
66

77
jest.mock('@azure/msal-common');
88

9+
mocked(StringUtils.isEmpty).mockImplementation((str) => {
10+
return (typeof str === "undefined" || !str || 0 === str.length);
11+
});
12+
913
describe('ConfidentialClientApplication', () => {
1014
const authority: Authority = {
1115
resolveEndpointsAsync: () => {
@@ -37,12 +41,12 @@ describe('ConfidentialClientApplication', () => {
3741
clientSecret: TEST_CONSTANTS.CLIENT_SECRET
3842
}
3943
};
40-
44+
4145
test('exports a class', () => {
4246
const authApp = new ConfidentialClientApplication(appConfig);
4347
expect(authApp).toBeInstanceOf(ConfidentialClientApplication);
4448
});
45-
49+
4650
test('acquireTokenByAuthorizationCode', async () => {
4751
const request: AuthorizationCodeRequest = {
4852
scopes: TEST_CONSTANTS.DEFAULT_GRAPH_SCOPE,
@@ -59,7 +63,7 @@ describe('ConfidentialClientApplication', () => {
5963
expect.objectContaining(expectedConfig)
6064
);
6165
});
62-
66+
6367
test('acquireTokenByRefreshToken', async () => {
6468
const request: RefreshTokenRequest = {
6569
scopes: TEST_CONSTANTS.DEFAULT_GRAPH_SCOPE,

0 commit comments

Comments
 (0)