Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
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
6 changes: 6 additions & 0 deletions change/@azure-msal-browser-native-auth-v2-error-contract.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "patch",
"comment": "Correct Native Auth V2 browser, user-not-found, password, and local-username error classification, add general attribute and MFA/JIT challenge classifiers, and remove obsolete error helpers. [#8961](https://github.com/AzureAD/microsoft-authentication-library-for-js/pull/8961)",
"packageName": "@azure/msal-browser",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -648,11 +648,7 @@ export class CustomAuthStandardController
correlationId
);

ArgumentValidator.ensureArgumentIsNotEmptyString(
"inputs.username",
inputs.username,
correlationId
);
this.ensureUsernameIsValidV2(inputs.username, correlationId);
if (inputs.scopes !== undefined && inputs.scopes !== null) {
ArgumentValidator.ensureArgumentIsStringArray(
"inputs.scopes",
Expand Down Expand Up @@ -745,11 +741,7 @@ export class CustomAuthStandardController
inputs,
correlationId
);
ArgumentValidator.ensureArgumentIsNotEmptyString(
"inputs.username",
inputs.username,
correlationId
);
this.ensureUsernameIsValidV2(inputs.username, correlationId);
if (inputs.scopes !== undefined && inputs.scopes !== null) {
ArgumentValidator.ensureArgumentIsStringArray(
"inputs.scopes",
Expand Down Expand Up @@ -860,11 +852,7 @@ export class CustomAuthStandardController
inputs,
correlationId
);
ArgumentValidator.ensureArgumentIsNotEmptyString(
"inputs.username",
inputs.username,
correlationId
);
this.ensureUsernameIsValidV2(inputs.username, correlationId);
if (inputs.scopes !== undefined && inputs.scopes !== null) {
ArgumentValidator.ensureArgumentIsStringArray(
"inputs.scopes",
Expand Down Expand Up @@ -989,6 +977,17 @@ export class CustomAuthStandardController
}
}

private ensureUsernameIsValidV2(
username: string | undefined,
correlationId?: string
): void {
ArgumentValidator.ensureArgumentIsNotEmptyString(
"inputs.username",
username,
correlationId
);
}

private getCorrelationId(
actionInputs: CustomAuthActionInputs | undefined
): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import { MsalCustomAuthError } from "../../error/MsalCustomAuthError.js";
import { UnexpectedError } from "../../error/UnexpectedError.js";
import { FailedStateV2 } from "./state/FailedStateV2.js";
import { AuthFlowErrorV2 } from "./error/AuthFlowErrorV2.js";
import type { AuthFlowErrorContextV2 } from "./error/AuthFlowErrorV2.js";
import { CustomAuthFlowScenarioV2 } from "./CustomAuthFlowScenarioV2.js";

type CreateWithErrorOptions = {
scenario?: CustomAuthFlowScenarioV2;
correlationId?: string;
errorContext?: AuthFlowErrorContextV2;
};

/**
Expand Down Expand Up @@ -66,6 +68,7 @@ export class CustomAuthResultV2<

/**
* Creates a failed result from the supplied error.
* Optional error context identifies MFA verification or authentication-method registration.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure it matters but should be ok

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks—left this unchanged. The optional context distinguishes MFA/registration verification errors from ordinary code errors.

* @param error - The error that occurred.
* @param options - The flow-error context.
* @returns A failed result carrying the unified V2 flow error.
Expand All @@ -74,18 +77,20 @@ export class CustomAuthResultV2<
error: unknown,
options: CreateWithErrorOptions
): CustomAuthResultV2<TState | FailedStateV2, TData> {
const { scenario = CustomAuthFlowScenarioV2.Unknown, correlationId } =
options;
const errorData = CustomAuthResultV2.createErrorData(
error,
correlationId
);
const flowError = new AuthFlowErrorV2(errorData, scenario);
const { correlationId } = options;
const flowError =
error instanceof AuthFlowErrorV2
? error
: new AuthFlowErrorV2(
CustomAuthResultV2.createErrorData(error, correlationId),
options.scenario ?? CustomAuthFlowScenarioV2.Unknown,
options.errorContext
);

const result = new CustomAuthResultV2<TState | FailedStateV2, TData>(
new FailedStateV2(),
undefined,
scenario
flowError.scenario
);
result.error = flowError;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
* Licensed under the MIT License.
*/

export const ATTRIBUTE_VALIDATION_ERROR = "attributeValidationError";
export const INVALID_ONE_TIME_CODE = "invalidOneTimeCode";
export const INVALID_USERNAME_OR_PASSWORD = "invalidUserNameOrPassword";
export const PASSWORD_BANNED = "passwordBanned";
export const PASSWORD_IS_INVALID = "passwordIsInvalid";
export const PASSWORD_RECENTLY_USED = "passwordRecentlyUsed";
export const PASSWORD_TOO_LONG = "passwordTooLong";
export const PASSWORD_TOO_SHORT = "passwordTooShort";
export const PASSWORD_TOO_WEAK = "passwordTooWeak";
export const USER_NOT_FOUND = "userNotFound";
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,25 @@ import { CustomAuthApiError } from "../../../error/CustomAuthApiError.js";
import { InvalidArgumentError } from "../../../error/InvalidArgumentError.js";
import { WEB_REDIRECT_REQUIRED } from "../../../network_client/custom_auth_api/v2/ErrorCodesV2.js";
import {
Comment thread
spetrescu84 marked this conversation as resolved.
Resolved
ATTRIBUTE_VALIDATION_ERROR,
INVALID_ONE_TIME_CODE,
INVALID_USERNAME_OR_PASSWORD,
PASSWORD_BANNED,
PASSWORD_IS_INVALID,
PASSWORD_RECENTLY_USED,
PASSWORD_TOO_LONG,
PASSWORD_TOO_SHORT,
PASSWORD_TOO_WEAK,
USER_NOT_FOUND,
} from "./AuthFlowErrorSubcodesV2.js";
import { CustomAuthFlowScenarioV2 } from "../CustomAuthFlowScenarioV2.js";
import type { AttributeValidationDetailV2 } from "../../../network_client/custom_auth_api/v2/response/ErrorResponsesV2.js";

export type AuthFlowErrorContextV2 =
| "default"
| "mfaVerification"
| "authMethodRegistration";

/**
* Error returned by a native authentication V2 operation.
* @warning This API is experimental. It may be changed in the future without notice. Do not use in production applications.
Expand All @@ -25,7 +36,8 @@ export class AuthFlowErrorV2 {

constructor(
public errorData: CustomAuthError,
scenario: CustomAuthFlowScenarioV2 = CustomAuthFlowScenarioV2.Unknown
scenario: CustomAuthFlowScenarioV2 = CustomAuthFlowScenarioV2.Unknown,
private readonly errorContext: AuthFlowErrorContextV2 = "default"
) {
this.scenario = scenario;
}
Expand Down Expand Up @@ -57,11 +69,17 @@ export class AuthFlowErrorV2 {
}

/**
* Checks whether the flow must continue in the browser.
* Checks if the error requires the flow to continue in the browser. The

@spetrescu84 Silviu Petrescu (spetrescu84) Nov 6, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: previous comment was ok, this explain how code does what it does

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified to describe the public behavior rather than the implementation: “Checks whether the flow must continue in the browser.”

* server surfaces this as `invalid_grant` with the `web_redirect_required`
* OAuth suberror, so a browser hand-off arrives as a `FailedStateV2` on which
* this detector returns true.
* @returns True if the browser is required, false otherwise.
*/
isBrowserRequired(): boolean {
return this.errorData.subError === WEB_REDIRECT_REQUIRED;
return (
this.errorData.error === "invalid_grant" &&
this.errorData.subError === WEB_REDIRECT_REQUIRED
);
}

/**
Expand All @@ -73,11 +91,15 @@ export class AuthFlowErrorV2 {
}

/**
* Checks whether the service rejected the username parameter.
* @returns True when the username is invalid, otherwise false.
* Checks whether the supplied username failed local validation. Local username

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: comment should be Checks whether the supplied username failed local validation. then @returns

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to the requested wording: “Checks whether the supplied username failed local validation.” Kept the @returns description.

* failures also satisfy `isInvalidInput()`, unlike server attribute errors.
* @returns True when local username validation failed.
*/
isInvalidUsername(): boolean {
return this.isUserInvalidError();
return (
this.errorData instanceof InvalidArgumentError &&
this.errorData.argName === "inputs.username"
);
}

/**
Expand All @@ -89,67 +111,57 @@ export class AuthFlowErrorV2 {
}

/**
* Checks whether the service rejected the supplied sign-in credentials. This
* classifier does not identify new-password policy failures.
* @returns True when the username or password is incorrect.
*/
isInvalidCredentials(): boolean {
return this.isPasswordIncorrectError();
}

/**
* Checks whether the service rejected a one-time code.
* @returns True when the one-time code is invalid, otherwise false.
* Checks whether the service rejected an ordinary one-time code. MFA and

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: previous comment was ok, this explain how code does what it does

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified to “Checks whether the service rejected a one-time code,” followed by @returns.

* registration verification failures are identified by `isInvalidChallenge()`.
* @returns True when the ordinary one-time code is invalid.
*/
isInvalidCode(): boolean {
return this.isInvalidCodeError();
return this.errorContext === "default" && this.isInvalidCodeError();
}

/**
* Checks whether a new password violates password policy.
* @returns True when the new password is invalid, otherwise false.
* Checks whether the service rejected an MFA or registration challenge.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: comment should be Checks whether the service rejected an MFA or registration challenge. then @returns...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to “Checks whether the service rejected an MFA or registration challenge,” followed by @returns.

* Use it to distinguish challenge verification from ordinary code verification.
* @returns True when the verification challenge is invalid.
*/
isInvalidPassword(): boolean {
isInvalidChallenge(): boolean {
return (
this.isInvalidPasswordError() ||
this.isPasswordPolicyViolationError()
(this.errorContext === "mfaVerification" ||
this.errorContext === "authMethodRegistration") &&
this.isInvalidCodeError()
);
}

/**
* Checks whether an account already exists for the supplied identifier.
* @returns True if the account already exists, otherwise false.
* Checks whether the service reports an attribute-validation error. It applies
* to all collected attributes, independently of the validation details.
* @returns True when the service reports an attribute-validation error.
*/
isUserAlreadyExists(): boolean {
return this.getAttributeValidationDetails().some(
(detail) => detail.code === "userAlreadyExists"
isInvalidAttributeError(): boolean {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be isInvalidAttributes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to isInvalidAttributes() and updated the test references. It now follows iOS’s password-first classification, so password and attribute detectors cannot both return true.

return (
this.errorData.error === "invalidRequest" &&
this.innerErrorCode === ATTRIBUTE_VALIDATION_ERROR
);
}

/**
* Checks whether the service requires attributes omitted from the request.
* @returns True when required attributes are missing, otherwise false.
* Checks whether the service rejected a supplied password. It recognizes

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: comment should be Checks whether the service rejected a supplied password.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to the requested wording: “Checks whether the service rejected a supplied password.”

* incorrect sign-in credentials, password-policy failures, and
* password-targeted validation details.
* @returns True when the supplied password is invalid, otherwise false.
*/
isMissingRequiredAttributes(): boolean {
return this.requiredAttributes.length > 0;
}

private isUserNotFoundError(): boolean {
isInvalidPassword(): boolean {
return (
this.errorData.error === "invalidRequest" &&
(this.innerErrorCode === USER_NOT_FOUND ||
this.errorData.errorDescription?.includes("AADSTS50034") ===
true)
this.isPasswordIncorrectError() ||
this.isInvalidPasswordError() ||
this.isPasswordAttributeValidationError()
);
}

private isUserInvalidError(): boolean {
private isUserNotFoundError(): boolean {
return (
this.errorData.error === "invalidRequest" &&
this.errorData.errorDescription?.includes("AADSTS90100") === true &&
this.errorData.errorDescription
.toLowerCase()
.includes("username parameter")
this.innerErrorCode === USER_NOT_FOUND
);
}

Expand All @@ -161,24 +173,29 @@ export class AuthFlowErrorV2 {
}

private isInvalidPasswordError(): boolean {
const innerErrorCode = this.innerErrorCode;

return (
this.errorData.error === "invalidRequest" &&
this.innerErrorCode === PASSWORD_TOO_WEAK
(innerErrorCode === PASSWORD_TOO_WEAK ||
innerErrorCode === PASSWORD_TOO_SHORT ||
innerErrorCode === PASSWORD_TOO_LONG ||
innerErrorCode === PASSWORD_IS_INVALID ||
innerErrorCode === PASSWORD_RECENTLY_USED ||
innerErrorCode === PASSWORD_BANNED)
);
}

private isPasswordPolicyViolationError(): boolean {
private isPasswordAttributeValidationError(): boolean {
if (!this.isInvalidAttributeError()) {
return false;
}

return this.getAttributeValidationDetails().some((detail) => {
const attributeIds: unknown = detail.attributeIds;

return (
detail.code === "passwordPolicyViolation" &&
Array.isArray(attributeIds) &&
attributeIds.some(
(attributeId: unknown) =>
typeof attributeId === "string" &&
attributeId.toLowerCase() === "password"
)
Array.isArray(attributeIds) && attributeIds.includes("password")
);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export class AuthMethodVerificationRequiredStateV2 extends AuthFlowActionRequire
return CustomAuthResultV2.createWithError(error, {
scenario: continuationState.scenario,
correlationId,
errorContext: "authMethodRegistration",
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class MFAVerificationRequiredStateV2 extends ChallengeVerificationStateBa
return CustomAuthResultV2.createWithError(error, {
scenario: continuationState.scenario,
correlationId,
errorContext: "mfaVerification",
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { CustomAuthError } from "./CustomAuthError.js";

export class InvalidArgumentError extends CustomAuthError {
constructor(argName: string, correlationId?: string) {
constructor(public readonly argName: string, correlationId?: string) {
const errorDescription = `The argument '${argName}' is invalid.`;

super("invalid_argument", errorDescription, correlationId);
Expand Down
Loading
Loading