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

Client Assertion implementation now accepts an async callback as well as a string argument #7014

Merged
merged 25 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c8e9c68
client assertion accepts callback instead of string argument
Robbie-Microsoft Apr 9, 2024
ab837fa
Change files
Robbie-Microsoft Apr 9, 2024
95781be
Merge branch 'dev' into client_assertion
Robbie-Microsoft Apr 9, 2024
f07f039
fixed broken unit tests
Robbie-Microsoft Apr 9, 2024
db71561
Merge branch 'dev' into client_assertion
Robbie-Microsoft Apr 9, 2024
efb092e
Merge branch 'dev' into client_assertion
Robbie-Microsoft Apr 9, 2024
8b70f4d
Pinned identity version in e2e samples
Robbie-Microsoft Apr 10, 2024
e23fe53
undo accidental package bump
Robbie-Microsoft Apr 10, 2024
e182749
Merge branch 'dev' into client_assertion
Robbie-Microsoft Apr 10, 2024
08ff604
Merge branch 'dev' into client_assertion
Robbie-Microsoft Apr 11, 2024
db2154b
Implemented Feedback in msal-common files
Robbie-Microsoft Apr 11, 2024
a1a91eb
Moved confidential client's setClientCredential from its contructor t…
Robbie-Microsoft Apr 23, 2024
a4dad0d
Merge branch 'dev' into client_assertion
Robbie-Microsoft Apr 23, 2024
7968ac3
Implemented GitHub Feedback
Robbie-Microsoft Apr 24, 2024
e303ea3
unit tests
Robbie-Microsoft Apr 25, 2024
76829e5
Updated tests
Robbie-Microsoft Apr 25, 2024
3250219
Merge branch 'dev' into client_assertion
Robbie-Microsoft Apr 26, 2024
561d01d
refactored code and made improvements
Robbie-Microsoft Apr 26, 2024
973f705
removed comment
Robbie-Microsoft Apr 26, 2024
e90cea2
Merge branch 'dev' into client_assertion
Robbie-Microsoft Apr 26, 2024
dd7bef9
Merge branch 'dev' into client_assertion
Robbie-Microsoft Apr 30, 2024
0de0a86
Merge branch 'dev' into client_assertion
Robbie-Microsoft Apr 30, 2024
6b23035
Merge branch 'dev' into client_assertion
Robbie-Microsoft Apr 30, 2024
d0e6c95
updated comment
Robbie-Microsoft Apr 30, 2024
f4a6721
fixed broken unit tests
Robbie-Microsoft Apr 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Client Assertion Implementation now accepts a callback instead of a string argument",
"packageName": "@azure/msal-common",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Client Assertion Implementation now accepts a callback instead of a string argument",
"packageName": "@azure/msal-node",
"email": "[email protected]",
"dependentChangeType": "patch"
}
11 changes: 10 additions & 1 deletion lib/msal-common/src/account/ClientCredentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
* Licensed under the MIT License.
*/

export type ClientAssertionConfig = {
clientId: string;
tokenEndpoint?: string;
};

export type ClientAssertionCallbackFunction = (
Robbie-Microsoft marked this conversation as resolved.
Show resolved Hide resolved
config: ClientAssertionConfig
) => Promise<string>;

/**
* Client Assertion credential for Confidential Clients
*/
export type ClientAssertion = {
assertion: string;
Robbie-Microsoft marked this conversation as resolved.
Show resolved Hide resolved
assertion: string | ClientAssertionCallbackFunction;
assertionType: string;
};

Expand Down
13 changes: 11 additions & 2 deletions lib/msal-common/src/client/AuthorizationCodeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ import { RequestValidator } from "../request/RequestValidator";
import { IPerformanceClient } from "../telemetry/performance/IPerformanceClient";
import { PerformanceEvents } from "../telemetry/performance/PerformanceEvent";
import { invokeAsync } from "../utils/FunctionWrappers";
import { ClientAssertion } from "../account/ClientCredentials";
import { getClientAssertion } from "../utils/ClientAssertionUtils";

/**
* Oauth2.0 Authorization Code client
Expand Down Expand Up @@ -364,9 +366,16 @@ export class AuthorizationCodeClient extends BaseClient {
}

if (this.config.clientCredentials.clientAssertion) {
const clientAssertion =
const clientAssertion: ClientAssertion =
this.config.clientCredentials.clientAssertion;
parameterBuilder.addClientAssertion(clientAssertion.assertion);

parameterBuilder.addClientAssertion(
await getClientAssertion(
clientAssertion.assertion,
this.config.authOptions.clientId,
request.resourceRequestUri
)
);
parameterBuilder.addClientAssertionType(
clientAssertion.assertionType
);
Expand Down
13 changes: 11 additions & 2 deletions lib/msal-common/src/client/RefreshTokenClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import { PerformanceEvents } from "../telemetry/performance/PerformanceEvent";
import { IPerformanceClient } from "../telemetry/performance/IPerformanceClient";
import { invoke, invokeAsync } from "../utils/FunctionWrappers";
import { generateCredentialKey } from "../cache/utils/CacheHelpers";
import { ClientAssertion } from "../account/ClientCredentials";
import { getClientAssertion } from "../utils/ClientAssertionUtils";

const DEFAULT_REFRESH_TOKEN_EXPIRATION_OFFSET_SECONDS = 300; // 5 Minutes

Expand Down Expand Up @@ -385,9 +387,16 @@ export class RefreshTokenClient extends BaseClient {
}

if (this.config.clientCredentials.clientAssertion) {
const clientAssertion =
const clientAssertion: ClientAssertion =
this.config.clientCredentials.clientAssertion;
parameterBuilder.addClientAssertion(clientAssertion.assertion);

parameterBuilder.addClientAssertion(
await getClientAssertion(
clientAssertion.assertion,
this.config.authOptions.clientId,
request.resourceRequestUri
)
);
parameterBuilder.addClientAssertionType(
clientAssertion.assertionType
);
Expand Down
7 changes: 6 additions & 1 deletion lib/msal-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ export { NativeRequest } from "./request/NativeRequest";
export { NativeSignOutRequest } from "./request/NativeSignOutRequest";
export { RequestParameterBuilder } from "./request/RequestParameterBuilder";
export { StoreInCache } from "./request/StoreInCache";
export { ClientAssertion } from "./account/ClientCredentials";
export {
ClientAssertion,
ClientAssertionCallbackFunction,
} from "./account/ClientCredentials";
// Response
export { AzureRegion } from "./authority/AzureRegion";
export { AzureRegionConfiguration } from "./authority/AzureRegionConfiguration";
Expand Down Expand Up @@ -182,6 +185,7 @@ export {
createClientConfigurationError,
} from "./error/ClientConfigurationError";
// Constants and Utils
export { getClientAssertion } from "./utils/ClientAssertionUtils";
export {
Constants,
OIDC_DEFAULT_SCOPES,
Expand Down Expand Up @@ -218,6 +222,7 @@ export {
} from "./utils/ProtocolUtils";
export * as TimeUtils from "./utils/TimeUtils";
export * as UrlUtils from "./utils/UrlUtils";
export * as ClientAssertionUtils from "./utils/ClientAssertionUtils";
export * from "./utils/FunctionWrappers";
// Server Telemetry
export { ServerTelemetryManager } from "./telemetry/server/ServerTelemetryManager";
Expand Down
25 changes: 25 additions & 0 deletions lib/msal-common/src/utils/ClientAssertionUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

import {
ClientAssertionCallbackFunction,
ClientAssertionConfig,
} from "../account/ClientCredentials";

export async function getClientAssertion(
clientAssertion: string | ClientAssertionCallbackFunction,
clientId: string,
tokenEndpoint?: string
): Promise<string> {
if (typeof clientAssertion === "string") {
return clientAssertion;
} else {
const config: ClientAssertionConfig = {
clientId: clientId,
tokenEndpoint: tokenEndpoint,
};
return clientAssertion(config);
}
}
99 changes: 93 additions & 6 deletions lib/msal-common/test/request/RequestParameterBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import {
ClientConfigurationErrorMessage,
createClientConfigurationError,
} from "../../src/error/ClientConfigurationError";
import { ClientAssertion, ClientAssertionCallbackFunction } from "../../src";
import { getClientAssertion } from "../../src/utils/ClientAssertionUtils";
import { ClientAssertionConfig } from "../../src/account/ClientCredentials";

describe("RequestParameterBuilder unit tests", () => {
it("constructor", () => {
Expand Down Expand Up @@ -379,14 +382,20 @@ describe("RequestParameterBuilder unit tests", () => {
sinon.restore();
});

it("adds clientAssertion and assertionType if they are passed in as strings", () => {
const clientAssertion = {
it("adds clientAssertion (string) and assertionType if they are provided by the developer", async () => {
const clientAssertion: ClientAssertion = {
assertion: "testAssertion",
assertionType: "jwt-bearer",
};

const requestParameterBuilder = new RequestParameterBuilder();
requestParameterBuilder.addClientAssertion(clientAssertion.assertion);
requestParameterBuilder.addClientAssertion(
await getClientAssertion(
clientAssertion.assertion,
"client_id",
"optional_token_endpoint"
)
);
requestParameterBuilder.addClientAssertionType(
clientAssertion.assertionType
);
Expand All @@ -407,14 +416,92 @@ describe("RequestParameterBuilder unit tests", () => {
).toBe(true);
});

it("doesn't add client assertion and client assertion type if they are empty strings", () => {
const clientAssertion = {
it("doesn't add client assertion (string) and client assertion type if they are empty strings", async () => {
const clientAssertion: ClientAssertion = {
assertion: "",
assertionType: "",
};

const requestParameterBuilder = new RequestParameterBuilder();
requestParameterBuilder.addClientAssertion(clientAssertion.assertion);
requestParameterBuilder.addClientAssertion(
await getClientAssertion(
clientAssertion.assertion,
"client_id",
"optional_token_endpoint"
)
);
requestParameterBuilder.addClientAssertionType(
clientAssertion.assertionType
);
const requestQueryString = requestParameterBuilder.createQueryString();
expect(
requestQueryString.includes(AADServerParamKeys.CLIENT_ASSERTION)
).toBe(false);
expect(
requestQueryString.includes(
AADServerParamKeys.CLIENT_ASSERTION_TYPE
)
).toBe(false);
});

it("adds clientAssertion (ClientAssertionCallbackFunction) and assertionType if they are provided by the developer", async () => {
const clientAssertionCallbackFunction: ClientAssertionCallbackFunction =
(_config: ClientAssertionConfig) => {
return Promise.resolve("testAssertion");
Robbie-Microsoft marked this conversation as resolved.
Show resolved Hide resolved
};

const clientAssertion: ClientAssertion = {
assertion: clientAssertionCallbackFunction,
assertionType: "jwt-bearer",
};

const requestParameterBuilder = new RequestParameterBuilder();
requestParameterBuilder.addClientAssertion(
await getClientAssertion(
clientAssertion.assertion,
"client_id",
"optional_token_endpoint"
)
);
requestParameterBuilder.addClientAssertionType(
clientAssertion.assertionType
);
const requestQueryString = requestParameterBuilder.createQueryString();
expect(
requestQueryString.includes(
`${AADServerParamKeys.CLIENT_ASSERTION}=${encodeURIComponent(
"testAssertion"
)}`
)
).toBe(true);
expect(
requestQueryString.includes(
`${
AADServerParamKeys.CLIENT_ASSERTION_TYPE
}=${encodeURIComponent("jwt-bearer")}`
)
).toBe(true);
});

it("doesn't add client assertion (ClientAssertionCallbackFunction) and client assertion type if they are empty strings", async () => {
const clientAssertionCallbackFunction: ClientAssertionCallbackFunction =
(_config: ClientAssertionConfig) => {
return Promise.resolve("");
};

const clientAssertion: ClientAssertion = {
assertion: clientAssertionCallbackFunction,
assertionType: "",
};

const requestParameterBuilder = new RequestParameterBuilder();
requestParameterBuilder.addClientAssertion(
await getClientAssertion(
clientAssertion.assertion,
"client_id",
"optional_token_endpoint"
)
);
requestParameterBuilder.addClientAssertionType(
clientAssertion.assertionType
);
Expand Down
108 changes: 56 additions & 52 deletions lib/msal-node/api/msal-node.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,61 @@
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
Robbie-Microsoft marked this conversation as resolved.
Show resolved Hide resolved

```ts
import { AccessTokenCache } from "@azure/msal-common";
import { AccessTokenEntity } from "@azure/msal-common";
import { AccountCache } from "@azure/msal-common";
import { AccountEntity } from "@azure/msal-common";
import { AccountInfo } from "@azure/msal-common";
import { AppMetadataCache } from "@azure/msal-common";
import { AppMetadataEntity } from "@azure/msal-common";
import { AuthenticationResult } from "@azure/msal-common";
import { AuthError } from "@azure/msal-common";
import { AuthErrorMessage } from "@azure/msal-common";
import { AuthorityMetadataEntity } from "@azure/msal-common";
import { BaseAuthRequest } from "@azure/msal-common";
import { CacheManager } from "@azure/msal-common";
import { ClientAuthError } from "@azure/msal-common";
import { ClientAuthErrorMessage } from "@azure/msal-common";
import { ClientConfiguration } from "@azure/msal-common";
import { ClientConfigurationError } from "@azure/msal-common";
import { ClientConfigurationErrorMessage } from "@azure/msal-common";
import { CommonAuthorizationCodeRequest } from "@azure/msal-common";
import { CommonAuthorizationUrlRequest } from "@azure/msal-common";
import { CommonClientCredentialRequest } from "@azure/msal-common";
import { CommonDeviceCodeRequest } from "@azure/msal-common";
import { CommonOnBehalfOfRequest } from "@azure/msal-common";
import { CommonRefreshTokenRequest } from "@azure/msal-common";
import { CommonSilentFlowRequest } from "@azure/msal-common";
import { CommonUsernamePasswordRequest } from "@azure/msal-common";
import { DeviceCodeResponse } from "@azure/msal-common";
import { ICachePlugin } from "@azure/msal-common";
import { ICrypto } from "@azure/msal-common";
import { IdTokenCache } from "@azure/msal-common";
import { IdTokenEntity } from "@azure/msal-common";
import { INetworkModule } from "@azure/msal-common";
import { InteractionRequiredAuthError } from "@azure/msal-common";
import { ISerializableTokenCache } from "@azure/msal-common";
import { Logger } from "@azure/msal-common";
import { LoggerOptions } from "@azure/msal-common";
import { LogLevel } from "@azure/msal-common";
import { NetworkRequestOptions } from "@azure/msal-common";
import { NetworkResponse } from "@azure/msal-common";
import { PkceCodes } from "@azure/msal-common";
import { PromptValue } from "@azure/msal-common";
import { ProtocolMode } from "@azure/msal-common";
import { RefreshTokenCache } from "@azure/msal-common";
import { RefreshTokenEntity } from "@azure/msal-common";
import { ResponseMode } from "@azure/msal-common";
import { ServerError } from "@azure/msal-common";
import { ServerTelemetryEntity } from "@azure/msal-common";
import { ServerTelemetryManager } from "@azure/msal-common";
import { ThrottlingEntity } from "@azure/msal-common";
import { TokenCacheContext } from "@azure/msal-common";
import { ValidCacheType } from "@azure/msal-common";
import {
AccessTokenCache,
AccessTokenEntity,
AccountCache,
AccountEntity,
AccountInfo,
AppMetadataCache,
AppMetadataEntity,
AuthenticationResult,
AuthError,
AuthErrorMessage,
AuthorityMetadataEntity,
BaseAuthRequest,
CacheManager,
ClientAssertion,
ClientAssertionCallbackFunction,
ClientAuthError,
ClientAuthErrorMessage,
ClientConfiguration,
ClientConfigurationError,
ClientConfigurationErrorMessage,
CommonAuthorizationCodeRequest,
CommonAuthorizationUrlRequest,
CommonClientCredentialRequest,
CommonDeviceCodeRequest,
CommonOnBehalfOfRequest,
CommonRefreshTokenRequest,
CommonSilentFlowRequest,
CommonUsernamePasswordRequest,
DeviceCodeResponse,
ICachePlugin,
ICrypto,
IdTokenCache,
IdTokenEntity,
INetworkModule,
InteractionRequiredAuthError,
ISerializableTokenCache,
Logger,
LoggerOptions,
LogLevel,
NetworkRequestOptions,
NetworkResponse,
PkceCodes,
PromptValue,
ProtocolMode,
RefreshTokenCache,
RefreshTokenEntity,
ResponseMode,
ServerError,
ServerTelemetryEntity,
ServerTelemetryManager,
ThrottlingEntity,
TokenCacheContext,
ValidCacheType,
} from "@azure/msal-common";

export { AccountInfo };

Expand Down Expand Up @@ -318,7 +322,7 @@ export type NodeAuthOptions = {
clientId: string;
authority?: string;
clientSecret?: string;
clientAssertion?: string;
clientAssertion?: string | ClientAssertion;
clientCertificate?: {
thumbprint: string;
privateKey: string;
Expand Down
Loading
Loading