Skip to content

Commit

Permalink
Change the signature of signup/signin methods (#2284)
Browse files Browse the repository at this point in the history
The signIn and signInUsingPassword methods have been refactored into one signIn method with optional password.
The signUp and signUpUsingPassword methods have been refactored into one signUp method with optional password.

PR for the MSAL repo is
AzureAD/microsoft-authentication-library-for-android#2010
  • Loading branch information
SaurabhMSFT authored Jan 22, 2024
1 parent dab3ca0 commit aabddb9
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 326 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
V.Next
---------
- [MINOR] Update SignIn/Signup parameters classes for Native Auth(#2284)
- [MINOR] Add IsQrPinAvailableCommand, controllers behavior and define constants for the isQrAvailable API (#2219)
- [MINOR] Add PreferredAuthMethod to interactive token flow (#2245)
- [MINOR] Implement updates of the native auth web API (#2261)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
package com.microsoft.identity.common.nativeauth.internal.commands

import com.microsoft.identity.common.nativeauth.internal.controllers.NativeAuthMsalController
import com.microsoft.identity.common.java.nativeauth.commands.parameters.BaseSignUpStartCommandParameters
import com.microsoft.identity.common.java.nativeauth.controllers.results.SignUpStartCommandResult
import com.microsoft.identity.common.java.logging.LogSession
import com.microsoft.identity.common.java.logging.Logger
import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignUpStartCommandParameters

/**
* Command class to call controllers to start the sign up flow.
* {@see com.microsoft.identity.common.java.controllers.CommandDispatcher}.
*/
class SignUpStartCommand(
private val parameters: BaseSignUpStartCommandParameters,
private val parameters: SignUpStartCommandParameters,
private val controller: NativeAuthMsalController,
publicApiId: String
) : BaseNativeAuthCommand<SignUpStartCommandResult>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,13 @@ import com.microsoft.identity.common.java.nativeauth.commands.parameters.ResetPa
import com.microsoft.identity.common.java.nativeauth.commands.parameters.ResetPasswordStartCommandParameters
import com.microsoft.identity.common.java.nativeauth.commands.parameters.ResetPasswordSubmitCodeCommandParameters
import com.microsoft.identity.common.java.nativeauth.commands.parameters.ResetPasswordSubmitNewPasswordCommandParameters
import com.microsoft.identity.common.java.nativeauth.commands.parameters.BaseSignUpStartCommandParameters
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.SignInWithContinuationTokenCommandParameters
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
Expand Down Expand Up @@ -135,12 +132,14 @@ class NativeAuthMsalController : BaseNativeAuthController() {
oAuth2Strategy = oAuth2Strategy,
parameters = parameters)

if (parameters is SignInStartUsingPasswordCommandParameters)
val hasPassword = parameters.password?.isNotEmpty() == true

if (hasPassword)
{
Logger.verbose(TAG, "Parameters is of type SignInStartUsingPasswordCommandParameters");
Logger.verbose(TAG, "Parameters has password");
val mergedScopes = addDefaultScopes(parameters.scopes)
var parametersWithScopes = CommandUtil.createSignInStartCommandParametersWithScopes(
parameters as SignInStartUsingPasswordCommandParameters,
parameters,
mergedScopes)

try {
Expand All @@ -155,7 +154,7 @@ class NativeAuthMsalController : BaseNativeAuthController() {
}
else
{
Logger.verbose(TAG, "Parameters is not of type SignInStartUsingPasswordCommandParameters");
Logger.verbose(TAG, "Parameters doesn't have password");
return processSignInInitiateApiResult(
initiateApiResult = initiateApiResult,
oAuth2Strategy = oAuth2Strategy)
Expand Down Expand Up @@ -369,26 +368,15 @@ class NativeAuthMsalController : BaseNativeAuthController() {
* Makes a call to the signup/start endpoint, mapping responses returned from the server into a command result.
* If the call is successful, additionally calls the signup/challenge endpoint, returning the result.
*/
fun signUpStart(parameters: BaseSignUpStartCommandParameters): SignUpStartCommandResult {
fun signUpStart(parameters: SignUpStartCommandParameters): SignUpStartCommandResult {
LogSession.logMethodCall(TAG, "${TAG}.signUpStart")
try {
val oAuth2Strategy = createOAuth2Strategy(parameters)

val signUpStartApiResult = if (parameters is SignUpStartUsingPasswordCommandParameters) {

Logger.verbose(TAG, "Parameters is of type SignUpStartUsingPasswordCommandParameters");
performSignUpStartUsingPasswordRequest(
val signUpStartApiResult = performSignUpStartUsingPasswordRequest(
oAuth2Strategy = oAuth2Strategy,
parameters = (parameters as SignUpStartUsingPasswordCommandParameters)
parameters
)
} else {

Logger.verbose(TAG, "Parameters is of type SignUpStartCommandParameters");
performSignUpStartRequest(
oAuth2Strategy = oAuth2Strategy,
parameters = (parameters as SignUpStartCommandParameters)
)
}
return when (signUpStartApiResult) {
is SignUpStartApiResult.Success -> {
performSignUpChallengeCall(
Expand Down Expand Up @@ -1231,7 +1219,7 @@ class NativeAuthMsalController : BaseNativeAuthController() {
}

@VisibleForTesting
fun performSignUpStartRequest(
fun performSignUpStartUsingPasswordRequest(
oAuth2Strategy: NativeAuthOAuth2Strategy,
parameters: SignUpStartCommandParameters
): SignUpStartApiResult {
Expand All @@ -1240,16 +1228,6 @@ class NativeAuthMsalController : BaseNativeAuthController() {
)
}

@VisibleForTesting
fun performSignUpStartUsingPasswordRequest(
oAuth2Strategy: NativeAuthOAuth2Strategy,
parameters: SignUpStartUsingPasswordCommandParameters
): SignUpStartApiResult {
return oAuth2Strategy.performSignUpStartUsingPassword(
commandParameters = parameters
)
}

private fun performSignUpChallengeCall(
oAuth2Strategy: NativeAuthOAuth2Strategy,
continuationToken: String
Expand Down Expand Up @@ -1534,7 +1512,7 @@ class NativeAuthMsalController : BaseNativeAuthController() {

private fun SignInTokenApiResult.toSignInStartCommandResult(
oAuth2Strategy: NativeAuthOAuth2Strategy,
parametersWithScopes: SignInStartUsingPasswordCommandParameters,
parametersWithScopes: SignInStartCommandParameters,
): SignInStartCommandResult {
LogSession.logMethodCall(TAG, "${TAG}.execute")
return when (this) {
Expand Down Expand Up @@ -1610,7 +1588,7 @@ class NativeAuthMsalController : BaseNativeAuthController() {
@VisibleForTesting
fun processSignInInitiateApiResult(
initiateApiResult: SignInInitiateApiResult,
parametersWithScopes: SignInStartUsingPasswordCommandParameters? = null,
parametersWithScopes: SignInStartCommandParameters? = null,
oAuth2Strategy: NativeAuthOAuth2Strategy,
usePassword: Boolean = false
): SignInStartCommandResult {
Expand Down Expand Up @@ -1653,7 +1631,7 @@ class NativeAuthMsalController : BaseNativeAuthController() {

private fun processSignInChallengeCall(
oAuth2Strategy: NativeAuthOAuth2Strategy,
parametersWithScopes: SignInStartUsingPasswordCommandParameters?,
parametersWithScopes: SignInStartCommandParameters?,
result: SignInChallengeApiResult,
usePassword: Boolean
): SignInStartCommandResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import com.microsoft.identity.common.java.commands.parameters.SilentTokenCommandParameters;
import com.microsoft.identity.common.java.nativeauth.commands.parameters.AcquireTokenNoFixedScopesCommandParameters;
import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignInStartUsingPasswordCommandParameters;
import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignInStartCommandParameters;
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.SignInWithContinuationTokenCommandParameters;
Expand All @@ -39,14 +39,14 @@
public class CommandUtil {

/**
* Adds scopes to [SignInStartUsingPasswordCommandParameters] object and returns a new
* [SignInStartUsingPasswordCommandParameters] object.
* Adds scopes to [SignInStartCommandParameters] object and returns a new
* [SignInStartCommandParameters] object.
* @param parameters input command parameter
* @param defaultScopes scopes to be added
* @return [SignInStartUsingPasswordCommandParameters] object with scopes
* @return [SignInStartCommandParameters] object with scopes
*/
public static SignInStartUsingPasswordCommandParameters createSignInStartCommandParametersWithScopes(
SignInStartUsingPasswordCommandParameters parameters,
public static SignInStartCommandParameters createSignInStartCommandParametersWithScopes(
SignInStartCommandParameters parameters,
List<String> defaultScopes
) {
return parameters.toBuilder()
Expand Down Expand Up @@ -103,14 +103,14 @@ public static SignInSubmitPasswordCommandParameters createSignInSubmitPasswordCo
}

/**
* Adds continuation token to [SignInStartUsingPasswordCommandParameters] object and returns a new
* Adds continuation token to [SignInStartCommandParameters] object and returns a new
* [SignInSubmitPasswordCommandParameters] object.
* @param parameters input command parameter
* @param continuationToken continuation token to be added
* @return [SignInStartUsingPasswordCommandParameters] object with continuation token
* @return [SignInSubmitPasswordCommandParameters] object with continuation token
*/
public static SignInSubmitPasswordCommandParameters createSignInSubmitPasswordCommandParameters(
SignInStartUsingPasswordCommandParameters parameters,
SignInStartCommandParameters parameters,
String continuationToken
) {
final SignInSubmitPasswordCommandParameters commandParameters =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package com.microsoft.identity.common.internal.providers.microsoft.nativeauth.in
import android.os.Build
import com.microsoft.identity.common.nativeauth.ApiConstants
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.SignInWithContinuationTokenCommandParameters
Expand All @@ -50,7 +49,6 @@ import io.mockk.every
import io.mockk.mockk
import org.junit.Assert
import org.junit.Before
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock
Expand All @@ -59,7 +57,6 @@ import org.powermock.core.classloader.annotations.PowerMockIgnore
import org.powermock.core.classloader.annotations.PrepareForTest
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import java.net.URL
import java.util.UUID

/**
Expand Down Expand Up @@ -172,7 +169,7 @@ class SignInOAuthStrategyTest {
responseType = MockApiResponseType.INITIATE_SUCCESS
)

val parameters = SignInStartUsingPasswordCommandParameters.builder()
val parameters = SignInStartCommandParameters.builder()
.platformComponents(mock<PlatformComponents>())
.username(USERNAME)
.password(PASSWORD)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ package com.microsoft.identity.common.internal.providers.microsoft.nativeauth.in

import android.os.Build
import com.microsoft.identity.common.nativeauth.ApiConstants
import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignUpStartCommandParameters
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.common.java.interfaces.PlatformComponents
import com.microsoft.identity.common.java.logging.DiagnosticContext
import com.microsoft.identity.common.java.nativeauth.commands.parameters.SignUpStartCommandParameters
import com.microsoft.identity.common.java.net.UrlConnectionHttpClient
import com.microsoft.identity.common.java.nativeauth.providers.NativeAuthOAuth2Configuration
import com.microsoft.identity.common.java.nativeauth.providers.NativeAuthOAuth2Strategy
Expand All @@ -47,7 +47,6 @@ import com.microsoft.identity.common.nativeauth.MockApiResponseType
import com.microsoft.identity.common.nativeauth.MockApiUtils.Companion.configureMockApi
import junit.framework.TestCase.assertTrue
import org.junit.Before
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package com.microsoft.identity.common.internal.providers.microsoft.nativeauth.in

import com.microsoft.identity.common.nativeauth.ApiConstants
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.interfaces.PlatformComponents
import com.microsoft.identity.common.java.net.UrlConnectionHttpClient
Expand All @@ -45,7 +44,6 @@ import com.microsoft.identity.common.nativeauth.MockApiResponseType
import com.microsoft.identity.common.nativeauth.MockApiUtils.Companion.configureMockApi
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Ignore
import org.junit.Test
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
Expand Down Expand Up @@ -123,14 +121,14 @@ class SignUpScenarioTest {
responseType = MockApiResponseType.SIGNUP_START_SUCCESS
)

val mockSignUpStartCommandParameters = SignUpStartUsingPasswordCommandParameters.builder()
val mockSignUpStartCommandParameters = SignUpStartCommandParameters.builder()
.platformComponents(mock<PlatformComponents>())
.username(username)
.clientId(clientId)
.password(password)
.build()

val signupStartResult = nativeAuthOAuth2Strategy.performSignUpStartUsingPassword(
val signupStartResult = nativeAuthOAuth2Strategy.performSignUpStart(
mockSignUpStartCommandParameters
)
assertTrue(signupStartResult is SignUpStartApiResult.Success)
Expand Down
Loading

0 comments on commit aabddb9

Please sign in to comment.