From 5a90b08d264c8c4c7716ae641e3e0e5e9073fd82 Mon Sep 17 00:00:00 2001 From: Saurabh Gautam Date: Mon, 15 Jan 2024 17:41:22 +0000 Subject: [PATCH 1/8] Change the signature of signup/signin methods --- .../NativeAuthPublicClientAppKotlinTest.java | 150 +++++ .../NativeAuthPublicClientApplicationTest.kt | 184 ++++++ .../internal/CommandParametersAdapter.java | 92 +-- .../INativeAuthPublicClientApplication.kt | 60 +- .../NativeAuthPublicClientApplication.kt | 537 +++++------------- .../statemachine/errors/SignInErrors.kt | 21 - .../statemachine/errors/SignUpErrors.kt | 24 - .../statemachine/results/SignInResult.kt | 9 +- .../statemachine/results/SignUpResult.kt | 11 +- ...veAuthPublicClientApplicationJavaTest.java | 286 +++++----- ...veAuthPublicClientApplicationKotlinTest.kt | 106 ++-- 11 files changed, 675 insertions(+), 805 deletions(-) create mode 100644 msal/src/androidTest/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientAppKotlinTest.java create mode 100644 msal/src/androidTest/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationTest.kt diff --git a/msal/src/androidTest/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientAppKotlinTest.java b/msal/src/androidTest/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientAppKotlinTest.java new file mode 100644 index 0000000000..961ad5a6c8 --- /dev/null +++ b/msal/src/androidTest/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientAppKotlinTest.java @@ -0,0 +1,150 @@ +// 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; + +import android.content.Context; + +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import com.microsoft.identity.client.AndroidTestUtil; +import com.microsoft.identity.client.PublicClientApplication; +import com.microsoft.identity.client.PublicClientApplicationTest; +import com.microsoft.identity.client.exception.MsalClientException; +import com.microsoft.identity.client.exception.MsalException; +import com.microsoft.identity.common.java.net.HttpUrlConnectionFactory; +import com.microsoft.identity.msal.test.R; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.Arrays; +import java.util.List; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.fail; + +/** + * Tests for {@link com.microsoft.identity.client.PublicClientApplication}. + */ +@RunWith(AndroidJUnit4.class) +public final class NativeAuthPublicClientAppKotlinTest { + private Context mAppContext; + private static final String CLIENT_ID = "client-id"; + private static final String CIAM_AUTHORITY = "https://msidlabciam1.ciamlogin.com/msidlabciam1.onmicrosoft.com"; + private final List CHALLENGE_TYPES = Arrays.asList("oob", "password"); + + @Before + public void setUp() { + System.setProperty( + "dexmaker.dexcache", + androidx.test.platform.app.InstrumentationRegistry + .getInstrumentation() + .getTargetContext() + .getCacheDir() + .getPath() + ); + + System.setProperty( + "org.mockito.android.target", + ApplicationProvider + .getApplicationContext() + .getCacheDir() + .getPath() + ); + + mAppContext = androidx.test.platform.app.InstrumentationRegistry.getInstrumentation().getContext().getApplicationContext(); + } + + @After + public void tearDown() { + HttpUrlConnectionFactory.clearMockedConnectionQueue(); + AndroidTestUtil.removeAllTokens(mAppContext); + } + + @Test + public void testNativeAuthConstructor() { + final Context context = new PublicClientApplicationTest.MockContext(mAppContext); + PublicClientApplicationTest.mockPackageManagerWithDefaultFlag(context, mAppContext); + PublicClientApplicationTest.mockHasCustomTabRedirect(context); + + try { + final INativeAuthPublicClientApplication app = PublicClientApplication.createNativeAuthPublicClientApplication( + context, + R.raw.test_msal_config_native_auth + ); + Assert.assertNotNull(app); + } catch (InterruptedException e) { + Assert.fail(e.getMessage()); + } catch (MsalException e) { + Assert.fail(e.getMessage()); + } + } + + @Test + public void testNativeAuthConstructorNoMetadata() { + final Context context = new PublicClientApplicationTest.MockContext(mAppContext); + PublicClientApplicationTest.mockPackageManagerWithDefaultFlag(context, mAppContext); + PublicClientApplicationTest.mockHasCustomTabRedirect(context); + + try { + final INativeAuthPublicClientApplication app = PublicClientApplication.createNativeAuthPublicClientApplication( + context, + CLIENT_ID, + CIAM_AUTHORITY, + null, + CHALLENGE_TYPES + ); + Assert.assertNotNull(app); + } catch (InterruptedException e) { + Assert.fail(e.getMessage()); + } catch (MsalException e) { + Assert.fail(e.getMessage()); + } + } + + @Test + public void testFailingNativeAuthConstructor() { + final Context context = new PublicClientApplicationTest.MockContext(mAppContext); + PublicClientApplicationTest.mockPackageManagerWithDefaultFlag(context, mAppContext); + PublicClientApplicationTest.mockHasCustomTabRedirect(context); + + // Should fail, as we are attempting to create a multiple account application + try { + final INativeAuthPublicClientApplication app = PublicClientApplication.createNativeAuthPublicClientApplication( + context, + R.raw.test_msal_config_multiple_account + ); + Assert.fail("Unintentionally created app"); + } catch (InterruptedException e) { + Assert.fail(e.getMessage()); + } catch (MsalException e) { + Assert.assertEquals("Expecting error.", + e.getErrorCode(), + MsalClientException.NATIVE_AUTH_INVALID_ACCOUNT_MODE_CONFIG_ERROR_CODE); + } + } +} \ No newline at end of file diff --git a/msal/src/androidTest/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationTest.kt b/msal/src/androidTest/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationTest.kt new file mode 100644 index 0000000000..cbecd88cbd --- /dev/null +++ b/msal/src/androidTest/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationTest.kt @@ -0,0 +1,184 @@ +// 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 + +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import com.microsoft.identity.client.AndroidTestUtil +import com.microsoft.identity.client.PublicClientApplication +import com.microsoft.identity.client.exception.MsalClientException +import com.microsoft.identity.client.exception.MsalException +import com.microsoft.identity.common.java.net.HttpUrlConnectionFactory +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.runBlocking +import org.junit.After +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import java.util.Arrays + +@ExperimentalCoroutinesApi +@RunWith(AndroidJUnit4::class) +class NativeAuthPublicClientAppKotlinTest { + private lateinit var context: Context + private val CLIENT_ID = "1234" + // The general format is https://.ciamlogin.com/.onmicrosoft.com + // See details here: https://microsoft.sharepoint-df.com/:w:/t/AADIEFtogether/ES9p_7m-qUtEuINcd0UlyekBh6TrWtrMJr12WS_O7BAgww?e=HQybBw + private val CIAM_AUTHORITY = "https://msidlabciam1.ciamlogin.com/msidlabciam1.onmicrosoft.com" + private val B2C_AUTHORITY = "https://msidlabb2c.b2clogin.com/tfp/msidlabb2c.onmicrosoft.com/b2c_1_ropc_auth/" + private val INVALID_AUTHORITY = "https://b2clogin.com" + private val EMPTY_STRING = "" + // TODO: Replace the link with https://learn.microsoft.com/ when the document about OAuth 2.0 Direct Interaction Grants is published. + // The definitions and scopes in OAuth 2.0 Direct Interaction Grants(3.4Challenge Types): + // https://aaronpk.github.io/oauth-direct-interaction-grant/draft-parecki-oauth-direct-interaction-grant.html#name-authorization-grants + private val challengeTypes = Arrays.asList("oob", "password") + + @Before + fun setup() { + System.setProperty( + "dexmaker.dexcache", + InstrumentationRegistry + .getInstrumentation() + .targetContext + .cacheDir + .path + ) + + System.setProperty( + "org.mockito.android.target", + ApplicationProvider + .getApplicationContext() + .cacheDir + .path + ) + + context = InstrumentationRegistry.getInstrumentation().context.applicationContext + } + + @After + fun tearDown() { + HttpUrlConnectionFactory.clearMockedConnectionQueue() + AndroidTestUtil.removeAllTokens(context) + } + + @Test + fun testWorkingInit() { + runBlocking { + try { + val app = PublicClientApplication.createNativeAuthPublicClientApplication( + context, + CLIENT_ID, + CIAM_AUTHORITY, + null, + challengeTypes + ) + Assert.assertNotNull("NAPCA can't be null", app) + } catch (exception: MsalException) { + Assert.fail(exception.message) + } + } + } + + @Test + fun testEmptyClientIdReturnsOnError() { + runBlocking { + try { + val app = PublicClientApplication.createNativeAuthPublicClientApplication( + context, + EMPTY_STRING, // Invalid parameters: empty client id should throw exception + CIAM_AUTHORITY, + null, + challengeTypes + ) + Assert.fail("NAPCA creation did not throw exception") + } catch (exception: IllegalArgumentException) { + return@runBlocking + } + Assert.fail("Empty client Id string should return error") + } + } + + @Test + fun testEmptyAuthorityReturnsOnError() { + runBlocking { + try { + val app = PublicClientApplication.createNativeAuthPublicClientApplication( + context, + CLIENT_ID, + EMPTY_STRING, // Invalid parameters: empty authority should throw exception + null, + challengeTypes + ) + Assert.fail("NAPCA creation did not throw exception") + } catch (exception: IllegalArgumentException) { + return@runBlocking + } + Assert.fail("Empty client Id string should return error") + } + } + + @Test + fun testB2CAuthorityReturnsOnError() { + runBlocking { + try { + val app = PublicClientApplication.createNativeAuthPublicClientApplication( + context, + CLIENT_ID, + B2C_AUTHORITY, + null, + challengeTypes + ) + Assert.fail("NAPCA creation did not throw exception") + } catch (exception: MsalException) { + // warp NATIVE_AUTH_INVALID_CIAM_AUTHORITY_ERROR into UNKNOWN_ERROR in catch block + if (exception.errorCode == MsalClientException.UNKNOWN_ERROR) + return@runBlocking + } + Assert.fail("B2C authority should return error") + } + } + + @Test + fun testInvalidAuthorityReturnsOnError() { + runBlocking { + try { + val app = PublicClientApplication.createNativeAuthPublicClientApplication( + context, + CLIENT_ID, + INVALID_AUTHORITY, + null, + challengeTypes + ) + Assert.fail("NAPCA creation did not throw exception") + } catch (exception: MsalException) { + // warp NATIVE_AUTH_INVALID_CIAM_AUTHORITY_ERROR into UNKNOWN_ERROR in catch block + if (exception.errorCode == MsalClientException.UNKNOWN_ERROR) + return@runBlocking + } + Assert.fail("Invalid authority string should return error") + } + } +} 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 bc18cd37b4..e67eb46756 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 @@ -74,17 +74,13 @@ import com.microsoft.identity.common.java.nativeauth.commands.parameters.ResetPasswordSubmitNewPasswordCommandParameters; import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignInResendCodeCommandParameters; import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignInStartCommandParameters; -import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignInStartUsingPasswordCommandParameters; import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignInSubmitCodeCommandParameters; import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignInSubmitPasswordCommandParameters; import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignUpResendCodeCommandParameters; import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignUpStartCommandParameters; -import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignUpStartUsingPasswordCommandParameters; import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignUpSubmitCodeCommandParameters; import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignUpSubmitPasswordCommandParameters; import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignUpSubmitUserAttributesCommandParameters; -import com.microsoft.identity.nativeauth.NativeAuthPublicClientApplicationConfiguration; - import java.util.AbstractMap; import java.util.ArrayList; import java.util.Arrays; @@ -369,41 +365,6 @@ public static DeviceCodeFlowCommandParameters createDeviceCodeFlowCommandParamet return commandParameters; } - /** - * Creates command parameter for [{@link com.microsoft.identity.common.nativeauth.internal.commands.SignUpStartCommand}] of Native Auth. - * @param configuration PCA configuration - * @param tokenCache token cache for storing results - * @param username email address of the user - * @return Command parameter object - * @throws ClientException - */ - public static SignUpStartCommandParameters createSignUpStartCommandParameters( - @NonNull final NativeAuthPublicClientApplicationConfiguration configuration, - @NonNull final OAuth2TokenCache tokenCache, - @NonNull final String username, - final Map userAttributes) { - final NativeAuthCIAMAuthority authority = ((NativeAuthCIAMAuthority) configuration.getDefaultAuthority()); - - return SignUpStartCommandParameters.builder() - .platformComponents(AndroidPlatformComponentsFactory.createFromContext(configuration.getAppContext())) - .applicationName(configuration.getAppContext().getPackageName()) - .applicationVersion(getPackageVersion(configuration.getAppContext())) - .clientId(configuration.getClientId()) - .isSharedDevice(configuration.getIsSharedDevice()) - .redirectUri(configuration.getRedirectUri()) - .oAuth2TokenCache(tokenCache) - .requiredBrokerProtocolVersion(configuration.getRequiredBrokerProtocolVersion()) - .sdkType(SdkType.MSAL) - .sdkVersion(PublicClientApplication.getSdkVersion()) - .powerOptCheckEnabled(configuration.isPowerOptCheckForEnabled()) - .authority(authority) - .username(username) - .challengeType(configuration.getChallengeTypes()) - .clientId(configuration.getClientId()) - .userAttributes(userAttributes) - .build(); - } - /** * Creates command parameter for [{@link com.microsoft.identity.common.nativeauth.internal.commands.SignUpStartCommand}] of Native Auth when password is provided * @param configuration PCA configuration @@ -413,16 +374,16 @@ public static SignUpStartCommandParameters createSignUpStartCommandParameters( * @return Command parameter object * @throws ClientException */ - public static SignUpStartUsingPasswordCommandParameters createSignUpStartUsingPasswordCommandParameters( + public static SignUpStartCommandParameters createSignUpStartCommandParameters( @NonNull final NativeAuthPublicClientApplicationConfiguration configuration, @NonNull final OAuth2TokenCache tokenCache, @NonNull final String username, - @NonNull final char[] password, + @Nullable final char[] password, final Map userAttributes) { final NativeAuthCIAMAuthority authority = ((NativeAuthCIAMAuthority) configuration.getDefaultAuthority()); - return SignUpStartUsingPasswordCommandParameters.builder() + return SignUpStartCommandParameters.builder() .platformComponents(AndroidPlatformComponentsFactory.createFromContext(configuration.getAppContext())) .applicationName(configuration.getAppContext().getPackageName()) .applicationVersion(getPackageVersion(configuration.getAppContext())) @@ -583,47 +544,6 @@ public static SignUpSubmitPasswordCommandParameters createSignUpSubmitPasswordCo .build(); } - /** - * Creates command parameter for [{@link com.microsoft.identity.common.nativeauth.internal.commands.SignInStartCommand}] of Native Auth. - * @param configuration PCA configuration - * @param tokenCache token cache for storing results - * @param username email address of the user - * @return Command parameter object - * @throws ClientException - */ - public static SignInStartCommandParameters createSignInStartCommandParameters( - @NonNull final NativeAuthPublicClientApplicationConfiguration configuration, - @NonNull final OAuth2TokenCache tokenCache, - @NonNull final String username) throws ClientException { - final AbstractAuthenticationScheme authenticationScheme = AuthenticationSchemeFactory.createScheme( - AndroidPlatformComponentsFactory.createFromContext(configuration.getAppContext()), - null - ); - - final NativeAuthCIAMAuthority authority = ((NativeAuthCIAMAuthority) configuration.getDefaultAuthority()); - - final SignInStartCommandParameters commandParameters = SignInStartCommandParameters.builder() - .platformComponents(AndroidPlatformComponentsFactory.createFromContext(configuration.getAppContext())) - .applicationName(configuration.getAppContext().getPackageName()) - .applicationVersion(getPackageVersion(configuration.getAppContext())) - .clientId(configuration.getClientId()) - .isSharedDevice(configuration.getIsSharedDevice()) - .redirectUri(configuration.getRedirectUri()) - .oAuth2TokenCache(tokenCache) - .requiredBrokerProtocolVersion(configuration.getRequiredBrokerProtocolVersion()) - .sdkType(SdkType.MSAL) - .sdkVersion(PublicClientApplication.getSdkVersion()) - .powerOptCheckEnabled(configuration.isPowerOptCheckForEnabled()) - .authority(authority) - .username(username) - .authenticationScheme(authenticationScheme) - .clientId(configuration.getClientId()) - .challengeType(configuration.getChallengeTypes()) - .build(); - - return commandParameters; - } - /** * Creates command parameter for [{@link com.microsoft.identity.common.nativeauth.internal.commands.SignInStartCommand}] of Native Auth using username and password * @param configuration PCA configuration @@ -634,11 +554,11 @@ public static SignInStartCommandParameters createSignInStartCommandParameters( * @return Command parameter object * @throws ClientException */ - public static SignInStartUsingPasswordCommandParameters createSignInStartUsingPasswordCommandParameters( + public static SignInStartCommandParameters createSignInStartUsingPasswordCommandParameters( @NonNull final NativeAuthPublicClientApplicationConfiguration configuration, @NonNull final OAuth2TokenCache tokenCache, @NonNull final String username, - @NonNull final char[] password, + @Nullable final char[] password, final List scopes) throws ClientException { final AbstractAuthenticationScheme authenticationScheme = AuthenticationSchemeFactory.createScheme( AndroidPlatformComponentsFactory.createFromContext(configuration.getAppContext()), @@ -647,7 +567,7 @@ public static SignInStartUsingPasswordCommandParameters createSignInStartUsingPa final NativeAuthCIAMAuthority authority = ((NativeAuthCIAMAuthority) configuration.getDefaultAuthority()); - final SignInStartUsingPasswordCommandParameters commandParameters = SignInStartUsingPasswordCommandParameters.builder() + final SignInStartCommandParameters commandParameters = SignInStartCommandParameters.builder() .platformComponents(AndroidPlatformComponentsFactory.createFromContext(configuration.getAppContext())) .applicationName(configuration.getAppContext().getPackageName()) .applicationVersion(getPackageVersion(configuration.getAppContext())) 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 d194091847..a9b433a985 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/INativeAuthPublicClientApplication.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/INativeAuthPublicClientApplication.kt @@ -28,9 +28,7 @@ import com.microsoft.identity.client.exception.MsalException import com.microsoft.identity.nativeauth.statemachine.results.GetAccountResult import com.microsoft.identity.nativeauth.statemachine.results.ResetPasswordStartResult import com.microsoft.identity.nativeauth.statemachine.results.SignInResult -import com.microsoft.identity.nativeauth.statemachine.results.SignInUsingPasswordResult import com.microsoft.identity.nativeauth.statemachine.results.SignUpResult -import com.microsoft.identity.nativeauth.statemachine.results.SignUpUsingPasswordResult /** @@ -71,89 +69,47 @@ interface INativeAuthPublicClientApplication : IPublicClientApplication { * Sign in a user with a given username; Kotlin coroutines variant. * * @param username username of the account to sign in. + * @param password password of the account to sign in. * @param scopes (Optional) scopes to request during the sign in. * @return [com.microsoft.identity.nativeauth.statemachine.results.SignInResult] see detailed possible return state under the object. * @throws [MsalException] if an account is already signed in. */ - suspend fun signIn(username: String, scopes: List? = null): SignInResult + suspend fun signIn(username: String, password: CharArray? = null, scopes: List? = null): SignInResult /** * Sign in a user with a given username; callback variant. * * @param username username of the account to sign in. + * @param password password of the account to sign in. * @param scopes (Optional) scopes to request during the sign in. * @param callback [com.microsoft.identity.nativeauth.NativeAuthPublicClientApplication.SignInCallback] to receive the result. * @return [com.microsoft.identity.nativeauth.statemachine.results.SignInResult] see detailed possible return state under the object. * @throws [MsalException] if an account is already signed in. */ - fun signIn(username: String, scopes: List? = null, callback: NativeAuthPublicClientApplication.SignInCallback) - - /** - * Sign in the account using username and password; Kotlin coroutines variant. - * - * @param username username of the account to sign in. - * @param password password of the account to sign in. - * @param scopes (Optional) list of scopes to request. - * @return [com.microsoft.identity.nativeauth.statemachine.results.SignInUsingPasswordResult] see detailed possible return state under the object. - * @throws MsalClientException if an account is already signed in. - */ - suspend fun signInUsingPassword(username: String, password: CharArray, scopes: List? = null): SignInUsingPasswordResult - - /** - * Sign in the account using username and password; callback variant. - * - * @param username username of the account to sign in. - * @param password password of the account to sign in. - * @param scopes (Optional) list of scopes to request. - * @param callback [com.microsoft.identity.nativeauth.NativeAuthPublicClientApplication.SignInUsingPasswordCallback] to receive the result. - * @return [com.microsoft.identity.nativeauth.statemachine.results.SignInUsingPasswordResult] see detailed possible return state under the object. - * @throws MsalClientException if an account is already signed in. - */ - fun signInUsingPassword(username: String, password: CharArray, scopes: List? = null, callback: NativeAuthPublicClientApplication.SignInUsingPasswordCallback) + fun signIn(username: String, password: CharArray? = null, scopes: List? = null, callback: NativeAuthPublicClientApplication.SignInCallback) /** * Sign up the account starting from a username; Kotlin coroutines variant. * * @param username username of the account to sign up. + * @param password password of the account to sign up. * @param attributes (Optional) user attributes to be used during account creation. * @return [com.microsoft.identity.nativeauth.statemachine.results.SignUpResult] see detailed possible return state under the object. * @throws MsalClientException if an account is already signed in. */ - suspend fun signUp(username: String, attributes: UserAttributes? = null): SignUpResult + suspend fun signUp(username: String, password: CharArray? = null, attributes: UserAttributes? = null): SignUpResult /** * Sign up the account starting from a username; callback variant. * * @param username username of the account to sign up. + * @param password password of the account to sign up. * @param attributes (Optional) user attributes to be used during account creation. * @param callback [com.microsoft.identity.nativeauth.NativeAuthPublicClientApplication.SignUpCallback] to receive the result. * @return [com.microsoft.identity.nativeauth.statemachine.results.SignUpResult] see detailed possible return state under the object. * @throws MsalClientException if an account is already signed in. */ - fun signUp(username: String, attributes: UserAttributes? = null, callback: NativeAuthPublicClientApplication.SignUpCallback) - - /** - * Sign up the account using username and password. Kotlin coroutines variant. - * - * @param username username of the account to sign up. - * @param password password of the account to sign up. - * @param attributes (Optional) user attributes to be used during account creation - * @return [com.microsoft.identity.nativeauth.statemachine.results.SignUpUsingPasswordResult] see detailed possible return state under the object. - * @throws MsalClientException if an account is already signed in. - */ - suspend fun signUpUsingPassword(username: String, password: CharArray, attributes: UserAttributes? = null): SignUpUsingPasswordResult - - /** - * Sign up the account using username and password; callback variant. - * - * @param username username of the account to sign up. - * @param password password of the account to sign up. - * @param attributes (Optional) user attributes to be used during account creation - * @param callback [com.microsoft.identity.nativeauth.NativeAuthPublicClientApplication.SignUpUsingPasswordCallback] to receive the result. - * @return [com.microsoft.identity.nativeauth.statemachine.results.SignUpUsingPasswordResult] see detailed possible return state under the object. - * @throws MsalClientException if an account is already signed in. - */ - fun signUpUsingPassword(username: String, password: CharArray, attributes: UserAttributes? = null, callback: NativeAuthPublicClientApplication.SignUpUsingPasswordCallback) + fun signUp(username: String, password: CharArray? = null, attributes: UserAttributes? = null, callback: NativeAuthPublicClientApplication.SignUpCallback) /** * Reset password for the account starting from a username; Kotlin coroutines variant. 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 f5d70f3ae6..b1c89d4cce 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt @@ -33,9 +33,7 @@ import com.microsoft.identity.client.exception.MsalException import com.microsoft.identity.client.internal.CommandParametersAdapter import com.microsoft.identity.nativeauth.statemachine.results.ResetPasswordStartResult import com.microsoft.identity.nativeauth.statemachine.results.SignInResult -import com.microsoft.identity.nativeauth.statemachine.results.SignInUsingPasswordResult import com.microsoft.identity.nativeauth.statemachine.results.SignUpResult -import com.microsoft.identity.nativeauth.statemachine.results.SignUpUsingPasswordResult import com.microsoft.identity.nativeauth.statemachine.states.Callback import com.microsoft.identity.nativeauth.statemachine.states.ResetPasswordCodeRequiredState import com.microsoft.identity.nativeauth.statemachine.states.SignInAfterSignUpState @@ -76,10 +74,8 @@ import com.microsoft.identity.nativeauth.statemachine.errors.ErrorTypes import com.microsoft.identity.nativeauth.statemachine.errors.ResetPasswordError import com.microsoft.identity.nativeauth.statemachine.errors.SignInError import com.microsoft.identity.nativeauth.statemachine.errors.SignInErrorTypes -import com.microsoft.identity.nativeauth.statemachine.errors.SignInUsingPasswordError import com.microsoft.identity.nativeauth.statemachine.errors.SignUpError import com.microsoft.identity.nativeauth.statemachine.errors.SignUpErrorTypes -import com.microsoft.identity.nativeauth.statemachine.errors.SignUpUsingPasswordError import com.microsoft.identity.nativeauth.statemachine.results.GetAccountResult import com.microsoft.identity.nativeauth.statemachine.states.AccountState import kotlinx.coroutines.CoroutineScope @@ -252,156 +248,26 @@ class NativeAuthPublicClientApplication( interface SignInCallback : Callback - /** - * Sign in a user with a given username; callback variant. - * - * @param username username of the account to sign in. - * @param scopes (Optional) scopes to request during the sign in. - * @param callback [com.microsoft.identity.nativeauth.NativeAuthPublicClientApplication.SignInCallback] to receive the result. - * @return [com.microsoft.identity.nativeauth.statemachine.results.SignInResult] see detailed possible return state under the object. - * @throws [MsalException] if an account is already signed in. - */ - override fun signIn(username: String, scopes: List?, callback: SignInCallback) { - LogSession.logMethodCall(TAG, "${TAG}.signIn(username: String, scopes: List?, callback: SignInCallback)") - pcaScope.launch { - try { - val result = signIn(username, scopes) - callback.onResult(result) - } catch (e: MsalException) { - Logger.error(TAG, "Exception thrown in signIn", e) - callback.onError(e) - } - } - } - - /** - * Sign in a user with a given username; Kotlin coroutines variant. - * - * @param username username of the account to sign in. - * @param scopes (Optional) scopes to request during the sign in. - * @return [com.microsoft.identity.nativeauth.statemachine.results.SignInResult] see detailed possible return state under the object. - * @throws [MsalException] if an account is already signed in. - */ - override suspend fun signIn( - username: String, - scopes: List? - ): SignInResult { - return withContext(Dispatchers.IO) { - LogSession.logMethodCall(TAG, "${TAG}.signIn") - - verifyNoUserIsSignedIn() - - val params = CommandParametersAdapter.createSignInStartCommandParameters( - nativeAuthConfig, - nativeAuthConfig.oAuth2TokenCache, - username - ) - - val command = SignInStartCommand( - params, - NativeAuthMsalController(), - PublicApiId.NATIVE_AUTH_SIGN_IN_WITH_EMAIL - ) - - val rawCommandResult = CommandDispatcher.submitSilentReturningFuture(command).get() - - return@withContext when (val result = rawCommandResult.checkAndWrapCommandResultType()) { - is SignInCommandResult.CodeRequired -> { - SignInResult.CodeRequired( - nextState = SignInCodeRequiredState( - continuationToken = result.continuationToken, - scopes = scopes, - config = nativeAuthConfig - ), - codeLength = result.codeLength, - sentTo = result.challengeTargetLabel, - channel = result.challengeChannel - ) - } - is SignInCommandResult.PasswordRequired -> { - SignInResult.PasswordRequired( - nextState = SignInPasswordRequiredState( - continuationToken = result.continuationToken, - scopes = scopes, - config = nativeAuthConfig - ) - ) - } - is SignInCommandResult.Complete -> { - Logger.warn( - TAG, - "Sign in received unexpected result $result" - ) - SignInError( - errorMessage = "unexpected state", - error = "unexpected_state", - correlationId = "UNSET" - ) - } - is SignInCommandResult.UserNotFound -> { - SignInError( - errorType = ErrorTypes.USER_NOT_FOUND, - errorMessage = result.errorDescription, - error = result.error, - correlationId = result.correlationId, - errorCodes = result.errorCodes - ) - } - is SignInCommandResult.InvalidCredentials -> { - Logger.warn( - TAG, - "Sign in received Unexpected result $result" - ) - SignInError( - errorMessage = "unexpected state", - error = "unexpected_state", - correlationId = result.correlationId, - errorCodes = result.errorCodes - ) - } - is INativeAuthCommandResult.Redirect -> { - SignInError( - errorType = ErrorTypes.BROWSER_REQUIRED, - errorMessage = result.errorDescription, - error = result.error, - correlationId = result.correlationId - ) - } - is INativeAuthCommandResult.UnknownError -> { - SignInError( - errorMessage = result.errorDescription, - error = result.error, - correlationId = result.correlationId, - errorCodes = result.errorCodes, - exception = result.exception - ) - } - } - } - } - - interface SignInUsingPasswordCallback : Callback - /** * Sign in the account using username and password; callback variant. * * @param username username of the account to sign in. - * @param password password of the account to sign in. + * @param password (Optional) password of the account to sign in. * @param scopes (Optional) list of scopes to request. - * @param callback [com.microsoft.identity.nativeauth.NativeAuthPublicClientApplication.SignInUsingPasswordCallback] to receive the result. + * @param callback [com.microsoft.identity.nativeauth.NativeAuthPublicClientApplication.SignInCallback] to receive the result. * @return [com.microsoft.identity.nativeauth.statemachine.results.SignInUsingPasswordResult] see detailed possible return state under the object. * @throws MsalClientException if an account is already signed in. */ - override fun signInUsingPassword( + override fun signIn( username: String, - password: CharArray, + password: CharArray?, scopes: List?, - callback: SignInUsingPasswordCallback + callback: SignInCallback ) { LogSession.logMethodCall(TAG, "${TAG}.signIn(username: String, password: String, scopes: List?, callback: SignInUsingPasswordCallback)") pcaScope.launch { try { - val result = signInUsingPassword(username, password, scopes) + val result = signIn(username, password, scopes) callback.onResult(result) } catch (e: MsalException) { Logger.error(TAG, "Exception thrown in signInUsingPassword", e) @@ -414,22 +280,24 @@ class NativeAuthPublicClientApplication( * Sign in the account using username and password; Kotlin coroutines variant. * * @param username username of the account to sign in. - * @param password password of the account to sign in. + * @param password (Optional) password of the account to sign in. * @param scopes (Optional) list of scopes to request. - * @return [com.microsoft.identity.nativeauth.statemachine.results.SignInUsingPasswordResult] see detailed possible return state under the object. + * @return [com.microsoft.identity.nativeauth.statemachine.results.SignInResult] see detailed possible return state under the object. * @throws MsalClientException if an account is already signed in. */ - override suspend fun signInUsingPassword( + override suspend fun signIn( username: String, - password: CharArray, + password: CharArray?, scopes: List? - ): SignInUsingPasswordResult { + ): SignInResult { LogSession.logMethodCall(TAG, "${TAG}.signInUsingPassword") return withContext(Dispatchers.IO) { LogSession.logMethodCall(TAG, "${TAG}.signInUsingPassword.withContext") verifyNoUserIsSignedIn() + val hasPassword = password != null && !password.isEmpty() + val params = CommandParametersAdapter.createSignInStartUsingPasswordCommandParameters( nativeAuthConfig, @@ -443,7 +311,7 @@ class NativeAuthPublicClientApplication( val command = SignInStartCommand( params, NativeAuthMsalController(), - PublicApiId.NATIVE_AUTH_SIGN_IN_WITH_EMAIL_PASSWORD + PublicApiId.NATIVE_AUTH_SIGN_IN_WITH_EMAIL ) val rawCommandResult = CommandDispatcher.submitSilentReturningFuture(command).get() @@ -451,22 +319,32 @@ class NativeAuthPublicClientApplication( return@withContext when (val result = rawCommandResult.checkAndWrapCommandResultType()) { is SignInCommandResult.Complete -> { - val authenticationResult = - AuthenticationResultAdapter.adapt(result.authenticationResult) - - SignInResult.Complete( - resultValue = AccountState.createFromAuthenticationResult( - authenticationResult, - nativeAuthConfig + if (hasPassword) { + val authenticationResult = + AuthenticationResultAdapter.adapt(result.authenticationResult) + + SignInResult.Complete( + resultValue = AccountState.createFromAuthenticationResult( + authenticationResult, + nativeAuthConfig + ) ) - ) + } else { + Logger.warn( + TAG, + "Sign in received unexpected result $result" + ) + SignInError( + errorMessage = "unexpected state", + error = "unexpected_state", + correlationId = "UNSET" + ) + } } is SignInCommandResult.CodeRequired -> { Logger.warn( TAG, - "Sign in with password flow was started, but server requires" + - "a code. Password was not sent to the API; switching to code " + - "authentication." + "Server requires a code" ) SignInResult.CodeRequired( nextState = SignInCodeRequiredState( @@ -480,18 +358,28 @@ class NativeAuthPublicClientApplication( ) } is SignInCommandResult.PasswordRequired -> { - Logger.warn( - TAG, - "Sign in using password received unexpected result $result" - ) - SignInUsingPasswordError( - errorMessage = "unexpected state", - error = "unexpected_state", - correlationId = "UNSET" - ) + if (hasPassword) { + Logger.warn( + TAG, + "Sign in using password received unexpected result $result" + ) + SignInError( + errorMessage = "unexpected state", + error = "unexpected_state", + correlationId = "UNSET" + ) + } else { + SignInResult.PasswordRequired( + nextState = SignInPasswordRequiredState( + continuationToken = result.continuationToken, + scopes = scopes, + config = nativeAuthConfig + ) + ) + } } is SignInCommandResult.UserNotFound -> { - SignInUsingPasswordError( + SignInError( errorType = ErrorTypes.USER_NOT_FOUND, errorMessage = result.errorDescription, error = result.error, @@ -500,16 +388,29 @@ class NativeAuthPublicClientApplication( ) } is SignInCommandResult.InvalidCredentials -> { - SignInUsingPasswordError( - errorType = SignInErrorTypes.INVALID_CREDENTIALS, - errorMessage = result.errorDescription, - error = result.error, - correlationId = result.correlationId, - errorCodes = result.errorCodes - ) + if (hasPassword) { + SignInError( + errorType = SignInErrorTypes.INVALID_CREDENTIALS, + errorMessage = result.errorDescription, + error = result.error, + correlationId = result.correlationId, + errorCodes = result.errorCodes + ) + } else { + Logger.warn( + TAG, + "Sign in received Unexpected result $result" + ) + SignInError( + errorMessage = "unexpected state", + error = "unexpected_state", + correlationId = result.correlationId, + errorCodes = result.errorCodes + ) + } } is INativeAuthCommandResult.Redirect -> { - SignInUsingPasswordError( + SignInError( errorType = ErrorTypes.BROWSER_REQUIRED, errorMessage = result.errorDescription, error = result.error, @@ -517,7 +418,7 @@ class NativeAuthPublicClientApplication( ) } is INativeAuthCommandResult.UnknownError -> { - SignInUsingPasswordError( + SignInError( errorMessage = result.errorDescription, error = result.error, correlationId = result.correlationId, @@ -532,28 +433,29 @@ class NativeAuthPublicClientApplication( } } - interface SignUpUsingPasswordCallback : Callback + + interface SignUpCallback : Callback /** * Sign up the account using username and password; callback variant. * * @param username username of the account to sign up. - * @param password password of the account to sign up. + * @param password (Optional) password of the account to sign up. * @param attributes (Optional) user attributes to be used during account creation - * @param callback [com.microsoft.identity.nativeauth.NativeAuthPublicClientApplication.SignUpUsingPasswordCallback] to receive the result. - * @return [com.microsoft.identity.nativeauth.statemachine.results.SignUpUsingPasswordResult] see detailed possible return state under the object. + * @param callback [com.microsoft.identity.nativeauth.NativeAuthPublicClientApplication.SignUpCallback] to receive the result. + * @return [com.microsoft.identity.nativeauth.statemachine.results.SignUpResult] see detailed possible return state under the object. * @throws MsalClientException if an account is already signed in. */ - override fun signUpUsingPassword( + override fun signUp( username: String, - password: CharArray, + password: CharArray?, attributes: UserAttributes?, - callback: SignUpUsingPasswordCallback + callback: SignUpCallback ) { LogSession.logMethodCall(TAG, "${TAG}.signUpUsingPassword") pcaScope.launch { try { - val result = signUpUsingPassword(username, password, attributes) + val result = signUp(username, password, attributes) callback.onResult(result) } catch (e: MsalException) { Logger.error(TAG, "Exception thrown in signUpUsingPassword", e) @@ -568,16 +470,18 @@ class NativeAuthPublicClientApplication( * @param username username of the account to sign up. * @param password password of the account to sign up. * @param attributes (Optional) user attributes to be used during account creation - * @return [com.microsoft.identity.nativeauth.statemachine.results.SignUpUsingPasswordResult] see detailed possible return state under the object. + * @return [com.microsoft.identity.nativeauth.statemachine.results.SignUpResult] see detailed possible return state under the object. * @throws MsalClientException if an account is already signed in. */ - override suspend fun signUpUsingPassword( + override suspend fun signUp( username: String, - password: CharArray, + password: CharArray?, attributes: UserAttributes? - ): SignUpUsingPasswordResult { + ): SignUpResult { LogSession.logMethodCall(TAG, "${TAG}.signUpUsingPassword(username: String, password: String, attributes: UserAttributes?)") + var hasPassword = password != null && !password.isEmpty() + return withContext(Dispatchers.IO) { val doesAccountExist = checkForPersistedAccount().get() if (doesAccountExist) { @@ -588,18 +492,18 @@ class NativeAuthPublicClientApplication( } val parameters = - CommandParametersAdapter.createSignUpStartUsingPasswordCommandParameters( + CommandParametersAdapter.createSignUpStartCommandParameters( nativeAuthConfig, nativeAuthConfig.oAuth2TokenCache, username, password, - attributes?.toMap() + attributes?.userAttributes ) val command = SignUpStartCommand( parameters, NativeAuthMsalController(), - PublicApiId.NATIVE_AUTH_SIGN_UP_START_WITH_PASSWORD + PublicApiId.NATIVE_AUTH_SIGN_UP_START ) try { @@ -641,8 +545,28 @@ class NativeAuthPublicClientApplication( ) } + is SignUpCommandResult.PasswordRequired -> { + if (hasPassword) { + Logger.warn(TAG, + "Sign up using password received unexpected result $result") + SignUpError( + errorMessage = "Unexpected state", + error = "unexpected_state", + correlationId = "UNSET" + ) + } else { + SignUpResult.PasswordRequired( + nextState = SignUpPasswordRequiredState( + continuationToken = result.continuationToken, + username = username, + config = nativeAuthConfig + ) + ) + } + } + is SignUpCommandResult.AuthNotSupported -> { - SignUpUsingPasswordError( + SignUpError( errorType = SignUpErrorTypes.AUTH_NOT_SUPPORTED, error = result.error, errorMessage = result.errorDescription, @@ -651,16 +575,28 @@ class NativeAuthPublicClientApplication( } is SignUpCommandResult.InvalidPassword -> { - SignUpUsingPasswordError( - errorType = ErrorTypes.INVALID_PASSWORD, - error = result.error, - errorMessage = result.errorDescription, - correlationId = result.correlationId - ) + if (hasPassword) { + SignUpError( + errorType = ErrorTypes.INVALID_PASSWORD, + error = result.error, + errorMessage = result.errorDescription, + correlationId = result.correlationId + ) + } else { + Logger.warn( + TAG, + "Sign up received unexpected result $result" + ) + SignUpError( + error = "unexpected_state", + errorMessage = "Unexpected state", + correlationId = result.correlationId, + ) + } } is SignUpCommandResult.UsernameAlreadyExists -> { - SignUpUsingPasswordError( + SignUpError( errorType = SignUpErrorTypes.USER_ALREADY_EXISTS, error = result.error, errorMessage = result.errorDescription, @@ -669,7 +605,7 @@ class NativeAuthPublicClientApplication( } is SignUpCommandResult.InvalidEmail -> { - SignUpUsingPasswordError( + SignUpError( errorType = SignUpErrorTypes.INVALID_USERNAME, error = result.error, errorMessage = result.errorDescription, @@ -678,7 +614,7 @@ class NativeAuthPublicClientApplication( } is SignUpCommandResult.InvalidAttributes -> { - SignUpUsingPasswordError( + SignUpError( errorType = SignUpErrorTypes.INVALID_ATTRIBUTES, error = result.error, errorMessage = result.errorDescription, @@ -687,7 +623,7 @@ class NativeAuthPublicClientApplication( } is INativeAuthCommandResult.Redirect -> { - SignUpUsingPasswordError( + SignUpError( errorType = ErrorTypes.BROWSER_REQUIRED, error = result.error, errorMessage = result.errorDescription, @@ -696,19 +632,7 @@ class NativeAuthPublicClientApplication( } is INativeAuthCommandResult.UnknownError -> { - SignUpUsingPasswordError( - errorMessage = "Unexpected state", - error = "unexpected_state", - correlationId = "UNSET" - ) - } - - is SignUpCommandResult.PasswordRequired -> { - Logger.warn( - TAG, - "Sign up using password received unexpected result $result" - ) - SignUpUsingPasswordError( + SignUpError( errorMessage = "Unexpected state", error = "unexpected_state", correlationId = "UNSET" @@ -721,189 +645,6 @@ class NativeAuthPublicClientApplication( } } - interface SignUpCallback : Callback - - /** - * Sign up the account starting from a username; callback variant. - * - * @param username username of the account to sign up. - * @param attributes (Optional) user attributes to be used during account creation. - * @param callback [com.microsoft.identity.nativeauth.NativeAuthPublicClientApplication.SignUpCallback] to receive the result. - * @return [com.microsoft.identity.nativeauth.statemachine.results.SignUpResult] see detailed possible return state under the object. - * @throws MsalClientException if an account is already signed in. - */ - override fun signUp( - username: String, - attributes: UserAttributes?, - callback: SignUpCallback - ) { - LogSession.logMethodCall(TAG, "${TAG}.signUp") - - pcaScope.launch { - try { - val result = signUp(username, attributes) - callback.onResult(result) - } catch (e: MsalException) { - Logger.error(TAG, "Exception thrown in signUp", e) - callback.onError(e) - } - } - } - - /** - * Sign up the account starting from a username; Kotlin coroutines variant. - * - * @param username username of the account to sign up. - * @param attributes (Optional) user attributes to be used during account creation. - * @return [com.microsoft.identity.nativeauth.statemachine.results.SignUpResult] see detailed possible return state under the object. - * @throws MsalClientException if an account is already signed in. - */ - override suspend fun signUp( - username: String, - attributes: UserAttributes? - ): SignUpResult { - LogSession.logMethodCall(TAG, "${TAG}.signUp(username: String, attributes: UserAttributes?)") - - return withContext(Dispatchers.IO) { - val doesAccountExist = checkForPersistedAccount().get() - if (doesAccountExist) { - throw MsalClientException( - MsalClientException.INVALID_PARAMETER, - "An account is already signed in." - ) - } - - val parameters = - CommandParametersAdapter.createSignUpStartCommandParameters( - nativeAuthConfig, - nativeAuthConfig.oAuth2TokenCache, - username, - attributes?.userAttributes - ) - - val command = SignUpStartCommand( - parameters, - NativeAuthMsalController(), - PublicApiId.NATIVE_AUTH_SIGN_UP_START - ) - val rawCommandResult = CommandDispatcher.submitSilentReturningFuture(command).get() - - return@withContext when (val result = rawCommandResult.checkAndWrapCommandResultType()) { - is SignUpCommandResult.Complete -> { - SignUpResult.Complete( - nextState = SignInAfterSignUpState( - continuationToken = result.continuationToken, - username = username, - config = nativeAuthConfig - ) - ) - } - - is SignUpCommandResult.AttributesRequired -> { - SignUpResult.AttributesRequired( - nextState = SignUpAttributesRequiredState( - continuationToken = result.continuationToken, - username = username, - config = nativeAuthConfig - ), - requiredAttributes = result.requiredAttributes.toListOfRequiredUserAttribute() - ) - } - - is SignUpCommandResult.CodeRequired -> { - SignUpResult.CodeRequired( - nextState = SignUpCodeRequiredState( - continuationToken = result.continuationToken, - username = username, - config = nativeAuthConfig - ), - codeLength = result.codeLength, - sentTo = result.challengeTargetLabel, - channel = result.challengeChannel, - ) - } - - is SignUpCommandResult.PasswordRequired -> { - SignUpResult.PasswordRequired( - nextState = SignUpPasswordRequiredState( - continuationToken = result.continuationToken, - username = username, - config = nativeAuthConfig - ) - ) - } - - is SignUpCommandResult.UsernameAlreadyExists -> { - SignUpError( - errorType = SignUpErrorTypes.USER_ALREADY_EXISTS, - error = result.error, - errorMessage = result.errorDescription, - correlationId = result.correlationId - ) - } - - is SignUpCommandResult.InvalidEmail -> { - SignUpError( - errorType = SignUpErrorTypes.INVALID_USERNAME, - error = result.error, - errorMessage = result.errorDescription, - correlationId = result.correlationId - ) - } - - is SignUpCommandResult.InvalidAttributes -> { - SignUpError( - errorType = SignUpErrorTypes.INVALID_ATTRIBUTES, - error = result.error, - errorMessage = result.errorDescription, - correlationId = result.correlationId - ) - } - - is INativeAuthCommandResult.Redirect -> { - SignUpError( - errorType = ErrorTypes.BROWSER_REQUIRED, - error = result.error, - errorMessage = result.errorDescription, - correlationId = result.correlationId - ) - } - - is INativeAuthCommandResult.UnknownError -> { - SignUpError( - error = result.error, - errorMessage = result.errorDescription, - correlationId = result.correlationId, - exception = result.exception - ) - } - - is SignUpCommandResult.InvalidPassword -> { - Logger.warn( - TAG, - "Sign up received unexpected result $result" - ) - SignUpError( - error = "unexpected_state", - errorMessage = "Unexpected state", - correlationId = result.correlationId, - ) - } - - is SignUpCommandResult.AuthNotSupported -> { - Logger.warn( - TAG, - "Sign up received unexpected result $result" - ) - SignUpError( - error = "unexpected_state", - errorMessage = "Unexpected state", - correlationId = result.correlationId, - ) - } - } - } - } interface ResetPasswordCallback : Callback diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/SignInErrors.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/SignInErrors.kt index e41f123886..8c9b2d8bea 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/SignInErrors.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/SignInErrors.kt @@ -2,7 +2,6 @@ package com.microsoft.identity.nativeauth.statemachine.errors import com.microsoft.identity.nativeauth.statemachine.results.SignInResult import com.microsoft.identity.nativeauth.statemachine.results.SignInSubmitPasswordResult -import com.microsoft.identity.nativeauth.statemachine.results.SignInUsingPasswordResult /** * SignInErrorTypes class holds the specific error type values that can be returned @@ -38,26 +37,6 @@ open class SignInError( override var exception: Exception? = null ): SignInResult, Error(errorType = errorType, error = error, errorMessage= errorMessage, correlationId = correlationId, errorCodes = errorCodes, exception = exception) { fun isUserNotFound(): Boolean = this.errorType == ErrorTypes.USER_NOT_FOUND -} -/** - * Sign in with password error. The user should use the utility methods of this class - * to identify and handle the error. This error is produced by - * [com.microsoft.identity.nativeauth.INativeAuthPublicClientApplication.signInUsingPassword] - * @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 errorCodes a list of specific error codes returned by the authentication server. - * @param exception an internal unexpected exception that happened. - */ -class SignInUsingPasswordError( - override val errorType: String? = null, - override val error: String? = null, - override val errorMessage: String?, - override val correlationId: String, - override val errorCodes: List? = null, - override var exception: Exception? = null -): SignInUsingPasswordResult, SignInError(errorType = errorType, error = error, errorMessage= errorMessage, correlationId = correlationId, errorCodes = errorCodes, exception = exception) { fun isInvalidCredentials(): Boolean = this.errorType == SignInErrorTypes.INVALID_CREDENTIALS } diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/SignUpErrors.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/SignUpErrors.kt index 6366f30e3e..344acd45c7 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/SignUpErrors.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/errors/SignUpErrors.kt @@ -26,7 +26,6 @@ package com.microsoft.identity.nativeauth.statemachine.errors import com.microsoft.identity.nativeauth.statemachine.results.SignUpResult import com.microsoft.identity.nativeauth.statemachine.results.SignUpSubmitAttributesResult import com.microsoft.identity.nativeauth.statemachine.results.SignUpSubmitPasswordResult -import com.microsoft.identity.nativeauth.statemachine.results.SignUpUsingPasswordResult /** * SignUpErrorTypes class holds the specific error type values that can be returned @@ -86,29 +85,6 @@ open class SignUpError ( fun isInvalidAttributes(): Boolean = this.errorType == SignUpErrorTypes.INVALID_ATTRIBUTES fun isInvalidPassword(): Boolean = this.errorType == ErrorTypes.INVALID_PASSWORD -} - -/** - * Sign up with password error. The user should use the utility methods of this class - * to identify and handle the error. This error is produced by - * [com.microsoft.identity.nativeauth.INativeAuthPublicClientApplication.signUpUsingPassword] - * @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 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 SignUpUsingPasswordError ( - override val errorType: String? = null, - override val error: String? = null, - override val errorMessage: String?, - override val correlationId: String, - override val errorCodes: List? = null, - val subError: String? = null, - override var exception: Exception? = null -): SignUpUsingPasswordResult, SignUpError(errorType = errorType, error = error, errorMessage= errorMessage, correlationId = correlationId, errorCodes = errorCodes, exception = exception) { fun isAuthNotSupported(): Boolean = this.errorType == SignUpErrorTypes.AUTH_NOT_SUPPORTED } diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/results/SignInResult.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/results/SignInResult.kt index b048de0fd9..4898660877 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/results/SignInResult.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/results/SignInResult.kt @@ -43,7 +43,6 @@ interface SignInResult : Result { Result.CompleteResult(resultValue = resultValue), SignInResult, SignInSubmitCodeResult, - SignInUsingPasswordResult, SignInSubmitPasswordResult /** @@ -59,7 +58,7 @@ interface SignInResult : Result { val codeLength: Int, val sentTo: String, val channel: String, - ) : Result.SuccessResult(nextState = nextState), SignInResult, SignInUsingPasswordResult, SignInSubmitPasswordResult + ) : Result.SuccessResult(nextState = nextState), SignInResult, SignInSubmitPasswordResult /** * PasswordRequired Result, which indicates that the valid password is required from the user to continue. @@ -71,12 +70,6 @@ interface SignInResult : Result { ) : SignInResult, Result.SuccessResult(nextState = nextState) } -/** - * Sign in with password result, produced by - * [com.microsoft.identity.nativeauth.INativeAuthPublicClientApplication.signInUsingPassword] - */ -interface SignInUsingPasswordResult : Result - /** * Sign in submit code result, produced by * [com.microsoft.identity.nativeauth.statemachine.states.SignInCodeRequiredState.submitCode] diff --git a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/results/SignUpResult.kt b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/results/SignUpResult.kt index fea316ee1d..b0ee76040c 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/results/SignUpResult.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/statemachine/results/SignUpResult.kt @@ -45,8 +45,7 @@ interface SignUpResult : Result { SignUpResult, SignUpSubmitCodeResult, SignUpSubmitPasswordResult, - SignUpSubmitAttributesResult, - SignUpUsingPasswordResult + SignUpSubmitAttributesResult /** * CodeRequired Result, which indicates a verification code is required from the user to continue. @@ -61,7 +60,7 @@ interface SignUpResult : Result { val codeLength: Int, val sentTo: String, val channel: String - ) : SignUpResult, Result.SuccessResult(nextState = nextState), SignUpUsingPasswordResult + ) : SignUpResult, Result.SuccessResult(nextState = nextState) /** * AttributesRequired Result, which indicates the server requires one or more attributes to be sent, before the account can be created. @@ -74,7 +73,6 @@ interface SignUpResult : Result { val requiredAttributes: List, ) : SignUpResult, Result.SuccessResult(nextState = nextState), - SignUpUsingPasswordResult, SignUpSubmitCodeResult, SignUpSubmitAttributesResult, SignUpSubmitPasswordResult @@ -90,11 +88,6 @@ interface SignUpResult : Result { SignUpSubmitCodeResult } -/** - * Sign up with password result, produced by [com.microsoft.identity.nativeauth.INativeAuthPublicClientApplication.signUpUsingPassword] - */ -interface SignUpUsingPasswordResult : Result - /** * Sign in submit code result, produced by * [com.microsoft.identity.nativeauth.statemachine.states.SignUpCodeRequiredState.submitCode] diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationJavaTest.java b/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationJavaTest.java index b5c5a146f3..8c758f4d7d 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationJavaTest.java +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationJavaTest.java @@ -34,15 +34,12 @@ import com.microsoft.identity.client.e2e.utils.AcquireTokenTestHelper; import com.microsoft.identity.client.exception.MsalClientException; import com.microsoft.identity.client.exception.MsalException; -import com.microsoft.identity.common.java.nativeauth.BuildValues; import com.microsoft.identity.nativeauth.statemachine.errors.GetAccessTokenError; import com.microsoft.identity.nativeauth.statemachine.errors.ResetPasswordError; import com.microsoft.identity.nativeauth.statemachine.errors.ResetPasswordSubmitPasswordError; import com.microsoft.identity.nativeauth.statemachine.errors.SignInError; -import com.microsoft.identity.nativeauth.statemachine.errors.SignInUsingPasswordError; import com.microsoft.identity.nativeauth.statemachine.errors.SignUpError; import com.microsoft.identity.nativeauth.statemachine.errors.SignUpSubmitAttributesError; -import com.microsoft.identity.nativeauth.statemachine.errors.SignUpUsingPasswordError; import com.microsoft.identity.nativeauth.statemachine.errors.SubmitCodeError; import com.microsoft.identity.nativeauth.statemachine.results.GetAccessTokenResult; import com.microsoft.identity.nativeauth.statemachine.results.GetAccountResult; @@ -53,14 +50,12 @@ import com.microsoft.identity.nativeauth.statemachine.results.ResetPasswordSubmitPasswordResult; import com.microsoft.identity.nativeauth.statemachine.results.SignInResult; import com.microsoft.identity.nativeauth.statemachine.results.SignInSubmitCodeResult; -import com.microsoft.identity.nativeauth.statemachine.results.SignInUsingPasswordResult; import com.microsoft.identity.nativeauth.statemachine.results.SignOutResult; import com.microsoft.identity.nativeauth.statemachine.results.SignUpResendCodeResult; import com.microsoft.identity.nativeauth.statemachine.results.SignUpResult; import com.microsoft.identity.nativeauth.statemachine.results.SignUpSubmitAttributesResult; import com.microsoft.identity.nativeauth.statemachine.results.SignUpSubmitCodeResult; import com.microsoft.identity.nativeauth.statemachine.results.SignUpSubmitPasswordResult; -import com.microsoft.identity.nativeauth.statemachine.results.SignUpUsingPasswordResult; import com.microsoft.identity.nativeauth.statemachine.states.AccountState; import com.microsoft.identity.nativeauth.statemachine.states.ResetPasswordCodeRequiredState; import com.microsoft.identity.nativeauth.statemachine.states.ResetPasswordPasswordRequiredState; @@ -205,11 +200,11 @@ public void testSignInScenario1() throws ExecutionException, InterruptedExceptio MockApiResponseType.TOKEN_SUCCESS ); - final ResultFuture signInWithPasswordResult = new ResultFuture<>(); - NativeAuthPublicClientApplication.SignInUsingPasswordCallback signInWithPasswordResultCallback - = new NativeAuthPublicClientApplication.SignInUsingPasswordCallback() { + final ResultFuture signInWithPasswordResult = new ResultFuture<>(); + NativeAuthPublicClientApplication.SignInCallback signInWithPasswordResultCallback + = new NativeAuthPublicClientApplication.SignInCallback() { @Override - public void onResult(SignInUsingPasswordResult result) { + public void onResult(SignInResult result) { signInWithPasswordResult.setResult(result); } @@ -219,7 +214,7 @@ public void onError(@NonNull BaseException exception) { } }; - application.signInUsingPassword( + application.signIn( "user@contoso.com", "password".toCharArray(), null, @@ -266,10 +261,10 @@ public void testSignInScenario4() throws ExecutionException, InterruptedExceptio ); // 1b. Call SDK interface - final ResultFuture signInResult = new ResultFuture<>(); - NativeAuthPublicClientApplication.SignInUsingPasswordCallback callback = new NativeAuthPublicClientApplication.SignInUsingPasswordCallback() { + final ResultFuture signInResult = new ResultFuture<>(); + NativeAuthPublicClientApplication.SignInCallback callback = new NativeAuthPublicClientApplication.SignInCallback() { @Override - public void onResult(SignInUsingPasswordResult result) { + public void onResult(SignInResult result) { signInResult.setResult(result); } @@ -278,12 +273,12 @@ public void onError(@NonNull BaseException exception) { signInResult.setException(exception); } }; - application.signInUsingPassword(username, password, null, callback); + application.signIn(username, password, null, callback); // 1a. Server returns invalid password error - SignInUsingPasswordResult result = signInResult.get(30, TimeUnit.SECONDS); - assertTrue(result instanceof SignInUsingPasswordError); + SignInResult result = signInResult.get(30, TimeUnit.SECONDS); + assertTrue(result instanceof SignInError); - SignInUsingPasswordError error = (SignInUsingPasswordError)result; + SignInError error = (SignInError)result; assertTrue(error.isInvalidCredentials()); } @@ -304,10 +299,10 @@ public void testSignInScenario5() throws ExecutionException, InterruptedExceptio ); // 1b. Call SDK interface - final ResultFuture signInResult = new ResultFuture<>(); - NativeAuthPublicClientApplication.SignInUsingPasswordCallback callback = new NativeAuthPublicClientApplication.SignInUsingPasswordCallback() { + final ResultFuture signInResult = new ResultFuture<>(); + NativeAuthPublicClientApplication.SignInCallback callback = new NativeAuthPublicClientApplication.SignInCallback() { @Override - public void onResult(SignInUsingPasswordResult result) { + public void onResult(SignInResult result) { signInResult.setResult(result); } @@ -316,12 +311,12 @@ public void onError(@NonNull BaseException exception) { signInResult.setException(exception); } }; - application.signInUsingPassword(username, password, null, callback); + application.signIn(username, password, null, callback); // 1a. Server returns invalid user error - SignInUsingPasswordResult result = signInResult.get(30, TimeUnit.SECONDS); - assertTrue(result instanceof SignInUsingPasswordError); + SignInResult result = signInResult.get(30, TimeUnit.SECONDS); + assertTrue(result instanceof SignInError); - SignInUsingPasswordError error = (SignInUsingPasswordError)result; + SignInError error = (SignInError)result; assertTrue(error.isUserNotFound()); } @@ -354,7 +349,7 @@ public void onError(@NonNull BaseException exception) { signInResult.setException(exception); } }; - application.signIn(username, null, callback); + application.signIn(username, null, null, callback); // 1a. Server returns invalid user error SignInResult result = signInResult.get(30, TimeUnit.SECONDS); assertTrue(result instanceof SignInError); @@ -404,7 +399,7 @@ public void onError(@NonNull BaseException exception) { signInResult.setException(exception); } }; - application.signIn(username, null, signInCallback); + application.signIn(username, null, null, signInCallback); // 1a. Server returns invalid user error assertTrue(signInResult.get(30, TimeUnit.SECONDS) instanceof SignInResult.CodeRequired); SignInCodeRequiredState nextState = (((SignInResult.CodeRequired) signInResult.get(30, TimeUnit.SECONDS)).getNextState()); @@ -496,10 +491,10 @@ public void testSignInScenario9() throws ExecutionException, InterruptedExceptio ); // 3c. Call SDK interface - final ResultFuture signInResult = new ResultFuture<>(); - NativeAuthPublicClientApplication.SignInUsingPasswordCallback callback = new NativeAuthPublicClientApplication.SignInUsingPasswordCallback() { + final ResultFuture signInResult = new ResultFuture<>(); + NativeAuthPublicClientApplication.SignInCallback callback = new NativeAuthPublicClientApplication.SignInCallback() { @Override - public void onResult(SignInUsingPasswordResult result) { + public void onResult(SignInResult result) { signInResult.setResult(result); } @@ -508,7 +503,7 @@ public void onError(@NonNull BaseException exception) { signInResult.setException(exception); } }; - application.signInUsingPassword(username, password, null, callback); + application.signIn(username, password, null, callback); // 3d. Server returns InvalidAuthMethodForUser error assertTrue(signInResult.get(30, TimeUnit.SECONDS) instanceof SignInResult.CodeRequired); } @@ -541,10 +536,10 @@ public void testSignInScenario10() throws ExecutionException, InterruptedExcepti ); // 3a. Call SDK interface - final ResultFuture signInResult = new ResultFuture<>(); - NativeAuthPublicClientApplication.SignInUsingPasswordCallback callback = new NativeAuthPublicClientApplication.SignInUsingPasswordCallback() { + final ResultFuture signInResult = new ResultFuture<>(); + NativeAuthPublicClientApplication.SignInCallback callback = new NativeAuthPublicClientApplication.SignInCallback() { @Override - public void onResult(SignInUsingPasswordResult result) { + public void onResult(SignInResult result) { signInResult.setResult(result); } @@ -553,10 +548,10 @@ public void onError(@NonNull BaseException exception) { signInResult.setException(exception); } }; - application.signInUsingPassword(username, password, null, callback); + application.signIn(username, password, null, callback); // 3b. Server returns BrowserRequired error - SignInUsingPasswordResult result = signInResult.get(30, TimeUnit.SECONDS); - assertTrue(result instanceof SignInUsingPasswordError); + SignInResult result = signInResult.get(30, TimeUnit.SECONDS); + assertTrue(result instanceof SignInError); } /** @@ -578,11 +573,11 @@ public void testSignInBlocked() throws ExecutionException, InterruptedException, ); - final ResultFuture signInWithPasswordResult = new ResultFuture<>(); - NativeAuthPublicClientApplication.SignInUsingPasswordCallback signInWithPasswordResultCallback - = new NativeAuthPublicClientApplication.SignInUsingPasswordCallback() { + final ResultFuture signInWithPasswordResult = new ResultFuture<>(); + NativeAuthPublicClientApplication.SignInCallback signInWithPasswordResultCallback + = new NativeAuthPublicClientApplication.SignInCallback() { @Override - public void onResult(SignInUsingPasswordResult result) { + public void onResult(SignInResult result) { signInWithPasswordResult.setResult(result); } @@ -592,7 +587,7 @@ public void onError(@NonNull BaseException exception) { } }; - application.signInUsingPassword( + application.signIn( username, password, null, @@ -602,10 +597,10 @@ public void onError(@NonNull BaseException exception) { assertTrue(signInWithPasswordResult.get(10, TimeUnit.SECONDS) instanceof SignInResult.Complete); ResultFuture signInExceptionResult = new ResultFuture<>(); - NativeAuthPublicClientApplication.SignInUsingPasswordCallback signInWithPasswordResultCallback2 - = new NativeAuthPublicClientApplication.SignInUsingPasswordCallback() { + NativeAuthPublicClientApplication.SignInCallback signInWithPasswordResultCallback2 + = new NativeAuthPublicClientApplication.SignInCallback() { @Override - public void onResult(SignInUsingPasswordResult result) { + public void onResult(SignInResult result) { } @@ -615,7 +610,7 @@ public void onError(@NonNull BaseException exception) { } }; - application.signInUsingPassword(username, password, null, signInWithPasswordResultCallback2); + application.signIn(username, password, null, signInWithPasswordResultCallback2); BaseException exception = signInExceptionResult.get(10, TimeUnit.SECONDS); assertEquals(MsalClientException.INVALID_PARAMETER, exception.getErrorCode()); assertEquals("An account is already signed in.", exception.getMessage()); @@ -645,14 +640,14 @@ public void testSignInSignOutSignIn() throws ExecutionException, InterruptedExce MockApiResponseType.TOKEN_SUCCESS ); - SignInUsingPasswordTestCallback signInUsingPasswordTestCallback = new SignInUsingPasswordTestCallback(); - application.signInUsingPassword( + SignInTestCallback signInTestCallback = new SignInTestCallback(); + application.signIn( username, password, null, - signInUsingPasswordTestCallback + signInTestCallback ); - SignInUsingPasswordResult signInWithPasswordResult = signInUsingPasswordTestCallback.get(); + SignInResult signInWithPasswordResult = signInTestCallback.get(); assertTrue(signInWithPasswordResult instanceof SignInResult.Complete); // Sign out @@ -680,15 +675,15 @@ public void testSignInSignOutSignIn() throws ExecutionException, InterruptedExce MockApiResponseType.TOKEN_SUCCESS ); - SignInUsingPasswordTestCallback signInUsingPasswordTestCallback2 = new SignInUsingPasswordTestCallback(); - application.signInUsingPassword( + SignInTestCallback signInTestCallback2 = new SignInTestCallback(); + application.signIn( username, password, null, - signInUsingPasswordTestCallback2 + signInTestCallback2 ); - SignInUsingPasswordResult signInWithPasswordResult2 = signInUsingPasswordTestCallback2.get(); + SignInResult signInWithPasswordResult2 = signInTestCallback2.get(); assertTrue(signInWithPasswordResult2 instanceof SignInResult.Complete); } @@ -716,14 +711,14 @@ public void testGetAccessToken() throws ExecutionException, InterruptedException MockApiResponseType.TOKEN_SUCCESS ); - SignInUsingPasswordTestCallback signInUsingPasswordTestCallback = new SignInUsingPasswordTestCallback(); - application.signInUsingPassword( + SignInTestCallback signInTestCallback = new SignInTestCallback(); + application.signIn( username, password, null, - signInUsingPasswordTestCallback + signInTestCallback ); - SignInUsingPasswordResult signInWithPasswordResult = signInUsingPasswordTestCallback.get(); + SignInResult signInWithPasswordResult = signInTestCallback.get(); assertTrue(signInWithPasswordResult instanceof SignInResult.Complete); // Get access token from sign in result @@ -776,14 +771,14 @@ public void testSignOutGetAccessToken() throws ExecutionException, InterruptedEx ); // sign in - SignInUsingPasswordTestCallback signInUsingPasswordTestCallback = new SignInUsingPasswordTestCallback(); - application.signInUsingPassword( + SignInTestCallback signInTestCallback = new SignInTestCallback(); + application.signIn( username, password, null, - signInUsingPasswordTestCallback + signInTestCallback ); - SignInUsingPasswordResult signInWithPasswordResult = signInUsingPasswordTestCallback.get(); + SignInResult signInWithPasswordResult = signInTestCallback.get(); assertTrue(signInWithPasswordResult instanceof SignInResult.Complete); // Sign out @@ -827,11 +822,11 @@ public void testSignUpPasswordBlocked() throws ExecutionException, InterruptedEx MockApiResponseType.TOKEN_SUCCESS ); - final ResultFuture signInWithPasswordResult = new ResultFuture<>(); - NativeAuthPublicClientApplication.SignInUsingPasswordCallback signInWithPasswordResultCallback - = new NativeAuthPublicClientApplication.SignInUsingPasswordCallback() { + final ResultFuture signInWithPasswordResult = new ResultFuture<>(); + NativeAuthPublicClientApplication.SignInCallback signInWithPasswordResultCallback + = new NativeAuthPublicClientApplication.SignInCallback() { @Override - public void onResult(SignInUsingPasswordResult result) { + public void onResult(SignInResult result) { signInWithPasswordResult.setResult(result); } @@ -841,7 +836,7 @@ public void onError(@NonNull BaseException exception) { } }; - application.signInUsingPassword( + application.signIn( username, password, null, @@ -851,11 +846,11 @@ public void onError(@NonNull BaseException exception) { assertTrue(signInWithPasswordResult.get(10, TimeUnit.SECONDS) instanceof SignInResult.Complete); ResultFuture signUpExceptionResult = new ResultFuture<>(); - NativeAuthPublicClientApplication.SignUpUsingPasswordCallback signUpWithPasswordResultCallback - = new NativeAuthPublicClientApplication.SignUpUsingPasswordCallback() { + NativeAuthPublicClientApplication.SignUpCallback signUpWithPasswordResultCallback + = new NativeAuthPublicClientApplication.SignUpCallback() { @Override - public void onResult(SignUpUsingPasswordResult result) { + public void onResult(SignUpResult result) { } @@ -864,7 +859,7 @@ public void onError(@NonNull BaseException exception) { signUpExceptionResult.setResult(exception); } }; - application.signUpUsingPassword(username, password, null, signUpWithPasswordResultCallback); + application.signUp(username, password, null, signUpWithPasswordResultCallback); BaseException exception = signUpExceptionResult.get(10, TimeUnit.SECONDS); assertEquals(MsalClientException.INVALID_PARAMETER, exception.getErrorCode()); @@ -895,11 +890,11 @@ public void testSignUpBlocked() throws ExecutionException, InterruptedException, MockApiResponseType.TOKEN_SUCCESS ); - final ResultFuture signInWithPasswordResult = new ResultFuture<>(); - NativeAuthPublicClientApplication.SignInUsingPasswordCallback signInWithPasswordResultCallback - = new NativeAuthPublicClientApplication.SignInUsingPasswordCallback() { + final ResultFuture signInWithPasswordResult = new ResultFuture<>(); + NativeAuthPublicClientApplication.SignInCallback signInWithPasswordResultCallback + = new NativeAuthPublicClientApplication.SignInCallback() { @Override - public void onResult(SignInUsingPasswordResult result) { + public void onResult(SignInResult result) { signInWithPasswordResult.setResult(result); } @@ -909,7 +904,7 @@ public void onError(@NonNull BaseException exception) { } }; - application.signInUsingPassword( + application.signIn( username, password, null, @@ -932,7 +927,7 @@ public void onError(@NonNull BaseException exception) { signUpExceptionResult.setResult(exception); } }; - application.signUp(username, null, signUpWithCodeResultCallback); + application.signUp(username, null, null, signUpWithCodeResultCallback); BaseException exception = signUpExceptionResult.get(10, TimeUnit.SECONDS); assertEquals(MsalClientException.INVALID_PARAMETER, exception.getErrorCode()); @@ -965,15 +960,15 @@ public void testResetPasswordBlocked() throws ExecutionException, InterruptedExc MockApiResponseType.TOKEN_SUCCESS ); - SignInUsingPasswordTestCallback signInWithPasswordResultTestCallback = new SignInUsingPasswordTestCallback(); + SignInTestCallback signInWithPasswordResultTestCallback = new SignInTestCallback(); - application.signInUsingPassword( + application.signIn( username, password, null, signInWithPasswordResultTestCallback ); - SignInUsingPasswordResult signInWithPasswordResult = signInWithPasswordResultTestCallback.get(); + SignInResult signInWithPasswordResult = signInWithPasswordResultTestCallback.get(); assertTrue(signInWithPasswordResult instanceof SignInResult.Complete); @@ -1142,11 +1137,11 @@ public void testSignInEmptyUsernameNoException() throws ExecutionException, Inte ); - final ResultFuture signInWithPasswordResult = new ResultFuture<>(); - NativeAuthPublicClientApplication.SignInUsingPasswordCallback signInWithPasswordResultCallback - = new NativeAuthPublicClientApplication.SignInUsingPasswordCallback() { + final ResultFuture signInWithPasswordResult = new ResultFuture<>(); + NativeAuthPublicClientApplication.SignInCallback signInWithPasswordResultCallback + = new NativeAuthPublicClientApplication.SignInCallback() { @Override - public void onResult(SignInUsingPasswordResult result) { + public void onResult(SignInResult result) { signInWithPasswordResult.setResult(result); } @@ -1156,17 +1151,17 @@ public void onError(@NonNull BaseException exception) { } }; - application.signInUsingPassword( + application.signIn( emptyString, "password".toCharArray(), null, signInWithPasswordResultCallback ); - SignInUsingPasswordResult result = signInWithPasswordResult.get(10, TimeUnit.SECONDS); - assertTrue(result instanceof SignInUsingPasswordError); + SignInResult result = signInWithPasswordResult.get(10, TimeUnit.SECONDS); + assertTrue(result instanceof SignInError); - SignInUsingPasswordError error = (SignInUsingPasswordError)result; + SignInError error = (SignInError)result; assertFalse(error.isBrowserRequired()); assertFalse(error.isUserNotFound()); @@ -1654,10 +1649,10 @@ private SignInAfterSignUpState signUpUser() throws ExecutionException, Interrupt ); // 1b. Call SDK interface - SignUpUsingPasswordTestCallback signUpUsingPasswordTestCallback = new SignUpUsingPasswordTestCallback(); - application.signUpUsingPassword(username, password, null, signUpUsingPasswordTestCallback); + SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); + application.signUp(username, password, null, signUpTestCallback); - SignUpUsingPasswordResult signUpResult = signUpUsingPasswordTestCallback.get(); + SignUpResult signUpResult = signUpTestCallback.get(); assertTrue(signUpResult instanceof SignUpResult.CodeRequired); // 2. submit (valid) code @@ -1705,10 +1700,10 @@ public void testSignUpScenario1() throws ExecutionException, InterruptedExceptio ); // 1b. Call SDK interface - SignUpUsingPasswordTestCallback signUpUsingPasswordTestCallback = new SignUpUsingPasswordTestCallback(); - application.signUpUsingPassword(username, password, null, signUpUsingPasswordTestCallback); + SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); + application.signUp(username, password, null, signUpTestCallback); - SignUpUsingPasswordResult signInResult = signUpUsingPasswordTestCallback.get(); + SignUpResult signInResult = signUpTestCallback.get(); assertTrue(signInResult instanceof SignUpResult.CodeRequired); @@ -1756,10 +1751,10 @@ public void testSignUpScenario2() throws ExecutionException, InterruptedExceptio ); // 1b. Call SDK interface - SignUpUsingPasswordTestCallback signUpUsingPasswordTestCallback = new SignUpUsingPasswordTestCallback(); - application.signUpUsingPassword(username, password, null, signUpUsingPasswordTestCallback); + SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); + application.signUp(username, password, null, signUpTestCallback); - SignUpUsingPasswordResult signUpResult = signUpUsingPasswordTestCallback.get(); + SignUpResult signUpResult = signUpTestCallback.get(); assertTrue(signUpResult instanceof SignUpResult.CodeRequired); @@ -1823,10 +1818,10 @@ public void testSignUpScenario3() throws ExecutionException, InterruptedExceptio ); // 1b. Call SDK interface - SignUpUsingPasswordTestCallback signUpUsingPasswordTestCallback = new SignUpUsingPasswordTestCallback(); - application.signUpUsingPassword(username, password, null, signUpUsingPasswordTestCallback); + SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); + application.signUp(username, password, null, signUpTestCallback); - SignUpUsingPasswordResult signUpResult = signUpUsingPasswordTestCallback.get(); + SignUpResult signUpResult = signUpTestCallback.get(); assertTrue(signUpResult instanceof SignUpResult.CodeRequired); @@ -1865,11 +1860,11 @@ public void testSignUpScenario4() throws ExecutionException, InterruptedExceptio ); // 1b. Call SDK interface - SignUpUsingPasswordTestCallback signUpUsingPasswordTestCallback = new SignUpUsingPasswordTestCallback(); - application.signUpUsingPassword(username, password, null, signUpUsingPasswordTestCallback); + SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); + application.signUp(username, password, null, signUpTestCallback); - assertTrue(signUpUsingPasswordTestCallback.get() instanceof SignUpUsingPasswordError); - assertTrue(((SignUpUsingPasswordError) signUpUsingPasswordTestCallback.get()).isBrowserRequired()); + assertTrue(signUpTestCallback.get() instanceof SignUpError); + assertTrue(((SignUpError) signUpTestCallback.get()).isBrowserRequired()); } /** @@ -1888,11 +1883,11 @@ public void testSignUpScenario5() throws ExecutionException, InterruptedExceptio ); // 1b. Call SDK interface - SignUpUsingPasswordTestCallback signUpUsingPasswordTestCallback = new SignUpUsingPasswordTestCallback(); - application.signUpUsingPassword(username, password, null, signUpUsingPasswordTestCallback); + SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); + application.signUp(username, password, null, signUpTestCallback); - assertTrue(signUpUsingPasswordTestCallback.get() instanceof SignUpUsingPasswordError); - assertTrue(((SignUpUsingPasswordError) signUpUsingPasswordTestCallback.get()).isAuthNotSupported()); + assertTrue(signUpTestCallback.get() instanceof SignUpError); + assertTrue(((SignUpError) signUpTestCallback.get()).isAuthNotSupported()); } /** @@ -1927,10 +1922,10 @@ public void testSignUpScenario6() throws ExecutionException, InterruptedExceptio ); // 1b. Call SDK interface - SignUpUsingPasswordTestCallback signUpUsingPasswordTestCallback = new SignUpUsingPasswordTestCallback(); + SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); - application.signUpUsingPassword(username, password, null, signUpUsingPasswordTestCallback); - SignUpUsingPasswordResult signUpResult = signUpUsingPasswordTestCallback.get(); + application.signUp(username, password, null, signUpTestCallback); + SignUpResult signUpResult = signUpTestCallback.get(); assertTrue(signUpResult instanceof SignUpResult.CodeRequired); @@ -2006,13 +2001,13 @@ public void testSignUpScenario7() throws ExecutionException, InterruptedExceptio // 1b. Call SDK interface UserAttributes invalidAttributes = UserAttributes.Builder.customAttribute("attribute", "valid_attribute").build(); - SignUpUsingPasswordTestCallback signUpUsingPasswordTestCallback = new SignUpUsingPasswordTestCallback(); + SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); - application.signUpUsingPassword(username, password, invalidAttributes, signUpUsingPasswordTestCallback); - SignUpUsingPasswordResult signUpResult = signUpUsingPasswordTestCallback.get(); + application.signUp(username, password, invalidAttributes, signUpTestCallback); + SignUpResult signUpResult = signUpTestCallback.get(); - assertTrue(signUpResult instanceof SignUpUsingPasswordError); - assertTrue(((SignUpUsingPasswordError) signUpResult).isInvalidAttributes()); + assertTrue(signUpResult instanceof SignUpError); + assertTrue(((SignUpError) signUpResult).isInvalidAttributes()); MockApiUtils.configureMockApi( MockApiEndpoint.SignUpStart, @@ -2029,10 +2024,10 @@ public void testSignUpScenario7() throws ExecutionException, InterruptedExceptio // 2b. Call SDK interface again UserAttributes validAttributes = UserAttributes.Builder.customAttribute("attribute", "valid_attribute").build(); - SignUpUsingPasswordTestCallback signUpSuccessCallback = new SignUpUsingPasswordTestCallback(); + SignUpTestCallback signUpSuccessCallback = new SignUpTestCallback(); - application.signUpUsingPassword(username, password, validAttributes, signUpSuccessCallback); - SignUpUsingPasswordResult signUpSuccessResult = signUpSuccessCallback.get(); + application.signUp(username, password, validAttributes, signUpSuccessCallback); + SignUpResult signUpSuccessResult = signUpSuccessCallback.get(); assertTrue(signUpSuccessResult instanceof SignUpResult.CodeRequired); } @@ -2064,7 +2059,7 @@ public void testSignUpScenario8() throws ExecutionException, InterruptedExceptio SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); - application.signUp(username, null, signUpTestCallback); + application.signUp(username, null, null, signUpTestCallback); SignUpResult signUpResult = signUpTestCallback.get(); assertTrue(signUpResult instanceof SignUpResult.CodeRequired); @@ -2118,7 +2113,7 @@ public void testSignUpScenario9() throws ExecutionException, InterruptedExceptio // 1b. Call SDK interface SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); - application.signUp(username, null, signUpTestCallback); + application.signUp(username, null, null, signUpTestCallback); SignUpResult signUpResult = signUpTestCallback.get(); assertTrue(signUpResult instanceof SignUpResult.CodeRequired); @@ -2188,7 +2183,7 @@ public void testSignUpScenario10() throws ExecutionException, InterruptedExcepti // 1b. Call SDK interface SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); - application.signUp(username, null, signUpTestCallback); + application.signUp(username, null, null, signUpTestCallback); SignUpResult signUpResult = signUpTestCallback.get(); @@ -2268,7 +2263,7 @@ public void testSignUpScenario11() throws ExecutionException, InterruptedExcepti // 1b. Call SDK interface SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); - application.signUp(username, null, signUpTestCallback); + application.signUp(username, null, null, signUpTestCallback); SignUpResult signUpResult = signUpTestCallback.get(); assertTrue(signUpResult instanceof SignUpResult.CodeRequired); @@ -2364,7 +2359,7 @@ public void testSignUpScenario12() throws ExecutionException, InterruptedExcepti // 1b. Call SDK interface SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); - application.signUp(username, null, signUpTestCallback); + application.signUp(username, null, null, signUpTestCallback); SignUpResult signUpResult = signUpTestCallback.get(); assertTrue(signUpResult instanceof SignUpResult.CodeRequired); @@ -2461,7 +2456,7 @@ public void testSignUpScenario13() throws ExecutionException, InterruptedExcepti // 1b. Call SDK interface SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); - application.signUp(username, null, signUpTestCallback); + application.signUp(username, null, null, signUpTestCallback); SignUpResult signUpResult = signUpTestCallback.get(); assertTrue(signUpResult instanceof SignUpResult.CodeRequired); @@ -2539,7 +2534,7 @@ public void testSignUpScenario14() throws ExecutionException, InterruptedExcepti SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); - application.signUp(username, null, signUpTestCallback); + application.signUp(username, null, null, signUpTestCallback); SignUpResult signUpResult = signUpTestCallback.get(); assertTrue(signUpResult instanceof SignUpError); @@ -2566,7 +2561,7 @@ public void testSignUpEmptyUsernameNoException() throws ExecutionException, Inte SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); - application.signUp(emptyString, null, signUpTestCallback); + application.signUp(emptyString, null, null, signUpTestCallback); SignUpResult signUpResult = signUpTestCallback.get(); assertTrue(signUpResult instanceof SignUpError); @@ -2587,13 +2582,13 @@ public void testSignUpInvalidPasswordReturnsError() throws ExecutionException, I MockApiResponseType.PASSWORD_TOO_WEAK ); - SignUpUsingPasswordTestCallback signUpUsingPasswordTestCallback = new SignUpUsingPasswordTestCallback(); - application.signUpUsingPassword(username, password, UserAttributes.Builder.customAttribute("password", password.toString()).build(), signUpUsingPasswordTestCallback); + SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); + application.signUp(username, password, UserAttributes.Builder.customAttribute("password", password.toString()).build(), signUpTestCallback); - SignUpUsingPasswordResult signUpResult = signUpUsingPasswordTestCallback.get(); + SignUpResult signUpResult = signUpTestCallback.get(); - assertTrue(signUpResult instanceof SignUpUsingPasswordError); - assertTrue(((SignUpUsingPasswordError) signUpResult).isInvalidPassword()); + assertTrue(signUpResult instanceof SignUpError); + assertTrue(((SignUpError) signUpResult).isInvalidPassword()); } @Test @@ -2606,13 +2601,13 @@ public void testSignUpUsingPasswordInvalidEmailReturnsError() throws ExecutionEx MockApiResponseType.INVALID_USERNAME ); - SignUpUsingPasswordTestCallback signUpUsingPasswordTestCallback = new SignUpUsingPasswordTestCallback(); - application.signUpUsingPassword(invalidUsername, password, UserAttributes.Builder.customAttribute("password", password.toString()).build(), signUpUsingPasswordTestCallback); + SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); + application.signUp(invalidUsername, password, UserAttributes.Builder.customAttribute("password", password.toString()).build(), signUpTestCallback); - SignUpUsingPasswordResult signUpResult = signUpUsingPasswordTestCallback.get(); + SignUpResult signUpResult = signUpTestCallback.get(); - assertTrue(signUpResult instanceof SignUpUsingPasswordError); - assertTrue(((SignUpUsingPasswordError) signUpResult).isInvalidUsername()); + assertTrue(signUpResult instanceof SignUpError); + assertTrue(((SignUpError) signUpResult).isInvalidUsername()); } @Test @@ -2626,7 +2621,7 @@ public void testSignUpInvalidEmailReturnsError() throws ExecutionException, Inte ); SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); - application.signUp(invalidUsername, UserAttributes.Builder.customAttribute("password", password.toString()).build(), signUpTestCallback); + application.signUp(invalidUsername, null, UserAttributes.Builder.customAttribute("password", password.toString()).build(), signUpTestCallback); SignUpResult signUpResult = signUpTestCallback.get(); @@ -2656,28 +2651,15 @@ class SignOutTestCallback extends TestCallback implements Account public void onError(@NonNull BaseException exception) { future.setException(exception); } } -class SignInUsingPasswordTestCallback extends TestCallback implements NativeAuthPublicClientApplication.SignInUsingPasswordCallback { +class SignInTestCallback extends TestCallback implements NativeAuthPublicClientApplication.SignInCallback { @Override - public void onResult(SignInUsingPasswordResult result) { future.setResult(result); } + public void onResult(SignInResult result) { future.setResult(result); } @Override public void onError(@NonNull BaseException exception) { future.setException(exception); } } -class SignUpUsingPasswordTestCallback extends TestCallback implements NativeAuthPublicClientApplication.SignUpUsingPasswordCallback { - - @Override - public void onResult(SignUpUsingPasswordResult result) { - future.setResult(result); - } - - @Override - public void onError(@NonNull BaseException exception) { - future.setException(exception); - } -} - class SignUpTestCallback extends TestCallback implements NativeAuthPublicClientApplication.SignUpCallback { @Override diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationKotlinTest.kt b/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationKotlinTest.kt index a84872e324..0cd4b197e0 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationKotlinTest.kt +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationKotlinTest.kt @@ -25,7 +25,6 @@ package com.microsoft.identity.nativeauth import android.app.Activity import android.content.Context import androidx.test.core.app.ApplicationProvider -import com.microsoft.identity.nativeauth.NativeAuthPublicClientApplication.SignInUsingPasswordCallback import com.microsoft.identity.client.PublicClientApplication import com.microsoft.identity.client.e2e.shadows.ShadowAndroidSdkStorageEncryptionManager import com.microsoft.identity.client.e2e.tests.PublicClientApplicationAbstractTest @@ -36,10 +35,8 @@ import com.microsoft.identity.nativeauth.statemachine.errors.GetAccessTokenError import com.microsoft.identity.nativeauth.statemachine.errors.ResetPasswordError import com.microsoft.identity.nativeauth.statemachine.errors.ResetPasswordSubmitPasswordError import com.microsoft.identity.nativeauth.statemachine.errors.SignInError -import com.microsoft.identity.nativeauth.statemachine.errors.SignInUsingPasswordError import com.microsoft.identity.nativeauth.statemachine.errors.SignUpError import com.microsoft.identity.nativeauth.statemachine.errors.SignUpSubmitAttributesError -import com.microsoft.identity.nativeauth.statemachine.errors.SignUpUsingPasswordError import com.microsoft.identity.nativeauth.statemachine.errors.SubmitCodeError import com.microsoft.identity.nativeauth.statemachine.results.GetAccessTokenResult import com.microsoft.identity.nativeauth.statemachine.results.GetAccountResult @@ -48,7 +45,6 @@ import com.microsoft.identity.nativeauth.statemachine.results.ResetPasswordResul import com.microsoft.identity.nativeauth.statemachine.results.ResetPasswordStartResult import com.microsoft.identity.nativeauth.statemachine.results.ResetPasswordSubmitCodeResult import com.microsoft.identity.nativeauth.statemachine.results.SignInResult -import com.microsoft.identity.nativeauth.statemachine.results.SignInUsingPasswordResult import com.microsoft.identity.nativeauth.statemachine.results.SignOutResult import com.microsoft.identity.nativeauth.statemachine.results.SignUpResendCodeResult import com.microsoft.identity.nativeauth.statemachine.results.SignUpResult @@ -181,7 +177,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr MockApiResponseType.TOKEN_SUCCESS ) - val result = application.signInUsingPassword(username, password) + val result = application.signIn(username, password) assertTrue(result is SignInResult.Complete) } @@ -222,10 +218,10 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr ) // 1b. Call SDK interface - val codeRequiredResult = application.signInUsingPassword(username, password) + val codeRequiredResult = application.signIn(username, password) // 1a. Server returns invalid password error - assertTrue(codeRequiredResult is SignInUsingPasswordError) - assertTrue((codeRequiredResult as SignInUsingPasswordError).isInvalidCredentials()) + assertTrue(codeRequiredResult is SignInError) + assertTrue((codeRequiredResult as SignInError).isInvalidCredentials()) } /** @@ -245,10 +241,10 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr ) // 1b. Call SDK interface - val codeRequiredResult = application.signInUsingPassword(username, password) + val codeRequiredResult = application.signIn(username, password) // 1a. Server returns invalid user error - assertTrue(codeRequiredResult is SignInUsingPasswordError) - assertTrue((codeRequiredResult as SignInUsingPasswordError).isUserNotFound()) + assertTrue(codeRequiredResult is SignInError) + assertTrue((codeRequiredResult as SignInError).isUserNotFound()) } /** @@ -359,11 +355,11 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr responseType = MockApiResponseType.TOKEN_SUCCESS ) - val result = application.signInUsingPassword(username, password) + val result = application.signIn(username, password) assertTrue(result is SignInResult.Complete) try { - application.signInUsingPassword(username, password) + application.signIn(username, password) } catch (exception: MsalException) { assertEquals(MsalClientException.INVALID_PARAMETER, exception.errorCode) assertEquals("An account is already signed in.", exception.message) @@ -471,7 +467,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr responseType = MockApiResponseType.TOKEN_SUCCESS ) - val signInResult = application.signInUsingPassword(username, password) + val signInResult = application.signIn(username, password) assertTrue(signInResult is SignInResult.Complete) val signOutResult = (signInResult as SignInResult.Complete).resultValue.signOut() @@ -495,7 +491,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr responseType = MockApiResponseType.TOKEN_SUCCESS ) - val secondSignInResult = application.signInUsingPassword(username, password) + val secondSignInResult = application.signIn(username, password) assertTrue(secondSignInResult is SignInResult.Complete) } @@ -523,7 +519,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr responseType = MockApiResponseType.TOKEN_SUCCESS ) - val signInResult = application.signInUsingPassword(username, password) + val signInResult = application.signIn(username, password) assertTrue(signInResult is SignInResult.Complete) val accessTokenState = (signInResult as SignInResult.Complete).resultValue.getAccessToken() @@ -568,7 +564,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr responseType = MockApiResponseType.TOKEN_SUCCESS ) - val signInResult = application.signInUsingPassword(username, password) + val signInResult = application.signIn(username, password) assertTrue(signInResult is SignInResult.Complete) val accountState = (signInResult as SignInResult.Complete).resultValue @@ -992,11 +988,11 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr responseType = MockApiResponseType.TOKEN_SUCCESS ) - val result = application.signInUsingPassword(username, password) + val result = application.signIn(username, password) assertTrue(result is SignInResult.Complete) try { - application.signInUsingPassword(username, password) + application.signIn(username, password) } catch (exception: MsalException) { assertEquals(MsalClientException.INVALID_PARAMETER, exception.errorCode) assertEquals("An account is already signed in.", exception.message) @@ -1029,7 +1025,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr responseType = MockApiResponseType.TOKEN_SUCCESS ) - val result = application.signInUsingPassword(username, password) + val result = application.signIn(username, password) assertTrue(result is SignInResult.Complete) try { @@ -1066,7 +1062,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr responseType = MockApiResponseType.TOKEN_SUCCESS ) - val result = application.signInUsingPassword(username, password) + val result = application.signIn(username, password) assertTrue(result is SignInResult.Complete) try { @@ -1123,9 +1119,9 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr ) // 3c. Call SDK interface - val signInResult = ResultFuture() - val callback: SignInUsingPasswordCallback = object : SignInUsingPasswordCallback { - override fun onResult(result: SignInUsingPasswordResult) { + val signInResult = ResultFuture() + val callback: NativeAuthPublicClientApplication.SignInCallback = object : NativeAuthPublicClientApplication.SignInCallback { + override fun onResult(result: SignInResult) { signInResult.setResult(result) } @@ -1133,7 +1129,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr signInResult.setException(exception) } } - application.signInUsingPassword(username, password, null, callback) + application.signIn(username, password, null, callback) // 3d. Server returns InvalidAuthMethodForUser error assertTrue(signInResult[30, TimeUnit.SECONDS] is SignInResult.CodeRequired) } @@ -1171,9 +1167,9 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr ) // 3a. Call SDK interface - val signInResult = ResultFuture() - val callback: SignInUsingPasswordCallback = object : SignInUsingPasswordCallback { - override fun onResult(result: SignInUsingPasswordResult) { + val signInResult = ResultFuture() + val callback: NativeAuthPublicClientApplication.SignInCallback = object : NativeAuthPublicClientApplication.SignInCallback { + override fun onResult(result: SignInResult) { signInResult.setResult(result) } @@ -1181,10 +1177,10 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr signInResult.setException(exception) } } - application.signInUsingPassword(username, password, null, callback) + application.signIn(username, password, null, callback) // 3b. Server returns BrowserRequired error - assertTrue(signInResult[30, TimeUnit.SECONDS] is SignInUsingPasswordError) - val result = signInResult.get() as SignInUsingPasswordError + assertTrue(signInResult[30, TimeUnit.SECONDS] is SignInError) + val result = signInResult.get() as SignInError assertTrue(result.isBrowserRequired()) } @@ -1204,9 +1200,9 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr responseType = MockApiResponseType.TOKEN_SUCCESS ) - val result = application.signInUsingPassword(emptyString, password) - assertTrue(result is SignInUsingPasswordError) - assertTrue((result as SignInUsingPasswordError).errorType == null) + val result = application.signIn(emptyString, password) + assertTrue(result is SignInError) + assertTrue((result as SignInError).errorType == null) } // Helper methods @@ -1232,7 +1228,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr ) // 1b. Call SDK interface - val result = application.signUpUsingPassword(username, password) + val result = application.signUp(username, password) assertTrue(result is SignUpResult.CodeRequired) // 2. submit (valid) code @@ -1275,7 +1271,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr ) // 1b. Call SDK interface - val result = application.signUpUsingPassword(username, password) + val result = application.signUp(username, password) assertTrue(result is SignUpResult.CodeRequired) @@ -1322,7 +1318,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr ) // 1b. Call SDK interface - val result = application.signUpUsingPassword(username, password) + val result = application.signUp(username, password) assertTrue(result is SignUpResult.CodeRequired) @@ -1380,7 +1376,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr ) // 1b. Call SDK interface - val result = application.signUpUsingPassword(username, password) + val result = application.signUp(username, password) assertTrue(result is SignUpResult.CodeRequired) @@ -1415,10 +1411,10 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr ) // 1b. Call SDK interface - val result = application.signUpUsingPassword(username, password) + val result = application.signUp(username, password) - assertTrue(result is SignUpUsingPasswordError) - assertTrue((result as SignUpUsingPasswordError).isBrowserRequired()) + assertTrue(result is SignUpError) + assertTrue((result as SignUpError).isBrowserRequired()) } /** @@ -1437,10 +1433,10 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr ) // 1b. Call SDK interface - val result = application.signUpUsingPassword(username, password) + val result = application.signUp(username, password) - assertTrue(result is SignUpUsingPasswordError) - assertTrue((result as SignUpUsingPasswordError).isAuthNotSupported()) + assertTrue(result is SignUpError) + assertTrue((result as SignUpError).isAuthNotSupported()) } /** @@ -1475,7 +1471,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr ) // 1b. Call SDK interface - val result = application.signUpUsingPassword(username, password) + val result = application.signUp(username, password) assertTrue(result is SignUpResult.CodeRequired) @@ -1541,10 +1537,10 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr // 1b. Call SDK interface val invalidAttributes = UserAttributes.Builder.customAttribute("attribute", "invalid_attribute").build() - val invalidAttributesResult = application.signUpUsingPassword(username, password, invalidAttributes) + val invalidAttributesResult = application.signUp(username, password, invalidAttributes) - assertTrue(invalidAttributesResult is SignUpUsingPasswordError) - assertTrue((invalidAttributesResult as SignUpUsingPasswordError).isInvalidAttributes()) + assertTrue(invalidAttributesResult is SignUpError) + assertTrue((invalidAttributesResult as SignUpError).isInvalidAttributes()) configureMockApi( endpointType = MockApiEndpoint.SignUpStart, @@ -1560,7 +1556,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr // 2b. Call SDK interface again val validAttributes = UserAttributes.Builder.customAttribute("attribute", "valid_attribute").build() - val result = application.signUpUsingPassword(username, password, validAttributes) + val result = application.signUp(username, password, validAttributes) assertTrue(result is SignUpResult.CodeRequired) } @@ -2028,9 +2024,9 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr responseType = MockApiResponseType.PASSWORD_TOO_WEAK ) - val result = application.signUpUsingPassword(username, password) - assertTrue(result is SignUpUsingPasswordError) - assertTrue((result as SignUpUsingPasswordError).isInvalidPassword()) + val result = application.signUp(username, password) + assertTrue(result is SignUpError) + assertTrue((result as SignUpError).isInvalidPassword()) } @Test @@ -2043,9 +2039,9 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr responseType = MockApiResponseType.INVALID_USERNAME ) - val result = application.signUpUsingPassword(invalidUsername, password) - assertTrue(result is SignUpUsingPasswordError) - assertTrue((result as SignUpUsingPasswordError).isInvalidUsername()) + val result = application.signUp(invalidUsername, password) + assertTrue(result is SignUpError) + assertTrue((result as SignUpError).isInvalidUsername()) } @Test From 87f57e7e6f9b771a0174e2fbd7486565283d5392 Mon Sep 17 00:00:00 2001 From: Saurabh Gautam Date: Thu, 18 Jan 2024 12:20:55 +0000 Subject: [PATCH 2/8] Update submodule pointer --- common | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common b/common index 389106f400..05a8886aa6 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 389106f40065229246aace1b3e57a4801cb92ad6 +Subproject commit 05a8886aa6b3c4c702d5662a49ebb89cc23283e8 From e06b11ab944e46161ac1eb58d82648d9c7d45a8a Mon Sep 17 00:00:00 2001 From: Saurabh Gautam Date: Thu, 18 Jan 2024 15:22:33 +0000 Subject: [PATCH 3/8] Remove the usage of usingpassword phrase --- .../internal/CommandParametersAdapter.java | 2 +- .../INativeAuthPublicClientApplication.kt | 8 ++++---- .../NativeAuthPublicClientApplication.kt | 18 +++++++++--------- ...iveAuthPublicClientApplicationJavaTest.java | 18 +++++++++--------- ...iveAuthPublicClientApplicationKotlinTest.kt | 16 ++++++++-------- 5 files changed, 31 insertions(+), 31 deletions(-) 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 e67eb46756..559248b2e5 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 @@ -554,7 +554,7 @@ public static SignUpSubmitPasswordCommandParameters createSignUpSubmitPasswordCo * @return Command parameter object * @throws ClientException */ - public static SignInStartCommandParameters createSignInStartUsingPasswordCommandParameters( + public static SignInStartCommandParameters createSignInStartCommandParameters( @NonNull final NativeAuthPublicClientApplicationConfiguration configuration, @NonNull final OAuth2TokenCache tokenCache, @NonNull final String username, 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 a9b433a985..779693ebea 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/INativeAuthPublicClientApplication.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/INativeAuthPublicClientApplication.kt @@ -69,7 +69,7 @@ interface INativeAuthPublicClientApplication : IPublicClientApplication { * Sign in a user with a given username; Kotlin coroutines variant. * * @param username username of the account to sign in. - * @param password password of the account to sign in. + * @param password (Optional) password of the account to sign in. * @param scopes (Optional) scopes to request during the sign in. * @return [com.microsoft.identity.nativeauth.statemachine.results.SignInResult] see detailed possible return state under the object. * @throws [MsalException] if an account is already signed in. @@ -80,7 +80,7 @@ interface INativeAuthPublicClientApplication : IPublicClientApplication { * Sign in a user with a given username; callback variant. * * @param username username of the account to sign in. - * @param password password of the account to sign in. + * @param password (Optional) password of the account to sign in. * @param scopes (Optional) scopes to request during the sign in. * @param callback [com.microsoft.identity.nativeauth.NativeAuthPublicClientApplication.SignInCallback] to receive the result. * @return [com.microsoft.identity.nativeauth.statemachine.results.SignInResult] see detailed possible return state under the object. @@ -92,7 +92,7 @@ interface INativeAuthPublicClientApplication : IPublicClientApplication { * Sign up the account starting from a username; Kotlin coroutines variant. * * @param username username of the account to sign up. - * @param password password of the account to sign up. + * @param password (Optional) password of the account to sign up. * @param attributes (Optional) user attributes to be used during account creation. * @return [com.microsoft.identity.nativeauth.statemachine.results.SignUpResult] see detailed possible return state under the object. * @throws MsalClientException if an account is already signed in. @@ -103,7 +103,7 @@ interface INativeAuthPublicClientApplication : IPublicClientApplication { * Sign up the account starting from a username; callback variant. * * @param username username of the account to sign up. - * @param password password of the account to sign up. + * @param password (Optional) password of the account to sign up. * @param attributes (Optional) user attributes to be used during account creation. * @param callback [com.microsoft.identity.nativeauth.NativeAuthPublicClientApplication.SignUpCallback] to receive the result. * @return [com.microsoft.identity.nativeauth.statemachine.results.SignUpResult] see detailed possible return state under the object. 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 b1c89d4cce..40476a34b2 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt @@ -255,7 +255,7 @@ class NativeAuthPublicClientApplication( * @param password (Optional) password of the account to sign in. * @param scopes (Optional) list of scopes to request. * @param callback [com.microsoft.identity.nativeauth.NativeAuthPublicClientApplication.SignInCallback] to receive the result. - * @return [com.microsoft.identity.nativeauth.statemachine.results.SignInUsingPasswordResult] see detailed possible return state under the object. + * @return [com.microsoft.identity.nativeauth.statemachine.results.SignInResult] see detailed possible return state under the object. * @throws MsalClientException if an account is already signed in. */ override fun signIn( @@ -264,13 +264,13 @@ class NativeAuthPublicClientApplication( scopes: List?, callback: SignInCallback ) { - LogSession.logMethodCall(TAG, "${TAG}.signIn(username: String, password: String, scopes: List?, callback: SignInUsingPasswordCallback)") + LogSession.logMethodCall(TAG, "${TAG}.signIn") pcaScope.launch { try { val result = signIn(username, password, scopes) callback.onResult(result) } catch (e: MsalException) { - Logger.error(TAG, "Exception thrown in signInUsingPassword", e) + Logger.error(TAG, "Exception thrown in signIn", e) callback.onError(e) } } @@ -290,16 +290,16 @@ class NativeAuthPublicClientApplication( password: CharArray?, scopes: List? ): SignInResult { - LogSession.logMethodCall(TAG, "${TAG}.signInUsingPassword") + LogSession.logMethodCall(TAG, "${TAG}.signIn") return withContext(Dispatchers.IO) { - LogSession.logMethodCall(TAG, "${TAG}.signInUsingPassword.withContext") + LogSession.logMethodCall(TAG, "${TAG}.signIn.withContext") verifyNoUserIsSignedIn() val hasPassword = password != null && !password.isEmpty() val params = - CommandParametersAdapter.createSignInStartUsingPasswordCommandParameters( + CommandParametersAdapter.createSignInStartCommandParameters( nativeAuthConfig, nativeAuthConfig.oAuth2TokenCache, username, @@ -452,13 +452,13 @@ class NativeAuthPublicClientApplication( attributes: UserAttributes?, callback: SignUpCallback ) { - LogSession.logMethodCall(TAG, "${TAG}.signUpUsingPassword") + LogSession.logMethodCall(TAG, "${TAG}.signUp") pcaScope.launch { try { val result = signUp(username, password, attributes) callback.onResult(result) } catch (e: MsalException) { - Logger.error(TAG, "Exception thrown in signUpUsingPassword", e) + Logger.error(TAG, "Exception thrown in signUp", e) callback.onError(e) } } @@ -478,7 +478,7 @@ class NativeAuthPublicClientApplication( password: CharArray?, attributes: UserAttributes? ): SignUpResult { - LogSession.logMethodCall(TAG, "${TAG}.signUpUsingPassword(username: String, password: String, attributes: UserAttributes?)") + LogSession.logMethodCall(TAG, "${TAG}.signUp") var hasPassword = password != null && !password.isEmpty() diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationJavaTest.java b/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationJavaTest.java index 8c758f4d7d..7bd80faa3f 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationJavaTest.java +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationJavaTest.java @@ -1676,7 +1676,7 @@ private SignInAfterSignUpState signUpUser() throws ExecutionException, Interrupt /** * Test Sign Up scenario 1: - * 1a -> signUpUsingPassword + * 1a -> signUp * 1b <- server requires code verification * 2a -> submit valid code * 2b <- sign up succeeds @@ -1725,7 +1725,7 @@ public void testSignUpScenario1() throws ExecutionException, InterruptedExceptio /** * Test Sign Up scenario 2: - * 1a -> signUpUsingPassword + * 1a -> signUp * 1b <- server requires code verification * 2a -> user prompts resend code, challenge endpoint is called * 2b <- codeRequired is returned @@ -1794,7 +1794,7 @@ public void testSignUpScenario2() throws ExecutionException, InterruptedExceptio /** * Test Sign Up scenario 3: - * 1a -> signUpUsingPassword + * 1a -> signUp * 1b <- server requires code verification * 2a -> submit valid code * 3a <- sign up token has expired, server returns token expired error @@ -1846,7 +1846,7 @@ public void testSignUpScenario3() throws ExecutionException, InterruptedExceptio /** * Test Sign Up scenario 4: - * 1a -> signUpUsingPassword + * 1a -> signUp * 1b <- server does not support password authentication */ @Test @@ -1869,7 +1869,7 @@ public void testSignUpScenario4() throws ExecutionException, InterruptedExceptio /** * Test Sign Up scenario 5: - * 1a -> signUpUsingPassword + * 1a -> signUp * 1b <- server does not support password authentication, returns error to use OOB instead */ @Test @@ -1892,7 +1892,7 @@ public void testSignUpScenario5() throws ExecutionException, InterruptedExceptio /** * Test Sign Up scenario 6: - * 1a -> signUpUsingPassword + * 1a -> signUp * 1b <- server requires code verification * 2a -> user prompts resend code, challenge endpoint is called * 2b <- codeRequired is returned @@ -1985,9 +1985,9 @@ public void testSignUpScenario6() throws ExecutionException, InterruptedExceptio /** * Test Sign Up scenario 7: - * 1a -> signUpUsingPassword with invalid custom attributes + * 1a -> signUp with invalid custom attributes * 1b <- server returns invalid attribute error - * 2a -> call signUpUsingPassword with correct attributes + * 2a -> call signUp with correct attributes * 2b <- server returns code required, flow continues */ @Test @@ -2592,7 +2592,7 @@ public void testSignUpInvalidPasswordReturnsError() throws ExecutionException, I } @Test - public void testSignUpUsingPasswordInvalidEmailReturnsError() throws ExecutionException, InterruptedException, TimeoutException { + public void testSignUpInvalidEmailReturnsError() throws ExecutionException, InterruptedException, TimeoutException { String correlationId = UUID.randomUUID().toString(); MockApiUtils.configureMockApi( diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationKotlinTest.kt b/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationKotlinTest.kt index 0cd4b197e0..ff37c23b19 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationKotlinTest.kt +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationKotlinTest.kt @@ -1247,7 +1247,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr /** * Test Sign Up scenario 1: - * 1a -> signUpUsingPassword + * 1a -> signUp * 1b <- server requires code verification * 2a -> submit valid code * 2b <- sign up succeeds @@ -1292,7 +1292,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr /** * Test Sign Up scenario 2: - * 1a -> signUpUsingPassword + * 1a -> signUp * 1b <- server requires code verification * 2a -> user prompts resend code, challenge endpoint is called * 2b <- codeRequired is returned @@ -1352,7 +1352,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr /** * Test Sign Up scenario 3: - * 1a -> signUpUsingPassword + * 1a -> signUp * 1b <- server requires code verification * 2a -> submit valid code * 3a <- sign up token has expired, server returns token expired error @@ -1397,7 +1397,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr /** * Test Sign Up scenario 4: - * 1a -> signUpUsingPassword + * 1a -> signUp * 1b <- server does not support password authentication */ @Test @@ -1419,7 +1419,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr /** * Test Sign Up scenario 5: - * 1a -> signUpUsingPassword + * 1a -> signUp * 1b <- server does not support password authentication, returns error to use OOB instead */ @Test @@ -1441,7 +1441,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr /** * Test Sign Up scenario 6: - * 1a -> signUpUsingPassword + * 1a -> signUp * 1b <- server requires code verification * 2a -> user prompts resend code, challenge endpoint is called * 2b <- codeRequired is returned @@ -1520,7 +1520,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr /** * Test Sign Up scenario 7: - * 1a -> signUpUsingPassword with invalid custom attributes + * 1a -> signUp with invalid custom attributes * 1b <- server returns invalid attribute error * 2a -> call signUpWithPassword with correct attributes * 2b <- server returns code required, flow continues @@ -2030,7 +2030,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr } @Test - fun testSignUpUsingPasswordInvalidEmailReturnsError() = runTest { + fun testSignUpInvalidEmailReturnsError() = runTest { val correlationId = UUID.randomUUID().toString() configureMockApi( From 271c3161f5c7a5be92970e6319c16d2494e0089e Mon Sep 17 00:00:00 2001 From: Saurabh Gautam Date: Thu, 18 Jan 2024 16:20:50 +0000 Subject: [PATCH 4/8] Test failures --- .../nativeauth/NativeAuthPublicClientApplicationJavaTest.java | 2 +- .../nativeauth/NativeAuthPublicClientApplicationKotlinTest.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationJavaTest.java b/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationJavaTest.java index 7bd80faa3f..8b4f498238 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationJavaTest.java +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationJavaTest.java @@ -2592,7 +2592,7 @@ public void testSignUpInvalidPasswordReturnsError() throws ExecutionException, I } @Test - public void testSignUpInvalidEmailReturnsError() throws ExecutionException, InterruptedException, TimeoutException { + public void testSignUpWithPasswordInvalidEmailReturnsError() throws ExecutionException, InterruptedException, TimeoutException { String correlationId = UUID.randomUUID().toString(); MockApiUtils.configureMockApi( diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationKotlinTest.kt b/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationKotlinTest.kt index ff37c23b19..e09172e7c8 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationKotlinTest.kt +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationKotlinTest.kt @@ -2030,7 +2030,7 @@ class NativeAuthPublicClientApplicationKotlinTest : PublicClientApplicationAbstr } @Test - fun testSignUpInvalidEmailReturnsError() = runTest { + fun testSignUpWithPasswordInvalidEmailReturnsError() = runTest { val correlationId = UUID.randomUUID().toString() configureMockApi( From 829a9a23408957e3235fee724133d491683de889 Mon Sep 17 00:00:00 2001 From: Saurabh Gautam Date: Thu, 18 Jan 2024 17:22:54 +0000 Subject: [PATCH 5/8] Delete new files --- .../NativeAuthPublicClientAppKotlinTest.java | 150 -------------- .../NativeAuthPublicClientApplicationTest.kt | 184 ------------------ 2 files changed, 334 deletions(-) delete mode 100644 msal/src/androidTest/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientAppKotlinTest.java delete mode 100644 msal/src/androidTest/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationTest.kt diff --git a/msal/src/androidTest/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientAppKotlinTest.java b/msal/src/androidTest/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientAppKotlinTest.java deleted file mode 100644 index 961ad5a6c8..0000000000 --- a/msal/src/androidTest/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientAppKotlinTest.java +++ /dev/null @@ -1,150 +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; - -import android.content.Context; - -import androidx.test.core.app.ApplicationProvider; -import androidx.test.ext.junit.runners.AndroidJUnit4; - -import com.microsoft.identity.client.AndroidTestUtil; -import com.microsoft.identity.client.PublicClientApplication; -import com.microsoft.identity.client.PublicClientApplicationTest; -import com.microsoft.identity.client.exception.MsalClientException; -import com.microsoft.identity.client.exception.MsalException; -import com.microsoft.identity.common.java.net.HttpUrlConnectionFactory; -import com.microsoft.identity.msal.test.R; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; - -import java.util.Arrays; -import java.util.List; - -import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.fail; - -/** - * Tests for {@link com.microsoft.identity.client.PublicClientApplication}. - */ -@RunWith(AndroidJUnit4.class) -public final class NativeAuthPublicClientAppKotlinTest { - private Context mAppContext; - private static final String CLIENT_ID = "client-id"; - private static final String CIAM_AUTHORITY = "https://msidlabciam1.ciamlogin.com/msidlabciam1.onmicrosoft.com"; - private final List CHALLENGE_TYPES = Arrays.asList("oob", "password"); - - @Before - public void setUp() { - System.setProperty( - "dexmaker.dexcache", - androidx.test.platform.app.InstrumentationRegistry - .getInstrumentation() - .getTargetContext() - .getCacheDir() - .getPath() - ); - - System.setProperty( - "org.mockito.android.target", - ApplicationProvider - .getApplicationContext() - .getCacheDir() - .getPath() - ); - - mAppContext = androidx.test.platform.app.InstrumentationRegistry.getInstrumentation().getContext().getApplicationContext(); - } - - @After - public void tearDown() { - HttpUrlConnectionFactory.clearMockedConnectionQueue(); - AndroidTestUtil.removeAllTokens(mAppContext); - } - - @Test - public void testNativeAuthConstructor() { - final Context context = new PublicClientApplicationTest.MockContext(mAppContext); - PublicClientApplicationTest.mockPackageManagerWithDefaultFlag(context, mAppContext); - PublicClientApplicationTest.mockHasCustomTabRedirect(context); - - try { - final INativeAuthPublicClientApplication app = PublicClientApplication.createNativeAuthPublicClientApplication( - context, - R.raw.test_msal_config_native_auth - ); - Assert.assertNotNull(app); - } catch (InterruptedException e) { - Assert.fail(e.getMessage()); - } catch (MsalException e) { - Assert.fail(e.getMessage()); - } - } - - @Test - public void testNativeAuthConstructorNoMetadata() { - final Context context = new PublicClientApplicationTest.MockContext(mAppContext); - PublicClientApplicationTest.mockPackageManagerWithDefaultFlag(context, mAppContext); - PublicClientApplicationTest.mockHasCustomTabRedirect(context); - - try { - final INativeAuthPublicClientApplication app = PublicClientApplication.createNativeAuthPublicClientApplication( - context, - CLIENT_ID, - CIAM_AUTHORITY, - null, - CHALLENGE_TYPES - ); - Assert.assertNotNull(app); - } catch (InterruptedException e) { - Assert.fail(e.getMessage()); - } catch (MsalException e) { - Assert.fail(e.getMessage()); - } - } - - @Test - public void testFailingNativeAuthConstructor() { - final Context context = new PublicClientApplicationTest.MockContext(mAppContext); - PublicClientApplicationTest.mockPackageManagerWithDefaultFlag(context, mAppContext); - PublicClientApplicationTest.mockHasCustomTabRedirect(context); - - // Should fail, as we are attempting to create a multiple account application - try { - final INativeAuthPublicClientApplication app = PublicClientApplication.createNativeAuthPublicClientApplication( - context, - R.raw.test_msal_config_multiple_account - ); - Assert.fail("Unintentionally created app"); - } catch (InterruptedException e) { - Assert.fail(e.getMessage()); - } catch (MsalException e) { - Assert.assertEquals("Expecting error.", - e.getErrorCode(), - MsalClientException.NATIVE_AUTH_INVALID_ACCOUNT_MODE_CONFIG_ERROR_CODE); - } - } -} \ No newline at end of file diff --git a/msal/src/androidTest/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationTest.kt b/msal/src/androidTest/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationTest.kt deleted file mode 100644 index cbecd88cbd..0000000000 --- a/msal/src/androidTest/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationTest.kt +++ /dev/null @@ -1,184 +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 - -import android.content.Context -import androidx.test.core.app.ApplicationProvider -import androidx.test.ext.junit.runners.AndroidJUnit4 -import androidx.test.platform.app.InstrumentationRegistry -import com.microsoft.identity.client.AndroidTestUtil -import com.microsoft.identity.client.PublicClientApplication -import com.microsoft.identity.client.exception.MsalClientException -import com.microsoft.identity.client.exception.MsalException -import com.microsoft.identity.common.java.net.HttpUrlConnectionFactory -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.runBlocking -import org.junit.After -import org.junit.Assert -import org.junit.Before -import org.junit.Test -import org.junit.runner.RunWith -import java.util.Arrays - -@ExperimentalCoroutinesApi -@RunWith(AndroidJUnit4::class) -class NativeAuthPublicClientAppKotlinTest { - private lateinit var context: Context - private val CLIENT_ID = "1234" - // The general format is https://.ciamlogin.com/.onmicrosoft.com - // See details here: https://microsoft.sharepoint-df.com/:w:/t/AADIEFtogether/ES9p_7m-qUtEuINcd0UlyekBh6TrWtrMJr12WS_O7BAgww?e=HQybBw - private val CIAM_AUTHORITY = "https://msidlabciam1.ciamlogin.com/msidlabciam1.onmicrosoft.com" - private val B2C_AUTHORITY = "https://msidlabb2c.b2clogin.com/tfp/msidlabb2c.onmicrosoft.com/b2c_1_ropc_auth/" - private val INVALID_AUTHORITY = "https://b2clogin.com" - private val EMPTY_STRING = "" - // TODO: Replace the link with https://learn.microsoft.com/ when the document about OAuth 2.0 Direct Interaction Grants is published. - // The definitions and scopes in OAuth 2.0 Direct Interaction Grants(3.4Challenge Types): - // https://aaronpk.github.io/oauth-direct-interaction-grant/draft-parecki-oauth-direct-interaction-grant.html#name-authorization-grants - private val challengeTypes = Arrays.asList("oob", "password") - - @Before - fun setup() { - System.setProperty( - "dexmaker.dexcache", - InstrumentationRegistry - .getInstrumentation() - .targetContext - .cacheDir - .path - ) - - System.setProperty( - "org.mockito.android.target", - ApplicationProvider - .getApplicationContext() - .cacheDir - .path - ) - - context = InstrumentationRegistry.getInstrumentation().context.applicationContext - } - - @After - fun tearDown() { - HttpUrlConnectionFactory.clearMockedConnectionQueue() - AndroidTestUtil.removeAllTokens(context) - } - - @Test - fun testWorkingInit() { - runBlocking { - try { - val app = PublicClientApplication.createNativeAuthPublicClientApplication( - context, - CLIENT_ID, - CIAM_AUTHORITY, - null, - challengeTypes - ) - Assert.assertNotNull("NAPCA can't be null", app) - } catch (exception: MsalException) { - Assert.fail(exception.message) - } - } - } - - @Test - fun testEmptyClientIdReturnsOnError() { - runBlocking { - try { - val app = PublicClientApplication.createNativeAuthPublicClientApplication( - context, - EMPTY_STRING, // Invalid parameters: empty client id should throw exception - CIAM_AUTHORITY, - null, - challengeTypes - ) - Assert.fail("NAPCA creation did not throw exception") - } catch (exception: IllegalArgumentException) { - return@runBlocking - } - Assert.fail("Empty client Id string should return error") - } - } - - @Test - fun testEmptyAuthorityReturnsOnError() { - runBlocking { - try { - val app = PublicClientApplication.createNativeAuthPublicClientApplication( - context, - CLIENT_ID, - EMPTY_STRING, // Invalid parameters: empty authority should throw exception - null, - challengeTypes - ) - Assert.fail("NAPCA creation did not throw exception") - } catch (exception: IllegalArgumentException) { - return@runBlocking - } - Assert.fail("Empty client Id string should return error") - } - } - - @Test - fun testB2CAuthorityReturnsOnError() { - runBlocking { - try { - val app = PublicClientApplication.createNativeAuthPublicClientApplication( - context, - CLIENT_ID, - B2C_AUTHORITY, - null, - challengeTypes - ) - Assert.fail("NAPCA creation did not throw exception") - } catch (exception: MsalException) { - // warp NATIVE_AUTH_INVALID_CIAM_AUTHORITY_ERROR into UNKNOWN_ERROR in catch block - if (exception.errorCode == MsalClientException.UNKNOWN_ERROR) - return@runBlocking - } - Assert.fail("B2C authority should return error") - } - } - - @Test - fun testInvalidAuthorityReturnsOnError() { - runBlocking { - try { - val app = PublicClientApplication.createNativeAuthPublicClientApplication( - context, - CLIENT_ID, - INVALID_AUTHORITY, - null, - challengeTypes - ) - Assert.fail("NAPCA creation did not throw exception") - } catch (exception: MsalException) { - // warp NATIVE_AUTH_INVALID_CIAM_AUTHORITY_ERROR into UNKNOWN_ERROR in catch block - if (exception.errorCode == MsalClientException.UNKNOWN_ERROR) - return@runBlocking - } - Assert.fail("Invalid authority string should return error") - } - } -} From f3c9795f7939fdd3f0b9d738719ef6c0963e2819 Mon Sep 17 00:00:00 2001 From: Saurabh Gautam Date: Mon, 22 Jan 2024 12:50:17 +0000 Subject: [PATCH 6/8] Code review comments --- .../nativeauth/NativeAuthPublicClientApplication.kt | 4 ++-- .../NativeAuthPublicClientApplicationJavaTest.java | 6 +++--- 2 files changed, 5 insertions(+), 5 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 ed999bbac6..938ee4351e 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt @@ -296,7 +296,7 @@ class NativeAuthPublicClientApplication( verifyNoUserIsSignedIn() - val hasPassword = password != null && !password.isEmpty() + val hasPassword = password?.isNotEmpty() == true val params = CommandParametersAdapter.createSignInStartCommandParameters( @@ -480,7 +480,7 @@ class NativeAuthPublicClientApplication( ): SignUpResult { LogSession.logMethodCall(TAG, "${TAG}.signUp") - var hasPassword = password != null && !password.isEmpty() + var hasPassword = password?.isNotEmpty() == true return withContext(Dispatchers.IO) { val doesAccountExist = checkForPersistedAccount().get() diff --git a/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationJavaTest.java b/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationJavaTest.java index e3000d43a6..8493da8cdb 100644 --- a/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationJavaTest.java +++ b/msal/src/test/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplicationJavaTest.java @@ -2672,7 +2672,7 @@ public void testSignUpInvalidPasswordReturnsError() throws ExecutionException, I ); SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); - application.signUp(username, password, UserAttributes.Builder.customAttribute("password", password.toString()).build(), signUpTestCallback); + application.signUp(username, password, null, signUpTestCallback); SignUpResult signUpResult = signUpTestCallback.get(); @@ -2691,7 +2691,7 @@ public void testSignUpWithPasswordInvalidEmailReturnsError() throws ExecutionExc ); SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); - application.signUp(invalidUsername, password, UserAttributes.Builder.customAttribute("password", password.toString()).build(), signUpTestCallback); + application.signUp(invalidUsername, password, null, signUpTestCallback); SignUpResult signUpResult = signUpTestCallback.get(); @@ -2710,7 +2710,7 @@ public void testSignUpInvalidEmailReturnsError() throws ExecutionException, Inte ); SignUpTestCallback signUpTestCallback = new SignUpTestCallback(); - application.signUp(invalidUsername, null, UserAttributes.Builder.customAttribute("password", password.toString()).build(), signUpTestCallback); + application.signUp(invalidUsername, null, null, signUpTestCallback); SignUpResult signUpResult = signUpTestCallback.get(); From b3a3590efe9a55274d230d75b7b69b12922ca509 Mon Sep 17 00:00:00 2001 From: Saurabh Gautam Date: Mon, 22 Jan 2024 13:15:20 +0000 Subject: [PATCH 7/8] Update submodule pointer --- common | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common b/common index bfae99742e..aabddb9d7e 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit bfae99742e84e7fe233bd12598b709187abe7819 +Subproject commit aabddb9d7e6319a31c2f58979bd85127f09217ed From e595c35b109ad63c0d86fc6229b1f8457beef04a Mon Sep 17 00:00:00 2001 From: Saurabh Gautam Date: Mon, 22 Jan 2024 15:47:59 +0000 Subject: [PATCH 8/8] Change function signature --- .../identity/nativeauth/NativeAuthPublicClientApplication.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 938ee4351e..629a8bbc65 100644 --- a/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt +++ b/msal/src/main/java/com/microsoft/identity/nativeauth/NativeAuthPublicClientApplication.kt @@ -497,7 +497,7 @@ class NativeAuthPublicClientApplication( nativeAuthConfig.oAuth2TokenCache, username, password, - attributes?.userAttributes + attributes?.toMap() ) val command = SignUpStartCommand(