capacitor-sso-webview 0.0.1 → 0.0.2

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 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 url,
31
- final String redirectHost,
32
- final String redirectPathContains,
33
- final String title,
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 u = request.getUrl();
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
- String code = u.getQueryParameter("code");
111
- String state = u.getQueryParameter("state");
116
+ @Override
117
+ public void onPageFinished(WebView view, String pageUrl) {
118
+ super.onPageFinished(view, pageUrl);
112
119
 
113
- dismiss();
120
+ Uri u = Uri.parse(pageUrl);
121
+ String host = u.getHost();
122
+ String path = u.getPath();
114
123
 
115
- if (listener != null) {
116
- listener.onRedirectIntercepted(
117
- u.toString(),
118
- code != null ? code : "",
119
- state != null ? state : ""
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
- return true; /* BLOKIR navigasi — code TIDAK dikonsumsi */
123
- }
124
- return false;
167
+ );
168
+ }
169
+
170
+ private void dismiss() {
171
+ SSOWebView.this.dismiss();
125
172
  }
126
173
  });
127
174
 
@@ -38,6 +38,7 @@ public class SSOWebViewPlugin extends Plugin {
38
38
  ret.put("url", u);
39
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-sso-webview",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "SSO WebView with URL intercept",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",