Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Change the signature of signup/signin methods #2010

Merged
merged 11 commits into from
Jan 22, 2024
2 changes: 1 addition & 1 deletion common
Submodule common updated 19 files
+1 −0 changelog.txt
+1 −1 common/src/main/java/com/microsoft/identity/common/nativeauth/internal/commands/SignInStartCommand.kt
+2 −2 common/src/main/java/com/microsoft/identity/common/nativeauth/internal/commands/SignUpStartCommand.kt
+13 −35 common/src/main/java/com/microsoft/identity/common/nativeauth/internal/controllers/NativeAuthMsalController.kt
+4 −4 common/src/main/java/com/microsoft/identity/common/nativeauth/internal/util/CommandUtil.java
+1 −4 ...om/microsoft/identity/common/internal/providers/microsoft/nativeauth/integration/SignInOAuthStrategyTest.kt
+8 −9 ...m/microsoft/identity/common/internal/providers/microsoft/nativeauth/integration/SignUpOAuth2StrategyTest.kt
+2 −4 ...icrosoft/identity/common/internal/providers/microsoft/nativeauth/integration/scenario/SignUpScenarioTest.kt
+18 −25 common/src/test/java/com/microsoft/identity/common/nativeauth/internal/controllers/NativeAuthControllerTest.kt
+0 −54 ...ain/com/microsoft/identity/common/java/nativeauth/commands/parameters/BaseSignUpStartCommandParameters.java
+35 −25 ...rc/main/com/microsoft/identity/common/java/nativeauth/commands/parameters/SignInStartCommandParameters.java
+0 −47 ...icrosoft/identity/common/java/nativeauth/commands/parameters/SignInStartUsingPasswordCommandParameters.java
+29 −4 ...rc/main/com/microsoft/identity/common/java/nativeauth/commands/parameters/SignUpStartCommandParameters.java
+0 −47 ...icrosoft/identity/common/java/nativeauth/commands/parameters/SignUpStartUsingPasswordCommandParameters.java
+2 −14 common4j/src/main/com/microsoft/identity/common/java/nativeauth/providers/NativeAuthOAuth2Strategy.kt
+2 −25 common4j/src/main/com/microsoft/identity/common/java/nativeauth/providers/NativeAuthRequestProvider.kt
+1 −1 common4j/src/main/com/microsoft/identity/common/java/nativeauth/providers/interactors/SignInInteractor.kt
+1 −17 common4j/src/main/com/microsoft/identity/common/java/nativeauth/providers/interactors/SignUpInteractor.kt
+15 −15 common4j/src/test/com/microsoft/identity/common/java/nativeauth/providers/NativeAuthRequestHandlerTest.kt
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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://<tenant>.ciamlogin.com/<tenant>.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<Context>()
.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")
}
}
}
Loading