Skip to content

Commit 8c01b1d

Browse files
Yuki-YuXinfadidurahalgo-1
authored
Fix msal test app after merging with the fadi/release/5.4.0 (#2118)
Fix update according to the following team feedback: **Batch2** - [hero scenario 6, use case 2.2.1] Use email and OTP to get token and sign in getParcelable(Constants.STATE) - [use case 2.2.6] Ability to provide scope to control auth strength of the token Update the scope of profile to User.Read Update the test app EmailSignInSignUp page: signIn(scopes = User.Read) - [use case 1.2.2] User is not registered with given email Update test app EmailPasswordSignInSignUp page: displayDialog("Unexpected result", actionResult.errorMessage) in sign in function - [use case 1.2.6] Ability to provide scope to control auth strength of the token Update the test app EmailPasswordSignInSignUp page: signIn(scopes = User.Read) Update test app EmailPasswordSignInSignUp page: displayDialog("Unexpected result", actionResult.errorMessage) in sign in function - [hero scenario 2, use case 2.1.2] Signup user with custom attributes with verify OTP as last step Update the test app SignUpCode page: currentState = (bundle?.getParcelable(Constants.STATE) as? SignUpCodeRequiredState)!! - [hero scenario 2, use case 2.1.2] Signup user with custom attributes with verify OTP as last step Update the test app SignUpCode page: currentState = (bundle?.getParcelable(Constants.STATE) as? SignUpCodeRequiredState)!! - [hero scenario 2, use case 2.1.2] Signup user with custom attributes with verify OTP as last step - [hero scenario 3, use case 2.1.3] Verify email OTP first and then collect custom attributes Update the test app SignUpAttributes page: currentState = (bundle?.getParcelable(Constants.STATE) as? SignUpAttributesRequiredState)!! SignUpAttributesUI disorder - [use case 1.1.4] Verify email address using email OTP and then set password In SignUpCodeFragment modify to currentState = (bundle?.getParcelable(Constants.STATE) as? SignUpCodeRequiredState)!! In SignUpPasswordFragment modify to currentState = (bundle?.getParcelable(Constants.STATE) as? SignUpPasswordRequiredState)!! **Batch1** - [use case 1.2.2] User is not registered with given email No error is popped up in UI (Probably this is a bug of the test app) - [use case 2.2.5] Resend email OTP App crashing - [use case 1.2.3] Password is incorrect No error is popped up in UI (Probably this is a bug of the test app) - [hero scenario 3, use case 2.1.3] Verify email OTP first and then collect custom attributes When submitting attributes (had to hard-code some), the test application doesn't return to the sign in / sign up screen - Authenticate user (get token) through web browser, retrieve tokens through native auth SDK interface (by setting forceRefresh=true): EmailSignInSignUpFragment does not have logic for handling redirects - [hero scenario 6, use case 2.2.1] Use email and OTP to get token and sign in --------- Co-authored-by: fadidurah <[email protected]> Co-authored-by: Harold Karibiye <[email protected]>
1 parent 2c25566 commit 8c01b1d

File tree

8 files changed

+85
-24
lines changed

8 files changed

+85
-24
lines changed

testapps/testapp/src/main/java/com/microsoft/identity/client/testapp/nativeauth/EmailAttributeSignUpFragment.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,13 @@ class EmailAttributeSignUpFragment : Fragment() {
111111
CoroutineScope(Dispatchers.Main).launch {
112112
try {
113113
val email = binding.emailText.text.toString()
114-
val password = CharArray(binding.passwordText.length());
115-
binding.passwordText.text?.getChars(0, binding.passwordText.length(), password, 0);
114+
var password: CharArray? = null
115+
if (binding.passwordText.length() > 0) {
116+
password = CharArray(binding.passwordText.length())
117+
}
118+
binding.passwordText.text?.getChars(0, binding.passwordText.length(), password, 0)
116119

117-
val attributes = UserAttributes.Builder
120+
val attributes = UserAttributes.Builder()
118121

119122
val attr1Key = binding.attr1KeyText.text.toString()
120123
if (attr1Key.isNotBlank()) {
@@ -136,7 +139,7 @@ class EmailAttributeSignUpFragment : Fragment() {
136139
attributes = attributes.build()
137140
)
138141

139-
password.fill('0');
142+
password?.fill('0')
140143

141144
when (actionResult) {
142145
is SignUpResult.CodeRequired -> {

testapps/testapp/src/main/java/com/microsoft/identity/client/testapp/nativeauth/EmailPasswordSignInSignUpFragment.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ class EmailPasswordSignInSignUpFragment : Fragment() {
120120

121121
val actionResult = authClient.signIn(
122122
username = email,
123-
password = password
123+
password = password,
124+
scopes = listOf("User.Read")
124125
)
125126

126127
password.fill('0');
@@ -147,6 +148,9 @@ class EmailPasswordSignInSignUpFragment : Fragment() {
147148
)
148149
)
149150
}
151+
else {
152+
displayDialog("Unexpected result", actionResult.errorMessage)
153+
}
150154
}
151155
else -> {
152156
displayDialog( "Unexpected result", actionResult.toString())
@@ -271,6 +275,7 @@ class EmailPasswordSignInSignUpFragment : Fragment() {
271275
binding.resultAccessToken.text = ""
272276
binding.resultIdToken.text = ""
273277
}
278+
274279
private fun displayAccount(accountState: AccountState) {
275280
CoroutineScope(Dispatchers.Main).launch {
276281
val accessTokenState = accountState.getAccessToken()
@@ -326,6 +331,7 @@ class EmailPasswordSignInSignUpFragment : Fragment() {
326331
}
327332
}
328333
}
334+
329335
private fun navigateToSignUp(
330336
nextState: SignUpCodeRequiredState,
331337
codeLength: Int,

testapps/testapp/src/main/java/com/microsoft/identity/client/testapp/nativeauth/EmailSignInSignUpFragment.kt

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,21 @@ package com.microsoft.identity.client.testapp.nativeauth
2424

2525
import android.app.AlertDialog
2626
import android.os.Bundle
27-
import android.util.Log
2827
import android.view.LayoutInflater
2928
import android.view.View
3029
import android.view.ViewGroup
3130
import android.widget.Toast
3231
import androidx.fragment.app.Fragment
32+
import com.microsoft.identity.client.Account
33+
import com.microsoft.identity.client.AcquireTokenParameters
34+
import com.microsoft.identity.client.AuthenticationCallback
35+
import com.microsoft.identity.client.IAuthenticationResult
3336
import com.microsoft.identity.client.exception.MsalException
3437
import com.microsoft.identity.client.testapp.Constants
3538
import com.microsoft.identity.client.testapp.R
3639
import com.microsoft.identity.client.testapp.databinding.FragmentEmailSisuBinding
3740
import com.microsoft.identity.nativeauth.INativeAuthPublicClientApplication
41+
import com.microsoft.identity.nativeauth.statemachine.errors.SignInError
3842
import com.microsoft.identity.nativeauth.statemachine.results.GetAccessTokenResult
3943
import com.microsoft.identity.nativeauth.statemachine.results.GetAccountResult
4044
import com.microsoft.identity.nativeauth.statemachine.results.SignInResult
@@ -112,7 +116,8 @@ class EmailSignInSignUpFragment : Fragment() {
112116
val email = binding.emailText.text.toString()
113117

114118
val actionResult = authClient.signIn(
115-
username = email
119+
username = email,
120+
scopes = listOf("User.Read")
116121
)
117122

118123
when (actionResult) {
@@ -124,6 +129,23 @@ class EmailSignInSignUpFragment : Fragment() {
124129
channel = actionResult.channel
125130
)
126131
}
132+
is SignInError -> {
133+
if (actionResult.isBrowserRequired()) {
134+
Toast.makeText(requireContext(), actionResult.errorMessage, Toast.LENGTH_SHORT).show()
135+
136+
authClient.acquireToken(
137+
AcquireTokenParameters(
138+
AcquireTokenParameters.Builder()
139+
.startAuthorizationFromActivity(requireActivity())
140+
.withScopes(mutableListOf("profile", "openid", "email"))
141+
.withCallback(getAuthInteractiveCallback())
142+
)
143+
)
144+
}
145+
else {
146+
displayDialog("Unexpected result", actionResult.errorMessage)
147+
}
148+
}
127149
else -> {
128150
displayDialog(getString(R.string.msal_exception_title), "Unexpected result: $actionResult")
129151
}
@@ -163,7 +185,7 @@ class EmailSignInSignUpFragment : Fragment() {
163185
)
164186
}
165187
else -> {
166-
displayDialog(getString(R.string.msal_exception_title), "Unexpected result: $actionResult")
188+
displayDialog( "Unexpected result", actionResult.toString())
167189
}
168190
}
169191
} catch (exception: MsalException) {
@@ -306,4 +328,38 @@ class EmailSignInSignUpFragment : Fragment() {
306328
.replace(R.id.scenario_fragment, fragment)
307329
.commit()
308330
}
331+
332+
/**
333+
* Callback used for interactive request.
334+
* If succeeds we use the access token to call the Microsoft Graph.
335+
* Does not check cache.
336+
*/
337+
private fun getAuthInteractiveCallback(): AuthenticationCallback {
338+
return object : AuthenticationCallback {
339+
340+
override fun onSuccess(authenticationResult: IAuthenticationResult) {
341+
/* Successfully got a token, use it to call a protected resource - MSGraph */
342+
343+
val accountResult = authenticationResult.account as Account
344+
345+
/* Update account */
346+
emptyFields()
347+
updateUI(STATUS.SignedIn)
348+
val idToken = accountResult.idToken
349+
binding.resultIdToken.text =
350+
getString(R.string.result_id_token_text) + idToken
351+
352+
Toast.makeText(requireContext(), getString(R.string.sign_in_successful_message), Toast.LENGTH_SHORT).show()
353+
}
354+
355+
override fun onError(exception: MsalException) {
356+
/* Failed to acquireToken */
357+
displayDialog(getString(R.string.msal_exception_title), exception.errorCode)
358+
}
359+
360+
override fun onCancel() {
361+
/* User canceled the authentication */
362+
}
363+
}
364+
}
309365
}

testapps/testapp/src/main/java/com/microsoft/identity/client/testapp/nativeauth/SignInCodeFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class SignInCodeFragment : Fragment() {
5656
_binding = FragmentCodeBinding.inflate(inflater, container, false)
5757

5858
val bundle = this.arguments
59-
currentState = bundle!!.getSerializable(Constants.STATE) as SignInCodeRequiredState
59+
currentState = (bundle?.getParcelable(Constants.STATE) as? SignInCodeRequiredState)!!
6060
codeLength = bundle.getInt(Constants.CODE_LENGTH)
6161
sentTo = bundle.getString(Constants.SENT_TO)
6262
channel = bundle.getString(Constants.CHANNEL)

testapps/testapp/src/main/java/com/microsoft/identity/client/testapp/nativeauth/SignUpAttributesFragment.kt

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class SignUpAttributesFragment : Fragment() {
5757
_binding = FragmentAttributeBinding.inflate(inflater, container, false)
5858

5959
val bundle = this.arguments
60-
currentState = bundle!!.getSerializable(Constants.STATE) as SignUpAttributesRequiredState
60+
currentState = (bundle?.getParcelable(Constants.STATE) as? SignUpAttributesRequiredState)!!
6161

6262
init()
6363

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

8282
val attr1Key = binding.attr1KeyText.text.toString()
83-
if (attr1Key.isNotBlank()) {
84-
val attr1Value = binding.attr1ValueText.toString()
85-
attributes
86-
.customAttribute(attr1Key, attr1Value)
87-
}
83+
val attr1Value = binding.attr1ValueText.text.toString()
84+
attributes.customAttribute(attr1Key, attr1Value)
8885

8986
val attr2Key = binding.attr2KeyText.text.toString()
90-
if (attr2Key.isNotBlank()) {
91-
val attr2Value = binding.attr2ValueText.toString()
92-
attributes
93-
.customAttribute(attr2Key, attr2Value)
94-
}
87+
val attr2Value = binding.attr2ValueText.text.toString()
88+
attributes.customAttribute(attr2Key, attr2Value)
9589

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

testapps/testapp/src/main/java/com/microsoft/identity/client/testapp/nativeauth/SignUpCodeFragment.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import com.microsoft.identity.nativeauth.statemachine.errors.SubmitCodeError
3737
import com.microsoft.identity.nativeauth.statemachine.results.SignInResult
3838
import com.microsoft.identity.nativeauth.statemachine.results.SignUpResendCodeResult
3939
import com.microsoft.identity.nativeauth.statemachine.results.SignUpResult
40+
import com.microsoft.identity.nativeauth.statemachine.states.SignInCodeRequiredState
4041
import com.microsoft.identity.nativeauth.statemachine.states.SignInContinuationState
4142
import com.microsoft.identity.nativeauth.statemachine.states.SignUpAttributesRequiredState
4243
import com.microsoft.identity.nativeauth.statemachine.states.SignUpCodeRequiredState
@@ -60,7 +61,7 @@ class SignUpCodeFragment : Fragment() {
6061
_binding = FragmentCodeBinding.inflate(inflater, container, false)
6162

6263
val bundle = this.arguments
63-
currentState = bundle!!.getSerializable(Constants.STATE) as SignUpCodeRequiredState
64+
currentState = (bundle?.getParcelable(Constants.STATE) as? SignUpCodeRequiredState)!!
6465
codeLength = bundle.getInt(Constants.CODE_LENGTH)
6566
sentTo = bundle.getString(Constants.SENT_TO)
6667
channel = bundle.getString(Constants.CHANNEL)

testapps/testapp/src/main/java/com/microsoft/identity/client/testapp/nativeauth/SignUpPasswordFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class SignUpPasswordFragment : Fragment() {
5555
_binding = FragmentPasswordBinding.inflate(inflater, container, false)
5656

5757
val bundle = this.arguments
58-
currentState = bundle!!.getSerializable(Constants.STATE) as SignUpPasswordRequiredState
58+
currentState = (bundle?.getParcelable(Constants.STATE) as? SignUpPasswordRequiredState)!!
5959

6060
init()
6161

testapps/testapp/src/main/res/layout/fragment_attribute.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
android:layout_height="wrap_content"
1717
android:text="@string/attributes_intro"
1818
app:layout_constraintStart_toStartOf="parent"
19-
app:layout_constraintTop_toBottomOf="parent"
19+
app:layout_constraintTop_toTopOf="parent"
20+
style="@style/TextViewStyle"
2021
/>
2122

2223
<TextView

0 commit comments

Comments
 (0)