Skip to content

Added Constants for Encoding Types #7766

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

Merged
merged 36 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
920bac3
Initial Implementation
Robbie-Microsoft Apr 2, 2025
73ffd22
Change files
Robbie-Microsoft Apr 2, 2025
f4f002a
Improvements
Robbie-Microsoft Apr 2, 2025
468de01
Improvements
Robbie-Microsoft Apr 2, 2025
dcd382f
Added unit tests
Robbie-Microsoft Apr 3, 2025
d8fbbca
Merge branch 'dev' into msi_v1_token_revocation
Robbie-Microsoft Apr 3, 2025
b09a464
Implemented some github feedback
Robbie-Microsoft Apr 3, 2025
0f7b2b9
Implemented github feedback
Robbie-Microsoft Apr 3, 2025
b32f45e
package-lock
Robbie-Microsoft Apr 3, 2025
604194d
Defined sha256 hash for test access token
Robbie-Microsoft Apr 4, 2025
3405bce
Switched to hex encoding, instead of base64, for the sha256 hash
Robbie-Microsoft Apr 7, 2025
f1d096f
Change files
Robbie-Microsoft Apr 7, 2025
203d5d0
Merge branch 'dev' into msi_v1_token_revocation
Robbie-Microsoft Apr 7, 2025
d9f15e6
Merge branch 'dev' into msi_v1_token_revocation
Robbie-Microsoft Apr 17, 2025
0832c02
Merge branch 'dev' into msi_v1_token_revocation
Robbie-Microsoft Apr 22, 2025
753ec2b
Merge branch 'dev' into msi_v1_token_revocation
Robbie-Microsoft May 2, 2025
aa2a42d
Merge branch 'dev' into msi_v1_token_revocation
Robbie-Microsoft May 19, 2025
659928c
Added constants for encoding types
Robbie-Microsoft May 20, 2025
0f15894
Merge branch 'dev' into msi_v1_token_revocation
Robbie-Microsoft May 21, 2025
091058f
Merge branch 'dev' into EncodingTypesConstants
Robbie-Microsoft May 21, 2025
3af69a5
ran apiExtractor
Robbie-Microsoft May 21, 2025
b3eb89c
Merge branch 'msi_v1_token_revocation' into EncodingTypesConstants
Robbie-Microsoft May 21, 2025
b48caf9
Implemented Neha's feedback
Robbie-Microsoft May 21, 2025
865697d
Merge branch 'dev' into msi_v1_token_revocation
Robbie-Microsoft May 21, 2025
2295235
Removed app service
Robbie-Microsoft May 21, 2025
c34e1e4
undid code deletion
Robbie-Microsoft May 21, 2025
f9b13fb
Added back happy path test
Robbie-Microsoft May 22, 2025
e7e0af7
added additional unit test
Robbie-Microsoft May 22, 2025
4a59853
typos
Robbie-Microsoft May 22, 2025
a3434fa
Merge branch 'dev' into msi_v1_token_revocation
Robbie-Microsoft May 22, 2025
669120f
Merge branch 'msi_v1_token_revocation' into EncodingTypesConstants
Robbie-Microsoft May 22, 2025
77c94d0
fixed incorrect comment
Robbie-Microsoft May 23, 2025
b29be2f
Merge branch 'dev' into msi_v1_token_revocation
Robbie-Microsoft May 27, 2025
5554ddf
Merge branch 'msi_v1_token_revocation' into EncodingTypesConstants
Robbie-Microsoft May 27, 2025
f203261
Change files
Robbie-Microsoft May 27, 2025
6e1f214
Merge branch 'dev' into EncodingTypesConstants
Robbie-Microsoft May 28, 2025
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": "none",
"comment": "Added Constants for Encoding Types #7766",
"packageName": "@azure/msal-common",
"email": "[email protected]",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "Added Constants for Encoding Types #7766",
"packageName": "@azure/msal-node",
"email": "[email protected]",
"dependentChangeType": "none"
}
2 changes: 2 additions & 0 deletions lib/msal-common/apiReview/msal-common.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2221,7 +2221,9 @@ const emptyInputScopeSet = "empty_input_scopeset";
//
// @public (undocumented)
export const EncodingTypes: {
readonly BASE64: "base64";
readonly HEX: "hex";
readonly UTF8: "utf-8";
};

// @public (undocumented)
Expand Down
2 changes: 2 additions & 0 deletions lib/msal-common/src/utils/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ export const ONE_DAY_IN_MS = 86400000;
export const DEFAULT_TOKEN_RENEWAL_OFFSET_SEC = 300;

export const EncodingTypes = {
BASE64: "base64",
HEX: "hex",
UTF8: "utf-8",
} as const;
export type EncodingTypes = (typeof EncodingTypes)[keyof typeof EncodingTypes];
17 changes: 10 additions & 7 deletions lib/msal-common/test/client/ClientTestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
createClientAuthError,
} from "../../src/error/ClientAuthError.js";
import { ServerTelemetryManager } from "../../src/telemetry/server/ServerTelemetryManager.js";
import { Constants } from "../../src/utils/Constants.js";
import { Constants, EncodingTypes } from "../../src/utils/Constants.js";
import { AuthorityOptions } from "../../src/authority/AuthorityOptions.js";
import { TokenKeys } from "../../src/cache/utils/CacheTypes.js";

Expand Down Expand Up @@ -196,18 +196,21 @@ export const mockCrypto = {
return RANDOM_TEST_GUID;
},
base64Decode(input: string): string {
return Buffer.from(input, "base64").toString("utf8");
return Buffer.from(input, EncodingTypes.BASE64).toString("utf8");
},
base64Encode(input: string): string {
return Buffer.from(input, "utf-8").toString("base64");
return Buffer.from(input, EncodingTypes.UTF8).toString(
EncodingTypes.BASE64
);
},
base64UrlEncode(input: string): string {
return Buffer.from(input, "utf-8").toString("base64url");
return Buffer.from(input, EncodingTypes.UTF8).toString("base64url");
},
encodeKid(input: string): string {
return Buffer.from(JSON.stringify({ kid: input }), "utf-8").toString(
"base64url"
);
return Buffer.from(
JSON.stringify({ kid: input }),
EncodingTypes.UTF8
).toString("base64url");
},
async getPublicKeyThumbprint(): Promise<string> {
return TEST_POP_VALUES.KID;
Expand Down
3 changes: 2 additions & 1 deletion lib/msal-node/src/client/ClientAssertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Constants,
createClientAuthError,
ClientAuthErrorCodes,
EncodingTypes,
} from "@azure/msal-common/node";
import { CryptoProvider } from "../crypto/CryptoProvider.js";
import { EncodingUtils } from "../utils/EncodingUtils.js";
Expand Down Expand Up @@ -145,7 +146,7 @@ export class ClientAssertion {
Object.assign(header, {
[thumbprintHeader]: EncodingUtils.base64EncodeUrl(
this.thumbprint,
"hex"
EncodingTypes.HEX
),
} as Partial<jwt.JwtHeader>);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
NetworkRequestOptions,
Logger,
ServerAuthorizationTokenResponse,
EncodingTypes,
} from "@azure/msal-common/node";
import { ManagedIdentityRequestParameters } from "../../config/ManagedIdentityRequestParameters.js";
import { BaseManagedIdentitySource } from "./BaseManagedIdentitySource.js";
Expand Down Expand Up @@ -290,7 +291,7 @@ export class AzureArc extends BaseManagedIdentitySource {
// attempt to read the contents of the secret file
let secret;
try {
secret = readFileSync(secretFilePath, "utf-8");
secret = readFileSync(secretFilePath, EncodingTypes.UTF8);
} catch (e) {
throw createManagedIdentityError(
ManagedIdentityErrorCodes.unableToReadSecretFile
Expand Down
6 changes: 3 additions & 3 deletions lib/msal-node/src/crypto/CryptoProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License.
*/

import { ICrypto, PkceCodes } from "@azure/msal-common/node";
import { EncodingTypes, ICrypto, PkceCodes } from "@azure/msal-common/node";
import { GuidGenerator } from "./GuidGenerator.js";
import { EncodingUtils } from "../utils/EncodingUtils.js";
import { PkceGenerator } from "./PkceGenerator.js";
Expand Down Expand Up @@ -106,8 +106,8 @@ export class CryptoProvider implements ICrypto {
*/
async hashString(plainText: string): Promise<string> {
return EncodingUtils.base64EncodeUrl(
this.hashUtils.sha256(plainText).toString("base64"),
"base64"
this.hashUtils.sha256(plainText).toString(EncodingTypes.BASE64),
EncodingTypes.BASE64
);
}
}
6 changes: 3 additions & 3 deletions lib/msal-node/src/crypto/PkceGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License.
*/

import { Constants, PkceCodes } from "@azure/msal-common/node";
import { Constants, EncodingTypes, PkceCodes } from "@azure/msal-common/node";
import { CharSet, RANDOM_OCTET_SIZE } from "../utils/Constants.js";
import { EncodingUtils } from "../utils/EncodingUtils.js";
import { HashUtils } from "./HashUtils.js";
Expand Down Expand Up @@ -56,8 +56,8 @@ export class PkceGenerator {
*/
private generateCodeChallengeFromVerifier(codeVerifier: string): string {
return EncodingUtils.base64EncodeUrl(
this.hashUtils.sha256(codeVerifier).toString("base64"),
"base64"
this.hashUtils.sha256(codeVerifier).toString(EncodingTypes.BASE64),
EncodingTypes.BASE64
);
}
}
6 changes: 3 additions & 3 deletions lib/msal-node/src/utils/EncodingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License.
*/

import { Constants } from "@azure/msal-common/node";
import { Constants, EncodingTypes } from "@azure/msal-common/node";

export class EncodingUtils {
/**
Expand All @@ -13,7 +13,7 @@ export class EncodingUtils {
* @param str text
*/
static base64Encode(str: string, encoding?: BufferEncoding): string {
return Buffer.from(str, encoding).toString("base64");
return Buffer.from(str, encoding).toString(EncodingTypes.BASE64);
}

/**
Expand All @@ -34,7 +34,7 @@ export class EncodingUtils {
* @param base64Str Base64 encoded text
*/
static base64Decode(base64Str: string): string {
return Buffer.from(base64Str, "base64").toString("utf8");
return Buffer.from(base64Str, EncodingTypes.BASE64).toString("utf8");
}

/**
Expand Down
13 changes: 8 additions & 5 deletions lib/msal-node/test/cache/TokenCache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
TokenCacheContext,
ICachePlugin,
buildStaticAuthorityOptions,
EncodingTypes,
} from "@azure/msal-common";
import { NodeStorage } from "../../src/cache/NodeStorage.js";
import { TokenCache } from "../../src/cache/TokenCache.js";
Expand Down Expand Up @@ -129,7 +130,7 @@ describe("TokenCache tests", () => {
context.tokenCache.deserialize(
await promises.readFile(
"./test/cache/cache-test-files/cache-unrecognized-entities.json",
"utf-8"
EncodingTypes.UTF8
)
);
};
Expand Down Expand Up @@ -194,9 +195,11 @@ describe("TokenCache tests", () => {
if (context.hasChanged == true) {
throw new Error("hasChanged should be false");
}
return promises.readFile(cachePath, "utf-8").then((data) => {
context.tokenCache.deserialize(data);
});
return promises
.readFile(cachePath, EncodingTypes.UTF8)
.then((data) => {
context.tokenCache.deserialize(data);
});
}
);

Expand Down Expand Up @@ -251,7 +254,7 @@ describe("TokenCache tests", () => {
const cachePath = "./test/cache/cache-test-files/default-cache.json";
const beforeCacheAccess = async (context: TokenCacheContext) => {
context.tokenCache.deserialize(
await promises.readFile(cachePath, "utf-8")
await promises.readFile(cachePath, EncodingTypes.UTF8)
);
};
const afterCacheAccess = async (context: TokenCacheContext) => {
Expand Down
10 changes: 5 additions & 5 deletions lib/msal-node/test/client/ClientAssertion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { CryptoProvider } from "../../src/crypto/CryptoProvider.js";
import { EncodingUtils } from "../../src/utils/EncodingUtils.js";
import { JwtConstants } from "../../src/utils/Constants.js";
import { getClientAssertionCallback } from "./ClientTestUtils.js";
import { getClientAssertion } from "@azure/msal-common";
import { EncodingTypes, getClientAssertion } from "@azure/msal-common";
import jwt from "jsonwebtoken";

jest.mock("jsonwebtoken");
Expand Down Expand Up @@ -65,7 +65,7 @@ describe("Client assertion test", () => {
[JwtConstants.ALGORITHM]: JwtConstants.RSA_256,
[JwtConstants.X5T]: EncodingUtils.base64EncodeUrl(
TEST_CONSTANTS.THUMBPRINT,
"hex"
EncodingTypes.HEX
),
},
};
Expand Down Expand Up @@ -95,7 +95,7 @@ describe("Client assertion test", () => {
[JwtConstants.ALGORITHM]: JwtConstants.PSS_256,
[JwtConstants.X5T_256]: EncodingUtils.base64EncodeUrl(
TEST_CONSTANTS.THUMBPRINT256,
"hex"
EncodingTypes.HEX
),
},
};
Expand Down Expand Up @@ -125,7 +125,7 @@ describe("Client assertion test", () => {
[JwtConstants.ALGORITHM]: JwtConstants.RSA_256,
[JwtConstants.X5T]: EncodingUtils.base64EncodeUrl(
TEST_CONSTANTS.THUMBPRINT,
"hex"
EncodingTypes.HEX
),
[JwtConstants.X5C]: TEST_CONSTANTS.X5C_FROM_PUBLIC_CERTIFICATE,
},
Expand Down Expand Up @@ -157,7 +157,7 @@ describe("Client assertion test", () => {
[JwtConstants.ALGORITHM]: JwtConstants.PSS_256,
[JwtConstants.X5T_256]: EncodingUtils.base64EncodeUrl(
TEST_CONSTANTS.THUMBPRINT256,
"hex"
EncodingTypes.HEX
),
[JwtConstants.X5C]: TEST_CONSTANTS.X5C_FROM_PUBLIC_CERTIFICATE,
},
Expand Down
3 changes: 2 additions & 1 deletion lib/msal-node/test/utils/CryptoKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License.
*/

import { EncodingTypes } from "@azure/msal-common";
import crypto from "crypto";

export class CryptoKeys {
Expand All @@ -28,6 +29,6 @@ export class CryptoKeys {
this._thumbprint = crypto
.createHash("sha512")
.update(publicKey)
.digest("hex");
.digest(EncodingTypes.HEX);
}
}