Skip to content

Commit b90b5ca

Browse files
authored
Cherrypick JavaScript api change for ESTS validation (#2663)
Need to update our JavaScript api's exposed name in Webview to align with platform-agnostic approach (have parity with ios).
1 parent 529ad89 commit b90b5ca

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

common/src/main/java/com/microsoft/identity/common/internal/broker/AuthUxJavaScriptInterface.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class AuthUxJavaScriptInterface {
4444
// long enough for AuthApp to call the broker api to fetch the number match
4545
companion object {
4646
val TAG = AuthUxJavaScriptInterface::class.java.simpleName
47-
private const val JAVASCRIPT_INTERFACE_NAME = "ClientBrokerJS"
47+
private const val JAVASCRIPT_INTERFACE_NAME = "broker"
4848

4949
fun getInterfaceName(): String {
5050
return JAVASCRIPT_INTERFACE_NAME
@@ -103,8 +103,8 @@ class AuthUxJavaScriptInterface {
103103
* https://microsoft-my.sharepoint-df.com/:w:/p/veenasoman/EY1AZIeT8X5KrXVz97Vx520B3Jj0fBLSPlklnoRvcmbh0Q?e=VzNFd1&ovuser=72f988bf-86f1-41af-91ab-2d7cd011db47%2Cfadidurah%40microsoft.com&clickparams=eyJBcHBOYW1lIjoiVGVhbXMtRGVza3RvcCIsIkFwcFZlcnNpb24iOiI0OS8yNTA1MDQwMTYwOSIsIkhhc0ZlZGVyYXRlZFVzZXIiOmZhbHNlfQ%3D%3D
104104
*/
105105
@JavascriptInterface
106-
fun postMessageToBroker(jsonPayload: String) {
107-
val methodTag = "$TAG:postMessageToBroker"
106+
fun receiveAuthUxMessage(jsonPayload: String) {
107+
val methodTag = "$TAG:receiveAuthUxMessage"
108108
Logger.info(methodTag, "Received a payload from AuthUX through JavaScript API.")
109109

110110
try {

common/src/main/java/com/microsoft/identity/common/internal/providers/oauth2/WebViewAuthorizationFragment.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import android.view.View;
3535
import android.view.ViewGroup;
3636
import android.webkit.PermissionRequest;
37-
import android.webkit.ValueCallback;
3837
import android.webkit.WebChromeClient;
3938
import android.webkit.WebSettings;
4039
import android.webkit.WebView;

common/src/main/java/com/microsoft/identity/common/internal/ui/webview/AzureActiveDirectoryWebViewClient.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public class AzureActiveDirectoryWebViewClient extends OAuth2WebViewClient {
121121
private final SwitchBrowserRequestHandler mSwitchBrowserRequestHandler;
122122
private HashMap<String, String> mRequestHeaders;
123123
private String mRequestUrl;
124+
private boolean mAuthUxJavaScriptInterfaceAdded = false;
124125

125126
public AzureActiveDirectoryWebViewClient(@NonNull final Activity activity,
126127
@NonNull final IAuthorizationCompletionCallback completionCallback,
@@ -143,6 +144,7 @@ public void initializeAuthUxJavaScriptApi(@NonNull final WebView view, final Str
143144
// If broker request, and a valid url, expose JavaScript API
144145
Logger.info(TAG, "Adding AuthUx JavaScript Interface");
145146
view.addJavascriptInterface(new AuthUxJavaScriptInterface(), AuthUxJavaScriptInterface.Companion.getInterfaceName());
147+
mAuthUxJavaScriptInterfaceAdded = true;
146148
}
147149
}
148150

@@ -152,6 +154,24 @@ private boolean shouldExposeJavaScriptInterface(final String url) {
152154
&& CommonFlightsManager.INSTANCE.getFlightsProvider().isFlightEnabled(CommonFlight.ENABLE_JS_API_FOR_AUTHUX);
153155
}
154156

157+
@Override
158+
public void onPageFinished(final WebView view,
159+
final String url) {
160+
super.onPageFinished(view, url);
161+
162+
if (mAuthUxJavaScriptInterfaceAdded) {
163+
// Add a function to the api. Must do this to first stringify the dict object, as Android @JavaScriptInterface does not support
164+
// passing dict objects through Javascript APIs, only Strings and primitive types. Server side will be sending message in a dict
165+
String jsScript = "window." + AuthUxJavaScriptInterface.Companion.getInterfaceName() + ".postMessageToBroker = function(message) { " +
166+
" window." + AuthUxJavaScriptInterface.Companion.getInterfaceName() + ".receiveAuthUxMessage(JSON.stringify(message)); " +
167+
"};";
168+
169+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
170+
view.evaluateJavascript(jsScript, null);
171+
}
172+
}
173+
}
174+
155175
/**
156176
* Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView.
157177
* This method was deprecated in API level 24.
@@ -218,10 +238,12 @@ private boolean handleUrl(final WebView view, final String url) {
218238
// If broker request, and a valid url, expose JavaScript API
219239
Logger.info(methodTag, "Adding AuthUx JavaScript Interface");
220240
view.addJavascriptInterface(new AuthUxJavaScriptInterface(), AuthUxJavaScriptInterface.Companion.getInterfaceName());
221-
} else {
241+
mAuthUxJavaScriptInterfaceAdded = true;
242+
} else if (mAuthUxJavaScriptInterfaceAdded) {
222243
// Remove AuthUx JavaScript Interface
223244
Logger.info(methodTag, "Removing AuthUx JavaScript Interface");
224245
view.removeJavascriptInterface(AuthUxJavaScriptInterface.Companion.getInterfaceName());
246+
mAuthUxJavaScriptInterfaceAdded = false;
225247
}
226248

227249

common/src/test/java/com/microsoft/identity/common/internal/broker/AuthUxJavaScriptInterfaceTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,27 @@ class AuthUxJavaScriptInterfaceTest {
6060
}
6161

6262
@Test
63-
fun `test postMessageToBroker with NUMBER_MATCH function`() {
63+
fun `test receiveAuthUxMessage with NUMBER_MATCH function`() {
6464
// Call the method
65-
authUxJavaScriptInterface.postMessageToBroker(numberMatchTestPayload)
65+
authUxJavaScriptInterface.receiveAuthUxMessage(numberMatchTestPayload)
6666

6767
// Verify that the data was stored in NumberMatchHelper
6868
val storedValue = NumberMatchHelper.numberMatchMap[mockSessionId]
6969
Assert.assertTrue(storedValue == mockNumberMatchValue)
7070
}
7171

7272
@Test
73-
fun `test postMessageToBroker with empty json`() {
73+
fun `test receiveAuthUxMessage with empty json`() {
7474
// Call the method
75-
authUxJavaScriptInterface.postMessageToBroker("{}")
75+
authUxJavaScriptInterface.receiveAuthUxMessage("{}")
7676

7777
// Should not get an exception
7878
}
7979

8080
@Test
81-
fun `test postMessageToBroker with non-json string`() {
81+
fun `test receiveAuthUxMessage with non-json string`() {
8282
// Call the method
83-
authUxJavaScriptInterface.postMessageToBroker("NotAJson")
83+
authUxJavaScriptInterface.receiveAuthUxMessage("NotAJson")
8484

8585
// Should not get an exception
8686
}

0 commit comments

Comments
 (0)