Skip to content

Commit

Permalink
Add InvalidError for NativeAuth (#2297)
Browse files Browse the repository at this point in the history
  • Loading branch information
SaurabhMSFT authored Jan 28, 2024
1 parent c0e8d76 commit 14fc4ac
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,8 @@ class NativeAuthMsalController : BaseNativeAuthController() {
errorDescription = signUpStartApiResult.errorDescription
)
}
is SignUpStartApiResult.InvalidEmail -> {
SignUpCommandResult.InvalidEmail(
is SignUpStartApiResult.InvalidUsername -> {
INativeAuthCommandResult.InvalidUsername(
error = signUpStartApiResult.error,
errorDescription = signUpStartApiResult.errorDescription
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class SignUpOAuth2StrategyTest {
val signupResult = nativeAuthOAuth2Strategy.performSignUpStart(
signUpStartCommandParameters
)
assertTrue(signupResult is SignUpStartApiResult.InvalidEmail)
assertTrue(signupResult is SignUpStartApiResult.InvalidUsername)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,14 @@ interface INativeAuthCommandResult {
open val correlationId: String = DiagnosticContext.INSTANCE.threadCorrelationId,
open val errorCodes: List<Int>? = null
)

data class InvalidUsername(
override val error: String?,
override val errorDescription: String?,
override val details: List<Map<String, String>>? = null,
override val correlationId: String = DiagnosticContext.INSTANCE.threadCorrelationId,
override val errorCodes: List<Int>? = null,
val exception: Exception? = null
) : Error(error, errorDescription, details, correlationId, errorCodes),
INativeAuthCommandResult, SignInStartCommandResult, SignUpStartCommandResult, SignUpSubmitPasswordCommandResult, ResetPasswordStartCommandResult
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,6 @@ interface SignUpCommandResult {
SignUpSubmitUserAttributesCommandResult,
SignUpSubmitCodeCommandResult

/**
* The signup operation cannot progress as the provided email address is invalid.
*/
data class InvalidEmail(
val error: String,
val errorDescription: String,
val correlationId: String = DiagnosticContext.INSTANCE.threadCorrelationId //TODO: This initialisation will be removed as part of PBI https://identitydivision.visualstudio.com/Engineering/_workitems/edit/2710164
) : SignUpStartCommandResult, SignUpSubmitPasswordCommandResult

/**
* The signup operation cannot progress as the provided password is not acceptable
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ data class ResetPasswordStartRequest private constructor(
): ResetPasswordStartRequest {
// Check for empty Strings and empty Maps
ArgUtils.validateNonNullArg(clientId, "clientId")
ArgUtils.validateNonNullArg(username, "username")
ArgUtils.validateNonNullArg(challengeType, "challengeType")
ArgUtils.validateNonNullArg(requestUrl, "requestUrl")
ArgUtils.validateNonNullArg(headers, "headers")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ data class SignInInitiateRequest private constructor(
headers: Map<String, String?>
): SignInInitiateRequest {
// Check for empty Strings and empty Maps
ArgUtils.validateNonNullArg(username, "username")
ArgUtils.validateNonNullArg(clientId, "clientId")
ArgUtils.validateNonNullArg(challengeType, "challengeType")
ArgUtils.validateNonNullArg(requestUrl, "requestUrl")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ data class SignUpStartRequest private constructor(
headers: Map<String, String?>
): SignUpStartRequest {
// Check for empty Strings and empty Maps
ArgUtils.validateNonNullArg(username, "username")
ArgUtils.validateNonNullArg(clientId, "clientId")
ArgUtils.validateNonNullArg(challengeType, "challengeType")
ArgUtils.validateNonNullArg(requestUrl, "requestUrl")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ import com.microsoft.identity.common.java.nativeauth.providers.IApiResponse
import com.microsoft.identity.common.java.nativeauth.providers.responses.ApiErrorResult
import com.microsoft.identity.common.java.nativeauth.util.isAttributeValidationFailed
import com.microsoft.identity.common.java.nativeauth.util.isAuthNotSupported
import com.microsoft.identity.common.java.nativeauth.util.isInvalidClient
import com.microsoft.identity.common.java.nativeauth.util.isInvalidEmail
import com.microsoft.identity.common.java.nativeauth.util.isInvalidUsername
import com.microsoft.identity.common.java.nativeauth.util.isInvalidParameter
import com.microsoft.identity.common.java.nativeauth.util.isPasswordBanned
import com.microsoft.identity.common.java.nativeauth.util.isPasswordInvalid
Expand All @@ -41,7 +40,6 @@ import com.microsoft.identity.common.java.nativeauth.util.isPasswordTooWeak
import com.microsoft.identity.common.java.nativeauth.util.isRedirect
import com.microsoft.identity.common.java.nativeauth.util.isUnsupportedChallengeType
import com.microsoft.identity.common.java.nativeauth.util.isUserAlreadyExists
import com.microsoft.identity.common.java.nativeauth.util.isVerificationRequired
import com.microsoft.identity.common.java.nativeauth.util.toAttributeList
import java.net.HttpURLConnection

Expand Down Expand Up @@ -79,8 +77,8 @@ data class SignUpStartApiResponse(
errorDescription = errorDescription.orEmpty()
)
}
errorCodes?.get(0).isInvalidParameter() and (errorDescription?.isInvalidEmail() == true) -> {
SignUpStartApiResult.InvalidEmail(
errorCodes?.get(0).isInvalidParameter() and (errorDescription?.isInvalidUsername() == true) -> {
SignUpStartApiResult.InvalidUsername(
error = error.orEmpty(),
errorDescription = errorDescription.orEmpty()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ sealed interface SignUpStartApiResult {
/**
* Signup start operation was started for a malformed email address.
*/
data class InvalidEmail(
data class InvalidUsername(
override val error: String,
override val errorDescription: String
): ApiErrorResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ internal fun String?.isVerificationRequired(): Boolean {
return this.contentEquals(other = "verification_required", ignoreCase = true)
}

internal fun String?.isInvalidEmail(): Boolean {
internal fun String?.isInvalidUsername(): Boolean {
return (this?.contains(other = "username parameter is empty or not valid", ignoreCase = true) == true )
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ class NativeAuthRequestHandlerTest {
)

// signup start tests
@Test(expected = ClientException::class)
fun testSignUpStartWithEmptyUsernameShouldThrowException() {
@Test
fun testSignUpStartWithEmptyUsernameShouldNotThrowException() {
val commandParameters = SignUpStartCommandParameters.builder()
.platformComponents(mock<PlatformComponents>())
.username(emptyString)
Expand Down Expand Up @@ -334,8 +334,8 @@ class NativeAuthRequestHandlerTest {
}

// signin tests
@Test(expected = ClientException::class)
fun testSignInInitiateWithEmptyUsernameShouldThrowException() {
@Test
fun testSignInInitiateWithEmptyUsernameShouldNotThrowException() {
val commandParameters = SignInStartCommandParameters.builder()
.platformComponents(mock<PlatformComponents>())
.username(emptyString)
Expand Down Expand Up @@ -457,8 +457,8 @@ class NativeAuthRequestHandlerTest {
)
}

@Test(expected = ClientException::class)
fun testSignInInitiateWithPasswordCommandParametersWithEmptyUsernameShouldThrowException() {
@Test
fun testSignInInitiateWithPasswordCommandParametersWithEmptyUsernameShouldNotThrowException() {
val commandParameters = SignInStartCommandParameters.builder()
.platformComponents(mock<PlatformComponents>())
.username(emptyString)
Expand Down Expand Up @@ -605,8 +605,8 @@ class NativeAuthRequestHandlerTest {
}

// sspr start tests
@Test(expected = ClientException::class)
fun testResetPasswordStartWithEmptyUsernameShouldThrowException() {
@Test
fun testResetPasswordStartWithEmptyUsernameShouldNotThrowException() {
val commandParameters = ResetPasswordStartCommandParameters.builder()
.platformComponents(mock<PlatformComponents>())
.username(emptyString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import org.junit.Assert.assertTrue
import org.junit.Test
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import java.net.URL

class NativeAuthResponseHandlerTest {
private val clientId = "1234"
Expand Down Expand Up @@ -280,7 +279,7 @@ class NativeAuthResponseHandlerTest {
subError = null
)
val apiResult = signUpStartApiResponse.toResult()
assertTrue(apiResult is SignUpStartApiResult.InvalidEmail)
assertTrue(apiResult is SignUpStartApiResult.InvalidUsername)
}

@Test
Expand Down

0 comments on commit 14fc4ac

Please sign in to comment.