Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Username checking in native auth requests #2385

Closed
wants to merge 3 commits into from
Closed

Conversation

Yuki-YuXin
Copy link
Contributor

@Yuki-YuXin Yuki-YuXin commented Apr 16, 2024

Put back ArgUtils.validateNonNullArg(username, "username") in all initial native auth requests.

Company PRs:
msal: AzureAD/microsoft-authentication-library-for-android#2080
native sample app: Azure-Samples/ms-identity-ciam-native-auth-android-sample#24

@@ -53,6 +53,7 @@ data class ResetPasswordStartRequest private constructor(
headers: Map<String, String?>
): ResetPasswordStartRequest {
// Check for empty Strings and empty Maps
ArgUtils.validateNonNullArg(username, "username")
Copy link
Contributor

@SammyO SammyO Apr 30, 2024

Choose a reason for hiding this comment

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

why was this removed in the first place?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure about the reason but I would prefer to keep the checking here.

@Test
fun testSignUpStartWithEmptyUsernameShouldNotThrowException() {
@Test(expected = ClientException::class)
fun testSignUpStartWithEmptyUsernameShouldThrowException() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Wasn't this changed (i.e. the exception removed) as part of last year's work to align some of our errors and return an InvalidUsernameError in the case of an empty string? Giannis worked on that I believe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I think so. But either CommandResultUtil.kt

if (this.status != ICommandResult.ResultStatus.COMPLETED) {
        var exception: Exception? = null
        var exceptionMessage: String? = ""

        if (this.result is Exception) {
            exception = this.result as Exception
            exceptionMessage = exception.message
        }

        return com.microsoft.identity.common.java.nativeauth.controllers.results.INativeAuthCommandResult.UnknownError(
            error = UNSUCCESSFUL_COMMAND_ERROR,
            errorDescription = exceptionMessage,
            exception = exception,
            correlationId = this.correlationId
        ) as ExpectedType

or NativeAuthPublicClientApplication.kt

return withContext(Dispatchers.IO) {
            try {
                verifyNoUserIsSignedIn()

                if (username.isBlank()) {
                    return@withContext SignUpError(
                        errorType = ErrorTypes.INVALID_USERNAME,
                        errorMessage = "Empty or blank username",
                        correlationId = "UNSET"
                    )
                }

can handle empty username.

I think the latter one act the main role of returning InvalidUsernameError before sending out the request/validating request parameters. Thus, I would prefer to keep the ThrowException here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Some thoughts:

  • if we're already doing a username is empty check in the interface, we don't need to do it again in the API layer.
  • we shouldn't just be throwing exceptions. An exception is a pretty heavy mechanism to give feedback to a developer. We should aim for something more graceful than that.
  • the fact that this "throwing an exception" was removed from this API layer a few months ago, means we had a reason why. If that reason has changed, let's talk about it. We can just revert the work we did a few months ago without good reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will abandon this PR.

@Yuki-YuXin Yuki-YuXin closed this May 7, 2024
@Yuki-YuXin Yuki-YuXin deleted the yuki/wrap-exception branch May 29, 2024 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants