Skip to content

Commit e71e5ef

Browse files
Support WeChat login in WebKitWebViewUIImplementation
1 parent 5eecae9 commit e71e5ef

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

packages/authgear-react-native/android/src/main/java/com/authgear/reactnative/AuthgearReactNativeModule.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
import java.security.SecureRandom;
1818
import java.security.Signature;
1919
import java.security.interfaces.RSAPublicKey;
20+
import java.util.Objects;
2021
import java.util.UUID;
2122

2223
import android.app.Activity;
24+
import android.content.BroadcastReceiver;
2325
import android.content.Context;
2426
import android.content.ContentResolver;
2527
import android.content.Intent;
28+
import android.content.IntentFilter;
2629
import android.content.SharedPreferences;
2730
import android.content.pm.PackageInfo;
2831
import android.graphics.Color;
@@ -46,13 +49,15 @@
4649
import com.facebook.react.bridge.ReadableMap;
4750
import com.facebook.react.bridge.WritableArray;
4851
import com.facebook.react.bridge.WritableMap;
52+
import com.facebook.react.modules.core.DeviceEventManagerModule;
4953

5054
import com.google.crypto.tink.shaded.protobuf.InvalidProtocolBufferException;
5155

5256
import androidx.annotation.NonNull;
5357
import androidx.annotation.RequiresApi;
5458
import androidx.biometric.BiometricManager;
5559
import androidx.biometric.BiometricPrompt;
60+
import androidx.core.content.ContextCompat;
5661
import androidx.fragment.app.FragmentActivity;
5762
import androidx.security.crypto.MasterKey;
5863
import androidx.security.crypto.EncryptedSharedPreferences;
@@ -63,6 +68,7 @@ public class AuthgearReactNativeModule extends ReactContextBaseJavaModule implem
6368

6469
private static class Handle {
6570
Promise mPromise;
71+
BroadcastReceiver mBroadcastReceiver;
6672

6773
Handle(Promise promise) {
6874
this.mPromise = promise;
@@ -786,16 +792,40 @@ public void openAuthorizeURLWithWebView(ReadableMap options, Promise promise) {
786792

787793
Context ctx = currentActivity;
788794

795+
String invocationID = options.getString("invocationID");
789796
Uri url = Uri.parse(options.getString("url"));
790797
Uri redirectURI = Uri.parse(options.getString("redirectURI"));
791798
Integer actionBarBackgroundColor = this.readColorInt(options, "actionBarBackgroundColor");
792799
Integer actionBarButtonTintColor = this.readColorInt(options, "actionBarButtonTintColor");
800+
String wechatRedirectURIString = options.getString("androidWechatRedirectURI");
793801

794802
WebKitWebViewActivity.Options webViewOptions = new WebKitWebViewActivity.Options();
795803
webViewOptions.url = url;
796804
webViewOptions.redirectURI = redirectURI;
797805
webViewOptions.actionBarBackgroundColor = actionBarBackgroundColor;
798806
webViewOptions.actionBarButtonTintColor = actionBarButtonTintColor;
807+
if (wechatRedirectURIString != null) {
808+
String intentAction = String.format("com.authgear.reactnative.callback.%s", invocationID);
809+
handle.mBroadcastReceiver = new BroadcastReceiver() {
810+
@Override
811+
public void onReceive(Context context, Intent intent) {
812+
if (Objects.equals(intent.getAction(), intentAction)) {
813+
String uriString = intent.getExtras().getString("uri");
814+
WritableMap map = Arguments.createMap();
815+
map.putString("invocationID", invocationID);
816+
map.putString("url", uriString);
817+
AuthgearReactNativeModule.this.sendEvent("authgear-react-native", map);
818+
}
819+
}
820+
};
821+
822+
IntentFilter intentFilter = new IntentFilter(intentAction);
823+
824+
webViewOptions.wechatRedirectURI = Uri.parse(wechatRedirectURIString);
825+
webViewOptions.wechatRedirectURIIntentAction = intentAction;
826+
827+
ContextCompat.registerReceiver(ctx.getApplicationContext(), handle.mBroadcastReceiver, intentFilter, ContextCompat.RECEIVER_NOT_EXPORTED);
828+
}
799829

800830
Intent intent = WebKitWebViewActivity.createIntent(ctx, webViewOptions);
801831
currentActivity.startActivityForResult(intent, requestCode);
@@ -820,6 +850,10 @@ private Integer readColorInt(ReadableMap map, String key) {
820850
return null;
821851
}
822852

853+
private void sendEvent(String name, ReadableMap map) {
854+
this.reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(name, map);
855+
}
856+
823857
@ReactMethod
824858
public void openAuthorizeURL(String urlString, String callbackURL, boolean shareSessionWithSystemBrowser, Promise promise) {
825859
final Handle handle = new Handle(promise);
@@ -981,6 +1015,9 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
9811015
if (startActivityHandle != null) {
9821016
final int tag = startActivityHandle.tag;
9831017
final Promise promise = startActivityHandle.value.mPromise;
1018+
if (startActivityHandle.value.mBroadcastReceiver != null) {
1019+
activity.getApplicationContext().unregisterReceiver(startActivityHandle.value.mBroadcastReceiver);
1020+
}
9841021
switch (tag) {
9851022
case ACTIVITY_PROMISE_TAG_CODE_AUTHORIZATION:
9861023
if (resultCode == Activity.RESULT_CANCELED) {

packages/authgear-react-native/android/src/main/java/com/authgear/reactnative/WebKitWebViewActivity.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public static class Options {
4949
public Integer actionBarBackgroundColor;
5050
public Integer actionBarButtonTintColor;
5151

52+
public Uri wechatRedirectURI;
53+
public String wechatRedirectURIIntentAction;
54+
5255
public Options() {}
5356

5457
private Options(Bundle bundle) {
@@ -60,18 +63,34 @@ private Options(Bundle bundle) {
6063
if (bundle.containsKey("actionBarButtonTintColor")) {
6164
this.actionBarButtonTintColor = bundle.getInt("actionBarButtonTintColor");
6265
}
66+
if (bundle.containsKey("wechatRedirectURI")) {
67+
this.wechatRedirectURI = bundle.getParcelable("wechatRedirectURI");
68+
}
69+
if (bundle.containsKey("wechatRedirectURIIntentAction")) {
70+
this.wechatRedirectURIIntentAction = bundle.getString("wechatRedirectURIIntentAction");
71+
}
6372
}
6473

6574
public Bundle toBundle() {
6675
Bundle bundle = new Bundle();
76+
6777
bundle.putParcelable("url", this.url);
6878
bundle.putParcelable("redirectURI", this.redirectURI);
79+
6980
if (this.actionBarBackgroundColor != null) {
7081
bundle.putInt("actionBarBackgroundColor", this.actionBarBackgroundColor);
7182
}
7283
if (this.actionBarButtonTintColor != null) {
7384
bundle.putInt("actionBarButtonTintColor", this.actionBarButtonTintColor);
7485
}
86+
87+
if (this.wechatRedirectURI != null) {
88+
bundle.putParcelable("wechatRedirectURI", this.wechatRedirectURI);
89+
}
90+
if (this.wechatRedirectURIIntentAction != null) {
91+
bundle.putString("wechatRedirectURIIntentAction", this.wechatRedirectURIIntentAction);
92+
}
93+
7594
return bundle;
7695
}
7796
}
@@ -122,6 +141,9 @@ private boolean shouldOverrideUrlLoading(Uri uri) {
122141
if (this.checkRedirectURI(uri)) {
123142
return true;
124143
}
144+
if (this.checkWechatRedirectURI(uri)) {
145+
return true;
146+
}
125147
return false;
126148
}
127149

@@ -137,6 +159,26 @@ private boolean checkRedirectURI(Uri uri) {
137159
return false;
138160
}
139161

162+
private boolean checkWechatRedirectURI(Uri uri) {
163+
Options options = this.activity.getOptions();
164+
Uri wechatRedirectURI = options.wechatRedirectURI;
165+
String wechatRedirectURIIntentAction = options.wechatRedirectURIIntentAction;
166+
if (wechatRedirectURI == null || wechatRedirectURIIntentAction == null) {
167+
return false;
168+
}
169+
170+
Uri withoutQuery = this.removeQueryAndFragment(uri);
171+
if (withoutQuery.toString().equals(wechatRedirectURI.toString())) {
172+
Intent intent = new Intent(wechatRedirectURIIntentAction);
173+
intent.setPackage(this.activity.getApplicationContext().getPackageName());
174+
intent.putExtra("uri", uri.toString());
175+
this.activity.getApplicationContext().sendBroadcast(intent);
176+
return true;
177+
}
178+
179+
return false;
180+
}
181+
140182
private Uri removeQueryAndFragment(Uri uri) {
141183
return uri.buildUpon().query(null).fragment(null).build();
142184
}

0 commit comments

Comments
 (0)