From 4154d30ca795f9f2cff0ff9d62f65040c007293c Mon Sep 17 00:00:00 2001 From: Disha Janardhan Date: Mon, 12 Oct 2026 11:36:01 -0700 Subject: [PATCH 01/10] Unify Native Auth V2 errors and stable classifiers Fixes AB#3784695 Remove operation-specific V2 error classes without deprecated wrappers. Preserve recoverable states and raw diagnostics; distinguish credentials from new-password policy. Pair with the Common diagnostic-propagation commit. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 0bcd671b-73fa-4ee6-bcd6-65d7eea4d753 --- changelog | 1 + common | 2 +- .../INativeAuthPublicClientApplication.kt | 4 +- .../NativeAuthPublicClientApplication.kt | 76 ++++----- .../statemachine/errors/CodeErrorsV2.kt | 55 ------- .../statemachine/errors/JITErrorsV2.kt | 80 ---------- .../statemachine/errors/MFAErrorsV2.kt | 86 ----------- .../statemachine/errors/NativeAuthErrorV2.kt | 106 ++++++++++++- .../errors/ResetPasswordErrorsV2.kt | 84 ---------- .../statemachine/errors/SignInErrorsV2.kt | 89 ----------- .../statemachine/errors/SignUpErrorsV2.kt | 87 ----------- .../results/NativeAuthResultV2.kt | 6 +- .../states/AttributesRequiredStateV2.kt | 25 +-- .../states/CodeRequiredStateV2.kt | 23 +-- .../statemachine/states/MFARequiredStateV2.kt | 17 +- .../states/MFAVerificationRequiredStateV2.kt | 25 +-- .../states/NewPasswordRequiredStateV2.kt | 19 +-- .../states/PasswordRequiredStateV2.kt | 66 ++++---- .../ResetPasswordMethodRequiredStateV2.kt | 15 +- .../states/SignInAfterResetPasswordStateV2.kt | 4 +- .../states/SignInAfterSignUpStateV2.kt | 4 +- .../nativeauth/SignInV2EmailPasswordTest.kt | 29 ++-- .../nativeauth/v2/NativeAuthV2ErrorsTest.kt | 145 +++++++++--------- .../v2/NativeAuthV2InterfaceJavaTest.java | 10 +- .../v2/NativeAuthV2InterfaceKotlinTest.kt | 57 ++++--- .../nativeauth/v2/NativeAuthV2SignInTest.kt | 82 +++++----- .../nativeauth/v2/NativeAuthV2SignUpTest.kt | 76 ++++++--- 27 files changed, 476 insertions(+), 797 deletions(-) delete mode 100644 msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/CodeErrorsV2.kt delete mode 100644 msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/JITErrorsV2.kt delete mode 100644 msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/MFAErrorsV2.kt delete mode 100644 msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/ResetPasswordErrorsV2.kt delete mode 100644 msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/SignInErrorsV2.kt delete mode 100644 msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/SignUpErrorsV2.kt diff --git a/changelog b/changelog index 265c9cde6..df4044d2a 100644 --- a/changelog +++ b/changelog @@ -2,6 +2,7 @@ MSAL Wiki : https://github.com/AzureAD/microsoft-authentication-library-for-andr vNext ---------- +- [MAJOR] Native Auth V2 errors now use NativeAuthErrorV2. Replace operation-specific V2 error type checks with its classifiers; rejected sign-in passwords use isInvalidCredentials, not isInvalidPassword. V1 is unchanged (AB#3784695). Version 8.5.0 ---------- diff --git a/common b/common index 72d000d1e..9a89d47b1 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 72d000d1e14378ec664b6ee11a789ff1784c055d +Subproject commit 9a89d47b1d95a96e864cfd2f1ecbc9a53c448812 diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/INativeAuthPublicClientApplication.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/INativeAuthPublicClientApplication.kt index 2718bedd6..2ec4bff1d 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/INativeAuthPublicClientApplication.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/INativeAuthPublicClientApplication.kt @@ -245,7 +245,7 @@ interface INativeAuthPublicClientApplication : IPublicClientApplication { * * @param parameters parameters used for the resetPassword operation. * @return [NativeAuthResultV2] see the detailed possible return states under the object. - * Returns a [com.microsoft.identity.nativeauth.statemachine.errors.ResetPasswordErrorV2] with + * Returns a [com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2] with * [com.microsoft.identity.nativeauth.statemachine.errors.ErrorTypes.CLIENT_EXCEPTION] if an * account is already signed in. */ @@ -259,7 +259,7 @@ interface INativeAuthPublicClientApplication : IPublicClientApplication { * @param parameters parameters used for the resetPassword operation. * @param callback [com.microsoft.identity.nativeauth.NativeAuthPublicClientApplication.NativeAuthV2Callback] to receive the result. * If an account is already signed in, the callback receives a - * [com.microsoft.identity.nativeauth.statemachine.errors.ResetPasswordErrorV2] with + * [com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2] with * [com.microsoft.identity.nativeauth.statemachine.errors.ErrorTypes.CLIENT_EXCEPTION]. */ fun resetPasswordV2(parameters: NativeAuthResetPasswordParameters, callback: NativeAuthPublicClientApplication.NativeAuthV2Callback) diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt index fb34dd7b9..09d35189a 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt @@ -77,13 +77,10 @@ import com.microsoft.identity.nativeauth.statemachine.errors.GetAccountError import com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2 import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 import com.microsoft.identity.nativeauth.statemachine.errors.ResetPasswordError -import com.microsoft.identity.nativeauth.statemachine.errors.ResetPasswordErrorV2 import com.microsoft.identity.nativeauth.statemachine.errors.SignInError import com.microsoft.identity.nativeauth.statemachine.errors.SignInErrorTypes -import com.microsoft.identity.nativeauth.statemachine.errors.SignInErrorV2 import com.microsoft.identity.nativeauth.statemachine.errors.SignUpError import com.microsoft.identity.nativeauth.statemachine.errors.SignUpErrorTypes -import com.microsoft.identity.nativeauth.statemachine.errors.SignUpErrorV2 import com.microsoft.identity.nativeauth.statemachine.results.GetAccountResult import com.microsoft.identity.nativeauth.statemachine.results.NativeAuthResultV2 import com.microsoft.identity.nativeauth.statemachine.results.ResetPasswordStartResult @@ -627,7 +624,7 @@ class NativeAuthPublicClientApplication( verifyNoUserIsSignedIn() if (parameters.username.isBlank()) { - return@withContext SignInErrorV2( + return@withContext NativeAuthErrorV2( errorType = ErrorTypes.INVALID_USERNAME, errorMessage = "Empty or blank username", correlationId = correlationId, @@ -669,7 +666,7 @@ class NativeAuthPublicClientApplication( ) } else { Logger.warn(TAG, result.correlationId, "V2 signIn Complete with no authentication result.") - SignInErrorV2( + NativeAuthErrorV2( errorMessage = "Sign in completed but no authentication result was returned.", correlationId = result.correlationId, scenario = NativeAuthFlowScenarioV2.SIGN_IN @@ -712,7 +709,7 @@ class NativeAuthPublicClientApplication( ) } is NativeAuthV2CommandResult.InvalidCredentials -> { - SignInErrorV2( + NativeAuthErrorV2( errorType = SignInErrorTypes.INVALID_CREDENTIALS, error = result.error, errorMessage = result.errorDescription, @@ -723,7 +720,7 @@ class NativeAuthPublicClientApplication( ) } is NativeAuthV2CommandResult.UserNotFound -> { - SignInErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.USER_NOT_FOUND, error = result.error, errorMessage = result.errorDescription, @@ -733,7 +730,7 @@ class NativeAuthPublicClientApplication( ) } is INativeAuthCommandResult.InvalidUsername -> { - SignInErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.INVALID_USERNAME, error = result.error, errorMessage = result.errorDescription, @@ -752,7 +749,7 @@ class NativeAuthPublicClientApplication( ) } is INativeAuthCommandResult.Redirect -> { - SignInErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, error = result.error, errorMessage = result.errorDescription, @@ -761,13 +758,15 @@ class NativeAuthPublicClientApplication( ) } is INativeAuthCommandResult.APIError -> { - SignInErrorV2( + NativeAuthErrorV2( error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = NativeAuthFlowScenarioV2.SIGN_IN, errorCodes = result.errorCodes, - exception = result.exception + exception = result.exception, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails ) } else -> { @@ -777,7 +776,7 @@ class NativeAuthPublicClientApplication( "V2 signIn received unsupported result: ", result ) - SignInErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.INVALID_STATE, errorMessage = "V2 sign-in returned an unsupported result.", correlationId = result.correlationId, @@ -789,7 +788,7 @@ class NativeAuthPublicClientApplication( throw e } catch (e: Exception) { Logger.error(TAG, correlationId, "Exception thrown in signInV2", e) - SignInErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.CLIENT_EXCEPTION, errorMessage = "MSAL client exception occurred in signInV2.", correlationId = correlationId, @@ -837,12 +836,12 @@ class NativeAuthPublicClientApplication( val passwordCopy = parameters.password?.copyOf() try { // Kept inside the try so an already-signed-in account is surfaced through the V2 - // result contract as a SignUpErrorV2, rather than escaping as a thrown + // result contract as a NativeAuthErrorV2, rather than escaping as a thrown // MsalClientException, matching signInV2 and resetPasswordV2. verifyNoUserIsSignedIn() if (parameters.username.isBlank()) { - return@withContext SignUpErrorV2( + return@withContext NativeAuthErrorV2( errorType = ErrorTypes.INVALID_USERNAME, errorMessage = "Empty or blank username", correlationId = correlationId, @@ -912,21 +911,24 @@ class NativeAuthPublicClientApplication( config = nativeAuthConfig ), scenario = NativeAuthFlowScenarioV2.SIGN_UP, - invalidAttributes = result.invalidAttributes + invalidAttributes = result.invalidAttributes, + error = NativeAuthErrorV2.fromAttributeError(result, NativeAuthFlowScenarioV2.SIGN_UP) ) } is NativeAuthV2CommandResult.UserAlreadyExists -> { - SignUpErrorV2( + NativeAuthErrorV2( errorType = SignUpErrorTypes.USER_ALREADY_EXISTS, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = NativeAuthFlowScenarioV2.SIGN_UP, - errorCodes = result.errorCodes + errorCodes = result.errorCodes, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails ) } is SignUpCommandResult.InvalidAttributes -> { - SignUpErrorV2( + NativeAuthErrorV2( errorType = SignUpErrorTypes.INVALID_ATTRIBUTES, error = result.error, errorMessage = result.errorDescription, @@ -935,7 +937,7 @@ class NativeAuthPublicClientApplication( ) } is INativeAuthCommandResult.InvalidUsername -> { - SignUpErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.INVALID_USERNAME, error = result.error, errorMessage = result.errorDescription, @@ -954,7 +956,7 @@ class NativeAuthPublicClientApplication( ) } is INativeAuthCommandResult.Redirect -> { - SignUpErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, error = result.error, errorMessage = result.errorDescription, @@ -963,13 +965,15 @@ class NativeAuthPublicClientApplication( ) } is INativeAuthCommandResult.APIError -> { - SignUpErrorV2( + NativeAuthErrorV2( error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = NativeAuthFlowScenarioV2.SIGN_UP, errorCodes = result.errorCodes, - exception = result.exception + exception = result.exception, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails ) } else -> { @@ -979,7 +983,7 @@ class NativeAuthPublicClientApplication( "V2 signUp received unsupported result: ", result ) - SignUpErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.INVALID_STATE, errorMessage = "V2 sign-up returned an unsupported result.", correlationId = result.correlationId, @@ -991,7 +995,7 @@ class NativeAuthPublicClientApplication( throw e } catch (e: Exception) { Logger.error(TAG, correlationId, "Exception thrown in signUpV2", e) - SignUpErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.CLIENT_EXCEPTION, errorMessage = "MSAL client exception occurred in signUpV2.", correlationId = correlationId, @@ -1036,12 +1040,12 @@ class NativeAuthPublicClientApplication( var correlationId = DiagnosticContext.INSTANCE.threadCorrelationId ?: "UNSET" try { // Kept inside the try so an already-signed-in account is surfaced through the V2 - // result contract as a ResetPasswordErrorV2, rather than escaping as a thrown + // result contract as a NativeAuthErrorV2, rather than escaping as a thrown // MsalClientException. This matches the V1 flows, which catch the same exception. verifyNoUserIsSignedIn() if (parameters.username.isBlank()) { - return@withContext ResetPasswordErrorV2( + return@withContext NativeAuthErrorV2( errorType = ErrorTypes.INVALID_USERNAME, errorMessage = "Empty or blank username", correlationId = correlationId, @@ -1104,7 +1108,7 @@ class NativeAuthPublicClientApplication( ) } else { Logger.warn(TAG, result.correlationId, "V2 resetPassword start Complete with no inline sign-in result.") - ResetPasswordErrorV2( + NativeAuthErrorV2( errorMessage = "Password reset completed but no sign-in result was returned.", correlationId = result.correlationId, scenario = NativeAuthFlowScenarioV2.RESET_PASSWORD @@ -1112,7 +1116,7 @@ class NativeAuthPublicClientApplication( } } is NativeAuthV2CommandResult.UserNotFound -> { - ResetPasswordErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.USER_NOT_FOUND, error = result.error, errorMessage = result.errorDescription, @@ -1122,7 +1126,7 @@ class NativeAuthPublicClientApplication( ) } is INativeAuthCommandResult.InvalidUsername -> { - ResetPasswordErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.INVALID_USERNAME, error = result.error, errorMessage = result.errorDescription, @@ -1141,7 +1145,7 @@ class NativeAuthPublicClientApplication( ) } is INativeAuthCommandResult.Redirect -> { - ResetPasswordErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, error = result.error, errorMessage = result.errorDescription, @@ -1150,13 +1154,15 @@ class NativeAuthPublicClientApplication( ) } is INativeAuthCommandResult.APIError -> { - ResetPasswordErrorV2( + NativeAuthErrorV2( error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = NativeAuthFlowScenarioV2.RESET_PASSWORD, errorCodes = result.errorCodes, - exception = result.exception + exception = result.exception, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails ) } else -> { @@ -1166,7 +1172,7 @@ class NativeAuthPublicClientApplication( "V2 resetPassword received unsupported result: ", result ) - ResetPasswordErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.INVALID_STATE, errorMessage = "V2 reset password returned an unsupported result.", correlationId = result.correlationId, @@ -1178,7 +1184,7 @@ class NativeAuthPublicClientApplication( throw e } catch (e: Exception) { Logger.error(TAG, correlationId, "Exception thrown in resetPasswordV2", e) - ResetPasswordErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.CLIENT_EXCEPTION, errorMessage = "MSAL client exception occurred in resetPasswordV2.", correlationId = correlationId, diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/CodeErrorsV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/CodeErrorsV2.kt deleted file mode 100644 index 3ac3dbd23..000000000 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/CodeErrorsV2.kt +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// All rights reserved. -// -// This code is licensed under the MIT License. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files(the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions : -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -package com.microsoft.identity.nativeauth.statemachine.errors - -import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 - -/** - * Submit code error for the Native Auth V2 surface. Use the utility methods of this class to - * identify and handle the error. This error is produced by - * [com.microsoft.identity.nativeauth.statemachine.states.CodeRequiredStateV2.submitCode] across the - * sign in, sign up and reset password scenarios; use [scenario] to determine which flow it belongs to. - * - * @param errorType the error type value of the error that occurred. - * @param error the error returned by the authentication server. - * @param errorMessage the error message returned by the authentication server. - * @param correlationId a unique identifier for the request that can help in diagnostics. - * @param scenario identifies which part of the Native Auth V2 surface produced this error. - * @param errorCodes a list of specific error codes returned by the authentication server. - * @param subError the sub error returned by the authentication server. - * @param exception an internal unexpected exception that happened. - */ -class SubmitCodeErrorV2( - errorType: String? = null, - error: String? = null, - errorMessage: String?, - correlationId: String, - scenario: NativeAuthFlowScenarioV2, - errorCodes: List? = null, - val subError: String? = null, - exception: Exception? = null -) : NativeAuthErrorV2(errorType, error, errorMessage, correlationId, scenario, errorCodes, exception) { - - fun isInvalidCode(): Boolean = this.errorType == ErrorTypes.INVALID_CODE -} diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/JITErrorsV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/JITErrorsV2.kt deleted file mode 100644 index eaad51a4c..000000000 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/JITErrorsV2.kt +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// All rights reserved. -// -// This code is licensed under the MIT License. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files(the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions : -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -package com.microsoft.identity.nativeauth.statemachine.errors - -import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 - -/** - * Register strong auth challenge error for the Native Auth V2 surface. Use the utility methods of - * this class to identify and handle the error. This error is produced by - * [com.microsoft.identity.nativeauth.statemachine.states.StrongAuthRegistrationRequiredStateV2.selectAuthMethod]. - * - * @param errorType the error type value of the error that occurred. - * @param error the error returned by the authentication server. - * @param errorMessage the error message returned by the authentication server. - * @param correlationId a unique identifier for the request that can help in diagnostics. - * @param scenario identifies which part of the Native Auth V2 surface produced this error. - * @param errorCodes a list of specific error codes returned by the authentication server. - * @param exception an internal unexpected exception that happened. - */ -class RegisterStrongAuthChallengeErrorV2( - errorType: String? = null, - error: String? = null, - errorMessage: String?, - correlationId: String, - scenario: NativeAuthFlowScenarioV2, - errorCodes: List? = null, - exception: Exception? = null -) : NativeAuthErrorV2(errorType, error, errorMessage, correlationId, scenario, errorCodes, exception) { - - fun isInvalidInput(): Boolean = this.errorType == ErrorTypes.INVALID_INPUT - - fun isVerificationContactBlocked(): Boolean = this.errorType == ErrorTypes.VERIFICATION_CONTACT_BLOCKED -} - -/** - * Register strong auth submit challenge error for the Native Auth V2 surface. Use the utility - * methods of this class to identify and handle the error. This error is produced by - * [com.microsoft.identity.nativeauth.statemachine.states.StrongAuthVerificationRequiredStateV2.submitChallenge]. - * - * @param errorType the error type value of the error that occurred. - * @param error the error returned by the authentication server. - * @param errorMessage the error message returned by the authentication server. - * @param correlationId a unique identifier for the request that can help in diagnostics. - * @param scenario identifies which part of the Native Auth V2 surface produced this error. - * @param errorCodes a list of specific error codes returned by the authentication server. - * @param exception an internal unexpected exception that happened. - */ -class RegisterStrongAuthSubmitChallengeErrorV2( - errorType: String? = null, - error: String? = null, - errorMessage: String?, - correlationId: String, - scenario: NativeAuthFlowScenarioV2, - errorCodes: List? = null, - exception: Exception? = null -) : NativeAuthErrorV2(errorType, error, errorMessage, correlationId, scenario, errorCodes, exception) { - - fun isInvalidChallenge(): Boolean = this.errorType == ErrorTypes.INVALID_CHALLENGE -} diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/MFAErrorsV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/MFAErrorsV2.kt deleted file mode 100644 index f313c4304..000000000 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/MFAErrorsV2.kt +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// All rights reserved. -// -// This code is licensed under the MIT License. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files(the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions : -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -package com.microsoft.identity.nativeauth.statemachine.errors - -import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 - -/** - * MFA request challenge error for the Native Auth V2 surface. Use the utility methods of this class - * to identify and handle the error. This error is produced by - * [com.microsoft.identity.nativeauth.statemachine.states.MFARequiredStateV2.selectAuthMethod]. - * - * @param errorType the error type value of the error that occurred. - * @param error the error returned by the authentication server. - * @param errorMessage the error message returned by the authentication server. - * @param correlationId a unique identifier for the request that can help in diagnostics. - * @param scenario identifies which part of the Native Auth V2 surface produced this error. - * @param errorCodes a list of specific error codes returned by the authentication server. - * @param subError the sub error returned by the authentication server. - * @param exception an internal unexpected exception that happened. - */ -class MFARequestChallengeErrorV2( - errorType: String? = null, - error: String? = null, - errorMessage: String?, - correlationId: String, - scenario: NativeAuthFlowScenarioV2, - errorCodes: List? = null, - val subError: String? = null, - exception: Exception? = null -) : NativeAuthErrorV2(errorType, error, errorMessage, correlationId, scenario, errorCodes, exception) { - - fun isAuthMethodBlocked(): Boolean = this.errorType == ErrorTypes.AUTH_METHOD_BLOCKED -} - -/** - * MFA submit challenge error for the Native Auth V2 surface. Use the utility methods of this class - * to identify and handle the error. This error is produced by - * [com.microsoft.identity.nativeauth.statemachine.states.MFAVerificationRequiredStateV2.submitChallenge]. - * - * @param errorType the error type value of the error that occurred. - * @param error the error returned by the authentication server. - * @param errorMessage the error message returned by the authentication server. - * @param correlationId a unique identifier for the request that can help in diagnostics. - * @param scenario identifies which part of the Native Auth V2 surface produced this error. - * @param errorCodes a list of specific error codes returned by the authentication server. - * @param subError the sub error returned by the authentication server. - * @param exception an internal unexpected exception that happened. - */ -class MFASubmitChallengeErrorV2( - errorType: String? = null, - error: String? = null, - errorMessage: String?, - correlationId: String, - scenario: NativeAuthFlowScenarioV2, - errorCodes: List? = null, - val subError: String? = null, - exception: Exception? = null -) : NativeAuthErrorV2(errorType, error, errorMessage, correlationId, scenario, errorCodes, exception) { - - /** - * Returns true when the submitted MFA challenge was rejected and the caller can retry on the - * same [com.microsoft.identity.nativeauth.statemachine.states.MFAVerificationRequiredStateV2]. - */ - fun isInvalidChallenge(): Boolean = this.errorType == ErrorTypes.INVALID_CODE -} diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/NativeAuthErrorV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/NativeAuthErrorV2.kt index 2474161c0..d80526020 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/NativeAuthErrorV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/NativeAuthErrorV2.kt @@ -27,14 +27,12 @@ import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 import com.microsoft.identity.nativeauth.statemachine.results.NativeAuthResultV2 /** - * NativeAuthErrorV2 is the base error type for the Native Auth V2 surface. All V2 errors are a + * The shared error type for every Native Auth V2 operation. All V2 errors are a * [NativeAuthResultV2], so any V2 state method or entry point that returns a result can also * return an error. * - * This base type is returned directly for flow-level errors that are not specific to a single - * operation (for example [isNotImplemented]). Operation-specific errors are represented by the - * dedicated subclasses (e.g. [SignInErrorV2], [SubmitCodeErrorV2]), each exposing only the - * utility methods relevant to that operation, mirroring the Native Auth V1 error surface. + * Use the classifiers to handle known failures. Raw service diagnostics are retained, including + * unknown errors. Migrate operation-specific V2 error checks to this type; V1 is unchanged. * * @param errorType the error type value of the error that occurred. * @param error the error returned by the authentication server. @@ -43,15 +41,19 @@ import com.microsoft.identity.nativeauth.statemachine.results.NativeAuthResultV2 * @param scenario identifies which part of the Native Auth V2 surface produced this error. * @param errorCodes a list of specific error codes returned by the authentication server. * @param exception an internal unexpected exception that happened. + * @param subError the unmodified service suberror. + * @param attributeValidationDetails service validation details, runtime-checked by classifiers. */ -open class NativeAuthErrorV2( +class NativeAuthErrorV2 @JvmOverloads constructor( override val errorType: String? = null, override val error: String? = null, override val errorMessage: String?, override val correlationId: String, override val scenario: NativeAuthFlowScenarioV2, override val errorCodes: List? = null, - override var exception: Exception? = null + override var exception: Exception? = null, + val subError: String? = null, + val attributeValidationDetails: Any? = null ) : NativeAuthResultV2, BrowserRequiredError, Error( @@ -67,4 +69,94 @@ open class NativeAuthErrorV2( * Returns true if the requested Native Auth V2 flow or step is not implemented yet. */ fun isNotImplemented(): Boolean = this.errorType == ErrorTypes.NOT_IMPLEMENTED + + /** Whether the service rejected the username. */ + fun isInvalidUsername(): Boolean = errorType == ErrorTypes.INVALID_USERNAME || + (error == "invalidRequest" && errorMessage?.contains("AADSTS90100") == true && + errorMessage.contains("username", ignoreCase = true)) + + /** Whether the account does not exist. */ + fun isUserNotFound(): Boolean = errorType == ErrorTypes.USER_NOT_FOUND || + (error == "invalidRequest" && errorMessage?.contains("AADSTS50034") == true) + + /** Whether sign-in credentials were rejected, not a new-password policy violation. */ + fun isInvalidCredentials(): Boolean = errorType == SignInErrorTypes.INVALID_CREDENTIALS || + (error == "invalidGrant" && subError == "invalidUserNameOrPassword") + + /** Whether a first-factor or MFA one-time code was rejected. */ + fun isInvalidCode(): Boolean = errorType == ErrorTypes.INVALID_CODE || + (error == "invalidGrant" && subError == "invalidOneTimeCode") + + /** Whether a new password violates policy; does not classify sign-in credential failures. */ + fun isInvalidPassword(): Boolean = !isInvalidCredentials() && + (errorType == ErrorTypes.INVALID_PASSWORD || + (error == "invalidRequest" && subError in PASSWORD_POLICY_SUBERRORS) || + validationDetails().any { detail -> + detail["code"] == "passwordPolicyViolation" && + (detail["attributeIds"] as? List<*>)?.any { + it is String && it.equals("password", ignoreCase = true) + } == true + }) + + /** Whether the identifier already belongs to an account. */ + fun isUserAlreadyExists(): Boolean = errorType == SignUpErrorTypes.USER_ALREADY_EXISTS || + subError == "userAlreadyExists" || + validationDetails().any { it["code"] == "userAlreadyExists" } + + /** Whether the request omitted required attributes. */ + fun isMissingRequiredAttributes(): Boolean = + validationDetails().any { it["code"] == "attributeRequired" } + + /** Whether submitted attributes failed service validation. */ + fun isInvalidAttributes(): Boolean = errorType == SignUpErrorTypes.INVALID_ATTRIBUTES || + subError == "attributeValidationError" || isMissingRequiredAttributes() + + /** Whether the service rejected a verification challenge. */ + fun isInvalidChallenge(): Boolean = isInvalidCode() || errorType == ErrorTypes.INVALID_CHALLENGE + + /** Whether the service blocked the selected authentication method. */ + fun isAuthMethodBlocked(): Boolean = errorType == ErrorTypes.AUTH_METHOD_BLOCKED + + /** Whether the service blocked the supplied verification contact. */ + fun isVerificationContactBlocked(): Boolean = errorType == ErrorTypes.VERIFICATION_CONTACT_BLOCKED + + /** Whether caller input is invalid. */ + fun isInvalidInput(): Boolean = errorType == ErrorTypes.INVALID_INPUT || + (errorType == ErrorTypes.CLIENT_EXCEPTION && exception is IllegalArgumentException) + + /** Whether the requested authentication mechanism is unsupported. */ + fun isAuthNotSupported(): Boolean = errorType == SignUpErrorTypes.AUTH_NOT_SUPPORTED + + /** Whether password reset failed after credential validation. */ + fun isPasswordResetFailed(): Boolean = errorType == ResetPasswordErrorTypes.PASSWORD_RESET_FAILED + + /** Whether no stable classifier describes this error; raw diagnostics remain available. */ + fun isGeneralError(): Boolean = !(isInvalidUsername() || isUserNotFound() || + isInvalidCredentials() || isInvalidCode() || isInvalidPassword() || + isUserAlreadyExists() || isMissingRequiredAttributes() || isInvalidAttributes() || + isInvalidChallenge() || isAuthMethodBlocked() || isVerificationContactBlocked() || + isInvalidInput() || isAuthNotSupported() || isPasswordResetFailed() || + isBrowserRequired() || isNotImplemented()) + + private fun validationDetails(): List> = + (attributeValidationDetails as? List<*>)?.mapNotNull { it as? Map<*, *> }.orEmpty() + + companion object { + internal fun fromAttributeError( + result: com.microsoft.identity.common.java.nativeauth.controllers.results.NativeAuthV2CommandResult.AttributesInvalid, + scenario: NativeAuthFlowScenarioV2 + ): NativeAuthErrorV2 = NativeAuthErrorV2( + errorType = SignUpErrorTypes.INVALID_ATTRIBUTES, + error = result.error, + errorMessage = result.errorDescription, + correlationId = result.correlationId, + scenario = scenario, + errorCodes = result.errorCodes, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails + ) + + private val PASSWORD_POLICY_SUBERRORS = setOf("passwordTooWeak", "passwordTooShort", + "passwordTooLong", "passwordIsInvalid", "passwordRecentlyUsed", "passwordBanned") + } } diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/ResetPasswordErrorsV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/ResetPasswordErrorsV2.kt deleted file mode 100644 index a846bbd52..000000000 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/ResetPasswordErrorsV2.kt +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// All rights reserved. -// -// This code is licensed under the MIT License. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files(the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions : -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -package com.microsoft.identity.nativeauth.statemachine.errors - -import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 - -/** - * Reset password error for the Native Auth V2 surface. Use the utility methods of this class to - * identify and handle the error. This error is produced while starting password reset or selecting - * a reset-password verification method. - * - * @param errorType the error type value of the error that occurred. - * @param error the error returned by the authentication server. - * @param errorMessage the error message returned by the authentication server. - * @param correlationId a unique identifier for the request that can help in diagnostics. - * @param scenario identifies which part of the Native Auth V2 surface produced this error. - * @param errorCodes a list of specific error codes returned by the authentication server. - * @param exception an internal unexpected exception that happened. - */ -class ResetPasswordErrorV2( - errorType: String? = null, - error: String? = null, - errorMessage: String?, - correlationId: String, - scenario: NativeAuthFlowScenarioV2, - errorCodes: List? = null, - exception: Exception? = null -) : NativeAuthErrorV2(errorType, error, errorMessage, correlationId, scenario, errorCodes, exception) { - - fun isUserNotFound(): Boolean = this.errorType == ErrorTypes.USER_NOT_FOUND - - fun isInvalidUsername(): Boolean = this.errorType == ErrorTypes.INVALID_USERNAME -} - -/** - * Submit new password error for the Native Auth V2 surface. Use the utility methods of this class - * to identify and handle the error. This error is produced by - * [com.microsoft.identity.nativeauth.statemachine.states.NewPasswordRequiredStateV2.submitNewPassword]. - * - * @param errorType the error type value of the error that occurred. - * @param error the error returned by the authentication server. - * @param errorMessage the error message returned by the authentication server. - * @param correlationId a unique identifier for the request that can help in diagnostics. - * @param scenario identifies which part of the Native Auth V2 surface produced this error. - * @param errorCodes a list of specific error codes returned by the authentication server. - * @param subError the sub error returned by the authentication server. - * @param exception an internal unexpected exception that happened. - */ -class SubmitNewPasswordErrorV2( - errorType: String? = null, - error: String? = null, - errorMessage: String?, - correlationId: String, - scenario: NativeAuthFlowScenarioV2, - errorCodes: List? = null, - val subError: String? = null, - exception: Exception? = null -) : NativeAuthErrorV2(errorType, error, errorMessage, correlationId, scenario, errorCodes, exception) { - - fun isInvalidPassword(): Boolean = this.errorType == ErrorTypes.INVALID_PASSWORD - - fun isPasswordResetFailed(): Boolean = this.errorType == ResetPasswordErrorTypes.PASSWORD_RESET_FAILED -} diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/SignInErrorsV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/SignInErrorsV2.kt deleted file mode 100644 index 5c1e69872..000000000 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/SignInErrorsV2.kt +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// All rights reserved. -// -// This code is licensed under the MIT License. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files(the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions : -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -package com.microsoft.identity.nativeauth.statemachine.errors - -import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 - -/** - * Sign in error for the Native Auth V2 surface. Use the utility methods of this class to identify - * and handle the error. This error is produced by - * [com.microsoft.identity.nativeauth.INativeAuthPublicClientApplication.signInV2]. - * - * @param errorType the error type value of the error that occurred. - * @param error the error returned by the authentication server. - * @param errorMessage the error message returned by the authentication server. - * @param correlationId a unique identifier for the request that can help in diagnostics. - * @param scenario identifies which part of the Native Auth V2 surface produced this error. - * @param errorCodes a list of specific error codes returned by the authentication server. - * @param subError the sub error returned by the authentication server. - * @param exception an internal unexpected exception that happened. - */ -class SignInErrorV2( - errorType: String? = null, - error: String? = null, - errorMessage: String?, - correlationId: String, - scenario: NativeAuthFlowScenarioV2, - errorCodes: List? = null, - val subError: String? = null, - exception: Exception? = null -) : NativeAuthErrorV2(errorType, error, errorMessage, correlationId, scenario, errorCodes, exception) { - - fun isUserNotFound(): Boolean = this.errorType == ErrorTypes.USER_NOT_FOUND - - fun isInvalidCredentials(): Boolean = this.errorType == SignInErrorTypes.INVALID_CREDENTIALS - - fun isInvalidUsername(): Boolean = this.errorType == ErrorTypes.INVALID_USERNAME -} - -/** - * Submit password error for the Native Auth V2 surface. Use the utility methods of this class to - * identify and handle the error. This error is produced by - * [com.microsoft.identity.nativeauth.statemachine.states.PasswordRequiredStateV2.submitPassword]. - * - * @param errorType the error type value of the error that occurred. - * @param error the error returned by the authentication server. - * @param errorMessage the error message returned by the authentication server. - * @param correlationId a unique identifier for the request that can help in diagnostics. - * @param scenario identifies which part of the Native Auth V2 surface produced this error. - * @param errorCodes a list of specific error codes returned by the authentication server. - * @param exception an internal unexpected exception that happened. - */ -class SubmitPasswordErrorV2( - errorType: String? = null, - error: String? = null, - errorMessage: String?, - correlationId: String, - scenario: NativeAuthFlowScenarioV2, - errorCodes: List? = null, - exception: Exception? = null -) : NativeAuthErrorV2(errorType, error, errorMessage, correlationId, scenario, errorCodes, exception) { - - /** - * Returns true if the password submitted through - * [com.microsoft.identity.nativeauth.statemachine.states.PasswordRequiredStateV2.submitPassword] - * was rejected. The caller can retry on the same state instance. - */ - fun isInvalidPassword(): Boolean = this.errorType == ErrorTypes.INVALID_PASSWORD -} diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/SignUpErrorsV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/SignUpErrorsV2.kt deleted file mode 100644 index f858d69f1..000000000 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/SignUpErrorsV2.kt +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// All rights reserved. -// -// This code is licensed under the MIT License. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files(the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions : -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -package com.microsoft.identity.nativeauth.statemachine.errors - -import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 - -/** - * Sign up error for the Native Auth V2 surface. Use the utility methods of this class to identify - * and handle the error. This error is produced by - * [com.microsoft.identity.nativeauth.INativeAuthPublicClientApplication.signUpV2]. - * - * @param errorType the error type value of the error that occurred. - * @param error the error returned by the authentication server. - * @param errorMessage the error message returned by the authentication server. - * @param correlationId a unique identifier for the request that can help in diagnostics. - * @param scenario identifies which part of the Native Auth V2 surface produced this error. - * @param errorCodes a list of specific error codes returned by the authentication server. - * @param exception an internal unexpected exception that happened. - */ -class SignUpErrorV2( - errorType: String? = null, - error: String? = null, - errorMessage: String?, - correlationId: String, - scenario: NativeAuthFlowScenarioV2, - errorCodes: List? = null, - exception: Exception? = null -) : NativeAuthErrorV2(errorType, error, errorMessage, correlationId, scenario, errorCodes, exception) { - - fun isUserAlreadyExists(): Boolean = this.errorType == SignUpErrorTypes.USER_ALREADY_EXISTS - - fun isInvalidUsername(): Boolean = this.errorType == ErrorTypes.INVALID_USERNAME - - fun isInvalidAttributes(): Boolean = this.errorType == SignUpErrorTypes.INVALID_ATTRIBUTES - - fun isInvalidPassword(): Boolean = this.errorType == ErrorTypes.INVALID_PASSWORD - - fun isAuthNotSupported(): Boolean = this.errorType == SignUpErrorTypes.AUTH_NOT_SUPPORTED -} - -/** - * Submit attributes error for the Native Auth V2 surface. Use the utility methods of this class to - * identify and handle the error. This error is produced by - * [com.microsoft.identity.nativeauth.statemachine.states.AttributesRequiredStateV2.submitAttributes] - * and [com.microsoft.identity.nativeauth.statemachine.states.AttributesInvalidStateV2.submitAttributes]. - * - * @param errorType the error type value of the error that occurred. - * @param error the error returned by the authentication server. - * @param errorMessage the error message returned by the authentication server. - * @param correlationId a unique identifier for the request that can help in diagnostics. - * @param scenario identifies which part of the Native Auth V2 surface produced this error. - * @param errorCodes a list of specific error codes returned by the authentication server. - * @param exception an internal unexpected exception that happened. - */ -class SubmitAttributesErrorV2( - errorType: String? = null, - error: String? = null, - errorMessage: String?, - correlationId: String, - scenario: NativeAuthFlowScenarioV2, - errorCodes: List? = null, - exception: Exception? = null -) : NativeAuthErrorV2(errorType, error, errorMessage, correlationId, scenario, errorCodes, exception) { - - fun isInvalidAttributes(): Boolean = this.errorType == SignUpErrorTypes.INVALID_ATTRIBUTES -} diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/results/NativeAuthResultV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/results/NativeAuthResultV2.kt index dfff8e01e..048cb6818 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/results/NativeAuthResultV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/results/NativeAuthResultV2.kt @@ -164,10 +164,12 @@ interface NativeAuthResultV2 : Result { * @param nextState the current state with follow-on methods. * @param invalidAttributes the names of the attributes that were rejected by the server. */ - class AttributesInvalid( + class AttributesInvalid @JvmOverloads constructor( override val nextState: AttributesInvalidStateV2, override val scenario: NativeAuthFlowScenarioV2, - invalidAttributes: List + invalidAttributes: List, + /** Shared error diagnostics and classifiers, while retaining the retry state. */ + val error: com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2? = null ) : Result.SuccessResult(nextState = nextState), NativeAuthResultV2 { val invalidAttributes: List = Collections.unmodifiableList(ArrayList(invalidAttributes)) } diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/AttributesRequiredStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/AttributesRequiredStateV2.kt index 47458b715..d3bb445a5 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/AttributesRequiredStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/AttributesRequiredStateV2.kt @@ -46,8 +46,6 @@ import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 import com.microsoft.identity.nativeauth.statemachine.errors.ErrorTypes import com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2 import com.microsoft.identity.nativeauth.statemachine.errors.SignUpErrorTypes -import com.microsoft.identity.nativeauth.statemachine.errors.SignUpErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SubmitAttributesErrorV2 import com.microsoft.identity.nativeauth.statemachine.results.NativeAuthResultV2 import com.microsoft.identity.nativeauth.toListOfRequiredUserAttributeV2 import com.microsoft.identity.nativeauth.toMap @@ -202,7 +200,8 @@ internal suspend fun submitAttributesInternal( NativeAuthResultV2.AttributesInvalid( nextState = AttributesInvalidStateV2(result.continuationState, scenario, config), scenario = scenario, - invalidAttributes = result.invalidAttributes + invalidAttributes = result.invalidAttributes, + error = NativeAuthErrorV2.fromAttributeError(result, scenario) ) } is NativeAuthV2CommandResult.SignInAfterSignUpRequired -> { @@ -212,17 +211,19 @@ internal suspend fun submitAttributesInternal( ) } is NativeAuthV2CommandResult.UserAlreadyExists -> { - SignUpErrorV2( + NativeAuthErrorV2( errorType = SignUpErrorTypes.USER_ALREADY_EXISTS, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, - errorCodes = result.errorCodes + errorCodes = result.errorCodes, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails ) } is SignUpCommandResult.InvalidAttributes -> { - SubmitAttributesErrorV2( + NativeAuthErrorV2( errorType = SignUpErrorTypes.INVALID_ATTRIBUTES, error = result.error, errorMessage = result.errorDescription, @@ -240,7 +241,7 @@ internal suspend fun submitAttributesInternal( ) } is INativeAuthCommandResult.Redirect -> { - SubmitAttributesErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, error = result.error, errorMessage = result.errorDescription, @@ -249,18 +250,20 @@ internal suspend fun submitAttributesInternal( ) } is INativeAuthCommandResult.APIError -> { - SubmitAttributesErrorV2( + NativeAuthErrorV2( error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, errorCodes = result.errorCodes, - exception = result.exception + exception = result.exception, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails ) } else -> { Logger.warnWithObject(TAG, result.correlationId, "V2 submitAttributes received unsupported result: ", result) - SubmitAttributesErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.INVALID_STATE, errorMessage = "V2 submit attributes returned an unsupported result.", correlationId = result.correlationId, @@ -272,7 +275,7 @@ internal suspend fun submitAttributesInternal( throw e } catch (e: Exception) { Logger.error(TAG, correlationId, "Exception thrown in submitAttributes", e) - SubmitAttributesErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.CLIENT_EXCEPTION, errorMessage = "MSAL client exception occurred in submitAttributes.", correlationId = correlationId, diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/CodeRequiredStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/CodeRequiredStateV2.kt index 14dda9d25..53f3e43a6 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/CodeRequiredStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/CodeRequiredStateV2.kt @@ -49,7 +49,6 @@ import com.microsoft.identity.nativeauth.NativeAuthPublicClientApplication import com.microsoft.identity.nativeauth.NativeAuthPublicClientApplicationConfiguration import com.microsoft.identity.nativeauth.statemachine.errors.ErrorTypes import com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SubmitCodeErrorV2 import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 import com.microsoft.identity.nativeauth.statemachine.results.NativeAuthResultV2 import com.microsoft.identity.nativeauth.toListOfRequiredUserAttributeV2 @@ -118,7 +117,7 @@ class CodeRequiredStateV2 internal constructor( ) val state = continuationState ?: return invalidState() if (code.isEmpty()) { - return SubmitCodeErrorV2( + return NativeAuthErrorV2( errorType = ErrorTypes.INVALID_CODE, errorMessage = "Code cannot be empty.", correlationId = correlationId, @@ -146,7 +145,7 @@ class CodeRequiredStateV2 internal constructor( throw e } catch (e: Exception) { Logger.error(TAG, correlationId, "Exception thrown in submitCode", e) - SubmitCodeErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.CLIENT_EXCEPTION, errorMessage = "MSAL client exception occurred in submitCode.", correlationId = correlationId, @@ -285,7 +284,7 @@ class CodeRequiredStateV2 internal constructor( private fun unsupportedSubmitCodeResult(result: INativeAuthCommandResult): NativeAuthResultV2 { Logger.warnWithObject(TAG, result.correlationId, "V2 submitCode received unsupported result: ", result) - return SubmitCodeErrorV2( + return NativeAuthErrorV2( errorType = ErrorTypes.INVALID_STATE, errorMessage = "V2 submit code returned an unsupported result.", correlationId = result.correlationId, @@ -296,7 +295,7 @@ class CodeRequiredStateV2 internal constructor( private fun mapSubmitCodeError(result: INativeAuthCommandResult): NativeAuthResultV2 = when (result) { is NativeAuthV2CommandResult.IncorrectCode -> { - SubmitCodeErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.INVALID_CODE, error = result.error, errorMessage = result.errorDescription, @@ -316,7 +315,7 @@ class CodeRequiredStateV2 internal constructor( ) } is INativeAuthCommandResult.Redirect -> { - SubmitCodeErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, error = result.error, errorMessage = result.errorDescription, @@ -325,18 +324,20 @@ class CodeRequiredStateV2 internal constructor( ) } is INativeAuthCommandResult.APIError -> { - SubmitCodeErrorV2( + NativeAuthErrorV2( error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, errorCodes = result.errorCodes, - exception = result.exception + exception = result.exception, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails ) } else -> { Logger.warn(TAG, result.correlationId, "Unexpected submit-code result.") - SubmitCodeErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.CLIENT_EXCEPTION, errorMessage = "Unexpected submit-code result.", correlationId = result.correlationId, @@ -429,7 +430,9 @@ class CodeRequiredStateV2 internal constructor( correlationId = result.correlationId, scenario = scenario, errorCodes = result.errorCodes, - exception = result.exception + exception = result.exception, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails ) } else -> { diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFARequiredStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFARequiredStateV2.kt index f09afa4f8..8a3c5643a 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFARequiredStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFARequiredStateV2.kt @@ -44,7 +44,6 @@ import com.microsoft.identity.nativeauth.NativeAuthPublicClientApplication import com.microsoft.identity.nativeauth.NativeAuthPublicClientApplicationConfiguration import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 import com.microsoft.identity.nativeauth.statemachine.errors.ErrorTypes -import com.microsoft.identity.nativeauth.statemachine.errors.MFARequestChallengeErrorV2 import com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2 import com.microsoft.identity.nativeauth.statemachine.results.NativeAuthResultV2 import com.microsoft.identity.nativeauth.utils.getCancellable @@ -152,7 +151,7 @@ class MFARequiredStateV2 internal constructor( val state = continuationState ?: return invalidState() val offeredMethod = authMethods.firstOrNull { it.id == method.id } - ?: return MFARequestChallengeErrorV2( + ?: return NativeAuthErrorV2( errorType = ErrorTypes.INVALID_STATE, errorMessage = "The selected authentication method is not one of the methods the server offered.", correlationId = correlationId, @@ -168,7 +167,7 @@ class MFARequiredStateV2 internal constructor( ignoreCase = true ) ) { - return MFARequestChallengeErrorV2( + return NativeAuthErrorV2( errorType = ErrorTypes.NOT_IMPLEMENTED, errorMessage = "Only email and SMS authentication methods are supported for multi-factor authentication.", correlationId = correlationId, @@ -216,7 +215,7 @@ class MFARequiredStateV2 internal constructor( ) } is INativeAuthCommandResult.Redirect -> { - MFARequestChallengeErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, error = result.error, errorMessage = result.errorDescription, @@ -225,18 +224,20 @@ class MFARequiredStateV2 internal constructor( ) } is INativeAuthCommandResult.APIError -> { - MFARequestChallengeErrorV2( + NativeAuthErrorV2( error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, errorCodes = result.errorCodes, - exception = result.exception + exception = result.exception, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails ) } else -> { Logger.warnWithObject(TAG, result.correlationId, "V2 selectAuthMethod received unsupported result: ", result) - MFARequestChallengeErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.INVALID_STATE, errorMessage = "V2 select authentication method returned an unsupported result.", correlationId = result.correlationId, @@ -248,7 +249,7 @@ class MFARequiredStateV2 internal constructor( throw e } catch (e: Exception) { Logger.error(TAG, correlationId, "Exception thrown in selectAuthMethod", e) - MFARequestChallengeErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.CLIENT_EXCEPTION, errorMessage = "MSAL client exception occurred in selectAuthMethod.", correlationId = correlationId, diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFAVerificationRequiredStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFAVerificationRequiredStateV2.kt index 961d202b1..beefaa4cb 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFAVerificationRequiredStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFAVerificationRequiredStateV2.kt @@ -44,7 +44,6 @@ import com.microsoft.identity.nativeauth.NativeAuthPublicClientApplication import com.microsoft.identity.nativeauth.NativeAuthPublicClientApplicationConfiguration import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 import com.microsoft.identity.nativeauth.statemachine.errors.ErrorTypes -import com.microsoft.identity.nativeauth.statemachine.errors.MFASubmitChallengeErrorV2 import com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2 import com.microsoft.identity.nativeauth.statemachine.results.NativeAuthResultV2 import com.microsoft.identity.nativeauth.utils.getCancellable @@ -58,8 +57,8 @@ import kotlinx.coroutines.withContext /** * State that requires the user to submit a multi-factor authentication challenge. * - * A wrong code is a recoverable [MFASubmitChallengeErrorV2] with - * [MFASubmitChallengeErrorV2.isInvalidChallenge]; the caller retries on this same state instance or + * A wrong code is a recoverable [NativeAuthErrorV2] with + * [NativeAuthErrorV2.isInvalidChallenge]; the caller retries on this same state instance or * requests a fresh challenge through [resendChallenge]. Successful resends return a fresh * [NativeAuthResultV2.MFAVerificationRequired] whose state carries the latest opaque continuation * required for subsequent submit/resend calls. No error result carries a next state. @@ -129,7 +128,7 @@ class MFAVerificationRequiredStateV2 internal constructor( ) val state = continuationState ?: return invalidState() if (challenge.isEmpty()) { - return MFASubmitChallengeErrorV2( + return NativeAuthErrorV2( errorType = ErrorTypes.INVALID_CODE, errorMessage = "Challenge cannot be empty.", correlationId = correlationId, @@ -157,7 +156,7 @@ class MFAVerificationRequiredStateV2 internal constructor( mapCompleteResult(result) } is NativeAuthV2CommandResult.IncorrectCode -> { - MFASubmitChallengeErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.INVALID_CODE, error = result.error, errorMessage = result.errorDescription, @@ -177,7 +176,7 @@ class MFAVerificationRequiredStateV2 internal constructor( ) } is INativeAuthCommandResult.Redirect -> { - MFASubmitChallengeErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, error = result.error, errorMessage = result.errorDescription, @@ -186,18 +185,20 @@ class MFAVerificationRequiredStateV2 internal constructor( ) } is INativeAuthCommandResult.APIError -> { - MFASubmitChallengeErrorV2( + NativeAuthErrorV2( error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, errorCodes = result.errorCodes, - exception = result.exception + exception = result.exception, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails ) } else -> { Logger.warnWithObject(TAG, result.correlationId, "V2 submitChallenge received unsupported result: ", result) - MFASubmitChallengeErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.INVALID_STATE, errorMessage = "V2 submit MFA challenge returned an unsupported result.", correlationId = result.correlationId, @@ -209,7 +210,7 @@ class MFAVerificationRequiredStateV2 internal constructor( throw e } catch (e: Exception) { Logger.error(TAG, correlationId, "Exception thrown in submitChallenge", e) - MFASubmitChallengeErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.CLIENT_EXCEPTION, errorMessage = "MSAL client exception occurred in submitChallenge.", correlationId = correlationId, @@ -307,7 +308,9 @@ class MFAVerificationRequiredStateV2 internal constructor( correlationId = result.correlationId, scenario = scenario, errorCodes = result.errorCodes, - exception = result.exception + exception = result.exception, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails ) } else -> { diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/NewPasswordRequiredStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/NewPasswordRequiredStateV2.kt index 9d905b641..2627a810d 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/NewPasswordRequiredStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/NewPasswordRequiredStateV2.kt @@ -44,7 +44,6 @@ import com.microsoft.identity.nativeauth.NativeAuthPublicClientApplicationConfig import com.microsoft.identity.nativeauth.statemachine.errors.ErrorTypes import com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2 import com.microsoft.identity.nativeauth.statemachine.errors.ResetPasswordErrorTypes -import com.microsoft.identity.nativeauth.statemachine.errors.SubmitNewPasswordErrorV2 import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 import com.microsoft.identity.nativeauth.statemachine.results.NativeAuthResultV2 import com.microsoft.identity.nativeauth.utils.getCancellable @@ -130,7 +129,7 @@ class NewPasswordRequiredStateV2 internal constructor( private suspend fun submitNewPasswordInternal(password: CharArray): NativeAuthResultV2 { val state = continuationState ?: return notImplemented() if (password.isEmpty()) { - return SubmitNewPasswordErrorV2( + return NativeAuthErrorV2( errorType = ErrorTypes.INVALID_PASSWORD, errorMessage = "Password cannot be empty.", correlationId = correlationId, @@ -164,7 +163,7 @@ class NewPasswordRequiredStateV2 internal constructor( ) } is NativeAuthV2CommandResult.PasswordNotAccepted -> { - SubmitNewPasswordErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.INVALID_PASSWORD, error = result.error, errorMessage = result.errorDescription, @@ -175,7 +174,7 @@ class NewPasswordRequiredStateV2 internal constructor( ) } is NativeAuthV2CommandResult.PasswordResetFailed -> { - SubmitNewPasswordErrorV2( + NativeAuthErrorV2( errorType = ResetPasswordErrorTypes.PASSWORD_RESET_FAILED, error = result.error, errorMessage = result.errorDescription, @@ -193,7 +192,7 @@ class NewPasswordRequiredStateV2 internal constructor( ) } is INativeAuthCommandResult.Redirect -> { - SubmitNewPasswordErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, error = result.error, errorMessage = result.errorDescription, @@ -202,18 +201,20 @@ class NewPasswordRequiredStateV2 internal constructor( ) } is INativeAuthCommandResult.APIError -> { - SubmitNewPasswordErrorV2( + NativeAuthErrorV2( error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, errorCodes = result.errorCodes, - exception = result.exception + exception = result.exception, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails ) } else -> { Logger.warnWithObject(TAG, result.correlationId, "V2 submitNewPassword received unsupported result: ", result) - SubmitNewPasswordErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.INVALID_STATE, errorMessage = "V2 submit new password returned an unsupported result.", correlationId = result.correlationId, @@ -225,7 +226,7 @@ class NewPasswordRequiredStateV2 internal constructor( throw e } catch (e: Exception) { Logger.error(TAG, correlationId, "Exception thrown in submitNewPassword", e) - SubmitNewPasswordErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.CLIENT_EXCEPTION, errorMessage = "MSAL client exception occurred in submitNewPassword.", correlationId = correlationId, diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/PasswordRequiredStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/PasswordRequiredStateV2.kt index 0c5c75961..af74858ce 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/PasswordRequiredStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/PasswordRequiredStateV2.kt @@ -48,8 +48,6 @@ import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 import com.microsoft.identity.nativeauth.statemachine.errors.ErrorTypes import com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2 import com.microsoft.identity.nativeauth.statemachine.errors.SignUpErrorTypes -import com.microsoft.identity.nativeauth.statemachine.errors.SignUpErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SubmitPasswordErrorV2 import com.microsoft.identity.nativeauth.statemachine.results.NativeAuthResultV2 import com.microsoft.identity.nativeauth.toListOfRequiredUserAttributeV2 import com.microsoft.identity.nativeauth.toListOfV2AuthMethods @@ -65,10 +63,10 @@ import kotlinx.coroutines.withContext /** * State that requires the user to submit a password. * - * A password rejected here is a [SubmitPasswordErrorV2] with - * [SubmitPasswordErrorV2.isInvalidPassword], not an invalid-credentials error: the account itself - * was already accepted at the sign-in entry point and only the deferred password was wrong. The - * caller retries on this same state instance; no error result carries a next state. + * A rejected sign-in password is a [NativeAuthErrorV2] classified by + * [NativeAuthErrorV2.isInvalidCredentials]; a rejected new sign-up password is classified by + * [NativeAuthErrorV2.isInvalidPassword]. The caller retries on this same state instance; + * no error result carries a next state. */ class PasswordRequiredStateV2 internal constructor( correlationId: String, @@ -137,8 +135,8 @@ class PasswordRequiredStateV2 internal constructor( ) val state = continuationState ?: return invalidState() if (password.isEmpty()) { - return SubmitPasswordErrorV2( - errorType = ErrorTypes.INVALID_PASSWORD, + return NativeAuthErrorV2( + errorType = if (scenario == NativeAuthFlowScenarioV2.SIGN_UP) ErrorTypes.INVALID_PASSWORD else ErrorTypes.INVALID_INPUT, errorMessage = "Password cannot be empty.", correlationId = correlationId, scenario = scenario @@ -185,13 +183,14 @@ class PasswordRequiredStateV2 internal constructor( ) } is NativeAuthV2CommandResult.IncorrectPassword -> { - SubmitPasswordErrorV2( - errorType = ErrorTypes.INVALID_PASSWORD, + NativeAuthErrorV2( + errorType = com.microsoft.identity.nativeauth.statemachine.errors.SignInErrorTypes.INVALID_CREDENTIALS, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, - errorCodes = result.errorCodes + errorCodes = result.errorCodes, + subError = result.subError ) } is NativeAuthV2CommandResult.NotImplemented -> { @@ -204,7 +203,7 @@ class PasswordRequiredStateV2 internal constructor( ) } is INativeAuthCommandResult.Redirect -> { - SubmitPasswordErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, error = result.error, errorMessage = result.errorDescription, @@ -213,18 +212,20 @@ class PasswordRequiredStateV2 internal constructor( ) } is INativeAuthCommandResult.APIError -> { - SubmitPasswordErrorV2( + NativeAuthErrorV2( error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, errorCodes = result.errorCodes, - exception = result.exception + exception = result.exception, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails ) } else -> { Logger.warnWithObject(TAG, result.correlationId, "V2 submitPassword received unsupported result: ", result) - SubmitPasswordErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.INVALID_STATE, errorMessage = "V2 submit password returned an unsupported result.", correlationId = result.correlationId, @@ -236,7 +237,7 @@ class PasswordRequiredStateV2 internal constructor( throw e } catch (e: Exception) { Logger.error(TAG, correlationId, "Exception thrown in submitPassword", e) - SubmitPasswordErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.CLIENT_EXCEPTION, errorMessage = "MSAL client exception occurred in submitPassword.", correlationId = correlationId, @@ -252,8 +253,8 @@ class PasswordRequiredStateV2 internal constructor( /** * Submits the deferred password during a V2 sign-up flow through the submit-attributes * endpoint (sign-up carries the password as the `password` attribute). A password rejected by - * the server's policy is surfaced as a [SubmitPasswordErrorV2] with - * [SubmitPasswordErrorV2.isInvalidPassword] so the caller can retry on this same state. + * the server's policy is surfaced as a [NativeAuthErrorV2] with + * [NativeAuthErrorV2.isInvalidPassword] so the caller can retry on this same state. */ private suspend fun submitSignUpPassword( state: NativeAuthV2ContinuationState, @@ -307,34 +308,39 @@ class PasswordRequiredStateV2 internal constructor( val onlyPasswordIsInvalid = result.invalidAttributes.isNotEmpty() && result.invalidAttributes.all { it.equals("password", ignoreCase = true) } if (onlyPasswordIsInvalid) { - SubmitPasswordErrorV2( - errorType = ErrorTypes.INVALID_PASSWORD, + NativeAuthErrorV2( + errorType = if (result.attributeValidationDetails == null) ErrorTypes.INVALID_PASSWORD else SignUpErrorTypes.INVALID_ATTRIBUTES, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, - errorCodes = result.errorCodes + errorCodes = result.errorCodes, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails ) } else { NativeAuthResultV2.AttributesInvalid( nextState = AttributesInvalidStateV2(result.continuationState, scenario, config), scenario = scenario, - invalidAttributes = result.invalidAttributes + invalidAttributes = result.invalidAttributes, + error = NativeAuthErrorV2.fromAttributeError(result, scenario) ) } } is NativeAuthV2CommandResult.UserAlreadyExists -> { - SignUpErrorV2( + NativeAuthErrorV2( errorType = SignUpErrorTypes.USER_ALREADY_EXISTS, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, - errorCodes = result.errorCodes + errorCodes = result.errorCodes, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails ) } is SignUpCommandResult.InvalidAttributes -> { - SubmitPasswordErrorV2( + NativeAuthErrorV2( errorType = SignUpErrorTypes.INVALID_ATTRIBUTES, error = result.error, errorMessage = result.errorDescription, @@ -352,7 +358,7 @@ class PasswordRequiredStateV2 internal constructor( ) } is INativeAuthCommandResult.Redirect -> { - SubmitPasswordErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, error = result.error, errorMessage = result.errorDescription, @@ -361,18 +367,20 @@ class PasswordRequiredStateV2 internal constructor( ) } is INativeAuthCommandResult.APIError -> { - SubmitPasswordErrorV2( + NativeAuthErrorV2( error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, errorCodes = result.errorCodes, - exception = result.exception + exception = result.exception, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails ) } else -> { Logger.warnWithObject(TAG, result.correlationId, "V2 submitPassword attributes received unsupported result: ", result) - SubmitPasswordErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.INVALID_STATE, errorMessage = "V2 submit sign-up password returned an unsupported result.", correlationId = result.correlationId, diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/ResetPasswordMethodRequiredStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/ResetPasswordMethodRequiredStateV2.kt index e48f58bb0..775134076 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/ResetPasswordMethodRequiredStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/ResetPasswordMethodRequiredStateV2.kt @@ -44,7 +44,6 @@ import com.microsoft.identity.nativeauth.NativeAuthPublicClientApplicationConfig import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 import com.microsoft.identity.nativeauth.statemachine.errors.ErrorTypes import com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.ResetPasswordErrorV2 import com.microsoft.identity.nativeauth.statemachine.results.NativeAuthResultV2 import com.microsoft.identity.nativeauth.utils.getCancellable import com.microsoft.identity.nativeauth.utils.serializable @@ -130,7 +129,7 @@ class ResetPasswordMethodRequiredStateV2 internal constructor( ) val state = continuationState ?: return invalidState() val offeredMethod = authMethods.firstOrNull { it.id == method.id } - ?: return ResetPasswordErrorV2( + ?: return NativeAuthErrorV2( errorType = ErrorTypes.INVALID_STATE, errorMessage = "The selected authentication method is not one of the methods the server offered.", correlationId = correlationId, @@ -192,20 +191,22 @@ class ResetPasswordMethodRequiredStateV2 internal constructor( correlationId = result.correlationId, scenario = scenario ) - is INativeAuthCommandResult.Redirect -> ResetPasswordErrorV2( + is INativeAuthCommandResult.Redirect -> NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario ) - is INativeAuthCommandResult.APIError -> ResetPasswordErrorV2( + is INativeAuthCommandResult.APIError -> NativeAuthErrorV2( error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, errorCodes = result.errorCodes, - exception = result.exception + exception = result.exception, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails ) else -> { Logger.warnWithObject( @@ -214,7 +215,7 @@ class ResetPasswordMethodRequiredStateV2 internal constructor( "V2 selectResetPasswordMethod received unsupported result: ", result ) - ResetPasswordErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.INVALID_STATE, errorMessage = "V2 reset password method selection returned an unsupported result.", correlationId = result.correlationId, @@ -226,7 +227,7 @@ class ResetPasswordMethodRequiredStateV2 internal constructor( throw e } catch (e: Exception) { Logger.error(TAG, correlationId, "Exception thrown in selectAuthMethod", e) - ResetPasswordErrorV2( + NativeAuthErrorV2( errorType = ErrorTypes.CLIENT_EXCEPTION, errorMessage = "MSAL client exception occurred in selectAuthMethod.", correlationId = correlationId, diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/SignInAfterResetPasswordStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/SignInAfterResetPasswordStateV2.kt index 7a4fab611..669b15c07 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/SignInAfterResetPasswordStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/SignInAfterResetPasswordStateV2.kt @@ -195,7 +195,9 @@ class SignInAfterResetPasswordStateV2 internal constructor( correlationId = result.correlationId, scenario = scenario, errorCodes = result.errorCodes, - exception = result.exception + exception = result.exception, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails ) } else -> { diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/SignInAfterSignUpStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/SignInAfterSignUpStateV2.kt index faf6bb9e9..60a828df0 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/SignInAfterSignUpStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/SignInAfterSignUpStateV2.kt @@ -195,7 +195,9 @@ class SignInAfterSignUpStateV2 internal constructor( correlationId = result.correlationId, scenario = scenario, errorCodes = result.errorCodes, - exception = result.exception + exception = result.exception, + subError = result.subError, + attributeValidationDetails = result.attributeValidationDetails ) } is NativeAuthV2CommandResult.NotImplemented -> { diff --git a/msal/src/test/java/com/microsoft/identity/client/e2e/tests/network/nativeauth/SignInV2EmailPasswordTest.kt b/msal/src/test/java/com/microsoft/identity/client/e2e/tests/network/nativeauth/SignInV2EmailPasswordTest.kt index 2c06f8f56..40b1576a1 100644 --- a/msal/src/test/java/com/microsoft/identity/client/e2e/tests/network/nativeauth/SignInV2EmailPasswordTest.kt +++ b/msal/src/test/java/com/microsoft/identity/client/e2e/tests/network/nativeauth/SignInV2EmailPasswordTest.kt @@ -30,9 +30,7 @@ import com.microsoft.identity.internal.testutils.nativeauth.api.models.NativeAut import com.microsoft.identity.nativeauth.INativeAuthPublicClientApplication import com.microsoft.identity.nativeauth.parameters.NativeAuthGetAccessTokenParameters import com.microsoft.identity.nativeauth.parameters.NativeAuthSignInParameters -import com.microsoft.identity.nativeauth.statemachine.errors.MFASubmitChallengeErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SignInErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SubmitPasswordErrorV2 +import com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2 import com.microsoft.identity.nativeauth.statemachine.results.GetAccessTokenResult import com.microsoft.identity.nativeauth.statemachine.results.NativeAuthResultV2 import kotlinx.coroutines.runBlocking @@ -119,8 +117,8 @@ class SignInV2EmailPasswordTest : NativeAuthPublicClientApplicationAbstractTest( val param = NativeAuthSignInParameters(username = INVALID_EMAIL) param.password = getSafePassword().toCharArray() val result = application.signInV2(param) - assertTrue(result is SignInErrorV2) - assertTrue((result as SignInErrorV2).isUserNotFound()) + assertTrue(result is NativeAuthErrorV2) + assertTrue((result as NativeAuthErrorV2).isUserNotFound()) } } @@ -137,8 +135,8 @@ class SignInV2EmailPasswordTest : NativeAuthPublicClientApplicationAbstractTest( val param = NativeAuthSignInParameters(username = config.email) param.password = INVALID_PASSWORD.toCharArray() val result = application.signInV2(param) - assertTrue(result is SignInErrorV2) - assertTrue((result as SignInErrorV2).isInvalidCredentials()) + assertTrue(result is NativeAuthErrorV2) + assertTrue((result as NativeAuthErrorV2).isInvalidCredentials()) } } @@ -148,7 +146,7 @@ class SignInV2EmailPasswordTest : NativeAuthPublicClientApplicationAbstractTest( */ @Ignore("Native Auth V2 sign-in endpoints are not available on the E2E slice.") @Test - fun testErrorDeferredPasswordIsInvalidPassword() { + fun testErrorDeferredPasswordIsInvalidCredentialsNotPasswordPolicy() { config = getConfig(defaultConfigType) application = setupPCA(config, defaultChallengeTypes, defaultCapabilities) @@ -158,8 +156,9 @@ class SignInV2EmailPasswordTest : NativeAuthPublicClientApplicationAbstractTest( val passwordState = (result as NativeAuthResultV2.PasswordRequired).nextState val wrongPassword = passwordState.submitPassword(INVALID_PASSWORD.toCharArray()) - assertTrue(wrongPassword is SubmitPasswordErrorV2) - assertTrue((wrongPassword as SubmitPasswordErrorV2).isInvalidPassword()) + assertTrue(wrongPassword is NativeAuthErrorV2) + assertTrue((wrongPassword as NativeAuthErrorV2).isInvalidCredentials()) + assertFalse(wrongPassword.isInvalidPassword()) // Retrying on the retained state succeeds; the error carried no state of its own. val retry = passwordState.submitPassword(getSafePassword().toCharArray()) @@ -202,8 +201,8 @@ class SignInV2EmailPasswordTest : NativeAuthPublicClientApplicationAbstractTest( assertTrue(challengeResult.codeLength > 0) val incorrect = challengeResult.nextState.submitChallenge(INCORRECT_CODE) - assertTrue(incorrect is MFASubmitChallengeErrorV2) - assertTrue((incorrect as MFASubmitChallengeErrorV2).isInvalidChallenge()) + assertTrue(incorrect is NativeAuthErrorV2) + assertTrue((incorrect as NativeAuthErrorV2).isInvalidChallenge()) val freshChallenge = challengeResult.nextState.resendChallenge() assertResult(freshChallenge) @@ -248,7 +247,7 @@ class SignInV2EmailPasswordTest : NativeAuthPublicClientApplicationAbstractTest( challengeChannel = "email" ) val staleResult = mfaState.selectAuthMethod(stale) - assertTrue(staleResult is com.microsoft.identity.nativeauth.statemachine.errors.MFARequestChallengeErrorV2) + assertTrue(staleResult is com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2) // The real method still works afterwards. val emailMethod = result.authMethods.first { it.challengeChannel == "email" } @@ -275,8 +274,8 @@ class SignInV2EmailPasswordTest : NativeAuthPublicClientApplicationAbstractTest( second.password = getSafePassword().toCharArray() val result = application.signInV2(second) - assertTrue(result is SignInErrorV2) - val error = result as SignInErrorV2 + assertTrue(result is NativeAuthErrorV2) + val error = result as NativeAuthErrorV2 assertFalse(error.isInvalidCredentials()) assertTrue(error.exception is MsalClientException) assertEquals("An account is already signed in.", error.exception!!.message) diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2ErrorsTest.kt b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2ErrorsTest.kt index 978284980..d23661dd4 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2ErrorsTest.kt +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2ErrorsTest.kt @@ -23,20 +23,9 @@ package com.microsoft.identity.nativeauth.v2 import com.microsoft.identity.client.exception.MsalClientException -import com.microsoft.identity.nativeauth.statemachine.errors.MFARequestChallengeErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.MFASubmitChallengeErrorV2 +import com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2 import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 import com.microsoft.identity.nativeauth.statemachine.errors.ErrorTypes -import com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.RegisterStrongAuthChallengeErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.RegisterStrongAuthSubmitChallengeErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.ResetPasswordErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SignInErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SignUpErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SubmitAttributesErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SubmitCodeErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SubmitNewPasswordErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SubmitPasswordErrorV2 import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertSame @@ -54,18 +43,47 @@ class NativeAuthV2ErrorsTest { private val correlationId = "correlation-id" private val errorMessage = "error message" + @Test + fun unifiedError_credentialsAreNotPasswordPolicyViolations() { + val error = NativeAuthErrorV2( + error = "invalidGrant", subError = "invalidUserNameOrPassword", + errorMessage = errorMessage, correlationId = correlationId, + scenario = NativeAuthFlowScenarioV2.SIGN_IN + ) + assertTrue(error.isInvalidCredentials()) + assertFalse(error.isInvalidPassword()) + assertFalse(error.isGeneralError()) + } + + @Test + fun unifiedError_validatesAttributeDetailShapes() { + fun error(details: Any?) = NativeAuthErrorV2( + errorMessage = errorMessage, correlationId = correlationId, + scenario = NativeAuthFlowScenarioV2.SIGN_UP, + attributeValidationDetails = details + ) + assertTrue(error(listOf(mapOf("code" to "attributeRequired"))).isMissingRequiredAttributes()) + assertTrue(error(listOf(mapOf("code" to "passwordPolicyViolation", + "attributeIds" to listOf(42, "PASSWORD")))).isInvalidPassword()) + listOf(null, "bad", listOf(null, 42, mapOf("code" to 42)), + listOf(mapOf("code" to "passwordPolicyViolation", "attributeIds" to "password")), + listOf(mapOf("code" to "passwordPolicyViolation", "attributeIds" to listOf("other")))) + .forEach { assertFalse(error(it).isInvalidPassword()) } + assertTrue(error("bad").isGeneralError()) + } + @Test fun testSignInErrorV2UtilityMethods() { assertTrue( - SignInErrorV2(errorType = "user_not_found", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isUserNotFound() + NativeAuthErrorV2(errorType = "user_not_found", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isUserNotFound() ) assertTrue( - SignInErrorV2(errorType = "invalid_credentials", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidCredentials() + NativeAuthErrorV2(errorType = "invalid_credentials", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidCredentials() ) assertTrue( - SignInErrorV2(errorType = "invalid_username", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidUsername() + NativeAuthErrorV2(errorType = "invalid_username", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidUsername() ) - val error = SignInErrorV2( + val error = NativeAuthErrorV2( errorType = "other", errorMessage = errorMessage, correlationId = correlationId, @@ -82,31 +100,31 @@ class NativeAuthV2ErrorsTest { @Test fun testSubmitPasswordErrorV2UtilityMethods() { assertTrue( - SubmitPasswordErrorV2(errorType = "invalid_password", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidPassword() + NativeAuthErrorV2(errorType = "invalid_password", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidPassword() ) assertFalse( - SubmitPasswordErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidPassword() + NativeAuthErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidPassword() ) } @Test fun testSignUpErrorV2UtilityMethods() { assertTrue( - SignUpErrorV2(errorType = "user_already_exists", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isUserAlreadyExists() + NativeAuthErrorV2(errorType = "user_already_exists", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isUserAlreadyExists() ) assertTrue( - SignUpErrorV2(errorType = "invalid_username", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidUsername() + NativeAuthErrorV2(errorType = "invalid_username", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidUsername() ) assertTrue( - SignUpErrorV2(errorType = "invalid_attributes", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidAttributes() + NativeAuthErrorV2(errorType = "invalid_attributes", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidAttributes() ) assertTrue( - SignUpErrorV2(errorType = "invalid_password", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidPassword() + NativeAuthErrorV2(errorType = "invalid_password", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidPassword() ) assertTrue( - SignUpErrorV2(errorType = "auth_not_supported", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isAuthNotSupported() + NativeAuthErrorV2(errorType = "auth_not_supported", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isAuthNotSupported() ) - val error = SignUpErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN) + val error = NativeAuthErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN) assertFalse(error.isUserAlreadyExists()) assertFalse(error.isInvalidUsername()) assertFalse(error.isInvalidAttributes()) @@ -118,22 +136,22 @@ class NativeAuthV2ErrorsTest { @Test fun testSubmitAttributesErrorV2UtilityMethods() { assertTrue( - SubmitAttributesErrorV2(errorType = "invalid_attributes", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidAttributes() + NativeAuthErrorV2(errorType = "invalid_attributes", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidAttributes() ) assertFalse( - SubmitAttributesErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidAttributes() + NativeAuthErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidAttributes() ) } @Test fun testResetPasswordErrorV2UtilityMethods() { assertTrue( - ResetPasswordErrorV2(errorType = "user_not_found", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isUserNotFound() + NativeAuthErrorV2(errorType = "user_not_found", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isUserNotFound() ) assertTrue( - ResetPasswordErrorV2(errorType = "invalid_username", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidUsername() + NativeAuthErrorV2(errorType = "invalid_username", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidUsername() ) - val error = ResetPasswordErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN) + val error = NativeAuthErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN) assertFalse(error.isUserNotFound()) assertFalse(error.isInvalidUsername()) assertEquals(NativeAuthFlowScenarioV2.UNKNOWN, error.scenario) @@ -142,12 +160,12 @@ class NativeAuthV2ErrorsTest { @Test fun testSubmitNewPasswordErrorV2UtilityMethods() { assertTrue( - SubmitNewPasswordErrorV2(errorType = "invalid_password", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidPassword() + NativeAuthErrorV2(errorType = "invalid_password", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidPassword() ) assertTrue( - SubmitNewPasswordErrorV2(errorType = "password_reset_failed", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isPasswordResetFailed() + NativeAuthErrorV2(errorType = "password_reset_failed", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isPasswordResetFailed() ) - val error = SubmitNewPasswordErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN, subError = "sub") + val error = NativeAuthErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN, subError = "sub") assertFalse(error.isInvalidPassword()) assertFalse(error.isPasswordResetFailed()) assertEquals("sub", error.subError) @@ -157,8 +175,8 @@ class NativeAuthV2ErrorsTest { fun testNativeAuthV2ErrorsDoNotExposeRetryState() { listOf( NativeAuthErrorV2::class.java, - SubmitCodeErrorV2::class.java, - SubmitNewPasswordErrorV2::class.java + NativeAuthErrorV2::class.java, + NativeAuthErrorV2::class.java ).forEach { errorClass -> assertFalse( "${errorClass.simpleName} must not expose retry state", @@ -170,9 +188,9 @@ class NativeAuthV2ErrorsTest { @Test fun testSubmitCodeErrorV2UtilityMethods() { assertTrue( - SubmitCodeErrorV2(errorType = "invalid_code", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidCode() + NativeAuthErrorV2(errorType = "invalid_code", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidCode() ) - val error = SubmitCodeErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN, subError = "sub") + val error = NativeAuthErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN, subError = "sub") assertFalse(error.isInvalidCode()) assertEquals("sub", error.subError) } @@ -180,9 +198,9 @@ class NativeAuthV2ErrorsTest { @Test fun testMFARequestChallengeErrorV2UtilityMethods() { assertTrue( - MFARequestChallengeErrorV2(errorType = "auth_method_blocked", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isAuthMethodBlocked() + NativeAuthErrorV2(errorType = "auth_method_blocked", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isAuthMethodBlocked() ) - val error = MFARequestChallengeErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN, subError = "sub") + val error = NativeAuthErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN, subError = "sub") assertFalse(error.isAuthMethodBlocked()) assertEquals("sub", error.subError) } @@ -190,9 +208,9 @@ class NativeAuthV2ErrorsTest { @Test fun testMFASubmitChallengeErrorV2UtilityMethods() { assertTrue( - MFASubmitChallengeErrorV2(errorType = ErrorTypes.INVALID_CODE, errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidChallenge() + NativeAuthErrorV2(errorType = ErrorTypes.INVALID_CODE, errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidChallenge() ) - val error = MFASubmitChallengeErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN, subError = "sub") + val error = NativeAuthErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN, subError = "sub") assertFalse(error.isInvalidChallenge()) assertEquals("sub", error.subError) } @@ -200,12 +218,12 @@ class NativeAuthV2ErrorsTest { @Test fun testRegisterStrongAuthChallengeErrorV2UtilityMethods() { assertTrue( - RegisterStrongAuthChallengeErrorV2(errorType = "invalid_input", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidInput() + NativeAuthErrorV2(errorType = "invalid_input", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidInput() ) assertTrue( - RegisterStrongAuthChallengeErrorV2(errorType = "verification_contact_blocked", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isVerificationContactBlocked() + NativeAuthErrorV2(errorType = "verification_contact_blocked", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isVerificationContactBlocked() ) - val error = RegisterStrongAuthChallengeErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN) + val error = NativeAuthErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN) assertFalse(error.isInvalidInput()) assertFalse(error.isVerificationContactBlocked()) } @@ -213,10 +231,10 @@ class NativeAuthV2ErrorsTest { @Test fun testRegisterStrongAuthSubmitChallengeErrorV2UtilityMethods() { assertTrue( - RegisterStrongAuthSubmitChallengeErrorV2(errorType = "invalid_challenge", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidChallenge() + NativeAuthErrorV2(errorType = "invalid_challenge", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidChallenge() ) assertFalse( - RegisterStrongAuthSubmitChallengeErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidChallenge() + NativeAuthErrorV2(errorType = "other", errorMessage = errorMessage, correlationId = correlationId, scenario = NativeAuthFlowScenarioV2.UNKNOWN).isInvalidChallenge() ) } @@ -233,19 +251,8 @@ class NativeAuthV2ErrorsTest { val exception = MsalClientException("client_error", "boom") val scenario = NativeAuthFlowScenarioV2.SIGN_IN - val allErrors = listOf( - SignInErrorV2("type", error, errorMessage, correlationId, scenario, errorCodes, "sub", exception), - SubmitPasswordErrorV2("type", error, errorMessage, correlationId, scenario, errorCodes, exception), - SignUpErrorV2("type", error, errorMessage, correlationId, scenario, errorCodes, exception), - SubmitAttributesErrorV2("type", error, errorMessage, correlationId, scenario, errorCodes, exception), - ResetPasswordErrorV2("type", error, errorMessage, correlationId, scenario, errorCodes, exception), - SubmitNewPasswordErrorV2("type", error, errorMessage, correlationId, scenario, errorCodes, "sub", exception), - SubmitCodeErrorV2("type", error, errorMessage, correlationId, scenario, errorCodes, "sub", exception), - MFARequestChallengeErrorV2("type", error, errorMessage, correlationId, scenario, errorCodes, "sub", exception), - MFASubmitChallengeErrorV2("type", error, errorMessage, correlationId, scenario, errorCodes, "sub", exception), - RegisterStrongAuthChallengeErrorV2("type", error, errorMessage, correlationId, scenario, errorCodes, exception), - RegisterStrongAuthSubmitChallengeErrorV2("type", error, errorMessage, correlationId, scenario, errorCodes, exception), - NativeAuthErrorV2("type", error, errorMessage, correlationId, scenario, errorCodes, exception) + val allErrors = listOf( + NativeAuthErrorV2("type", error, errorMessage, correlationId, scenario, errorCodes, exception, "sub") ) for (built in allErrors) { @@ -268,17 +275,17 @@ class NativeAuthV2ErrorsTest { val scenario = NativeAuthFlowScenarioV2.UNKNOWN val allErrors = listOf( - SignInErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), - SubmitPasswordErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), - SignUpErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), - SubmitAttributesErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), - ResetPasswordErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), - SubmitNewPasswordErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), - SubmitCodeErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), - MFARequestChallengeErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), - MFASubmitChallengeErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), - RegisterStrongAuthChallengeErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), - RegisterStrongAuthSubmitChallengeErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), + NativeAuthErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), + NativeAuthErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), + NativeAuthErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), + NativeAuthErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), + NativeAuthErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), + NativeAuthErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), + NativeAuthErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), + NativeAuthErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), + NativeAuthErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), + NativeAuthErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), + NativeAuthErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario), NativeAuthErrorV2(errorMessage = errorMessage, correlationId = correlationId, scenario = scenario) ) diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2InterfaceJavaTest.java b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2InterfaceJavaTest.java index a45268b2c..9ee81e1e2 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2InterfaceJavaTest.java +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2InterfaceJavaTest.java @@ -41,8 +41,6 @@ import com.microsoft.identity.nativeauth.parameters.NativeAuthSignUpParameters; import com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2; import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2; -import com.microsoft.identity.nativeauth.statemachine.errors.SignInErrorV2; -import com.microsoft.identity.nativeauth.statemachine.errors.SignUpErrorV2; import com.microsoft.identity.nativeauth.statemachine.results.NativeAuthResultV2; import com.microsoft.identity.nativeauth.statemachine.states.MFAVerificationRequiredStateV2; import com.microsoft.identity.nativeauth.statemachine.states.SignInAfterResetPasswordStateV2; @@ -97,8 +95,8 @@ public void signInV2RejectsBlankUsername() throws ExecutionException, Interrupte final ResultFuture future = new ResultFuture<>(); application.signInV2(new NativeAuthSignInParameters(" "), newCallback(future)); final NativeAuthResultV2 result = future.get(30, TimeUnit.SECONDS); - assertTrue(result instanceof SignInErrorV2); - assertTrue(((SignInErrorV2) result).isInvalidUsername()); + assertTrue(result instanceof NativeAuthErrorV2); + assertTrue(((NativeAuthErrorV2) result).isInvalidUsername()); assertEquals(NativeAuthFlowScenarioV2.SIGN_IN, result.getScenario()); } @@ -109,8 +107,8 @@ public void signUpV2RejectsBlankUsername() throws ExecutionException, Interrupte final ResultFuture future = new ResultFuture<>(); application.signUpV2(new NativeAuthSignUpParameters(" "), newCallback(future)); final NativeAuthResultV2 result = future.get(30, TimeUnit.SECONDS); - assertTrue(result instanceof SignUpErrorV2); - assertTrue(((SignUpErrorV2) result).isInvalidUsername()); + assertTrue(result instanceof NativeAuthErrorV2); + assertTrue(((NativeAuthErrorV2) result).isInvalidUsername()); assertEquals(NativeAuthFlowScenarioV2.SIGN_UP, result.getScenario()); } diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2InterfaceKotlinTest.kt b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2InterfaceKotlinTest.kt index b664531d3..66b220fa5 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2InterfaceKotlinTest.kt +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2InterfaceKotlinTest.kt @@ -71,9 +71,6 @@ import com.microsoft.identity.nativeauth.parameters.NativeAuthSignInParameters import com.microsoft.identity.nativeauth.parameters.NativeAuthSignUpParameters import com.microsoft.identity.nativeauth.statemachine.errors.ErrorTypes import com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.ResetPasswordErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SubmitCodeErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SubmitNewPasswordErrorV2 import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 import com.microsoft.identity.nativeauth.statemachine.results.NativeAuthResultV2 import com.microsoft.identity.nativeauth.statemachine.states.AttributesInvalidStateV2 @@ -252,8 +249,8 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { AuthMethod("sms-1", "sms", "+X XXX XXX 34", "sms") ) - assertTrue(result is ResetPasswordErrorV2) - assertEquals(ErrorTypes.INVALID_STATE, (result as ResetPasswordErrorV2).errorType) + assertTrue(result is NativeAuthErrorV2) + assertEquals(ErrorTypes.INVALID_STATE, (result as NativeAuthErrorV2).errorType) } @Test @@ -294,12 +291,12 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { INativeAuthCommandResult.Redirect(correlationId, "browser"), NativeAuthV2SelectResetPasswordMethodCommand::class ) - assertTrue((state.selectAuthMethod(method) as ResetPasswordErrorV2).isBrowserRequired()) + assertTrue((state.selectAuthMethod(method) as NativeAuthErrorV2).isBrowserRequired()) enqueueResult(apiError(), NativeAuthV2SelectResetPasswordMethodCommand::class) assertEquals( errorCodes, - (state.selectAuthMethod(method) as ResetPasswordErrorV2).errorCodes + (state.selectAuthMethod(method) as NativeAuthErrorV2).errorCodes ) every { @@ -307,7 +304,7 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { match { NativeAuthV2SelectResetPasswordMethodCommand::class.java.isInstance(it) } ) } throws IllegalStateException("dispatcher unavailable") - val clientError = state.selectAuthMethod(method) as ResetPasswordErrorV2 + val clientError = state.selectAuthMethod(method) as NativeAuthErrorV2 assertEquals(ErrorTypes.CLIENT_EXCEPTION, clientError.errorType) assertTrue(clientError.exception is IllegalStateException) } @@ -334,7 +331,7 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { ) assertEquals( ErrorTypes.INVALID_STATE, - (methodRequired.nextState.selectAuthMethod(methodRequired.authMethods.first()) as ResetPasswordErrorV2).errorType + (methodRequired.nextState.selectAuthMethod(methodRequired.authMethods.first()) as NativeAuthErrorV2).errorType ) enqueueResult( @@ -343,7 +340,7 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { ) assertEquals( ErrorTypes.INVALID_STATE, - (application.resetPasswordV2(NativeAuthResetPasswordParameters(username)) as ResetPasswordErrorV2).errorType + (application.resetPasswordV2(NativeAuthResetPasswordParameters(username)) as NativeAuthErrorV2).errorType ) val codeState = codeRequiredState() @@ -353,7 +350,7 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { ) assertEquals( ErrorTypes.INVALID_STATE, - (codeState.submitCode("123456") as SubmitCodeErrorV2).errorType + (codeState.submitCode("123456") as NativeAuthErrorV2).errorType ) enqueueResult( @@ -377,7 +374,7 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { ) assertEquals( ErrorTypes.INVALID_STATE, - (passwordState.submitNewPassword("Password!".toCharArray()) as SubmitNewPasswordErrorV2).errorType + (passwordState.submitNewPassword("Password!".toCharArray()) as NativeAuthErrorV2).errorType ) val signInState = signInAfterResetState() @@ -403,8 +400,8 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { NativeAuthV2ResetPasswordStartCommand::class ) val userNotFound = application.resetPasswordV2(NativeAuthResetPasswordParameters(username)) - assertTrue(userNotFound is ResetPasswordErrorV2) - assertEquals(errorCodes, (userNotFound as ResetPasswordErrorV2).errorCodes) + assertTrue(userNotFound is NativeAuthErrorV2) + assertEquals(errorCodes, (userNotFound as NativeAuthErrorV2).errorCodes) enqueueResult( INativeAuthCommandResult.APIError( @@ -416,7 +413,7 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { NativeAuthV2ResetPasswordStartCommand::class ) val apiError = application.resetPasswordV2(NativeAuthResetPasswordParameters(username)) - assertEquals(errorCodes, (apiError as ResetPasswordErrorV2).errorCodes) + assertEquals(errorCodes, (apiError as NativeAuthErrorV2).errorCodes) enqueueCancellation(NativeAuthV2ResetPasswordStartCommand::class) assertCancellation { @@ -431,7 +428,7 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { try { val error = application.resetPasswordV2( NativeAuthResetPasswordParameters(username = " ") - ) as ResetPasswordErrorV2 + ) as NativeAuthErrorV2 assertTrue(error.isInvalidUsername()) assertEquals(correlationId, error.correlationId) @@ -451,8 +448,8 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { // matches the V1 flows, whose guard sits inside the try/catch that converts it to an // error result. val suspendResult = application.resetPasswordV2(NativeAuthResetPasswordParameters(username)) - assertTrue(suspendResult is ResetPasswordErrorV2) - assertEquals(ErrorTypes.CLIENT_EXCEPTION, (suspendResult as ResetPasswordErrorV2).errorType) + assertTrue(suspendResult is NativeAuthErrorV2) + assertEquals(ErrorTypes.CLIENT_EXCEPTION, (suspendResult as NativeAuthErrorV2).errorType) val suspendCause = suspendResult.exception assertTrue(suspendCause is MsalClientException) assertEquals("An account is already signed in.", suspendCause?.message) @@ -473,8 +470,8 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { } ) val result = callbackResult.get(10, TimeUnit.SECONDS) - assertTrue(result is ResetPasswordErrorV2) - assertEquals(ErrorTypes.CLIENT_EXCEPTION, (result as ResetPasswordErrorV2).errorType) + assertTrue(result is NativeAuthErrorV2) + assertEquals(ErrorTypes.CLIENT_EXCEPTION, (result as NativeAuthErrorV2).errorType) val callbackCause = result.exception assertTrue(callbackCause is MsalClientException) assertEquals("An account is already signed in.", callbackCause?.message) @@ -497,7 +494,7 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { ), NativeAuthV2ResetPasswordSubmitCodeCommand::class ) - val incorrectCode = state.submitCode("123456") as SubmitCodeErrorV2 + val incorrectCode = state.submitCode("123456") as NativeAuthErrorV2 assertTrue(incorrectCode.isInvalidCode()) assertEquals(errorCodes, incorrectCode.errorCodes) assertEquals(subError, incorrectCode.subError) @@ -512,7 +509,7 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { INativeAuthCommandResult.Redirect(correlationId, "browser"), NativeAuthV2ResetPasswordSubmitCodeCommand::class ) - assertTrue((state.submitCode("123456") as SubmitCodeErrorV2).isBrowserRequired()) + assertTrue((state.submitCode("123456") as NativeAuthErrorV2).isBrowserRequired()) enqueueResult( NativeAuthV2CommandResult.NotImplemented(correlationId, "unsupported", "unsupported"), @@ -521,7 +518,7 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { assertTrue((state.submitCode("123456") as NativeAuthErrorV2).isNotImplemented()) enqueueResult(apiError(), NativeAuthV2ResetPasswordSubmitCodeCommand::class) - assertEquals(errorCodes, (state.submitCode("123456") as SubmitCodeErrorV2).errorCodes) + assertEquals(errorCodes, (state.submitCode("123456") as NativeAuthErrorV2).errorCodes) enqueueResult( NativeAuthV2CommandResult.Complete(correlationId, null, null, null), @@ -579,7 +576,7 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { fun submitCodeRejectsEmptyInputWithRetryableInvalidCodeError() = runTest { val state = codeRequiredState() - val error = state.submitCode("") as SubmitCodeErrorV2 + val error = state.submitCode("") as NativeAuthErrorV2 assertTrue(error.isInvalidCode()) } @@ -639,7 +636,7 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { ), NativeAuthV2SubmitNewPasswordCommand::class ) - val rejected = state.submitNewPassword("Password!".toCharArray()) as SubmitNewPasswordErrorV2 + val rejected = state.submitNewPassword("Password!".toCharArray()) as NativeAuthErrorV2 assertTrue(rejected.isInvalidPassword()) assertEquals(errorCodes, rejected.errorCodes) assertEquals(subError, rejected.subError) @@ -649,7 +646,7 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { NativeAuthV2SubmitNewPasswordCommand::class ) assertTrue( - (state.submitNewPassword("Password!".toCharArray()) as SubmitNewPasswordErrorV2) + (state.submitNewPassword("Password!".toCharArray()) as NativeAuthErrorV2) .isPasswordResetFailed() ) @@ -668,7 +665,7 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { enqueueResult(apiError(), NativeAuthV2SubmitNewPasswordCommand::class) assertEquals( errorCodes, - (state.submitNewPassword("Password!".toCharArray()) as SubmitNewPasswordErrorV2).errorCodes + (state.submitNewPassword("Password!".toCharArray()) as NativeAuthErrorV2).errorCodes ) enqueueResult( @@ -676,7 +673,7 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { NativeAuthV2SubmitNewPasswordCommand::class ) assertTrue( - (state.submitNewPassword("Password!".toCharArray()) as SubmitNewPasswordErrorV2) + (state.submitNewPassword("Password!".toCharArray()) as NativeAuthErrorV2) .isBrowserRequired() ) @@ -711,7 +708,7 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { val state = (codeState.submitCode("123456") as NativeAuthResultV2.NewPasswordRequired).nextState - val error = state.submitNewPassword(charArrayOf()) as SubmitNewPasswordErrorV2 + val error = state.submitNewPassword(charArrayOf()) as NativeAuthErrorV2 assertTrue(error.isInvalidPassword()) } @@ -738,7 +735,7 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { ) } throws IllegalStateException("dispatcher unavailable") val dispatchFailurePassword = "Password!".toCharArray() - val error = state.submitNewPassword(dispatchFailurePassword) as SubmitNewPasswordErrorV2 + val error = state.submitNewPassword(dispatchFailurePassword) as NativeAuthErrorV2 assertEquals(ErrorTypes.CLIENT_EXCEPTION, error.errorType) assertPasswordCleared(dispatchFailurePassword) diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt index 56881d2b3..5c4efb04d 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt @@ -72,12 +72,7 @@ import com.microsoft.identity.nativeauth.NativeAuthPublicClientApplicationConfig import com.microsoft.identity.nativeauth.parameters.NativeAuthSignInParameters import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 import com.microsoft.identity.nativeauth.statemachine.errors.ErrorTypes -import com.microsoft.identity.nativeauth.statemachine.errors.MFARequestChallengeErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.MFASubmitChallengeErrorV2 import com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SignInErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SubmitCodeErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SubmitPasswordErrorV2 import com.microsoft.identity.nativeauth.statemachine.results.NativeAuthResultV2 import com.microsoft.identity.nativeauth.statemachine.states.Callback import com.microsoft.identity.nativeauth.statemachine.states.CodeRequiredStateV2 @@ -164,8 +159,8 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { every { DiagnosticContext.INSTANCE.threadCorrelationId } returns correlationId try { val result = application.signInV2(signInParameters(username = " ")) - assertTrue(result is SignInErrorV2) - val error = result as SignInErrorV2 + assertTrue(result is NativeAuthErrorV2) + val error = result as NativeAuthErrorV2 assertTrue(error.isInvalidUsername()) assertEquals(NativeAuthFlowScenarioV2.SIGN_IN, error.scenario) assertEquals(correlationId, error.correlationId) @@ -226,8 +221,8 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { val result = application.signInV2(signInParameters(password = "WrongPassword!".toCharArray())) - assertTrue(result is SignInErrorV2) - result as SignInErrorV2 + assertTrue(result is NativeAuthErrorV2) + result as NativeAuthErrorV2 assertTrue(result.isInvalidCredentials()) assertFalse(result.isUserNotFound()) assertEquals(errorCodes, result.errorCodes) @@ -249,7 +244,7 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { NativeAuthV2SignInStartCommand::class ) - val result = application.signInV2(signInParameters()) as SignInErrorV2 + val result = application.signInV2(signInParameters()) as NativeAuthErrorV2 assertTrue(result.isUserNotFound()) assertFalse(result.isInvalidCredentials()) @@ -263,7 +258,7 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { NativeAuthV2SignInStartCommand::class ) - val result = application.signInV2(signInParameters()) as SignInErrorV2 + val result = application.signInV2(signInParameters()) as NativeAuthErrorV2 assertTrue(result.isBrowserRequired()) } @@ -274,7 +269,7 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { every { unsupportedResult.correlationId } returns correlationId enqueueResult(unsupportedResult, NativeAuthV2SignInStartCommand::class) - val result = application.signInV2(signInParameters()) as SignInErrorV2 + val result = application.signInV2(signInParameters()) as NativeAuthErrorV2 assertEquals(ErrorTypes.INVALID_STATE, result.errorType) assertEquals(correlationId, result.correlationId) @@ -288,7 +283,7 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { unsupportedResult(), NativeAuthV2SignInSubmitCodeCommand::class ) - val submitCodeResult = codeState.submitCode("123456") as SubmitCodeErrorV2 + val submitCodeResult = codeState.submitCode("123456") as NativeAuthErrorV2 assertEquals( submitCodeResult.exception?.stackTraceToString(), ErrorTypes.INVALID_STATE, @@ -302,7 +297,7 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { ) assertEquals( ErrorTypes.INVALID_STATE, - (passwordState.submitPassword("Password!".toCharArray()) as SubmitPasswordErrorV2).errorType + (passwordState.submitPassword("Password!".toCharArray()) as NativeAuthErrorV2).errorType ) val mfaState = mfaRequiredState() @@ -312,7 +307,7 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { ) assertEquals( ErrorTypes.INVALID_STATE, - (mfaState.selectAuthMethod(mfaState.authMethods.single()) as MFARequestChallengeErrorV2).errorType + (mfaState.selectAuthMethod(mfaState.authMethods.single()) as NativeAuthErrorV2).errorType ) val verificationState = mfaVerificationState(mfaState) @@ -322,7 +317,7 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { ) assertEquals( ErrorTypes.INVALID_STATE, - (verificationState.submitChallenge("123456") as MFASubmitChallengeErrorV2).errorType + (verificationState.submitChallenge("123456") as NativeAuthErrorV2).errorType ) enqueueResult( @@ -428,8 +423,8 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { // Android V1 behaviour is preserved: V2 rejects sign-in while an account is signed in, // and does not implement the iOS repeated/switch-account behaviour. val suspendResult = application.signInV2(signInParameters()) - assertTrue(suspendResult is SignInErrorV2) - assertEquals(ErrorTypes.CLIENT_EXCEPTION, (suspendResult as SignInErrorV2).errorType) + assertTrue(suspendResult is NativeAuthErrorV2) + assertEquals(ErrorTypes.CLIENT_EXCEPTION, (suspendResult as NativeAuthErrorV2).errorType) val cause = suspendResult.exception assertTrue(cause is MsalClientException) assertEquals("An account is already signed in.", cause?.message) @@ -444,8 +439,8 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { } ) val delivered = callbackResult.get(10, TimeUnit.SECONDS) - assertTrue(delivered is SignInErrorV2) - assertEquals(ErrorTypes.CLIENT_EXCEPTION, (delivered as SignInErrorV2).errorType) + assertTrue(delivered is NativeAuthErrorV2) + assertEquals(ErrorTypes.CLIENT_EXCEPTION, (delivered as NativeAuthErrorV2).errorType) } finally { unmockkObject(NativeAuthPublicClientApplication.Companion) } @@ -496,7 +491,7 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { NativeAuthV2SignInSubmitCodeCommand::class ) - val result = state.submitCode("00000000") as SubmitCodeErrorV2 + val result = state.submitCode("00000000") as NativeAuthErrorV2 assertTrue(result.isInvalidCode()) assertEquals("invalidOneTimeCode", result.subError) @@ -613,7 +608,7 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { // ----------------------------------------------------------------------------------------- @Test - fun submitPasswordWithWrongPasswordIsInvalidPasswordNotInvalidCredentials() = runTest { + fun submitPasswordWithWrongPasswordIsInvalidCredentialsNotPasswordPolicy() = runTest { val state = passwordRequiredState() enqueueResult( NativeAuthV2CommandResult.IncorrectPassword( @@ -628,9 +623,10 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { val result = state.submitPassword("WrongPassword!".toCharArray()) - assertTrue(result is SubmitPasswordErrorV2) - result as SubmitPasswordErrorV2 - assertTrue(result.isInvalidPassword()) + assertTrue(result is NativeAuthErrorV2) + result as NativeAuthErrorV2 + assertTrue(result.isInvalidInput()) + assertFalse(result.isInvalidPassword()) assertEquals(errorCodes, result.errorCodes) } @@ -638,9 +634,10 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { fun submitPasswordRejectsAnEmptyPasswordWithoutIssuingACommand() = runTest { val state = passwordRequiredState() - val result = state.submitPassword(CharArray(0)) as SubmitPasswordErrorV2 + val result = state.submitPassword(CharArray(0)) as NativeAuthErrorV2 - assertTrue(result.isInvalidPassword()) + assertTrue(result.isInvalidCredentials()) + assertFalse(result.isInvalidPassword()) } @Test @@ -731,13 +728,13 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { INativeAuthCommandResult.Redirect(correlationId, "browser"), NativeAuthV2SubmitPasswordCommand::class ) - assertTrue((state.submitPassword("Password123!".toCharArray()) as SubmitPasswordErrorV2).isBrowserRequired()) + assertTrue((state.submitPassword("Password123!".toCharArray()) as NativeAuthErrorV2).isBrowserRequired()) enqueueResult( INativeAuthCommandResult.APIError("api_error", "API failed", correlationId = correlationId, errorCodes = errorCodes), NativeAuthV2SubmitPasswordCommand::class ) - val apiError = state.submitPassword("Password123!".toCharArray()) as SubmitPasswordErrorV2 + val apiError = state.submitPassword("Password123!".toCharArray()) as NativeAuthErrorV2 assertEquals(errorCodes, apiError.errorCodes) assertFalse(apiError.isInvalidPassword()) } @@ -766,7 +763,8 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { ) val result = future.get(30, TimeUnit.SECONDS) - assertTrue((result as SubmitPasswordErrorV2).isInvalidPassword()) + assertTrue((result as NativeAuthErrorV2).isInvalidCredentials()) + assertFalse(result.isInvalidPassword()) } // ----------------------------------------------------------------------------------------- @@ -871,8 +869,8 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { com.microsoft.identity.nativeauth.AuthMethod("not-offered", "oob", null, "email") ) - assertTrue(result is MFARequestChallengeErrorV2) - assertEquals(ErrorTypes.INVALID_STATE, (result as MFARequestChallengeErrorV2).errorType) + assertTrue(result is NativeAuthErrorV2) + assertEquals(ErrorTypes.INVALID_STATE, (result as NativeAuthErrorV2).errorType) } @Test @@ -883,8 +881,8 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { val result = state.selectAuthMethod(state.authMethods.single()) - assertTrue(result is MFARequestChallengeErrorV2) - val error = result as MFARequestChallengeErrorV2 + assertTrue(result is NativeAuthErrorV2) + val error = result as NativeAuthErrorV2 assertTrue(error.isNotImplemented()) assertFalse(error.isAuthMethodBlocked()) assertFalse(error.isBrowserRequired()) @@ -903,7 +901,7 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { NativeAuthV2SelectMFAMethodCommand::class ) - val result = state.selectAuthMethod(state.authMethods.single()) as MFARequestChallengeErrorV2 + val result = state.selectAuthMethod(state.authMethods.single()) as NativeAuthErrorV2 assertFalse(result.isAuthMethodBlocked()) assertEquals("accessDenied", result.error) @@ -926,7 +924,7 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { ), NativeAuthV2SubmitMFAChallengeCommand::class ) - val wrongCode = verificationState.submitChallenge("000000") as MFASubmitChallengeErrorV2 + val wrongCode = verificationState.submitChallenge("000000") as NativeAuthErrorV2 assertTrue(wrongCode.isInvalidChallenge()) assertEquals("invalidOneTimeCode", wrongCode.subError) @@ -975,14 +973,14 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { ), NativeAuthV2SubmitMFAChallengeCommand::class ) - assertTrue((verificationState.submitChallenge("111111") as MFASubmitChallengeErrorV2).isInvalidChallenge()) + assertTrue((verificationState.submitChallenge("111111") as NativeAuthErrorV2).isInvalidChallenge()) } @Test fun submitChallengeRejectsAnEmptyCodeWithoutIssuingACommand() = runTest { val verificationState = mfaVerificationState(mfaRequiredState()) - val result = verificationState.submitChallenge("") as MFASubmitChallengeErrorV2 + val result = verificationState.submitChallenge("") as NativeAuthErrorV2 assertTrue(result.isInvalidChallenge()) assertEquals(ErrorTypes.INVALID_CODE, result.errorType) @@ -996,7 +994,7 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { INativeAuthCommandResult.Redirect(correlationId, "browser"), NativeAuthV2SubmitMFAChallengeCommand::class ) - assertTrue((verificationState.submitChallenge("123456") as MFASubmitChallengeErrorV2).isBrowserRequired()) + assertTrue((verificationState.submitChallenge("123456") as NativeAuthErrorV2).isBrowserRequired()) enqueueResult( NativeAuthV2CommandResult.NotImplemented(correlationId, "not_implemented", "nope"), @@ -1066,7 +1064,7 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { } ) - assertTrue((future.get(30, TimeUnit.SECONDS) as MFASubmitChallengeErrorV2).isInvalidChallenge()) + assertTrue((future.get(30, TimeUnit.SECONDS) as NativeAuthErrorV2).isInvalidChallenge()) } @Test @@ -1164,8 +1162,8 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { val staleSelection = restoredMfa.selectAuthMethod( com.microsoft.identity.nativeauth.AuthMethod("not-offered", "oob", null, "email") ) - assertTrue(staleSelection is MFARequestChallengeErrorV2) - assertEquals(ErrorTypes.INVALID_STATE, (staleSelection as MFARequestChallengeErrorV2).errorType) + assertTrue(staleSelection is NativeAuthErrorV2) + assertEquals(ErrorTypes.INVALID_STATE, (staleSelection as NativeAuthErrorV2).errorType) } // ----------------------------------------------------------------------------------------- diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignUpTest.kt b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignUpTest.kt index 21152f8c5..5ec4471f1 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignUpTest.kt +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignUpTest.kt @@ -68,10 +68,6 @@ import com.microsoft.identity.nativeauth.parameters.NativeAuthSignUpParameters import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 import com.microsoft.identity.nativeauth.statemachine.errors.ErrorTypes import com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SignUpErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SubmitAttributesErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SubmitCodeErrorV2 -import com.microsoft.identity.nativeauth.statemachine.errors.SubmitPasswordErrorV2 import com.microsoft.identity.nativeauth.statemachine.results.NativeAuthResultV2 import com.microsoft.identity.nativeauth.statemachine.states.AttributesInvalidStateV2 import com.microsoft.identity.nativeauth.statemachine.states.AttributesRequiredStateV2 @@ -155,8 +151,8 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { every { DiagnosticContext.INSTANCE.threadCorrelationId } returns correlationId try { val result = application.signUpV2(signUpParameters(username = " ")) - assertTrue(result is SignUpErrorV2) - val error = result as SignUpErrorV2 + assertTrue(result is NativeAuthErrorV2) + val error = result as NativeAuthErrorV2 assertTrue(error.isInvalidUsername()) assertEquals(NativeAuthFlowScenarioV2.SIGN_UP, error.scenario) assertEquals(correlationId, error.correlationId) @@ -177,11 +173,51 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { NativeAuthV2SignUpStartCommand::class ) - val result = application.signUpV2(signUpParameters()) as SignUpErrorV2 + val result = application.signUpV2(signUpParameters()) as NativeAuthErrorV2 assertTrue(result.isInvalidAttributes()) } + @Test + fun signUpV2RetainsRequiredAttributeDiagnosticsAlongsideRecoverableState() = runTest { + enqueueResult( + NativeAuthV2CommandResult.AttributesInvalid( + correlationId, createContinuationState(), listOf("city"), + "invalidRequest", "attribute validation", listOf(1002027), + subError = "attributeValidationError", + attributeValidationDetails = listOf(mapOf("code" to "attributeRequired", "attributeIds" to listOf("city"))) + ), + NativeAuthV2SignUpStartCommand::class + ) + val result = application.signUpV2(signUpParameters()) as NativeAuthResultV2.AttributesInvalid + val error = result.error!! + assertTrue(error.isMissingRequiredAttributes()) + assertTrue(error.isInvalidAttributes()) + assertFalse(error.isInvalidPassword()) + assertEquals("attributeValidationError", error.subError) + assertEquals(listOf(1002027), error.errorCodes) + assertEquals(listOf("city"), result.invalidAttributes) + } + + @Test + fun signUpV2PreservesUnknownServiceDiagnostics() = runTest { + val exception = IllegalStateException("test diagnostic") + enqueueResult( + INativeAuthCommandResult.APIError( + "futureError", "service diagnostic", correlationId = correlationId, + errorCodes = listOf(1234), exception = exception, subError = "futureSubError" + ), + NativeAuthV2SignUpStartCommand::class + ) + val error = application.signUpV2(signUpParameters()) as NativeAuthErrorV2 + assertTrue(error.isGeneralError()) + assertEquals("futureError", error.error) + assertEquals("service diagnostic", error.errorMessage) + assertEquals("futureSubError", error.subError) + assertEquals(listOf(1234), error.errorCodes) + assertEquals(exception, error.exception) + } + @Test fun signUpV2WithPasswordSurfacesCodeRequired() = runTest { enqueueResult( @@ -274,7 +310,7 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { NativeAuthV2SignUpStartCommand::class ) - val result = application.signUpV2(signUpParameters()) as SignUpErrorV2 + val result = application.signUpV2(signUpParameters()) as NativeAuthErrorV2 assertTrue(result.isUserAlreadyExists()) assertEquals(listOf(1003037), result.errorCodes) @@ -288,7 +324,7 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { NativeAuthV2SignUpStartCommand::class ) - val result = application.signUpV2(signUpParameters()) as SignUpErrorV2 + val result = application.signUpV2(signUpParameters()) as NativeAuthErrorV2 assertTrue(result.isBrowserRequired()) } @@ -314,7 +350,7 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { ) assertEquals( ErrorTypes.INVALID_STATE, - (application.signUpV2(signUpParameters()) as SignUpErrorV2).errorType + (application.signUpV2(signUpParameters()) as NativeAuthErrorV2).errorType ) val codeState = codeRequiredState() @@ -324,7 +360,7 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { ) assertEquals( ErrorTypes.INVALID_STATE, - (codeState.submitCode("123456") as SubmitCodeErrorV2).errorType + (codeState.submitCode("123456") as NativeAuthErrorV2).errorType ) val attributesState = attributesRequiredState() @@ -334,7 +370,7 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { ) assertEquals( ErrorTypes.INVALID_STATE, - (attributesState.submitAttributes(UserAttributes.Builder().build()) as SubmitAttributesErrorV2).errorType + (attributesState.submitAttributes(UserAttributes.Builder().build()) as NativeAuthErrorV2).errorType ) val passwordState = signUpPasswordRequiredState() @@ -344,7 +380,7 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { ) assertEquals( ErrorTypes.INVALID_STATE, - (passwordState.submitPassword("Password!".toCharArray()) as SubmitPasswordErrorV2).errorType + (passwordState.submitPassword("Password!".toCharArray()) as NativeAuthErrorV2).errorType ) val signInState = signInAfterSignUpState() @@ -458,8 +494,8 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { } returns mockk() try { val result = application.signUpV2(signUpParameters()) - assertTrue(result is SignUpErrorV2) - assertEquals(ErrorTypes.CLIENT_EXCEPTION, (result as SignUpErrorV2).errorType) + assertTrue(result is NativeAuthErrorV2) + assertEquals(ErrorTypes.CLIENT_EXCEPTION, (result as NativeAuthErrorV2).errorType) assertTrue(result.exception is MsalClientException) } finally { unmockkObject(NativeAuthPublicClientApplication.Companion) @@ -549,7 +585,7 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { NativeAuthV2SignUpSubmitCodeCommand::class ) - val result = state.submitCode("000000") as SubmitCodeErrorV2 + val result = state.submitCode("000000") as NativeAuthErrorV2 assertTrue(result.isInvalidCode()) } @@ -647,7 +683,7 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { NativeAuthV2SubmitAttributesCommand::class ) - val result = state.submitAttributes(UserAttributes.Builder().build()) as SignUpErrorV2 + val result = state.submitAttributes(UserAttributes.Builder().build()) as NativeAuthErrorV2 assertTrue(result.isUserAlreadyExists()) } @@ -660,7 +696,7 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { NativeAuthV2SubmitAttributesCommand::class ) - val result = state.submitAttributes(UserAttributes.Builder().build()) as SubmitAttributesErrorV2 + val result = state.submitAttributes(UserAttributes.Builder().build()) as NativeAuthErrorV2 assertTrue(result.isBrowserRequired()) } @@ -680,7 +716,7 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { val result = state.submitAttributes( UserAttributes.Builder().build() - ) as SubmitAttributesErrorV2 + ) as NativeAuthErrorV2 assertTrue(result.isInvalidAttributes()) } @@ -733,7 +769,7 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { NativeAuthV2SubmitAttributesCommand::class ) - val result = state.submitPassword("weak".toCharArray()) as SubmitPasswordErrorV2 + val result = state.submitPassword("weak".toCharArray()) as NativeAuthErrorV2 assertTrue(result.isInvalidPassword()) } From beba73e8165fa6a81070a9f2dfb3250979150829 Mon Sep 17 00:00:00 2001 From: Disha Janardhan Date: Mon, 12 Oct 2026 11:49:49 -0700 Subject: [PATCH 02/10] Align Common dependency with standalone unified-errors branch Fixes AB#3784695 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 0bcd671b-73fa-4ee6-bcd6-65d7eea4d753 --- common | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common b/common index 9a89d47b1..1505acff7 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 9a89d47b1d95a96e864cfd2f1ecbc9a53c448812 +Subproject commit 1505acff7e479a67a0cd47cfa94ad079dc158918 From 92ece8fb8d0b834151380c956d0bb6e6467284c5 Mon Sep 17 00:00:00 2001 From: Disha Janardhan Date: Mon, 12 Oct 2026 11:36:02 -0700 Subject: [PATCH 03/10] Port Native Auth V2 entry scopes and claims to Android Fixes AB#3784690 Thread typed entry claims and scopes through public APIs and adapters, snapshot callback inputs, and retain null-versus-empty continuation semantics. Include JS8898 empty/short OTP service-validation parity. Token retains scope and omits claims; V1 is unchanged. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 0bcd671b-73fa-4ee6-bcd6-65d7eea4d753 --- changelog | 1 + common | 2 +- .../internal/CommandParametersAdapter.java | 30 ++++- .../NativeAuthPublicClientApplication.kt | 24 +++- .../NativeAuthResetPasswordParameters.kt | 10 +- .../NativeAuthSignInContinuationParameters.kt | 4 +- .../parameters/NativeAuthSignUpParameters.kt | 7 ++ .../states/CodeRequiredStateV2.kt | 8 -- .../states/MFAVerificationRequiredStateV2.kt | 8 -- .../v2/NativeAuthV2InterfaceKotlinTest.kt | 117 +++++++++++++++++- .../nativeauth/v2/NativeAuthV2SignInTest.kt | 39 ++++-- .../nativeauth/v2/NativeAuthV2SignUpTest.kt | 14 +++ 12 files changed, 225 insertions(+), 39 deletions(-) diff --git a/changelog b/changelog index df4044d2a..e6e339ccc 100644 --- a/changelog +++ b/changelog @@ -3,6 +3,7 @@ MSAL Wiki : https://github.com/AzureAD/microsoft-authentication-library-for-andr vNext ---------- - [MAJOR] Native Auth V2 errors now use NativeAuthErrorV2. Replace operation-specific V2 error type checks with its classifiers; rejected sign-in passwords use isInvalidCredentials, not isInvalidPassword. V1 is unchanged (AB#3784695). +- [MINOR] Native Auth V2 sign-in, sign-up and password-reset entries accept scopes and claims. Scope is also sent at token exchange; claims are sent only at entry. Null continuation scopes retain entry scopes, while explicit empty scopes request default OIDC scopes (AB#3784690). Version 8.5.0 ---------- diff --git a/common b/common index 1505acff7..52e5eb6b3 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 1505acff7e479a67a0cd47cfa94ad079dc158918 +Subproject commit 52e5eb6b3ca1cd5690ce97f7b3f7461b2fd4ea57 diff --git a/msal/src/main/java/com/microsoft/identity/client/internal/CommandParametersAdapter.java b/msal/src/main/java/com/microsoft/identity/client/internal/CommandParametersAdapter.java index 8a96decec..303ea7aed 100644 --- a/msal/src/main/java/com/microsoft/identity/client/internal/CommandParametersAdapter.java +++ b/msal/src/main/java/com/microsoft/identity/client/internal/CommandParametersAdapter.java @@ -1077,9 +1077,7 @@ public static ResetPasswordSubmitNewPasswordCommandParameters createResetPasswor /** * Creates command parameter for [NativeAuthV2ResetPasswordStartCommand] of Native Auth. * - * No scopes are supplied at the start of a reset-password flow. Matching MSAL iOS/macOS, the - * scopes for the resulting access token are supplied at the sign-in-after-reset-password step - * instead, since no token is issued until then. + * Scopes and claims are supplied at V2 authorize-challenge entry. * * @param configuration PCA configuration * @param tokenCache token cache for storing results @@ -1090,6 +1088,16 @@ public static ResetPasswordV2StartCommandParameters createResetPasswordV2StartCo @NonNull final NativeAuthPublicClientApplicationConfiguration configuration, @NonNull final OAuth2TokenCache tokenCache, @NonNull final String username) throws ClientException { + return createResetPasswordV2StartCommandParameters(configuration, tokenCache, username, null, null); + } + + /** Creates V2 reset entry parameters with optional scopes and claims. */ + public static ResetPasswordV2StartCommandParameters createResetPasswordV2StartCommandParameters( + @NonNull final NativeAuthPublicClientApplicationConfiguration configuration, + @NonNull final OAuth2TokenCache tokenCache, + @NonNull final String username, + @Nullable final List scopes, + @Nullable final ClaimsRequest claimsRequest) throws ClientException { final NativeAuthCIAMAuthority authority = ((NativeAuthCIAMAuthority) configuration.getDefaultAuthority()); @@ -1100,6 +1108,8 @@ public static ResetPasswordV2StartCommandParameters createResetPasswordV2StartCo final ResetPasswordV2StartCommandParameters commandParameters = ResetPasswordV2StartCommandParameters.builder() + .scopes(scopes == null ? null : new ArrayList<>(scopes)) + .claimsRequestJson(ClaimsRequest.getJsonStringFromClaimsRequest(claimsRequest)) .platformComponents(AndroidPlatformComponentsFactory.createFromContext(configuration.getAppContext())) .applicationName(configuration.getAppContext().getPackageName()) .applicationVersion(getPackageVersion(configuration.getAppContext())) @@ -1141,6 +1151,18 @@ public static SignUpV2StartCommandParameters createSignUpV2StartCommandParameter @NonNull final String username, @Nullable final char[] password, @Nullable final Map attributes) throws ClientException { + return createSignUpV2StartCommandParameters(configuration, tokenCache, username, password, attributes, null, null); + } + + /** Creates V2 sign-up entry parameters with optional scopes and claims. */ + public static SignUpV2StartCommandParameters createSignUpV2StartCommandParameters( + @NonNull final NativeAuthPublicClientApplicationConfiguration configuration, + @NonNull final OAuth2TokenCache tokenCache, + @NonNull final String username, + @Nullable final char[] password, + @Nullable final Map attributes, + @Nullable final List scopes, + @Nullable final ClaimsRequest claimsRequest) throws ClientException { final NativeAuthCIAMAuthority authority = ((NativeAuthCIAMAuthority) configuration.getDefaultAuthority()); @@ -1151,6 +1173,8 @@ public static SignUpV2StartCommandParameters createSignUpV2StartCommandParameter final SignUpV2StartCommandParameters commandParameters = SignUpV2StartCommandParameters.builder() + .scopes(scopes == null ? null : new ArrayList<>(scopes)) + .claimsRequestJson(ClaimsRequest.getJsonStringFromClaimsRequest(claimsRequest)) .platformComponents(AndroidPlatformComponentsFactory.createFromContext(configuration.getAppContext())) .applicationName(configuration.getAppContext().getPackageName()) .applicationVersion(getPackageVersion(configuration.getAppContext())) diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt index 09d35189a..98007e472 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt @@ -810,7 +810,9 @@ class NativeAuthPublicClientApplication( val parametersSnapshot = NativeAuthSignInParameters(parameters.username).also { it.password = parameters.password?.copyOf() it.scopes = parameters.scopes?.toList() - it.claimsRequest = parameters.claimsRequest + it.claimsRequest = parameters.claimsRequest?.let { claims -> + ClaimsRequest.getClaimsRequestFromJsonString(ClaimsRequest.getJsonStringFromClaimsRequest(claims)) + } } pcaScope.launchOwningPasswordSnapshot(parametersSnapshot.password) { try { @@ -854,7 +856,9 @@ class NativeAuthPublicClientApplication( nativeAuthConfig.oAuth2TokenCache, parameters.username, passwordCopy, - parameters.attributes?.toMap() + parameters.attributes?.toMap(), + parameters.scopes?.toList(), + parameters.claimsRequest ) correlationId = cmdParams.correlationId ?: correlationId @@ -1016,6 +1020,10 @@ class NativeAuthPublicClientApplication( ) val parametersSnapshot = NativeAuthSignUpParameters(parameters.username).also { it.password = parameters.password?.copyOf() + it.scopes = parameters.scopes?.toList() + it.claimsRequest = parameters.claimsRequest?.let { claims -> + ClaimsRequest.getClaimsRequestFromJsonString(ClaimsRequest.getJsonStringFromClaimsRequest(claims)) + } it.attributes = parameters.attributes?.let { attributes -> UserAttributes(attributes.toMap()) } @@ -1056,7 +1064,9 @@ class NativeAuthPublicClientApplication( val cmdParams = CommandParametersAdapter.createResetPasswordV2StartCommandParameters( nativeAuthConfig, nativeAuthConfig.oAuth2TokenCache, - parameters.username + parameters.username, + parameters.scopes?.toList(), + parameters.claimsRequest ) correlationId = cmdParams.correlationId ?: correlationId @@ -1201,9 +1211,15 @@ class NativeAuthPublicClientApplication( correlationId = null, methodName = "${TAG}.resetPasswordV2(parameters: NativeAuthResetPasswordParameters, callback: NativeAuthV2Callback)" ) + val parametersSnapshot = NativeAuthResetPasswordParameters(parameters.username).also { + it.scopes = parameters.scopes?.toList() + it.claimsRequest = parameters.claimsRequest?.let { claims -> + ClaimsRequest.getClaimsRequestFromJsonString(ClaimsRequest.getJsonStringFromClaimsRequest(claims)) + } + } pcaScope.launch { try { - callback.onResult(resetPasswordV2(parameters)) + callback.onResult(resetPasswordV2(parametersSnapshot)) } catch (e: MsalException) { Logger.error(TAG, "Exception thrown in resetPasswordV2", e) callback.onError(e) diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/parameters/NativeAuthResetPasswordParameters.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/parameters/NativeAuthResetPasswordParameters.kt index f01ad7d31..04b9ae333 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/parameters/NativeAuthResetPasswordParameters.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/parameters/NativeAuthResetPasswordParameters.kt @@ -23,6 +23,8 @@ package com.microsoft.identity.nativeauth.parameters +import com.microsoft.identity.client.claims.ClaimsRequest + /** * Encapsulates the parameters passed to the resetPassword methods of NativeAuthPublicClientApplication. * @@ -30,4 +32,10 @@ package com.microsoft.identity.nativeauth.parameters */ class NativeAuthResetPasswordParameters( val username: String -) \ No newline at end of file +) { + /** V2-only requested scopes. Null or empty uses default OIDC scopes; ignored by V1. */ + var scopes: List? = null + + /** V2-only claims sent at authorize-challenge entry; ignored by V1. */ + var claimsRequest: ClaimsRequest? = null +} \ No newline at end of file diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/parameters/NativeAuthSignInContinuationParameters.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/parameters/NativeAuthSignInContinuationParameters.kt index 0077d85b5..85b33f079 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/parameters/NativeAuthSignInContinuationParameters.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/parameters/NativeAuthSignInContinuationParameters.kt @@ -33,11 +33,13 @@ class NativeAuthSignInContinuationParameters { /** * Permissions you want included in the access token received. * Not all scopes are guaranteed to be included in the access token returned. + * For V2, null retains entry scopes; an explicit empty list requests only default OIDC scopes. */ var scopes: List? = null /** - * The claims parameter that needs to be sent to the service. + * Claims sent to the service in V1. In V2, claims are retained locally for token/cache + * semantics, but the token exchange omits claims; supply service claims at flow entry. */ var claimsRequest: ClaimsRequest? = null } \ No newline at end of file diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/parameters/NativeAuthSignUpParameters.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/parameters/NativeAuthSignUpParameters.kt index ac07dbf13..21481980d 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/parameters/NativeAuthSignUpParameters.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/parameters/NativeAuthSignUpParameters.kt @@ -24,6 +24,7 @@ package com.microsoft.identity.nativeauth.parameters import com.microsoft.identity.nativeauth.UserAttributes +import com.microsoft.identity.client.claims.ClaimsRequest /** * Encapsulates the parameters passed to the signUp methods of NativeAuthPublicClientApplication @@ -47,4 +48,10 @@ class NativeAuthSignUpParameters( * Supply them through the dedicated [username] and [password] properties instead. */ var attributes: UserAttributes? = null + + /** V2-only requested scopes. Null or empty uses default OIDC scopes; ignored by V1. */ + var scopes: List? = null + + /** V2-only claims sent at authorize-challenge entry; ignored by V1. */ + var claimsRequest: ClaimsRequest? = null } \ No newline at end of file diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/CodeRequiredStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/CodeRequiredStateV2.kt index 53f3e43a6..b9534fb5c 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/CodeRequiredStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/CodeRequiredStateV2.kt @@ -116,14 +116,6 @@ class CodeRequiredStateV2 internal constructor( methodName = "${TAG}.submitCode(code: String)" ) val state = continuationState ?: return invalidState() - if (code.isEmpty()) { - return NativeAuthErrorV2( - errorType = ErrorTypes.INVALID_CODE, - errorMessage = "Code cannot be empty.", - correlationId = correlationId, - scenario = scenario - ) - } if (scenario == NativeAuthFlowScenarioV2.UNKNOWN) { return invalidState() } diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFAVerificationRequiredStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFAVerificationRequiredStateV2.kt index beefaa4cb..cd2ea9062 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFAVerificationRequiredStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFAVerificationRequiredStateV2.kt @@ -127,14 +127,6 @@ class MFAVerificationRequiredStateV2 internal constructor( methodName = "${TAG}.submitChallenge(challenge: String)" ) val state = continuationState ?: return invalidState() - if (challenge.isEmpty()) { - return NativeAuthErrorV2( - errorType = ErrorTypes.INVALID_CODE, - errorMessage = "Challenge cannot be empty.", - correlationId = correlationId, - scenario = scenario - ) - } return withContext(Dispatchers.IO) { try { val parameters = CommandParametersAdapter.createNativeAuthV2SubmitMFAChallengeCommandParameters( diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2InterfaceKotlinTest.kt b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2InterfaceKotlinTest.kt index 66b220fa5..4aa414234 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2InterfaceKotlinTest.kt +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2InterfaceKotlinTest.kt @@ -127,6 +127,106 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { private lateinit var application: INativeAuthPublicClientApplication private val username = "user@email.com" + @Test + fun allV2EntriesPropagateNullableScopesAndTypedClaimsThroughAdapters() = runTest { + val claims = com.microsoft.identity.client.claims.ClaimsRequest().also { + it.requestClaimInIdToken("auth_time", null) + } + val expectedClaims = com.microsoft.identity.client.claims.ClaimsRequest.getJsonStringFromClaimsRequest(claims) + listOf?>(null, emptyList(), listOf("User.Read")).forEach { scopes -> + var captured: com.microsoft.identity.common.java.nativeauth.commands.parameters.BaseSignInTokenCommandParameters? = null + fun capture(command: BaseCommand<*>) { + captured = command.parameters as com.microsoft.identity.common.java.nativeauth.commands.parameters.BaseSignInTokenCommandParameters + } + fun assertCaptured() { + assertTrue(captured != null) + assertEquals(scopes, captured?.scopes) + assertEquals(expectedClaims, captured?.claimsRequestJson) + captured = null + } + val result = INativeAuthCommandResult.APIError("test_stop", "test stop", correlationId = correlationId) + enqueueResult(result, com.microsoft.identity.common.nativeauth.internal.commands.NativeAuthV2SignInStartCommand::class, ::capture) + application.signInV2(NativeAuthSignInParameters(username).also { + it.scopes = scopes + it.claimsRequest = claims + }) + assertCaptured() + enqueueResult(result, NativeAuthV2SignUpStartCommand::class, ::capture) + application.signUpV2(NativeAuthSignUpParameters(username).also { + it.scopes = scopes + it.claimsRequest = claims + }) + assertCaptured() + enqueueResult(result, NativeAuthV2ResetPasswordStartCommand::class, ::capture) + application.resetPasswordV2(NativeAuthResetPasswordParameters(username).also { + it.scopes = scopes + it.claimsRequest = claims + }) + assertCaptured() + } + } + + @Test + fun sharedEntryScopesAndClaimsDoNotChangeV1CommandInputsOrErrors() = runTest { + val claims = com.microsoft.identity.client.claims.ClaimsRequest().also { it.requestClaimInIdToken("auth_time", null) } + var signUpInputs: com.microsoft.identity.common.java.nativeauth.commands.parameters.SignUpStartCommandParameters? = null + var resetInputs: com.microsoft.identity.common.java.nativeauth.commands.parameters.ResetPasswordStartCommandParameters? = null + val result = INativeAuthCommandResult.APIError("test_stop", "test stop", correlationId = correlationId) + enqueueResult(result, com.microsoft.identity.common.nativeauth.internal.commands.SignUpStartCommand::class) { + signUpInputs = it.parameters as com.microsoft.identity.common.java.nativeauth.commands.parameters.SignUpStartCommandParameters + } + enqueueResult(result, com.microsoft.identity.common.nativeauth.internal.commands.ResetPasswordStartCommand::class) { + resetInputs = it.parameters as com.microsoft.identity.common.java.nativeauth.commands.parameters.ResetPasswordStartCommandParameters + } + val signUp = application.signUp(NativeAuthSignUpParameters(username).also { + it.scopes = listOf("V2.Only") + it.claimsRequest = claims + }) + val reset = application.resetPassword(NativeAuthResetPasswordParameters(username).also { + it.scopes = listOf("V2.Only") + it.claimsRequest = claims + }) + assertTrue(signUp is com.microsoft.identity.nativeauth.statemachine.errors.SignUpError) + assertTrue(reset is com.microsoft.identity.nativeauth.statemachine.errors.ResetPasswordError) + assertEquals(username, signUpInputs?.username) + assertEquals(null, signUpInputs?.userAttributes) + assertEquals(null, signUpInputs?.password) + assertEquals(username, resetInputs?.username) + assertEquals("test_stop", (reset as com.microsoft.identity.nativeauth.statemachine.errors.ResetPasswordError).error) + } + + @Test + fun resetPasswordCallbackSnapshotsScopesAndMutableClaimsBeforeLaunch() = runTest { + val scopes = mutableListOf("User.Read") + val claims = com.microsoft.identity.client.claims.ClaimsRequest().also { + it.requestClaimInIdToken("auth_time", null) + } + val expectedClaims = com.microsoft.identity.client.claims.ClaimsRequest.getJsonStringFromClaimsRequest(claims) + var captured: com.microsoft.identity.common.java.nativeauth.commands.parameters.BaseSignInTokenCommandParameters? = null + enqueueResult( + INativeAuthCommandResult.APIError("test_stop", "test stop", correlationId = correlationId), + NativeAuthV2ResetPasswordStartCommand::class + ) { command -> + captured = command.parameters as com.microsoft.identity.common.java.nativeauth.commands.parameters.BaseSignInTokenCommandParameters + } + val parameters = NativeAuthResetPasswordParameters(username).also { + it.scopes = scopes + it.claimsRequest = claims + } + val future = ResultFuture() + application.resetPasswordV2(parameters, object : NativeAuthPublicClientApplication.NativeAuthV2Callback { + override fun onResult(result: NativeAuthResultV2) = future.setResult(result) + override fun onError(exception: BaseException) = future.setException(exception) + }) + scopes.clear() + claims.requestClaimInIdToken("changed", null) + parameters.claimsRequest = null + assertTrue(future.get(30, TimeUnit.SECONDS) is NativeAuthErrorV2) + assertTrue(captured != null) + assertEquals(listOf("User.Read"), captured?.scopes) + assertEquals(expectedClaims, captured?.claimsRequestJson) + } + override fun getConfigFilePath() = "src/test/res/raw/native_auth_native_only_test_config.json" @Before @@ -573,12 +673,21 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { } @Test - fun submitCodeRejectsEmptyInputWithRetryableInvalidCodeError() = runTest { + fun submitCodeSendsEmptyAndShortInputToServiceAndRemainsRetryable() = runTest { val state = codeRequiredState() - val error = state.submitCode("") as NativeAuthErrorV2 - - assertTrue(error.isInvalidCode()) + listOf("", "1").forEach { code -> + var submittedCode: String? = null + enqueueResult( + NativeAuthV2CommandResult.IncorrectCode(correlationId, "invalidGrant", "invalid", "invalidOneTimeCode"), + NativeAuthV2ResetPasswordSubmitCodeCommand::class + ) { command -> + submittedCode = (command.parameters as com.microsoft.identity.common.java.nativeauth.commands.parameters.NativeAuthV2SubmitCodeCommandParameters).code + } + val error = state.submitCode(code) as NativeAuthErrorV2 + assertTrue(error.isInvalidCode()) + assertEquals(code, submittedCode) + } } @Test diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt index 5c4efb04d..a53a233b5 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt @@ -374,17 +374,25 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { @Test fun signInV2CallbackSnapshotsThePasswordSynchronouslyBeforeLaunch() = runTest { var capturedPassword: CharArray? = null + var capturedScopes: List? = null + var capturedClaims: String? = null enqueueResult( NativeAuthV2CommandResult.PasswordRequired(correlationId, createContinuationState()), NativeAuthV2SignInStartCommand::class ) { command -> - capturedPassword = (command.parameters as SignInV2StartCommandParameters).password?.copyOf() + val commandParameters = command.parameters as SignInV2StartCommandParameters + capturedPassword = commandParameters.password?.copyOf() + capturedScopes = commandParameters.scopes + capturedClaims = commandParameters.claimsRequestJson } val callerPassword = "Password123!".toCharArray() val future = ResultFuture() + val scopes = mutableListOf("User.Read") + val claims = com.microsoft.identity.client.claims.ClaimsRequest().also { it.requestClaimInIdToken("auth_time", null) } + val expectedClaims = com.microsoft.identity.client.claims.ClaimsRequest.getJsonStringFromClaimsRequest(claims) application.signInV2( - signInParameters(password = callerPassword), + signInParameters(password = callerPassword, scopes = scopes).also { it.claimsRequest = claims }, object : NativeAuthPublicClientApplication.NativeAuthV2Callback { override fun onResult(result: NativeAuthResultV2) = future.setResult(result) override fun onError(exception: BaseException) = future.setException(exception) @@ -395,10 +403,14 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { // the coroutine body, as opposed to synchronously before launch), this mutation could race // with -- and change -- what actually gets submitted to Common. callerPassword.fill('X') + scopes.clear() + claims.requestClaimInIdToken("changed", null) future.get(10, TimeUnit.SECONDS) assertEquals("Password123!", String(capturedPassword!!)) + assertEquals(listOf("User.Read"), capturedScopes) + assertEquals(expectedClaims, capturedClaims) } @Test @@ -625,7 +637,7 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { assertTrue(result is NativeAuthErrorV2) result as NativeAuthErrorV2 - assertTrue(result.isInvalidInput()) + assertTrue(result.isInvalidCredentials()) assertFalse(result.isInvalidPassword()) assertEquals(errorCodes, result.errorCodes) } @@ -636,7 +648,7 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { val result = state.submitPassword(CharArray(0)) as NativeAuthErrorV2 - assertTrue(result.isInvalidCredentials()) + assertTrue(result.isInvalidInput()) assertFalse(result.isInvalidPassword()) } @@ -977,13 +989,22 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { } @Test - fun submitChallengeRejectsAnEmptyCodeWithoutIssuingACommand() = runTest { + fun submitChallengeSendsEmptyAndShortCodesToServiceAndRemainsRetryable() = runTest { val verificationState = mfaVerificationState(mfaRequiredState()) - val result = verificationState.submitChallenge("") as NativeAuthErrorV2 - - assertTrue(result.isInvalidChallenge()) - assertEquals(ErrorTypes.INVALID_CODE, result.errorType) + listOf("", "1").forEach { code -> + var submittedCode: String? = null + enqueueResult( + NativeAuthV2CommandResult.IncorrectCode(correlationId, "invalidGrant", "invalid", "invalidOneTimeCode"), + NativeAuthV2SubmitMFAChallengeCommand::class + ) { command -> + submittedCode = (command.parameters as com.microsoft.identity.common.java.nativeauth.commands.parameters.NativeAuthV2SubmitMFAChallengeCommandParameters).code + } + val result = verificationState.submitChallenge(code) as NativeAuthErrorV2 + assertTrue(result.isInvalidChallenge()) + assertTrue(result.isInvalidCode()) + assertEquals(code, submittedCode) + } } @Test diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignUpTest.kt b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignUpTest.kt index 5ec4471f1..16eeb5c23 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignUpTest.kt +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignUpTest.kt @@ -449,6 +449,8 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { var capturedUsername: String? = null var capturedPassword: CharArray? = null var capturedAttributes: Map? = null + var capturedScopes: List? = null + var capturedClaims: String? = null enqueueResult( NativeAuthV2CommandResult.PasswordRequired(correlationId, createContinuationState()), NativeAuthV2SignUpStartCommand::class @@ -457,6 +459,8 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { capturedUsername = commandParameters.username capturedPassword = commandParameters.password?.copyOf() capturedAttributes = commandParameters.attributes?.toMap() + capturedScopes = commandParameters.scopes + capturedClaims = commandParameters.claimsRequestJson } val callerPassword = "Password123!".toCharArray() @@ -466,6 +470,11 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { password = callerPassword, attributes = attributesBuilder.build() ) + val scopes = mutableListOf("User.Read") + val claims = com.microsoft.identity.client.claims.ClaimsRequest().also { it.requestClaimInIdToken("auth_time", null) } + val expectedClaims = com.microsoft.identity.client.claims.ClaimsRequest.getJsonStringFromClaimsRequest(claims) + parameters.scopes = scopes + parameters.claimsRequest = claims val future = ResultFuture() application.signUpV2( parameters, @@ -478,12 +487,17 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { attributesBuilder.city("Seattle") parameters.password = null parameters.attributes = null + scopes.clear() + claims.requestClaimInIdToken("changed", null) + parameters.claimsRequest = null future.get(10, TimeUnit.SECONDS) assertEquals(username, capturedUsername) assertEquals("Password123!", String(capturedPassword!!)) assertEquals(mapOf("city" to "Redmond"), capturedAttributes) + assertEquals(listOf("User.Read"), capturedScopes) + assertEquals(expectedClaims, capturedClaims) } @Test From 7ac5b2d593ed270d67c910ad2af87aeaf01a2166 Mon Sep 17 00:00:00 2001 From: Disha Janardhan Date: Mon, 12 Oct 2026 12:07:07 -0700 Subject: [PATCH 04/10] Correct unified-error password assertions and pair repaired Common Fixes AB#3784695 Rejected credentials are invalid credentials; empty password input is invalid input. Use independently validated Common diagnostic-forwarding repair. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 0bcd671b-73fa-4ee6-bcd6-65d7eea4d753 --- common | 2 +- .../identity/nativeauth/v2/NativeAuthV2SignInTest.kt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/common b/common index 1505acff7..3265533f2 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 1505acff7e479a67a0cd47cfa94ad079dc158918 +Subproject commit 3265533f27d8c4054fc5b020ec0228bb872b8344 diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt index 5c4efb04d..747fe71b2 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt @@ -625,7 +625,7 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { assertTrue(result is NativeAuthErrorV2) result as NativeAuthErrorV2 - assertTrue(result.isInvalidInput()) + assertTrue(result.isInvalidCredentials()) assertFalse(result.isInvalidPassword()) assertEquals(errorCodes, result.errorCodes) } @@ -636,7 +636,7 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { val result = state.submitPassword(CharArray(0)) as NativeAuthErrorV2 - assertTrue(result.isInvalidCredentials()) + assertTrue(result.isInvalidInput()) assertFalse(result.isInvalidPassword()) } From 12a5e97e8bf08a05a2927e1780d6c8db3579974f Mon Sep 17 00:00:00 2001 From: Disha Janardhan Date: Mon, 12 Oct 2026 12:07:07 -0700 Subject: [PATCH 05/10] Correct unified-error password assertions and pair repaired Common Fixes AB#3784695 Rejected credentials are invalid credentials; empty password input is invalid input. Use independently validated Common diagnostic-forwarding repair. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 0bcd671b-73fa-4ee6-bcd6-65d7eea4d753 (cherry picked from commit 7ac5b2d593ed270d67c910ad2af87aeaf01a2166) --- common | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common b/common index 52e5eb6b3..5325fa9f9 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 52e5eb6b3ca1cd5690ce97f7b3f7461b2fd4ea57 +Subproject commit 5325fa9f9579e6f58e348c16f3dd97e0c542708a From f92d6c06ed448f29d97b93e24ef98c78b1b4dfb3 Mon Sep 17 00:00:00 2001 From: Disha Janardhan Date: Mon, 12 Oct 2026 12:16:54 -0700 Subject: [PATCH 06/10] Pair validated canonical OIDC scope Common update Fixes AB#3784690 Use the Common scope-canonicalization fix validated with MSAL/Common focused suites; retain carried unified-error repair and existing scopes/claims wire policy. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 0bcd671b-73fa-4ee6-bcd6-65d7eea4d753 --- common | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common b/common index 5325fa9f9..41f9c0f74 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 5325fa9f9579e6f58e348c16f3dd97e0c542708a +Subproject commit 41f9c0f74a636b1ae2f62abcb3ee6a94906f1d86 From 45206542702b92de90c0c78a4295b142c3a72605 Mon Sep 17 00:00:00 2001 From: Disha Janardhan Date: Thu, 15 Oct 2026 12:38:38 -0700 Subject: [PATCH 07/10] Expose Native Auth V2 service diagnostics and address review feedback Preserve service trace IDs, timestamps, and numeric codes across V2 public error mappings. Keep password-policy classification independent of credential classification, correct sign-up documentation, and pair the updated Common commit. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 55791345-e680-4927-a364-f6de503184be --- common | 2 +- .../INativeAuthPublicClientApplication.kt | 9 ++++-- .../NativeAuthPublicClientApplication.kt | 29 +++++++++++++++++ .../statemachine/errors/NativeAuthErrorV2.kt | 27 +++++++++------- .../states/AttributesRequiredStateV2.kt | 7 ++++ .../states/CodeRequiredStateV2.kt | 12 +++++++ .../statemachine/states/MFARequiredStateV2.kt | 5 +++ .../states/MFAVerificationRequiredStateV2.kt | 12 +++++++ .../states/NewPasswordRequiredStateV2.kt | 7 ++++ .../states/PasswordRequiredStateV2.kt | 16 ++++++++++ .../ResetPasswordMethodRequiredStateV2.kt | 5 +++ .../states/SignInAfterResetPasswordStateV2.kt | 5 +++ .../states/SignInAfterSignUpStateV2.kt | 5 +++ .../nativeauth/v2/NativeAuthV2ErrorsTest.kt | 32 +++++++++++++++++++ .../nativeauth/v2/NativeAuthV2SignInTest.kt | 12 +++++-- .../nativeauth/v2/NativeAuthV2SignUpTest.kt | 24 ++++++++++++-- 16 files changed, 190 insertions(+), 19 deletions(-) diff --git a/common b/common index 3265533f2..0e45650da 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 3265533f27d8c4054fc5b020ec0228bb872b8344 +Subproject commit 0e45650da664d3bcc93b6876ec042382bb6613f6 diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/INativeAuthPublicClientApplication.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/INativeAuthPublicClientApplication.kt index 2ec4bff1d..3b3bf4ca2 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/INativeAuthPublicClientApplication.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/INativeAuthPublicClientApplication.kt @@ -223,7 +223,9 @@ interface INativeAuthPublicClientApplication : IPublicClientApplication { * * @param parameters parameters used for the signUp operation. * @return [NativeAuthResultV2] see the detailed possible return states under the object. - * @throws MsalClientException if an account is already signed in. + * Returns a [com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2] with + * [com.microsoft.identity.nativeauth.statemachine.errors.ErrorTypes.CLIENT_EXCEPTION] if an + * account is already signed in, rather than throwing [MsalClientException]. */ suspend fun signUpV2(parameters: NativeAuthSignUpParameters): NativeAuthResultV2 @@ -234,7 +236,10 @@ interface INativeAuthPublicClientApplication : IPublicClientApplication { * * @param parameters parameters used for the signUp operation. * @param callback [com.microsoft.identity.nativeauth.NativeAuthPublicClientApplication.NativeAuthV2Callback] to receive the result. - * @throws MsalClientException if an account is already signed in. + * If an account is already signed in, the callback receives a + * [com.microsoft.identity.nativeauth.statemachine.errors.NativeAuthErrorV2] with + * [com.microsoft.identity.nativeauth.statemachine.errors.ErrorTypes.CLIENT_EXCEPTION] through + * [com.microsoft.identity.nativeauth.statemachine.states.Callback.onResult]. */ fun signUpV2(parameters: NativeAuthSignUpParameters, callback: NativeAuthPublicClientApplication.NativeAuthV2Callback) diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt index 09d35189a..2bf917de4 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt @@ -715,6 +715,8 @@ class NativeAuthPublicClientApplication( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = NativeAuthFlowScenarioV2.SIGN_IN, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, subError = result.subError ) @@ -726,6 +728,8 @@ class NativeAuthPublicClientApplication( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = NativeAuthFlowScenarioV2.SIGN_IN, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes ) } @@ -736,6 +740,8 @@ class NativeAuthPublicClientApplication( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = NativeAuthFlowScenarioV2.SIGN_IN, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes ) } @@ -751,6 +757,9 @@ class NativeAuthPublicClientApplication( is INativeAuthCommandResult.Redirect -> { NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, + traceId = result.traceId, + timestamp = result.timestamp, + errorCodes = result.errorCodes, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, @@ -763,6 +772,8 @@ class NativeAuthPublicClientApplication( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = NativeAuthFlowScenarioV2.SIGN_IN, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, exception = result.exception, subError = result.subError, @@ -922,6 +933,8 @@ class NativeAuthPublicClientApplication( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = NativeAuthFlowScenarioV2.SIGN_UP, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, subError = result.subError, attributeValidationDetails = result.attributeValidationDetails @@ -943,6 +956,8 @@ class NativeAuthPublicClientApplication( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = NativeAuthFlowScenarioV2.SIGN_UP, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes ) } @@ -958,6 +973,9 @@ class NativeAuthPublicClientApplication( is INativeAuthCommandResult.Redirect -> { NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, + traceId = result.traceId, + timestamp = result.timestamp, + errorCodes = result.errorCodes, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, @@ -970,6 +988,8 @@ class NativeAuthPublicClientApplication( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = NativeAuthFlowScenarioV2.SIGN_UP, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, exception = result.exception, subError = result.subError, @@ -1122,6 +1142,8 @@ class NativeAuthPublicClientApplication( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = NativeAuthFlowScenarioV2.RESET_PASSWORD, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes ) } @@ -1132,6 +1154,8 @@ class NativeAuthPublicClientApplication( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = NativeAuthFlowScenarioV2.RESET_PASSWORD, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes ) } @@ -1147,6 +1171,9 @@ class NativeAuthPublicClientApplication( is INativeAuthCommandResult.Redirect -> { NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, + traceId = result.traceId, + timestamp = result.timestamp, + errorCodes = result.errorCodes, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, @@ -1159,6 +1186,8 @@ class NativeAuthPublicClientApplication( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = NativeAuthFlowScenarioV2.RESET_PASSWORD, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, exception = result.exception, subError = result.subError, diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/NativeAuthErrorV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/NativeAuthErrorV2.kt index d80526020..33c698e65 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/NativeAuthErrorV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/NativeAuthErrorV2.kt @@ -43,6 +43,8 @@ import com.microsoft.identity.nativeauth.statemachine.results.NativeAuthResultV2 * @param exception an internal unexpected exception that happened. * @param subError the unmodified service suberror. * @param attributeValidationDetails service validation details, runtime-checked by classifiers. + * @param traceId the unmodified service trace identifier, when available. + * @param timestamp the unmodified service timestamp, when available. */ class NativeAuthErrorV2 @JvmOverloads constructor( override val errorType: String? = null, @@ -53,7 +55,9 @@ class NativeAuthErrorV2 @JvmOverloads constructor( override val errorCodes: List? = null, override var exception: Exception? = null, val subError: String? = null, - val attributeValidationDetails: Any? = null + val attributeValidationDetails: Any? = null, + val traceId: String? = null, + val timestamp: String? = null ) : NativeAuthResultV2, BrowserRequiredError, Error( @@ -87,16 +91,15 @@ class NativeAuthErrorV2 @JvmOverloads constructor( fun isInvalidCode(): Boolean = errorType == ErrorTypes.INVALID_CODE || (error == "invalidGrant" && subError == "invalidOneTimeCode") - /** Whether a new password violates policy; does not classify sign-in credential failures. */ - fun isInvalidPassword(): Boolean = !isInvalidCredentials() && - (errorType == ErrorTypes.INVALID_PASSWORD || - (error == "invalidRequest" && subError in PASSWORD_POLICY_SUBERRORS) || - validationDetails().any { detail -> - detail["code"] == "passwordPolicyViolation" && - (detail["attributeIds"] as? List<*>)?.any { - it is String && it.equals("password", ignoreCase = true) - } == true - }) + /** Whether password-specific errors or validation details indicate a new-password policy violation. */ + fun isInvalidPassword(): Boolean = errorType == ErrorTypes.INVALID_PASSWORD || + (error == "invalidRequest" && subError in PASSWORD_POLICY_SUBERRORS) || + validationDetails().any { detail -> + detail["code"] == "passwordPolicyViolation" && + (detail["attributeIds"] as? List<*>)?.any { + it is String && it.equals("password", ignoreCase = true) + } == true + } /** Whether the identifier already belongs to an account. */ fun isUserAlreadyExists(): Boolean = errorType == SignUpErrorTypes.USER_ALREADY_EXISTS || @@ -151,6 +154,8 @@ class NativeAuthErrorV2 @JvmOverloads constructor( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, subError = result.subError, attributeValidationDetails = result.attributeValidationDetails diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/AttributesRequiredStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/AttributesRequiredStateV2.kt index d3bb445a5..b50228af4 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/AttributesRequiredStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/AttributesRequiredStateV2.kt @@ -217,6 +217,8 @@ internal suspend fun submitAttributesInternal( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, subError = result.subError, attributeValidationDetails = result.attributeValidationDetails @@ -243,6 +245,9 @@ internal suspend fun submitAttributesInternal( is INativeAuthCommandResult.Redirect -> { NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, + traceId = result.traceId, + timestamp = result.timestamp, + errorCodes = result.errorCodes, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, @@ -255,6 +260,8 @@ internal suspend fun submitAttributesInternal( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, exception = result.exception, subError = result.subError, diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/CodeRequiredStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/CodeRequiredStateV2.kt index 53f3e43a6..f6e436ade 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/CodeRequiredStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/CodeRequiredStateV2.kt @@ -301,6 +301,8 @@ class CodeRequiredStateV2 internal constructor( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, subError = result.subError ) @@ -317,6 +319,9 @@ class CodeRequiredStateV2 internal constructor( is INativeAuthCommandResult.Redirect -> { NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, + traceId = result.traceId, + timestamp = result.timestamp, + errorCodes = result.errorCodes, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, @@ -329,6 +334,8 @@ class CodeRequiredStateV2 internal constructor( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, exception = result.exception, subError = result.subError, @@ -417,6 +424,9 @@ class CodeRequiredStateV2 internal constructor( is INativeAuthCommandResult.Redirect -> { NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, + traceId = result.traceId, + timestamp = result.timestamp, + errorCodes = result.errorCodes, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, @@ -429,6 +439,8 @@ class CodeRequiredStateV2 internal constructor( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, exception = result.exception, subError = result.subError, diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFARequiredStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFARequiredStateV2.kt index 8a3c5643a..981b282a4 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFARequiredStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFARequiredStateV2.kt @@ -217,6 +217,9 @@ class MFARequiredStateV2 internal constructor( is INativeAuthCommandResult.Redirect -> { NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, + traceId = result.traceId, + timestamp = result.timestamp, + errorCodes = result.errorCodes, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, @@ -229,6 +232,8 @@ class MFARequiredStateV2 internal constructor( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, exception = result.exception, subError = result.subError, diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFAVerificationRequiredStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFAVerificationRequiredStateV2.kt index beefaa4cb..17c82553d 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFAVerificationRequiredStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/MFAVerificationRequiredStateV2.kt @@ -162,6 +162,8 @@ class MFAVerificationRequiredStateV2 internal constructor( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, subError = result.subError ) @@ -178,6 +180,9 @@ class MFAVerificationRequiredStateV2 internal constructor( is INativeAuthCommandResult.Redirect -> { NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, + traceId = result.traceId, + timestamp = result.timestamp, + errorCodes = result.errorCodes, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, @@ -190,6 +195,8 @@ class MFAVerificationRequiredStateV2 internal constructor( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, exception = result.exception, subError = result.subError, @@ -295,6 +302,9 @@ class MFAVerificationRequiredStateV2 internal constructor( is INativeAuthCommandResult.Redirect -> { NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, + traceId = result.traceId, + timestamp = result.timestamp, + errorCodes = result.errorCodes, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, @@ -307,6 +317,8 @@ class MFAVerificationRequiredStateV2 internal constructor( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, exception = result.exception, subError = result.subError, diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/NewPasswordRequiredStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/NewPasswordRequiredStateV2.kt index 2627a810d..474fe3d01 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/NewPasswordRequiredStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/NewPasswordRequiredStateV2.kt @@ -169,6 +169,8 @@ class NewPasswordRequiredStateV2 internal constructor( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, subError = result.subError ) @@ -194,6 +196,9 @@ class NewPasswordRequiredStateV2 internal constructor( is INativeAuthCommandResult.Redirect -> { NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, + traceId = result.traceId, + timestamp = result.timestamp, + errorCodes = result.errorCodes, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, @@ -206,6 +211,8 @@ class NewPasswordRequiredStateV2 internal constructor( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, exception = result.exception, subError = result.subError, diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/PasswordRequiredStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/PasswordRequiredStateV2.kt index af74858ce..bf748c497 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/PasswordRequiredStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/PasswordRequiredStateV2.kt @@ -189,6 +189,8 @@ class PasswordRequiredStateV2 internal constructor( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, subError = result.subError ) @@ -205,6 +207,9 @@ class PasswordRequiredStateV2 internal constructor( is INativeAuthCommandResult.Redirect -> { NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, + traceId = result.traceId, + timestamp = result.timestamp, + errorCodes = result.errorCodes, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, @@ -217,6 +222,8 @@ class PasswordRequiredStateV2 internal constructor( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, exception = result.exception, subError = result.subError, @@ -314,6 +321,8 @@ class PasswordRequiredStateV2 internal constructor( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, subError = result.subError, attributeValidationDetails = result.attributeValidationDetails @@ -334,6 +343,8 @@ class PasswordRequiredStateV2 internal constructor( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, subError = result.subError, attributeValidationDetails = result.attributeValidationDetails @@ -360,6 +371,9 @@ class PasswordRequiredStateV2 internal constructor( is INativeAuthCommandResult.Redirect -> { NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, + traceId = result.traceId, + timestamp = result.timestamp, + errorCodes = result.errorCodes, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, @@ -372,6 +386,8 @@ class PasswordRequiredStateV2 internal constructor( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, exception = result.exception, subError = result.subError, diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/ResetPasswordMethodRequiredStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/ResetPasswordMethodRequiredStateV2.kt index 775134076..3c5ed76cf 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/ResetPasswordMethodRequiredStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/ResetPasswordMethodRequiredStateV2.kt @@ -193,6 +193,9 @@ class ResetPasswordMethodRequiredStateV2 internal constructor( ) is INativeAuthCommandResult.Redirect -> NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, + traceId = result.traceId, + timestamp = result.timestamp, + errorCodes = result.errorCodes, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, @@ -203,6 +206,8 @@ class ResetPasswordMethodRequiredStateV2 internal constructor( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, exception = result.exception, subError = result.subError, diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/SignInAfterResetPasswordStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/SignInAfterResetPasswordStateV2.kt index 669b15c07..cfaa6deff 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/SignInAfterResetPasswordStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/SignInAfterResetPasswordStateV2.kt @@ -182,6 +182,9 @@ class SignInAfterResetPasswordStateV2 internal constructor( is INativeAuthCommandResult.Redirect -> { NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, + traceId = result.traceId, + timestamp = result.timestamp, + errorCodes = result.errorCodes, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, @@ -194,6 +197,8 @@ class SignInAfterResetPasswordStateV2 internal constructor( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, exception = result.exception, subError = result.subError, diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/SignInAfterSignUpStateV2.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/SignInAfterSignUpStateV2.kt index 60a828df0..8ce2f8a69 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/SignInAfterSignUpStateV2.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/states/SignInAfterSignUpStateV2.kt @@ -182,6 +182,9 @@ class SignInAfterSignUpStateV2 internal constructor( is INativeAuthCommandResult.Redirect -> { NativeAuthErrorV2( errorType = ErrorTypes.BROWSER_REQUIRED, + traceId = result.traceId, + timestamp = result.timestamp, + errorCodes = result.errorCodes, error = result.error, errorMessage = result.errorDescription, correlationId = result.correlationId, @@ -194,6 +197,8 @@ class SignInAfterSignUpStateV2 internal constructor( errorMessage = result.errorDescription, correlationId = result.correlationId, scenario = scenario, + traceId = result.traceId, + timestamp = result.timestamp, errorCodes = result.errorCodes, exception = result.exception, subError = result.subError, diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2ErrorsTest.kt b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2ErrorsTest.kt index d23661dd4..5c012ca29 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2ErrorsTest.kt +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2ErrorsTest.kt @@ -28,6 +28,7 @@ import com.microsoft.identity.nativeauth.statemachine.NativeAuthFlowScenarioV2 import com.microsoft.identity.nativeauth.statemachine.errors.ErrorTypes import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull import org.junit.Assert.assertSame import org.junit.Assert.assertTrue import org.junit.Test @@ -43,6 +44,16 @@ class NativeAuthV2ErrorsTest { private val correlationId = "correlation-id" private val errorMessage = "error message" + @Test + fun unifiedError_localErrorsHaveNoServiceDiagnostics() { + val error = NativeAuthErrorV2( + errorMessage = errorMessage, correlationId = correlationId, + scenario = NativeAuthFlowScenarioV2.SIGN_IN + ) + assertNull(error.traceId) + assertNull(error.timestamp) + } + @Test fun unifiedError_credentialsAreNotPasswordPolicyViolations() { val error = NativeAuthErrorV2( @@ -55,6 +66,27 @@ class NativeAuthV2ErrorsTest { assertFalse(error.isGeneralError()) } + @Test + fun unifiedError_passwordPolicyDetailsAreIndependentOfCredentialClassification() { + val error = NativeAuthErrorV2( + error = "invalidGrant", + subError = "invalidUserNameOrPassword", + errorMessage = errorMessage, + correlationId = correlationId, + scenario = NativeAuthFlowScenarioV2.SIGN_IN, + attributeValidationDetails = listOf( + mapOf( + "code" to "passwordPolicyViolation", + "attributeIds" to listOf("password") + ) + ) + ) + + assertTrue(error.isInvalidCredentials()) + assertTrue(error.isInvalidPassword()) + assertFalse(error.isGeneralError()) + } + @Test fun unifiedError_validatesAttributeDetailShapes() { fun error(details: Any?) = NativeAuthErrorV2( diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt index 747fe71b2..2bf193630 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt @@ -214,7 +214,9 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { "invalid_grant", "AADSTS50126: invalid username or password.", "invalidUserNameOrPassword", - errorCodes + errorCodes, + traceId = "password-trace", + timestamp = "password-time" ), NativeAuthV2SignInStartCommand::class ) @@ -227,6 +229,8 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { assertFalse(result.isUserNotFound()) assertEquals(errorCodes, result.errorCodes) assertEquals("invalidUserNameOrPassword", result.subError) + assertEquals("password-trace", result.traceId) + assertEquals("password-time", result.timestamp) assertEquals(NativeAuthFlowScenarioV2.SIGN_IN, result.scenario) // A recoverable-looking error must not hand back a state to continue from. assertNull((result as NativeAuthResultV2).let { (it as? NativeAuthResultV2.PasswordRequired)?.nextState }) @@ -616,7 +620,9 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { "invalid_grant", "AADSTS50126: invalid username or password.", "invalidUserNameOrPassword", - errorCodes + errorCodes, + traceId = "password-trace", + timestamp = "password-time" ), NativeAuthV2SubmitPasswordCommand::class ) @@ -628,6 +634,8 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { assertTrue(result.isInvalidCredentials()) assertFalse(result.isInvalidPassword()) assertEquals(errorCodes, result.errorCodes) + assertEquals("password-trace", result.traceId) + assertEquals("password-time", result.timestamp) } @Test diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignUpTest.kt b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignUpTest.kt index 5ec4471f1..d375cf107 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignUpTest.kt +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignUpTest.kt @@ -185,6 +185,8 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { correlationId, createContinuationState(), listOf("city"), "invalidRequest", "attribute validation", listOf(1002027), subError = "attributeValidationError", + traceId = "attribute-trace", + timestamp = "attribute-time", attributeValidationDetails = listOf(mapOf("code" to "attributeRequired", "attributeIds" to listOf("city"))) ), NativeAuthV2SignUpStartCommand::class @@ -196,6 +198,8 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { assertFalse(error.isInvalidPassword()) assertEquals("attributeValidationError", error.subError) assertEquals(listOf(1002027), error.errorCodes) + assertEquals("attribute-trace", error.traceId) + assertEquals("attribute-time", error.timestamp) assertEquals(listOf("city"), result.invalidAttributes) } @@ -205,7 +209,8 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { enqueueResult( INativeAuthCommandResult.APIError( "futureError", "service diagnostic", correlationId = correlationId, - errorCodes = listOf(1234), exception = exception, subError = "futureSubError" + errorCodes = listOf(1234), exception = exception, subError = "futureSubError", + traceId = "service-trace", timestamp = "service-time" ), NativeAuthV2SignUpStartCommand::class ) @@ -216,6 +221,8 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { assertEquals("futureSubError", error.subError) assertEquals(listOf(1234), error.errorCodes) assertEquals(exception, error.exception) + assertEquals("service-trace", error.traceId) + assertEquals("service-time", error.timestamp) } @Test @@ -320,13 +327,19 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { @Test fun signUpV2MapsBrowserRequiredToSignUpError() = runTest { enqueueResult( - INativeAuthCommandResult.Redirect(correlationId, "browser"), + INativeAuthCommandResult.Redirect( + correlationId, "browser", errorCodes = listOf(123), + traceId = "browser-trace", timestamp = "browser-time" + ), NativeAuthV2SignUpStartCommand::class ) val result = application.signUpV2(signUpParameters()) as NativeAuthErrorV2 assertTrue(result.isBrowserRequired()) + assertEquals(listOf(123), result.errorCodes) + assertEquals("browser-trace", result.traceId) + assertEquals("browser-time", result.timestamp) } @Test @@ -580,7 +593,9 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { "invalid_grant", "AADSTS50181: invalid code.", "invalidOneTimeCode", - listOf(50181) + listOf(50181), + traceId = "code-trace", + timestamp = "code-time" ), NativeAuthV2SignUpSubmitCodeCommand::class ) @@ -588,6 +603,9 @@ class NativeAuthV2SignUpTest : PublicClientApplicationAbstractTest() { val result = state.submitCode("000000") as NativeAuthErrorV2 assertTrue(result.isInvalidCode()) + assertEquals(listOf(50181), result.errorCodes) + assertEquals("code-trace", result.traceId) + assertEquals("code-time", result.timestamp) } @Test From 9b8b35465b3ce35b9422bb83881fcacb67a2c85a Mon Sep 17 00:00:00 2001 From: Disha Janardhan Date: Thu, 15 Oct 2026 23:45:12 -0700 Subject: [PATCH 08/10] Increase Native Auth V2 sign-in state coverage Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 495388ac-9812-45ec-9120-b110dcbe5903 --- .../nativeauth/v2/NativeAuthV2SignInTest.kt | 170 ++++++++++++++++++ 1 file changed, 170 insertions(+) diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt index 2bf193630..a0119d683 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2SignInTest.kt @@ -96,6 +96,7 @@ import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNotSame import org.junit.Assert.assertNull +import org.junit.Assert.assertSame import org.junit.Assert.assertTrue import org.junit.Assert.fail import org.junit.Before @@ -1174,6 +1175,175 @@ class NativeAuthV2SignInTest : PublicClientApplicationAbstractTest() { assertEquals(ErrorTypes.INVALID_STATE, (staleSelection as NativeAuthErrorV2).errorType) } + @Test + fun submitPasswordMapsNotImplemented() = runTest { + val state = passwordRequiredState() + enqueueResult( + NativeAuthV2CommandResult.NotImplemented(correlationId, "unsupported", "Password is unavailable."), + NativeAuthV2SubmitPasswordCommand::class + ) + + val result = state.submitPassword("Password123!".toCharArray()) as NativeAuthErrorV2 + + assertTrue(result.isNotImplemented()) + assertEquals("unsupported", result.error) + assertEquals("Password is unavailable.", result.errorMessage) + assertEquals(correlationId, result.correlationId) + assertEquals(NativeAuthFlowScenarioV2.SIGN_IN, result.scenario) + } + + @Test + fun selectAuthMethodMapsNotImplemented() = runTest { + val state = mfaRequiredState() + enqueueResult( + NativeAuthV2CommandResult.NotImplemented(correlationId, "unsupported", "MFA is unavailable."), + NativeAuthV2SelectMFAMethodCommand::class + ) + + val result = state.selectAuthMethod(state.authMethods.single()) as NativeAuthErrorV2 + + assertTrue(result.isNotImplemented()) + assertEquals("unsupported", result.error) + assertEquals("MFA is unavailable.", result.errorMessage) + assertEquals(correlationId, result.correlationId) + assertEquals(NativeAuthFlowScenarioV2.SIGN_IN, result.scenario) + } + + @Test + fun selectAuthMethodPreservesBrowserRedirectDiagnostics() = runTest { + val state = mfaRequiredState() + enqueueResult( + INativeAuthCommandResult.Redirect( + correlationId = correlationId, + redirectReason = "browser", + traceId = "mfa-trace", + timestamp = "mfa-time", + errorCodes = errorCodes + ), + NativeAuthV2SelectMFAMethodCommand::class + ) + + val result = state.selectAuthMethod(state.authMethods.single()) as NativeAuthErrorV2 + + assertTrue(result.isBrowserRequired()) + assertEquals("mfa-trace", result.traceId) + assertEquals("mfa-time", result.timestamp) + assertEquals(errorCodes, result.errorCodes) + assertEquals(correlationId, result.correlationId) + } + + @Test + fun signInStatesPreserveAllApiErrorDiagnostics() = runTest { + val passwordState = passwordRequiredState() + val mfaState = mfaRequiredState() + val verificationState = mfaVerificationState(mfaState) + val failure = IllegalStateException("service failure") + val details = listOf(mapOf("name" to "email", "reason" to "invalid")) + val apiError = INativeAuthCommandResult.APIError( + error = "service_error", + errorDescription = "Verification failed.", + correlationId = correlationId, + traceId = "service-trace", + timestamp = "service-time", + errorCodes = errorCodes, + subError = "verification_failed", + attributeValidationDetails = details, + exception = failure + ) + enqueueResult(apiError, NativeAuthV2SubmitPasswordCommand::class) + enqueueResult(apiError, NativeAuthV2SelectMFAMethodCommand::class) + enqueueResult(apiError, NativeAuthV2SubmitMFAChallengeCommand::class) + + val results = listOf( + passwordState.submitPassword("Password123!".toCharArray()), + mfaState.selectAuthMethod(mfaState.authMethods.single()), + verificationState.submitChallenge("123456") + ) + + results.forEach { result -> + assertTrue(result is NativeAuthErrorV2) + result as NativeAuthErrorV2 + assertEquals("service_error", result.error) + assertEquals("Verification failed.", result.errorMessage) + assertEquals(correlationId, result.correlationId) + assertEquals(NativeAuthFlowScenarioV2.SIGN_IN, result.scenario) + assertEquals("service-trace", result.traceId) + assertEquals("service-time", result.timestamp) + assertEquals(errorCodes, result.errorCodes) + assertEquals("verification_failed", result.subError) + assertEquals(details, result.attributeValidationDetails) + assertSame(failure, result.exception) + } + } + + @Test + fun submitPasswordMapsClientExceptionAndClearsOwnedBuffer() = runTest { + val state = passwordRequiredState() + val password = "Password123!".toCharArray() + val failure = IllegalStateException("dispatcher failed") + lateinit var ownedPassword: CharArray + every { + CommandDispatcher.submitSilentReturningFuture(ofType()) + } answers { + ownedPassword = (firstArg().parameters as NativeAuthV2SubmitPasswordCommandParameters).password + throw failure + } + + val result = state.submitPassword(password) as NativeAuthErrorV2 + + assertEquals(ErrorTypes.CLIENT_EXCEPTION, result.errorType) + assertSame(failure, result.exception) + assertEquals(correlationId, result.correlationId) + assertEquals(NativeAuthFlowScenarioV2.SIGN_IN, result.scenario) + assertPasswordCleared(ownedPassword) + assertEquals("Password123!", String(password)) + } + + @Test + fun selectAuthMethodMapsClientException() = runTest { + val state = mfaRequiredState() + val failure = IllegalStateException("dispatcher failed") + every { + CommandDispatcher.submitSilentReturningFuture(ofType()) + } throws failure + + val result = state.selectAuthMethod(state.authMethods.single()) as NativeAuthErrorV2 + + assertEquals(ErrorTypes.CLIENT_EXCEPTION, result.errorType) + assertSame(failure, result.exception) + assertEquals(correlationId, result.correlationId) + assertEquals(NativeAuthFlowScenarioV2.SIGN_IN, result.scenario) + } + + @Test + fun submitChallengeMapsClientException() = runTest { + val state = mfaVerificationState(mfaRequiredState()) + val failure = IllegalStateException("dispatcher failed") + every { + CommandDispatcher.submitSilentReturningFuture(ofType()) + } throws failure + + val result = state.submitChallenge("123456") as NativeAuthErrorV2 + + assertEquals(ErrorTypes.CLIENT_EXCEPTION, result.errorType) + assertSame(failure, result.exception) + assertEquals(correlationId, result.correlationId) + assertEquals(NativeAuthFlowScenarioV2.SIGN_IN, result.scenario) + } + + @Test + fun selectAuthMethodPropagatesCancellation() = runTest { + val state = mfaRequiredState() + enqueueCancellation(NativeAuthV2SelectMFAMethodCommand::class) + + try { + state.selectAuthMethod(state.authMethods.single()) + fail("Expected CancellationException") + } catch (_: CancellationException) { + // Cancellation must not be converted to an authentication error. + } + } + // ----------------------------------------------------------------------------------------- // Helpers // ----------------------------------------------------------------------------------------- From dcddf0624a9561e946a7486c5253a00a755ed5f0 Mon Sep 17 00:00:00 2001 From: Disha Janardhan Date: Mon, 19 Oct 2026 09:31:18 -0700 Subject: [PATCH 09/10] Update Common submodule to latest dev Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: eafe5634-2142-4235-a7dc-d26bc1baa0d5 --- common | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common b/common index 0e45650da..cc25e6e77 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 0e45650da664d3bcc93b6876ec042382bb6613f6 +Subproject commit cc25e6e77ab023f5b16332329a200112f45f7c56 From a29b38de8087486c49d27ac30e3a3ac1ef10189d Mon Sep 17 00:00:00 2001 From: Disha Janardhan Date: Tue, 20 Oct 2026 11:37:09 -0700 Subject: [PATCH 10/10] Handle Native Auth V2 callback snapshot failures Return CLIENT_EXCEPTION results on the PCA scope when synchronous input snapshots fail. Clear owned password copies immediately on snapshot failure or cancellation without modifying caller buffers, and preserve callback exception routing. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 15870531-a0a1-4433-91b7-7cde867b4083 --- .../NativeAuthPublicClientApplication.kt | 76 ++++++-- .../v2/NativeAuthV2InterfaceKotlinTest.kt | 164 ++++++++++++++++++ 2 files changed, 225 insertions(+), 15 deletions(-) diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt index e52d350d9..f5c0cd274 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt @@ -818,16 +818,32 @@ class NativeAuthPublicClientApplication( correlationId = null, methodName = "${TAG}.signInV2(parameters: NativeAuthSignInParameters, callback: NativeAuthV2Callback)" ) - val parametersSnapshot = NativeAuthSignInParameters(parameters.username).also { - it.password = parameters.password?.copyOf() - it.scopes = parameters.scopes?.toList() - it.claimsRequest = parameters.claimsRequest?.let { claims -> + val parametersSnapshot = NativeAuthSignInParameters(parameters.username) + val snapshotError = try { + parametersSnapshot.password = parameters.password?.copyOf() + parametersSnapshot.scopes = parameters.scopes?.toList() + parametersSnapshot.claimsRequest = parameters.claimsRequest?.let { claims -> ClaimsRequest.getClaimsRequestFromJsonString(ClaimsRequest.getJsonStringFromClaimsRequest(claims)) } + null + } catch (e: CancellationException) { + StringUtil.overwriteWithNull(parametersSnapshot.password) + throw e + } catch (e: Exception) { + StringUtil.overwriteWithNull(parametersSnapshot.password) + val correlationId = DiagnosticContext.INSTANCE.threadCorrelationId ?: "UNSET" + Logger.error(TAG, correlationId, "Exception thrown in signInV2", e) + NativeAuthErrorV2( + errorType = ErrorTypes.CLIENT_EXCEPTION, + errorMessage = "MSAL client exception occurred in signInV2.", + correlationId = correlationId, + scenario = NativeAuthFlowScenarioV2.SIGN_IN, + exception = e + ) } pcaScope.launchOwningPasswordSnapshot(parametersSnapshot.password) { try { - callback.onResult(signInV2(parametersSnapshot)) + callback.onResult(snapshotError ?: signInV2(parametersSnapshot)) } catch (e: MsalException) { Logger.error(TAG, "Exception thrown in signInV2", e) callback.onError(e) @@ -1038,19 +1054,35 @@ class NativeAuthPublicClientApplication( correlationId = null, methodName = "${TAG}.signUpV2(parameters: NativeAuthSignUpParameters, callback: NativeAuthV2Callback)" ) - val parametersSnapshot = NativeAuthSignUpParameters(parameters.username).also { - it.password = parameters.password?.copyOf() - it.scopes = parameters.scopes?.toList() - it.claimsRequest = parameters.claimsRequest?.let { claims -> + val parametersSnapshot = NativeAuthSignUpParameters(parameters.username) + val snapshotError = try { + parametersSnapshot.password = parameters.password?.copyOf() + parametersSnapshot.scopes = parameters.scopes?.toList() + parametersSnapshot.claimsRequest = parameters.claimsRequest?.let { claims -> ClaimsRequest.getClaimsRequestFromJsonString(ClaimsRequest.getJsonStringFromClaimsRequest(claims)) } - it.attributes = parameters.attributes?.let { attributes -> + parametersSnapshot.attributes = parameters.attributes?.let { attributes -> UserAttributes(attributes.toMap()) } + null + } catch (e: CancellationException) { + StringUtil.overwriteWithNull(parametersSnapshot.password) + throw e + } catch (e: Exception) { + StringUtil.overwriteWithNull(parametersSnapshot.password) + val correlationId = DiagnosticContext.INSTANCE.threadCorrelationId ?: "UNSET" + Logger.error(TAG, correlationId, "Exception thrown in signUpV2", e) + NativeAuthErrorV2( + errorType = ErrorTypes.CLIENT_EXCEPTION, + errorMessage = "MSAL client exception occurred in signUpV2.", + correlationId = correlationId, + scenario = NativeAuthFlowScenarioV2.SIGN_UP, + exception = e + ) } pcaScope.launchOwningPasswordSnapshot(parametersSnapshot.password) { try { - callback.onResult(signUpV2(parametersSnapshot)) + callback.onResult(snapshotError ?: signUpV2(parametersSnapshot)) } catch (e: MsalException) { Logger.error(TAG, "Exception thrown in signUpV2", e) callback.onError(e) @@ -1240,15 +1272,29 @@ class NativeAuthPublicClientApplication( correlationId = null, methodName = "${TAG}.resetPasswordV2(parameters: NativeAuthResetPasswordParameters, callback: NativeAuthV2Callback)" ) - val parametersSnapshot = NativeAuthResetPasswordParameters(parameters.username).also { - it.scopes = parameters.scopes?.toList() - it.claimsRequest = parameters.claimsRequest?.let { claims -> + val parametersSnapshot = NativeAuthResetPasswordParameters(parameters.username) + val snapshotError = try { + parametersSnapshot.scopes = parameters.scopes?.toList() + parametersSnapshot.claimsRequest = parameters.claimsRequest?.let { claims -> ClaimsRequest.getClaimsRequestFromJsonString(ClaimsRequest.getJsonStringFromClaimsRequest(claims)) } + null + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + val correlationId = DiagnosticContext.INSTANCE.threadCorrelationId ?: "UNSET" + Logger.error(TAG, correlationId, "Exception thrown in resetPasswordV2", e) + NativeAuthErrorV2( + errorType = ErrorTypes.CLIENT_EXCEPTION, + errorMessage = "MSAL client exception occurred in resetPasswordV2.", + correlationId = correlationId, + scenario = NativeAuthFlowScenarioV2.RESET_PASSWORD, + exception = e + ) } pcaScope.launch { try { - callback.onResult(resetPasswordV2(parametersSnapshot)) + callback.onResult(snapshotError ?: resetPasswordV2(parametersSnapshot)) } catch (e: MsalException) { Logger.error(TAG, "Exception thrown in resetPasswordV2", e) callback.onError(e) diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2InterfaceKotlinTest.kt b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2InterfaceKotlinTest.kt index 4aa414234..09062326f 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2InterfaceKotlinTest.kt +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/v2/NativeAuthV2InterfaceKotlinTest.kt @@ -34,6 +34,8 @@ import com.microsoft.identity.client.exception.MsalClientException import com.microsoft.identity.client.exception.MsalException import com.microsoft.identity.client.IAccount import com.microsoft.identity.client.IAuthenticationResult +import com.microsoft.identity.client.claims.ClaimsRequest +import com.microsoft.identity.client.claims.RequestedClaimAdditionalInformation import com.microsoft.identity.common.java.commands.BaseCommand import com.microsoft.identity.common.java.commands.ICommandResult import com.microsoft.identity.common.java.controllers.CommandDispatcher @@ -55,6 +57,7 @@ import com.microsoft.identity.common.java.nativeauth.providers.responses.v2.Nati import com.microsoft.identity.common.java.result.FinalizableResultFuture import com.microsoft.identity.common.java.result.ILocalAuthenticationResult import com.microsoft.identity.common.java.util.ResultFuture +import com.microsoft.identity.common.java.util.StringUtil import com.microsoft.identity.common.nativeauth.internal.commands.NativeAuthV2ResendCodeCommand import com.microsoft.identity.common.nativeauth.internal.commands.NativeAuthV2ResetPasswordSubmitCodeCommand import com.microsoft.identity.common.nativeauth.internal.commands.NativeAuthV2ResetPasswordStartCommand @@ -89,6 +92,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel import kotlinx.coroutines.launch @@ -114,6 +118,7 @@ import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config import java.io.File import java.util.concurrent.CountDownLatch +import java.util.concurrent.CopyOnWriteArrayList import java.util.concurrent.ExecutionException import java.util.concurrent.TimeUnit import kotlin.reflect.KClass @@ -227,6 +232,165 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() { assertEquals(expectedClaims, captured?.claimsRequestJson) } + @Test + fun callbackSnapshotFailuresReturnClientExceptionsOnPcaScopeAndClearOwnedPasswords() { + val claims = ClaimsRequest().also { + it.requestClaimInIdToken("auth_time", RequestedClaimAdditionalInformation().also { info -> + info.setValues(null) + }) + } + val clearedPasswords = CopyOnWriteArrayList() + mockkStatic(StringUtil::class) + mockkObject(DiagnosticContext.INSTANCE) + try { + every { DiagnosticContext.INSTANCE.threadCorrelationId } returns correlationId + every { StringUtil.overwriteWithNull(any()) } answers { + firstArg()?.let { clearedPasswords.add(it) } + callOriginal() + } + listOf( + NativeAuthFlowScenarioV2.SIGN_IN, + NativeAuthFlowScenarioV2.SIGN_UP, + NativeAuthFlowScenarioV2.RESET_PASSWORD + ).forEach { scenario -> + clearedPasswords.clear() + val password = "Password123!".toCharArray() + val callerThread = Thread.currentThread() + val future = ResultFuture() + var callbackThread: Thread? = null + var scopeChildActive = false + val callback = object : NativeAuthPublicClientApplication.NativeAuthV2Callback { + override fun onResult(result: NativeAuthResultV2) { + callbackThread = Thread.currentThread() + scopeChildActive = NativeAuthPublicClientApplication.pcaScope + .coroutineContext[Job]!!.children.any { it.isActive } + future.setResult(result) + } + override fun onError(exception: BaseException) = future.setException(exception) + } + invokeCallbackEntry(scenario, password, claims, null, callback) + + assertEquals("Password123!", String(password)) + if (scenario == NativeAuthFlowScenarioV2.RESET_PASSWORD) { + assertTrue(clearedPasswords.isEmpty()) + } else { + assertTrue(clearedPasswords.isNotEmpty()) + } + clearedPasswords.forEach { + assertTrue(it !== password) + assertTrue(it.all { char -> char == '\u0000' }) + } + val error = future.get(30, TimeUnit.SECONDS) as NativeAuthErrorV2 + assertTrue(callbackThread !== callerThread) + assertTrue(scopeChildActive) + assertEquals(ErrorTypes.CLIENT_EXCEPTION, error.errorType) + assertEquals(scenario, error.scenario) + assertEquals(correlationId, error.correlationId) + assertTrue(error.exception is NullPointerException) + runBlocking { + NativeAuthPublicClientApplication.pcaScope.coroutineContext[Job]!! + .children.toList().forEach { it.join() } + } + } + } finally { + unmockkObject(DiagnosticContext.INSTANCE) + unmockkStatic(StringUtil::class) + } + } + + @Test + fun callbackSnapshotCancellationIsRethrownAndClearsOnlyOwnedPasswords() { + val cancellation = CancellationException("snapshot cancelled") + val scopes = object : AbstractList() { + override val size: Int get() = throw cancellation + override fun get(index: Int): String = throw cancellation + } + val clearedPasswords = mutableListOf() + mockkStatic(StringUtil::class) + try { + every { StringUtil.overwriteWithNull(any()) } answers { + firstArg()?.let { clearedPasswords.add(it) } + callOriginal() + } + listOf( + NativeAuthFlowScenarioV2.SIGN_IN, + NativeAuthFlowScenarioV2.SIGN_UP, + NativeAuthFlowScenarioV2.RESET_PASSWORD + ).forEach { scenario -> + clearedPasswords.clear() + val password = "Password123!".toCharArray() + val callback = object : NativeAuthPublicClientApplication.NativeAuthV2Callback { + override fun onResult(result: NativeAuthResultV2) { fail("Cancellation must not deliver a result") } + override fun onError(exception: BaseException) { fail("Cancellation must not deliver an error") } + } + try { + invokeCallbackEntry(scenario, password, null, scopes, callback) + fail("Expected CancellationException") + } catch (e: CancellationException) { + assertSame(cancellation, e) + } + assertEquals("Password123!", String(password)) + assertEquals(if (scenario == NativeAuthFlowScenarioV2.RESET_PASSWORD) 0 else 1, clearedPasswords.size) + clearedPasswords.forEach { + assertTrue(it !== password) + assertTrue(it.all { char -> char == '\u0000' }) + } + } + } finally { + unmockkStatic(StringUtil::class) + } + } + + @Test + fun callbackSnapshotFailuresPreserveCallbackExceptionRouting() { + val claims = ClaimsRequest().also { + it.requestClaimInIdToken("auth_time", RequestedClaimAdditionalInformation().also { info -> + info.setValues(null) + }) + } + listOf( + NativeAuthFlowScenarioV2.SIGN_IN, + NativeAuthFlowScenarioV2.SIGN_UP, + NativeAuthFlowScenarioV2.RESET_PASSWORD + ).forEach { scenario -> + assertRoutesToOnError { future -> + invokeCallbackEntry(scenario, "Password123!".toCharArray(), claims, null, throwingCallback(future)) + } + } + } + + private fun invokeCallbackEntry( + scenario: NativeAuthFlowScenarioV2, + password: CharArray, + claims: ClaimsRequest?, + scopes: List?, + callback: NativeAuthPublicClientApplication.NativeAuthV2Callback + ) { + when (scenario) { + NativeAuthFlowScenarioV2.SIGN_IN -> application.signInV2( + NativeAuthSignInParameters(username).also { + it.password = password + it.claimsRequest = claims + it.scopes = scopes + }, callback + ) + NativeAuthFlowScenarioV2.SIGN_UP -> application.signUpV2( + NativeAuthSignUpParameters(username).also { + it.password = password + it.claimsRequest = claims + it.scopes = scopes + }, callback + ) + NativeAuthFlowScenarioV2.RESET_PASSWORD -> application.resetPasswordV2( + NativeAuthResetPasswordParameters(username).also { + it.claimsRequest = claims + it.scopes = scopes + }, callback + ) + else -> fail("Unexpected entry scenario: $scenario") + } + } + override fun getConfigFilePath() = "src/test/res/raw/native_auth_native_only_test_config.json" @Before