Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ MSAL Wiki : https://github.com/AzureAD/microsoft-authentication-library-for-andr
vNext
----------
- [MAJOR] Native Auth V2 errors now use NativeAuthErrorV2. Replace operation-specific V2 error type checks with its classifiers; rejected sign-in passwords use isInvalidCredentials, not isInvalidPassword. V1 is unchanged (AB#3784695).
- [MINOR] Native Auth V2 sign-in, sign-up and password-reset entries accept scopes and claims. Scope is also sent at token exchange; claims are sent only at entry. Null continuation scopes retain entry scopes, while explicit empty scopes request default OIDC scopes (AB#3784690).

Version 8.5.0
----------
Expand Down
2 changes: 1 addition & 1 deletion common
Submodule common updated 14 files
+30 −10 ...rc/main/java/com/microsoft/identity/common/nativeauth/internal/controllers/v2/NativeAuthV2FlowController.kt
+57 −9 ...java/com/microsoft/identity/common/nativeauth/internal/controllers/v2/NativeAuthV2SSPRFlowControllerTest.kt
+5 −3 ...va/com/microsoft/identity/common/nativeauth/internal/controllers/v2/NativeAuthV2SignInFlowControllerTest.kt
+55 −2 ...va/com/microsoft/identity/common/nativeauth/internal/controllers/v2/NativeAuthV2SignUpFlowControllerTest.kt
+2 −1 ...ain/com/microsoft/identity/common/java/nativeauth/commands/parameters/BaseSignInTokenCommandParameters.java
+2 −2 common4j/src/main/com/microsoft/identity/common/java/nativeauth/providers/NativeAuthV2OAuth2Strategy.kt
+5 −1 ...on4j/src/main/com/microsoft/identity/common/java/nativeauth/providers/interactors/NativeAuthV2Interactor.kt
+35 −5 .../main/com/microsoft/identity/common/java/nativeauth/providers/requests/v2/AuthorizeChallengeStartRequest.kt
+2 −1 ...4j/src/main/com/microsoft/identity/common/java/nativeauth/providers/requests/v2/NativeAuthV2TokenRequest.kt
+0 −1 ...j/src/main/com/microsoft/identity/common/java/nativeauth/providers/requests/v2/NativeAuthV2VerifyRequest.kt
+20 −1 common4j/src/main/com/microsoft/identity/common/java/nativeauth/providers/v2/NativeAuthV2RequestProvider.kt
+15 −8 .../src/test/com/microsoft/identity/common/java/nativeauth/providers/interactors/NativeAuthV2InteractorTest.kt
+46 −0 ...com/microsoft/identity/common/java/nativeauth/providers/requests/v2/NativeAuthV2RequestSerializationTest.kt
+5 −2 ...rc/test/com/microsoft/identity/common/java/nativeauth/providers/requests/v2/NativeAuthV2TokenRequestTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1077,9 +1077,7 @@ public static ResetPasswordSubmitNewPasswordCommandParameters createResetPasswor
/**
* Creates command parameter for [NativeAuthV2ResetPasswordStartCommand] of Native Auth.
*
* No scopes are supplied at the start of a reset-password flow. Matching MSAL iOS/macOS, the
* scopes for the resulting access token are supplied at the sign-in-after-reset-password step
* instead, since no token is issued until then.
* Scopes and claims are supplied at V2 authorize-challenge entry.
*
* @param configuration PCA configuration
* @param tokenCache token cache for storing results
Expand All @@ -1090,6 +1088,16 @@ public static ResetPasswordV2StartCommandParameters createResetPasswordV2StartCo
@NonNull final NativeAuthPublicClientApplicationConfiguration configuration,
@NonNull final OAuth2TokenCache tokenCache,
@NonNull final String username) throws ClientException {
return createResetPasswordV2StartCommandParameters(configuration, tokenCache, username, null, null);
}

/** Creates V2 reset entry parameters with optional scopes and claims. */
public static ResetPasswordV2StartCommandParameters createResetPasswordV2StartCommandParameters(
@NonNull final NativeAuthPublicClientApplicationConfiguration configuration,
@NonNull final OAuth2TokenCache tokenCache,
@NonNull final String username,
@Nullable final List<String> scopes,
@Nullable final ClaimsRequest claimsRequest) throws ClientException {

final NativeAuthCIAMAuthority authority = ((NativeAuthCIAMAuthority) configuration.getDefaultAuthority());

Expand All @@ -1100,6 +1108,8 @@ public static ResetPasswordV2StartCommandParameters createResetPasswordV2StartCo

final ResetPasswordV2StartCommandParameters commandParameters =
ResetPasswordV2StartCommandParameters.builder()
.scopes(scopes == null ? null : new ArrayList<>(scopes))
.claimsRequestJson(ClaimsRequest.getJsonStringFromClaimsRequest(claimsRequest))
.platformComponents(AndroidPlatformComponentsFactory.createFromContext(configuration.getAppContext()))
.applicationName(configuration.getAppContext().getPackageName())
.applicationVersion(getPackageVersion(configuration.getAppContext()))
Expand Down Expand Up @@ -1141,6 +1151,18 @@ public static SignUpV2StartCommandParameters createSignUpV2StartCommandParameter
@NonNull final String username,
@Nullable final char[] password,
@Nullable final Map<String, String> attributes) throws ClientException {
return createSignUpV2StartCommandParameters(configuration, tokenCache, username, password, attributes, null, null);
}

/** Creates V2 sign-up entry parameters with optional scopes and claims. */
public static SignUpV2StartCommandParameters createSignUpV2StartCommandParameters(
@NonNull final NativeAuthPublicClientApplicationConfiguration configuration,
@NonNull final OAuth2TokenCache tokenCache,
@NonNull final String username,
@Nullable final char[] password,
@Nullable final Map<String, String> attributes,
@Nullable final List<String> scopes,
@Nullable final ClaimsRequest claimsRequest) throws ClientException {

final NativeAuthCIAMAuthority authority = ((NativeAuthCIAMAuthority) configuration.getDefaultAuthority());

Expand All @@ -1151,6 +1173,8 @@ public static SignUpV2StartCommandParameters createSignUpV2StartCommandParameter

final SignUpV2StartCommandParameters commandParameters =
SignUpV2StartCommandParameters.builder()
.scopes(scopes == null ? null : new ArrayList<>(scopes))
.claimsRequestJson(ClaimsRequest.getJsonStringFromClaimsRequest(claimsRequest))
.platformComponents(AndroidPlatformComponentsFactory.createFromContext(configuration.getAppContext()))
.applicationName(configuration.getAppContext().getPackageName())
.applicationVersion(getPackageVersion(configuration.getAppContext()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,9 @@ class NativeAuthPublicClientApplication(
val parametersSnapshot = NativeAuthSignInParameters(parameters.username).also {
it.password = parameters.password?.copyOf()
it.scopes = parameters.scopes?.toList()
it.claimsRequest = parameters.claimsRequest
it.claimsRequest = parameters.claimsRequest?.let { claims ->
ClaimsRequest.getClaimsRequestFromJsonString(ClaimsRequest.getJsonStringFromClaimsRequest(claims))
}
}
pcaScope.launchOwningPasswordSnapshot(parametersSnapshot.password) {
try {
Expand Down Expand Up @@ -865,7 +867,9 @@ class NativeAuthPublicClientApplication(
nativeAuthConfig.oAuth2TokenCache,
parameters.username,
passwordCopy,
parameters.attributes?.toMap()
parameters.attributes?.toMap(),
parameters.scopes?.toList(),
parameters.claimsRequest
)
correlationId = cmdParams.correlationId ?: correlationId

Expand Down Expand Up @@ -1036,6 +1040,10 @@ class NativeAuthPublicClientApplication(
)
val parametersSnapshot = NativeAuthSignUpParameters(parameters.username).also {
it.password = parameters.password?.copyOf()
it.scopes = parameters.scopes?.toList()
it.claimsRequest = parameters.claimsRequest?.let { claims ->
ClaimsRequest.getClaimsRequestFromJsonString(ClaimsRequest.getJsonStringFromClaimsRequest(claims))
}
it.attributes = parameters.attributes?.let { attributes ->
UserAttributes(attributes.toMap())
}
Expand Down Expand Up @@ -1076,7 +1084,9 @@ class NativeAuthPublicClientApplication(
val cmdParams = CommandParametersAdapter.createResetPasswordV2StartCommandParameters(
nativeAuthConfig,
nativeAuthConfig.oAuth2TokenCache,
parameters.username
parameters.username,
parameters.scopes?.toList(),
parameters.claimsRequest
)
correlationId = cmdParams.correlationId ?: correlationId

Expand Down Expand Up @@ -1230,9 +1240,15 @@ class NativeAuthPublicClientApplication(
correlationId = null,
methodName = "${TAG}.resetPasswordV2(parameters: NativeAuthResetPasswordParameters, callback: NativeAuthV2Callback)"
)
val parametersSnapshot = NativeAuthResetPasswordParameters(parameters.username).also {
it.scopes = parameters.scopes?.toList()
it.claimsRequest = parameters.claimsRequest?.let { claims ->
ClaimsRequest.getClaimsRequestFromJsonString(ClaimsRequest.getJsonStringFromClaimsRequest(claims))
}
}
pcaScope.launch {
try {
callback.onResult(resetPasswordV2(parameters))
callback.onResult(resetPasswordV2(parametersSnapshot))
} catch (e: MsalException) {
Logger.error(TAG, "Exception thrown in resetPasswordV2", e)
callback.onError(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@

package com.microsoft.identity.nativeauth.parameters

import com.microsoft.identity.client.claims.ClaimsRequest

/**
* Encapsulates the parameters passed to the resetPassword methods of NativeAuthPublicClientApplication.
*
* @param username username of the account whose password will be reset.
*/
class NativeAuthResetPasswordParameters(
val username: String
)
) {
/** V2-only requested scopes. Null or empty uses default OIDC scopes; ignored by V1. */
var scopes: List<String>? = null

/** V2-only claims sent at authorize-challenge entry; ignored by V1. */
var claimsRequest: ClaimsRequest? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ class NativeAuthSignInContinuationParameters {
/**
* Permissions you want included in the access token received.
* Not all scopes are guaranteed to be included in the access token returned.
* For V2, null retains entry scopes; an explicit empty list requests only default OIDC scopes.
*/
var scopes: List<String>? = null

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These actually need to be removed they can't be sent, if used by v2 same class, we need to make another class for v2 specifically, I am not sure if it even has any parameters.


/**
* The claims parameter that needs to be sent to the service.
* Claims sent to the service in V1. In V2, claims are retained locally for token/cache
* semantics, but the token exchange omits claims; supply service claims at flow entry.
*/
var claimsRequest: ClaimsRequest? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.microsoft.identity.nativeauth.parameters

import com.microsoft.identity.nativeauth.UserAttributes
import com.microsoft.identity.client.claims.ClaimsRequest

/**
* Encapsulates the parameters passed to the signUp methods of NativeAuthPublicClientApplication
Expand All @@ -47,4 +48,10 @@ class NativeAuthSignUpParameters(
* Supply them through the dedicated [username] and [password] properties instead.
*/
var attributes: UserAttributes? = null

/** V2-only requested scopes. Null or empty uses default OIDC scopes; ignored by V1. */
var scopes: List<String>? = null

/** V2-only claims sent at authorize-challenge entry; ignored by V1. */
var claimsRequest: ClaimsRequest? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,6 @@ class CodeRequiredStateV2 internal constructor(
methodName = "${TAG}.submitCode(code: String)"
)
val state = continuationState ?: return invalidState()
if (code.isEmpty()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why was this removed in this PR?

return NativeAuthErrorV2(
errorType = ErrorTypes.INVALID_CODE,
errorMessage = "Code cannot be empty.",
correlationId = correlationId,
scenario = scenario
)
}
if (scenario == NativeAuthFlowScenarioV2.UNKNOWN) {
return invalidState()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,6 @@ class MFAVerificationRequiredStateV2 internal constructor(
methodName = "${TAG}.submitChallenge(challenge: String)"
)
val state = continuationState ?: return invalidState()
if (challenge.isEmpty()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why was this removed in this PR?

return NativeAuthErrorV2(
errorType = ErrorTypes.INVALID_CODE,
errorMessage = "Challenge cannot be empty.",
correlationId = correlationId,
scenario = scenario
)
}
return withContext(Dispatchers.IO) {
try {
val parameters = CommandParametersAdapter.createNativeAuthV2SubmitMFAChallengeCommandParameters(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,106 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() {
private lateinit var application: INativeAuthPublicClientApplication
private val username = "user@email.com"

@Test
fun allV2EntriesPropagateNullableScopesAndTypedClaimsThroughAdapters() = runTest {
val claims = com.microsoft.identity.client.claims.ClaimsRequest().also {
it.requestClaimInIdToken("auth_time", null)
}
val expectedClaims = com.microsoft.identity.client.claims.ClaimsRequest.getJsonStringFromClaimsRequest(claims)
listOf<List<String>?>(null, emptyList(), listOf("User.Read")).forEach { scopes ->
var captured: com.microsoft.identity.common.java.nativeauth.commands.parameters.BaseSignInTokenCommandParameters? = null
fun capture(command: BaseCommand<*>) {
captured = command.parameters as com.microsoft.identity.common.java.nativeauth.commands.parameters.BaseSignInTokenCommandParameters
}
fun assertCaptured() {
assertTrue(captured != null)
assertEquals(scopes, captured?.scopes)
assertEquals(expectedClaims, captured?.claimsRequestJson)
captured = null
}
val result = INativeAuthCommandResult.APIError("test_stop", "test stop", correlationId = correlationId)
enqueueResult(result, com.microsoft.identity.common.nativeauth.internal.commands.NativeAuthV2SignInStartCommand::class, ::capture)
application.signInV2(NativeAuthSignInParameters(username).also {
it.scopes = scopes
it.claimsRequest = claims
})
assertCaptured()
enqueueResult(result, NativeAuthV2SignUpStartCommand::class, ::capture)
application.signUpV2(NativeAuthSignUpParameters(username).also {
it.scopes = scopes
it.claimsRequest = claims
})
assertCaptured()
enqueueResult(result, NativeAuthV2ResetPasswordStartCommand::class, ::capture)
application.resetPasswordV2(NativeAuthResetPasswordParameters(username).also {
it.scopes = scopes
it.claimsRequest = claims
})
assertCaptured()
}
}

@Test
fun sharedEntryScopesAndClaimsDoNotChangeV1CommandInputsOrErrors() = runTest {
val claims = com.microsoft.identity.client.claims.ClaimsRequest().also { it.requestClaimInIdToken("auth_time", null) }
var signUpInputs: com.microsoft.identity.common.java.nativeauth.commands.parameters.SignUpStartCommandParameters? = null
var resetInputs: com.microsoft.identity.common.java.nativeauth.commands.parameters.ResetPasswordStartCommandParameters? = null
val result = INativeAuthCommandResult.APIError("test_stop", "test stop", correlationId = correlationId)
enqueueResult(result, com.microsoft.identity.common.nativeauth.internal.commands.SignUpStartCommand::class) {
signUpInputs = it.parameters as com.microsoft.identity.common.java.nativeauth.commands.parameters.SignUpStartCommandParameters
}
enqueueResult(result, com.microsoft.identity.common.nativeauth.internal.commands.ResetPasswordStartCommand::class) {
resetInputs = it.parameters as com.microsoft.identity.common.java.nativeauth.commands.parameters.ResetPasswordStartCommandParameters
}
val signUp = application.signUp(NativeAuthSignUpParameters(username).also {
it.scopes = listOf("V2.Only")
it.claimsRequest = claims
})
val reset = application.resetPassword(NativeAuthResetPasswordParameters(username).also {
it.scopes = listOf("V2.Only")
it.claimsRequest = claims
})
assertTrue(signUp is com.microsoft.identity.nativeauth.statemachine.errors.SignUpError)
assertTrue(reset is com.microsoft.identity.nativeauth.statemachine.errors.ResetPasswordError)
assertEquals(username, signUpInputs?.username)
assertEquals(null, signUpInputs?.userAttributes)
assertEquals(null, signUpInputs?.password)
assertEquals(username, resetInputs?.username)
assertEquals("test_stop", (reset as com.microsoft.identity.nativeauth.statemachine.errors.ResetPasswordError).error)
}

@Test
fun resetPasswordCallbackSnapshotsScopesAndMutableClaimsBeforeLaunch() = runTest {
val scopes = mutableListOf("User.Read")
val claims = com.microsoft.identity.client.claims.ClaimsRequest().also {
it.requestClaimInIdToken("auth_time", null)
}
val expectedClaims = com.microsoft.identity.client.claims.ClaimsRequest.getJsonStringFromClaimsRequest(claims)
var captured: com.microsoft.identity.common.java.nativeauth.commands.parameters.BaseSignInTokenCommandParameters? = null
enqueueResult(
INativeAuthCommandResult.APIError("test_stop", "test stop", correlationId = correlationId),
NativeAuthV2ResetPasswordStartCommand::class
) { command ->
captured = command.parameters as com.microsoft.identity.common.java.nativeauth.commands.parameters.BaseSignInTokenCommandParameters
}
val parameters = NativeAuthResetPasswordParameters(username).also {
it.scopes = scopes
it.claimsRequest = claims
}
val future = ResultFuture<NativeAuthResultV2>()
application.resetPasswordV2(parameters, object : NativeAuthPublicClientApplication.NativeAuthV2Callback {
override fun onResult(result: NativeAuthResultV2) = future.setResult(result)
override fun onError(exception: BaseException) = future.setException(exception)
})
scopes.clear()
claims.requestClaimInIdToken("changed", null)
parameters.claimsRequest = null
assertTrue(future.get(30, TimeUnit.SECONDS) is NativeAuthErrorV2)
assertTrue(captured != null)
assertEquals(listOf("User.Read"), captured?.scopes)
assertEquals(expectedClaims, captured?.claimsRequestJson)
}

override fun getConfigFilePath() = "src/test/res/raw/native_auth_native_only_test_config.json"

@Before
Expand Down Expand Up @@ -573,12 +673,21 @@ class NativeAuthV2InterfaceKotlinTest : PublicClientApplicationAbstractTest() {
}

@Test
fun submitCodeRejectsEmptyInputWithRetryableInvalidCodeError() = runTest {
fun submitCodeSendsEmptyAndShortInputToServiceAndRemainsRetryable() = runTest {
val state = codeRequiredState()

val error = state.submitCode("") as NativeAuthErrorV2

assertTrue(error.isInvalidCode())
listOf("", "1").forEach { code ->
var submittedCode: String? = null
enqueueResult(
NativeAuthV2CommandResult.IncorrectCode(correlationId, "invalidGrant", "invalid", "invalidOneTimeCode"),
NativeAuthV2ResetPasswordSubmitCodeCommand::class
) { command ->
submittedCode = (command.parameters as com.microsoft.identity.common.java.nativeauth.commands.parameters.NativeAuthV2SubmitCodeCommandParameters).code
}
val error = state.submitCode(code) as NativeAuthErrorV2
assertTrue(error.isInvalidCode())
assertEquals(code, submittedCode)
}
}

@Test
Expand Down
Loading
Loading