Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,13 @@ class EmailAttributeSignUpFragment : Fragment() {
CoroutineScope(Dispatchers.Main).launch {
try {
val email = binding.emailText.text.toString()
val password = CharArray(binding.passwordText.length());
binding.passwordText.text?.getChars(0, binding.passwordText.length(), password, 0);
var password: CharArray? = null
if (binding.passwordText.length() > 0) {
password = CharArray(binding.passwordText.length())
}
binding.passwordText.text?.getChars(0, binding.passwordText.length(), password, 0)

val attributes = UserAttributes.Builder
val attributes = UserAttributes.Builder()

val attr1Key = binding.attr1KeyText.text.toString()
if (attr1Key.isNotBlank()) {
Expand All @@ -136,7 +139,7 @@ class EmailAttributeSignUpFragment : Fragment() {
attributes = attributes.build()
)

password.fill('0');
password?.fill('0')

when (actionResult) {
is SignUpResult.CodeRequired -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ class EmailPasswordSignInSignUpFragment : Fragment() {

val actionResult = authClient.signIn(
username = email,
password = password
password = password,
scopes = listOf("User.Read")
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not against it, but is it necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

[use case 2.2.6] Ability to provide scope to control auth strength of the token
In this test case, we need to modify the code, providing scopes = listOf("User.Read") . If it had been there all along, the testers wouldn't have needed any modifications

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it.

)

password.fill('0');
Expand All @@ -147,6 +148,9 @@ class EmailPasswordSignInSignUpFragment : Fragment() {
)
)
}
else {
displayDialog("Unexpected result", actionResult.errorMessage)
}
}
else -> {
displayDialog( "Unexpected result", actionResult.toString())
Expand Down Expand Up @@ -271,6 +275,7 @@ class EmailPasswordSignInSignUpFragment : Fragment() {
binding.resultAccessToken.text = ""
binding.resultIdToken.text = ""
}

private fun displayAccount(accountState: AccountState) {
CoroutineScope(Dispatchers.Main).launch {
val accessTokenState = accountState.getAccessToken()
Expand Down Expand Up @@ -326,6 +331,7 @@ class EmailPasswordSignInSignUpFragment : Fragment() {
}
}
}

private fun navigateToSignUp(
nextState: SignUpCodeRequiredState,
codeLength: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ package com.microsoft.identity.client.testapp.nativeauth

import android.app.AlertDialog
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.microsoft.identity.client.Account
import com.microsoft.identity.client.AcquireTokenParameters
import com.microsoft.identity.client.AuthenticationCallback
import com.microsoft.identity.client.IAuthenticationResult
import com.microsoft.identity.client.exception.MsalException
import com.microsoft.identity.client.testapp.Constants
import com.microsoft.identity.client.testapp.R
import com.microsoft.identity.client.testapp.databinding.FragmentEmailSisuBinding
import com.microsoft.identity.nativeauth.INativeAuthPublicClientApplication
import com.microsoft.identity.nativeauth.statemachine.errors.SignInError
import com.microsoft.identity.nativeauth.statemachine.results.GetAccessTokenResult
import com.microsoft.identity.nativeauth.statemachine.results.GetAccountResult
import com.microsoft.identity.nativeauth.statemachine.results.SignInResult
Expand Down Expand Up @@ -112,7 +116,8 @@ class EmailSignInSignUpFragment : Fragment() {
val email = binding.emailText.text.toString()

val actionResult = authClient.signIn(
username = email
username = email,
scopes = listOf("User.Read")
)

when (actionResult) {
Expand All @@ -124,6 +129,23 @@ class EmailSignInSignUpFragment : Fragment() {
channel = actionResult.channel
)
}
is SignInError -> {
if (actionResult.isBrowserRequired()) {
Toast.makeText(requireContext(), actionResult.errorMessage, Toast.LENGTH_SHORT).show()

authClient.acquireToken(
AcquireTokenParameters(
AcquireTokenParameters.Builder()
.startAuthorizationFromActivity(requireActivity())
.withScopes(mutableListOf("profile", "openid", "email"))
.withCallback(getAuthInteractiveCallback())
)
)
}
else {
displayDialog("Unexpected result", actionResult.errorMessage)
}
}
else -> {
displayDialog(getString(R.string.msal_exception_title), "Unexpected result: $actionResult")
}
Expand Down Expand Up @@ -163,7 +185,7 @@ class EmailSignInSignUpFragment : Fragment() {
)
}
else -> {
displayDialog(getString(R.string.msal_exception_title), "Unexpected result: $actionResult")
displayDialog( "Unexpected result", actionResult.toString())
}
}
} catch (exception: MsalException) {
Expand Down Expand Up @@ -306,4 +328,38 @@ class EmailSignInSignUpFragment : Fragment() {
.replace(R.id.scenario_fragment, fragment)
.commit()
}

/**
* Callback used for interactive request.
* If succeeds we use the access token to call the Microsoft Graph.
* Does not check cache.
*/
private fun getAuthInteractiveCallback(): AuthenticationCallback {
return object : AuthenticationCallback {

override fun onSuccess(authenticationResult: IAuthenticationResult) {
/* Successfully got a token, use it to call a protected resource - MSGraph */

val accountResult = authenticationResult.account as Account

/* Update account */
emptyFields()
updateUI(STATUS.SignedIn)
val idToken = accountResult.idToken
binding.resultIdToken.text =
getString(R.string.result_id_token_text) + idToken

Toast.makeText(requireContext(), getString(R.string.sign_in_successful_message), Toast.LENGTH_SHORT).show()
}

override fun onError(exception: MsalException) {
/* Failed to acquireToken */
displayDialog(getString(R.string.msal_exception_title), exception.errorCode)
}

override fun onCancel() {
/* User canceled the authentication */
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class SignInCodeFragment : Fragment() {
_binding = FragmentCodeBinding.inflate(inflater, container, false)

val bundle = this.arguments
currentState = bundle!!.getSerializable(Constants.STATE) as SignInCodeRequiredState
currentState = (bundle?.getParcelable(Constants.STATE) as? SignInCodeRequiredState)!!
codeLength = bundle.getInt(Constants.CODE_LENGTH)
sentTo = bundle.getString(Constants.SENT_TO)
channel = bundle.getString(Constants.CHANNEL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class SignUpAttributesFragment : Fragment() {
_binding = FragmentAttributeBinding.inflate(inflater, container, false)

val bundle = this.arguments
currentState = bundle!!.getSerializable(Constants.STATE) as SignUpAttributesRequiredState
currentState = (bundle?.getParcelable(Constants.STATE) as? SignUpAttributesRequiredState)!!

init()

Expand All @@ -77,21 +77,15 @@ class SignUpAttributesFragment : Fragment() {
private fun create() {
CoroutineScope(Dispatchers.Main).launch {
try {
val attributes = UserAttributes.Builder
val attributes = UserAttributes.Builder()

val attr1Key = binding.attr1KeyText.text.toString()
if (attr1Key.isNotBlank()) {
val attr1Value = binding.attr1ValueText.toString()
attributes
.customAttribute(attr1Key, attr1Value)
}
val attr1Value = binding.attr1ValueText.text.toString()
attributes.customAttribute(attr1Key, attr1Value)

val attr2Key = binding.attr2KeyText.text.toString()
if (attr2Key.isNotBlank()) {
val attr2Value = binding.attr2ValueText.toString()
attributes
.customAttribute(attr2Key, attr2Value)
}
val attr2Value = binding.attr2ValueText.text.toString()
attributes.customAttribute(attr2Key, attr2Value)

val actionResult = currentState.submitAttributes(attributes.build())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.microsoft.identity.nativeauth.statemachine.errors.SubmitCodeError
import com.microsoft.identity.nativeauth.statemachine.results.SignInResult
import com.microsoft.identity.nativeauth.statemachine.results.SignUpResendCodeResult
import com.microsoft.identity.nativeauth.statemachine.results.SignUpResult
import com.microsoft.identity.nativeauth.statemachine.states.SignInCodeRequiredState
import com.microsoft.identity.nativeauth.statemachine.states.SignInContinuationState
import com.microsoft.identity.nativeauth.statemachine.states.SignUpAttributesRequiredState
import com.microsoft.identity.nativeauth.statemachine.states.SignUpCodeRequiredState
Expand All @@ -60,7 +61,7 @@ class SignUpCodeFragment : Fragment() {
_binding = FragmentCodeBinding.inflate(inflater, container, false)

val bundle = this.arguments
currentState = bundle!!.getSerializable(Constants.STATE) as SignUpCodeRequiredState
currentState = (bundle?.getParcelable(Constants.STATE) as? SignUpCodeRequiredState)!!
codeLength = bundle.getInt(Constants.CODE_LENGTH)
sentTo = bundle.getString(Constants.SENT_TO)
channel = bundle.getString(Constants.CHANNEL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class SignUpPasswordFragment : Fragment() {
_binding = FragmentPasswordBinding.inflate(inflater, container, false)

val bundle = this.arguments
currentState = bundle!!.getSerializable(Constants.STATE) as SignUpPasswordRequiredState
currentState = (bundle?.getParcelable(Constants.STATE) as? SignUpPasswordRequiredState)!!

init()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
android:layout_height="wrap_content"
android:text="@string/attributes_intro"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
style="@style/TextViewStyle"
/>

<TextView
Expand Down