Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ import io.mockk.slot
import io.mockk.verify
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertSame
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Before
import org.junit.Test

Expand Down Expand Up @@ -817,6 +819,138 @@ class NativeAuthV2SignInFlowControllerTest {
}
}

@Test
fun testSignInSubmissionsPreserveUnknownErrorDiagnostics() {
val state = mockContinuationState()
val apiError = NativeAuthV2InteractionApiResult.UnknownError(
correlationId = correlationId,
error = "service_error",
errorDescription = "The service could not verify the request.",
errorCodes = listOf(50126),
subError = "verification_failed",
attributeValidationDetails = listOf(mapOf("name" to "email", "reason" to "invalid")),
traceId = "service-trace",
timestamp = "service-time"
)
every { mockStrategy.performPasswordVerify(state, any()) } returns apiError
every { mockStrategy.performVerify(state, any()) } returns apiError
every { mockStrategy.performMethodChallenge(state, "email-1") } returns apiError

val results = listOf(
controller.submitPassword(submitPasswordParameters(state)),
controller.submitSignInCode(submitCodeParameters(state, "123456")),
controller.selectMFAMethod(selectMFAMethodParameters(state, "email-1")),
controller.submitMFAChallenge(submitMFAChallengeParameters(state, "123456"))
)

results.forEach { result ->
assertTrue(result is INativeAuthCommandResult.APIError)
result as INativeAuthCommandResult.APIError
assertEquals(apiError.correlationId, result.correlationId)
assertEquals(apiError.error, result.error)
assertEquals(apiError.errorDescription, result.errorDescription)
assertEquals(apiError.errorCodes, result.errorCodes)
assertEquals(apiError.subError, result.subError)
assertEquals(apiError.attributeValidationDetails, result.attributeValidationDetails)
assertEquals(apiError.traceId, result.traceId)
assertEquals(apiError.timestamp, result.timestamp)
}
}

@Test
fun testSignInSubmissionsPreserveBrowserRedirectDiagnostics() {
val state = mockContinuationState()
val redirect = NativeAuthV2InteractionApiResult.Redirect(
correlationId = correlationId,
redirectReason = "browser_required",
traceId = "redirect-trace",
timestamp = "redirect-time",
errorCodes = listOf(50076)
)
every { mockStrategy.performPasswordVerify(state, any()) } returns redirect
every { mockStrategy.performVerify(state, any()) } returns redirect
every { mockStrategy.performMethodChallenge(state, "email-1") } returns redirect

val results = listOf(
controller.submitPassword(submitPasswordParameters(state)),
controller.submitSignInCode(submitCodeParameters(state, "123456")),
controller.selectMFAMethod(selectMFAMethodParameters(state, "email-1")),
controller.submitMFAChallenge(submitMFAChallengeParameters(state, "123456"))
)

results.forEach { result ->
assertTrue(result is INativeAuthCommandResult.Redirect)
result as INativeAuthCommandResult.Redirect
assertEquals(redirect.correlationId, result.correlationId)
assertEquals(redirect.redirectReason, result.redirectReason)
assertEquals(redirect.traceId, result.traceId)
assertEquals(redirect.timestamp, result.timestamp)
assertEquals(redirect.errorCodes, result.errorCodes)
}
}

@Test
fun testSelectMFAMethodPreservesRiskVerificationErrors() {
val state = mockContinuationState()
val riskState = mockContinuationState()
every { mockStrategy.performMethodChallenge(state, "sms-1") } returns
NativeAuthV2InteractionApiResult.RiskVerificationRequired(correlationId, riskState)
every { mockStrategy.performRiskVerification(riskState) } returns
NativeAuthV2InteractionApiResult.UnknownError(
correlationId,
"risk_failed",
"Risk verification failed.",
traceId = "risk-trace"
)

val result = controller.selectMFAMethod(selectMFAMethodParameters(state, "sms-1"))

assertTrue(result is INativeAuthCommandResult.APIError)
result as INativeAuthCommandResult.APIError
assertEquals("risk_failed", result.error)
assertEquals("risk-trace", result.traceId)
verify(exactly = 1) { mockStrategy.performRiskVerification(riskState) }
verify(exactly = 0) { mockStrategy.performVerify(any(), any()) }
}

@Test
fun testSubmitPasswordRethrowsStrategyFailureAndClearsPassword() {
val state = mockContinuationState()
val password = "Password123!".toCharArray()
val failure = IllegalStateException("verification failed")
every { mockStrategy.performPasswordVerify(state, any()) } throws failure

try {
controller.submitPassword(submitPasswordParameters(state, password))
fail("Expected the strategy exception")
} catch (exception: IllegalStateException) {
assertSame(failure, exception)
}

assertArrayEquals(CharArray(password.size), password)
}

@Test
fun testMfaCommandsRethrowStrategyFailures() {
val state = mockContinuationState()
val failure = IllegalStateException("verification failed")
every { mockStrategy.performMethodChallenge(state, "email-1") } throws failure
every { mockStrategy.performVerify(state, any()) } throws failure

try {
controller.selectMFAMethod(selectMFAMethodParameters(state, "email-1"))
fail("Expected the method-selection exception")
} catch (exception: IllegalStateException) {
assertSame(failure, exception)
}
try {
controller.submitMFAChallenge(submitMFAChallengeParameters(state, "123456"))
fail("Expected the challenge-verification exception")
} catch (exception: IllegalStateException) {
assertSame(failure, exception)
}
}

// -----------------------------------------------------------------------------------------
// Helpers
// -----------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.microsoft.identity.common.java.nativeauth.controllers.results.INative
import com.microsoft.identity.common.java.nativeauth.controllers.results.NativeAuthV2CommandResult
import com.microsoft.identity.common.java.nativeauth.controllers.results.SignUpCommandResult
import com.microsoft.identity.common.java.nativeauth.providers.NativeAuthV2OAuth2Strategy
import com.microsoft.identity.common.java.nativeauth.providers.responses.signin.SignInTokenApiResponse
import com.microsoft.identity.common.java.nativeauth.providers.responses.signin.SignInTokenApiResult
import com.microsoft.identity.common.java.nativeauth.providers.responses.v2.AuthorizeChallengeApiResult
import com.microsoft.identity.common.java.nativeauth.providers.responses.v2.NativeAuthV2ContinuationState
Expand Down Expand Up @@ -146,28 +147,44 @@ class NativeAuthV2SignUpFlowControllerTest {
@Test
fun testSignUpStartSurfacesUserAlreadyExists() {
val entryState = mockContinuationState()
val details = listOf(mapOf(
"code" to "userAlreadyExists",
"attributeIds" to listOf("email"),
"message" to "An account already exists for this email."
))
stubAuthorizeChallengeStart(entryState)
every { mockStrategy.performSignUpStart(entryState) } returns
NativeAuthV2InteractionApiResult.UserAlreadyExists(
correlationId = correlationId,
error = "user_already_exists",
errorDescription = "AADSTS1003037: account already exists.",
errorCodes = listOf(1003037)
errorCodes = listOf(1003037),
subError = "attributeValidationError",
traceId = "service-trace",
timestamp = "service-time",
attributeValidationDetails = details
)

val result = controller.signUpStart(signUpStartParameters())

assertTrue(result is NativeAuthV2CommandResult.UserAlreadyExists)
assertEquals(
listOf(1003037),
(result as NativeAuthV2CommandResult.UserAlreadyExists).errorCodes
)
result as NativeAuthV2CommandResult.UserAlreadyExists
assertEquals(listOf(1003037), result.errorCodes)
assertEquals("attributeValidationError", result.subError)
assertEquals("service-trace", result.traceId)
assertEquals("service-time", result.timestamp)
assertEquals(details, result.attributeValidationDetails)
}

@Test
fun testSignUpStartMapsInvalidAttributesToRetryableAttributesInvalid() {
val entryState = mockContinuationState()
val submittedState = mockContinuationState()
val details = listOf(mapOf(
"code" to "attributeRequired",
"attributeIds" to listOf("city"),
"message" to "Enter a city."
))
every {
entryState.withAdditionalSubmittedAttributes(
match {
Expand All @@ -190,19 +207,62 @@ class NativeAuthV2SignUpFlowControllerTest {
invalidAttributes = listOf("city"),
error = "attribute_validation_failed",
errorDescription = "AADSTS1002027: attribute validation failed.",
errorCodes = listOf(1002027)
errorCodes = listOf(1002027),
subError = "attributeValidationError",
traceId = "service-trace",
timestamp = "service-time",
attributeValidationDetails = details
)

val result = controller.signUpStart(signUpStartParameters())

assertTrue(result is NativeAuthV2CommandResult.AttributesInvalid)
result as NativeAuthV2CommandResult.AttributesInvalid
assertEquals(listOf("city"), result.invalidAttributes)
assertEquals("service-trace", result.traceId)
assertEquals("service-time", result.timestamp)
assertEquals("attributeValidationError", result.subError)
assertEquals(details, result.attributeValidationDetails)
// The validation-error body carries no fresh continuation token, so retry with a state
// that remembers the email and password submitted by the rejected request.
assertSame(submittedState, result.continuationState)
}

@Test
fun testSignUpStartPreservesUnknownErrorDiagnostics() {
val entryState = mockContinuationState()
val details = listOf(mapOf(
"code" to "futureValidationCode",
"attributeIds" to listOf("city"),
"message" to "Unrecognized attribute diagnostic."
))
stubAuthorizeChallengeStart(entryState)
every { mockStrategy.performSignUpStart(entryState) } returns
NativeAuthV2InteractionApiResult.UnknownError(
correlationId = correlationId,
error = "future_error",
errorDescription = "Unrecognized service error.",
errorCodes = listOf(90000),
subError = "future_suberror",
traceId = "service-trace",
timestamp = "service-time",
attributeValidationDetails = details
)

val result = controller.signUpStart(signUpStartParameters())

assertTrue(result is INativeAuthCommandResult.APIError)
result as INativeAuthCommandResult.APIError
assertEquals("future_error", result.error)
assertEquals("Unrecognized service error.", result.errorDescription)
assertEquals(listOf(90000), result.errorCodes)
assertEquals(correlationId, result.correlationId)
assertEquals("future_suberror", result.subError)
assertEquals("service-trace", result.traceId)
assertEquals("service-time", result.timestamp)
assertEquals(details, result.attributeValidationDetails)
}

@Test
fun testSignUpStartWithoutPasswordDefersToPasswordRequired() {
val entryState = mockContinuationState()
Expand Down Expand Up @@ -238,12 +298,19 @@ class NativeAuthV2SignUpFlowControllerTest {
every { mockStrategy.performSignUpStart(entryState) } returns
NativeAuthV2InteractionApiResult.Redirect(
correlationId = correlationId,
redirectReason = "fallback_required"
redirectReason = "fallback_required",
errorCodes = listOf(123),
traceId = "redirect-trace",
timestamp = "redirect-time"
)

val result = controller.signUpStart(signUpStartParameters())

assertTrue(result is INativeAuthCommandResult.Redirect)
result as INativeAuthCommandResult.Redirect
assertEquals(listOf(123), result.errorCodes)
assertEquals("redirect-trace", result.traceId)
assertEquals("redirect-time", result.timestamp)
}

@Test
Expand Down Expand Up @@ -470,7 +537,9 @@ class NativeAuthV2SignUpFlowControllerTest {
AuthorizeChallengeApiResult.UnknownError(
correlationId = correlationId,
error = "expected_test_error",
errorDescription = "Expected test error."
errorDescription = "Expected test error.",
traceId = "authorize-trace",
timestamp = "authorize-time"
)

val result = controller.signInAfterSignUp(signInAfterSignUpParameters(state))
Expand All @@ -480,6 +549,8 @@ class NativeAuthV2SignUpFlowControllerTest {
"expected_test_error",
(result as INativeAuthCommandResult.APIError).error
)
assertEquals("authorize-trace", result.traceId)
assertEquals("authorize-time", result.timestamp)
}

@Test
Expand Down Expand Up @@ -536,17 +607,27 @@ class NativeAuthV2SignUpFlowControllerTest {
correlationId = correlationId,
claimsRequestJson = any()
)
} returns SignInTokenApiResult.UnknownError(
} returns SignInTokenApiResponse(
statusCode = 400,
correlationId = correlationId,
continuationToken = null,
error = "invalid_grant",
errorDescription = "The authorization code is no longer valid.",
errorCodes = emptyList()
)
errorCodes = emptyList(),
errorUri = null,
traceId = "token-trace",
timestamp = "token-time",
subError = "future_suberror"
).toErrorResult()

val result = controller.signInAfterSignUp(signInAfterSignUpParameters(state))

assertTrue(result is INativeAuthCommandResult.APIError)
assertEquals("invalid_grant", (result as INativeAuthCommandResult.APIError).error)
result as INativeAuthCommandResult.APIError
assertEquals("invalid_grant", result.error)
assertEquals("token-trace", result.traceId)
assertEquals("token-time", result.timestamp)
assertEquals("future_suberror", result.subError)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ import com.microsoft.identity.common.java.nativeauth.util.ILoggable
*/
interface INativeAuthCommandResult : ILoggable {
val correlationId: String
data class Redirect(
/** Service diagnostics are absent on locally generated results. */
val traceId: String? get() = null
val timestamp: String? get() = null

data class Redirect @JvmOverloads constructor(
override val correlationId: String,
val redirectReason: String
) : Error(error = BROWSER_REQUIRED_ERROR, errorDescription = redirectReason.ifEmpty { BROWSER_REQUIRED_ERROR_DESCRIPTION }, correlationId = correlationId),
val redirectReason: String,
override val errorCodes: List<Int>? = null,
override val traceId: String? = null,
override val timestamp: String? = null
) : Error(error = BROWSER_REQUIRED_ERROR, errorDescription = redirectReason.ifEmpty { BROWSER_REQUIRED_ERROR_DESCRIPTION }, correlationId = correlationId, errorCodes = errorCodes),
SignInStartCommandResult, SignInWithContinuationTokenCommandResult, SignInSubmitCodeCommandResult, SignInResendCodeCommandResult, SignInSubmitPasswordCommandResult,
SignUpStartCommandResult, SignUpSubmitCodeCommandResult, SignUpResendCodeCommandResult, SignUpSubmitPasswordCommandResult, SignUpSubmitUserAttributesCommandResult,
ResetPasswordStartCommandResult, ResetPasswordSubmitCodeCommandResult, ResetPasswordResendCodeCommandResult, ResetPasswordSubmitNewPasswordCommandResult,
Expand All @@ -60,13 +67,17 @@ interface INativeAuthCommandResult : ILoggable {
/**
* UnknownError is base class to represent various kinds of errors in NativeAuth.
*/
data class APIError(
data class APIError @JvmOverloads constructor(
override val error: String?,
override val errorDescription: String?,
override val details: List<Map<String, String>>? = null,
override val correlationId: String,
override val errorCodes: List<Int>? = null,
val exception: Exception? = null
val exception: Exception? = null,
val subError: String? = null,
val attributeValidationDetails: List<Map<String, Any?>>? = null,
override val traceId: String? = null,
override val timestamp: String? = null
): Error(error, errorDescription, details, correlationId, errorCodes), INativeAuthCommandResult,
SignInStartCommandResult, SignInWithContinuationTokenCommandResult, SignInSubmitCodeCommandResult,
SignInResendCodeCommandResult, SignInSubmitPasswordCommandResult,
Expand Down
Loading
Loading