capacitor-sso-webview 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -17,20 +17,20 @@ import android.widget.TextView;
|
|
|
17
17
|
public class SSOWebView {
|
|
18
18
|
|
|
19
19
|
public interface Listener {
|
|
20
|
-
void onRedirectIntercepted(String url, String code, String state);
|
|
20
|
+
void onRedirectIntercepted(String url, String code, String state, String body);
|
|
21
21
|
void onCancelled();
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
private Dialog
|
|
24
|
+
private Dialog dialog;
|
|
25
25
|
private WebView webView;
|
|
26
26
|
private boolean alreadyRedirected = false;
|
|
27
27
|
|
|
28
28
|
public void open(
|
|
29
29
|
final Activity activity,
|
|
30
|
-
final String
|
|
31
|
-
final String
|
|
32
|
-
final String
|
|
33
|
-
final String
|
|
30
|
+
final String url,
|
|
31
|
+
final String redirectHost,
|
|
32
|
+
final String redirectPathContains,
|
|
33
|
+
final String title,
|
|
34
34
|
final Listener listener
|
|
35
35
|
) {
|
|
36
36
|
alreadyRedirected = false;
|
|
@@ -93,9 +93,10 @@ public class SSOWebView {
|
|
|
93
93
|
cm.setAcceptThirdPartyCookies(webView, true);
|
|
94
94
|
|
|
95
95
|
webView.setWebViewClient(new WebViewClient() {
|
|
96
|
+
|
|
96
97
|
@Override
|
|
97
98
|
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
|
|
98
|
-
Uri
|
|
99
|
+
Uri u = request.getUrl();
|
|
99
100
|
String host = u.getHost();
|
|
100
101
|
String path = u.getPath();
|
|
101
102
|
|
|
@@ -106,22 +107,68 @@ public class SSOWebView {
|
|
|
106
107
|
|
|
107
108
|
if (isRedirect && !alreadyRedirected) {
|
|
108
109
|
alreadyRedirected = true;
|
|
110
|
+
/* Biarkan WebView navigate — server akan return JSON body */
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
109
115
|
|
|
110
|
-
|
|
111
|
-
|
|
116
|
+
@Override
|
|
117
|
+
public void onPageFinished(WebView view, String pageUrl) {
|
|
118
|
+
super.onPageFinished(view, pageUrl);
|
|
112
119
|
|
|
113
|
-
|
|
120
|
+
Uri u = Uri.parse(pageUrl);
|
|
121
|
+
String host = u.getHost();
|
|
122
|
+
String path = u.getPath();
|
|
114
123
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
124
|
+
boolean isRedirectPage =
|
|
125
|
+
host != null && host.equalsIgnoreCase(redirectHost) &&
|
|
126
|
+
path != null && path.toLowerCase()
|
|
127
|
+
.contains(redirectPathContains.toLowerCase());
|
|
128
|
+
|
|
129
|
+
if (!isRedirectPage) return;
|
|
130
|
+
|
|
131
|
+
String code = u.getQueryParameter("code");
|
|
132
|
+
String state = u.getQueryParameter("state");
|
|
133
|
+
final String finalUrl = pageUrl;
|
|
134
|
+
final String finalCode = code != null ? code : "";
|
|
135
|
+
final String finalState = state != null ? state : "";
|
|
136
|
+
|
|
137
|
+
/* Baca body JSON via evaluateJavascript — tidak fetch ulang */
|
|
138
|
+
view.evaluateJavascript(
|
|
139
|
+
"(function(){ return document.body ? document.body.innerText : ''; })()",
|
|
140
|
+
value -> {
|
|
141
|
+
/* Unescape JS string result */
|
|
142
|
+
String body = value;
|
|
143
|
+
if (body != null) {
|
|
144
|
+
if (body.startsWith("\"") && body.endsWith("\"")) {
|
|
145
|
+
body = body.substring(1, body.length() - 1);
|
|
146
|
+
}
|
|
147
|
+
body = body.replace("\\\"", "\"")
|
|
148
|
+
.replace("\\n", "\n")
|
|
149
|
+
.replace("\\r", "")
|
|
150
|
+
.replace("\\/", "/");
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
final String finalBody = body != null ? body : "";
|
|
154
|
+
android.util.Log.d("SSOWebView",
|
|
155
|
+
"body len=" + finalBody.length() +
|
|
156
|
+
" preview=" + finalBody.substring(0, Math.min(120, finalBody.length())));
|
|
157
|
+
|
|
158
|
+
/* Tutup dialog setelah body dibaca */
|
|
159
|
+
activity.runOnUiThread(this::dismiss);
|
|
160
|
+
|
|
161
|
+
if (listener != null) {
|
|
162
|
+
listener.onRedirectIntercepted(
|
|
163
|
+
finalUrl, finalCode, finalState, finalBody
|
|
164
|
+
);
|
|
165
|
+
}
|
|
121
166
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
private void dismiss() {
|
|
171
|
+
SSOWebView.this.dismiss();
|
|
125
172
|
}
|
|
126
173
|
});
|
|
127
174
|
|
|
@@ -33,11 +33,12 @@ public class SSOWebViewPlugin extends Plugin {
|
|
|
33
33
|
title,
|
|
34
34
|
new SSOWebView.Listener() {
|
|
35
35
|
@Override
|
|
36
|
-
public void onRedirectIntercepted(String u, String code, String state) {
|
|
36
|
+
public void onRedirectIntercepted(String u, String code, String state, String body) {
|
|
37
37
|
JSObject ret = new JSObject();
|
|
38
|
-
ret.put("url",
|
|
39
|
-
ret.put("code",
|
|
38
|
+
ret.put("url", u);
|
|
39
|
+
ret.put("code", code);
|
|
40
40
|
ret.put("state", state);
|
|
41
|
+
ret.put("body", body);
|
|
41
42
|
notifyListeners("ssoRedirect", ret);
|
|
42
43
|
}
|
|
43
44
|
|