iwashi1t 4 anni fa
parent
commit
15110382eb
No account linked to committer's email address

+ 42
- 0
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java Vedi File

@@ -25,6 +25,7 @@ import android.webkit.ConsoleMessage;
25 25
 import android.webkit.CookieManager;
26 26
 import android.webkit.DownloadListener;
27 27
 import android.webkit.GeolocationPermissions;
28
+import android.webkit.HttpAuthHandler;
28 29
 import android.webkit.JavascriptInterface;
29 30
 import android.webkit.RenderProcessGoneDetail;
30 31
 import android.webkit.SslErrorHandler;
@@ -533,6 +534,19 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
533 534
     view.loadUrl(BLANK_URL);
534 535
   }
535 536
 
537
+  @ReactProp(name = "basicAuthCredential")
538
+  public void setBasicAuthCredential(WebView view, @Nullable ReadableMap credential) {
539
+    @Nullable BasicAuthCredential basicAuthCredential = null;
540
+    if (credential != null) {
541
+      if (credential.hasKey("username") && credential.hasKey("password")) {
542
+        String username = credential.getString("username");
543
+        String password = credential.getString("password");
544
+        basicAuthCredential = new BasicAuthCredential(username, password);
545
+      }
546
+    }
547
+    ((RNCWebView) view).setBasicAuthCredential(basicAuthCredential);
548
+  }
549
+
536 550
   @ReactProp(name = "onContentSizeChange")
537 551
   public void setOnContentSizeChange(WebView view, boolean sendContentSizeChangeEvents) {
538 552
     ((RNCWebView) view).setSendContentSizeChangeEvents(sendContentSizeChangeEvents);
@@ -777,11 +791,16 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
777 791
     ReadableArray mUrlPrefixesForDefaultIntent;
778 792
     protected RNCWebView.ProgressChangedFilter progressChangedFilter = null;
779 793
     protected @Nullable String ignoreErrFailedForThisURL = null;
794
+    protected @Nullable BasicAuthCredential basicAuthCredential = null;
780 795
 
781 796
     public void setIgnoreErrFailedForThisURL(@Nullable String url) {
782 797
       ignoreErrFailedForThisURL = url;
783 798
     }
784 799
 
800
+    public void setBasicAuthCredential(@Nullable BasicAuthCredential credential) {
801
+      basicAuthCredential = credential;
802
+    }
803
+
785 804
     @Override
786 805
     public void onPageFinished(WebView webView, String url) {
787 806
       super.onPageFinished(webView, url);
@@ -866,6 +885,15 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
866 885
       return this.shouldOverrideUrlLoading(view, url);
867 886
     }
868 887
 
888
+    @Override
889
+    public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
890
+      if (basicAuthCredential != null) {
891
+        handler.proceed(basicAuthCredential.username, basicAuthCredential.password);
892
+        return;
893
+      }
894
+      super.onReceivedHttpAuthRequest(view, handler, host, realm);
895
+    }
896
+
869 897
     @Override
870 898
     public void onReceivedSslError(final WebView webView, final SslErrorHandler handler, final SslError error) {
871 899
         handler.cancel();
@@ -1215,6 +1243,10 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1215 1243
       mRNCWebViewClient.setIgnoreErrFailedForThisURL(url);
1216 1244
     }
1217 1245
 
1246
+    public void setBasicAuthCredential(BasicAuthCredential credential) {
1247
+      mRNCWebViewClient.setBasicAuthCredential(credential);
1248
+    }
1249
+
1218 1250
     public void setSendContentSizeChangeEvents(boolean sendContentSizeChangeEvents) {
1219 1251
       this.sendContentSizeChangeEvents = sendContentSizeChangeEvents;
1220 1252
     }
@@ -1470,3 +1502,13 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1470 1502
     }
1471 1503
   }
1472 1504
 }
1505
+
1506
+class BasicAuthCredential {
1507
+  String username;
1508
+  String password;
1509
+
1510
+  BasicAuthCredential(String username, String password) {
1511
+    this.username = username;
1512
+    this.password = password;
1513
+  }
1514
+}

+ 1
- 0
apple/RNCWebView.h Vedi File

@@ -64,6 +64,7 @@
64 64
 @property (nonatomic, copy) NSString * _Nullable allowingReadAccessToURL;
65 65
 @property (nonatomic, assign) BOOL pullToRefreshEnabled;
66 66
 @property (nonatomic, weak) UIRefreshControl * refreshControl;
67
+@property (nonatomic, copy) NSDictionary * _Nullable basicAuthCredential;
67 68
 
68 69
 #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 /* iOS 13 */
69 70
 @property (nonatomic, assign) WKContentMode contentMode;

+ 9
- 0
apple/RNCWebView.m Vedi File

@@ -781,6 +781,15 @@ static NSDictionary* customCertificatesForHost;
781 781
             }
782 782
         }
783 783
     }
784
+    if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodHTTPBasic) {
785
+        NSString *username = [_basicAuthCredential valueForKey:@"username"];
786
+        NSString *password = [_basicAuthCredential valueForKey:@"password"];
787
+        if (username && password) {
788
+            NSURLCredential *credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceNone];
789
+            completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
790
+            return;
791
+        }
792
+    }
784 793
     completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
785 794
 }
786 795
 

+ 1
- 0
apple/RNCWebViewManager.m Vedi File

@@ -75,6 +75,7 @@ RCT_EXPORT_VIEW_PROPERTY(applicationNameForUserAgent, NSString)
75 75
 RCT_EXPORT_VIEW_PROPERTY(cacheEnabled, BOOL)
76 76
 RCT_EXPORT_VIEW_PROPERTY(allowsLinkPreview, BOOL)
77 77
 RCT_EXPORT_VIEW_PROPERTY(allowingReadAccessToURL, NSString)
78
+RCT_EXPORT_VIEW_PROPERTY(basicAuthCredential, NSDictionary)
78 79
 
79 80
 #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
80 81
 RCT_EXPORT_VIEW_PROPERTY(contentInsetAdjustmentBehavior, UIScrollViewContentInsetAdjustmentBehavior)

+ 12
- 0
docs/Reference.md Vedi File

@@ -71,6 +71,7 @@ This document lays out the current public properties and methods for the React N
71 71
 - [`pullToRefreshEnabled`](Reference.md#pullToRefreshEnabled)
72 72
 - [`ignoreSilentHardwareSwitch`](Reference.md#ignoreSilentHardwareSwitch)
73 73
 - [`onFileDownload`](Reference.md#onFileDownload)
74
+- [`basicAuthCredential`](Reference.md#basicAuthCredential)
74 75
 
75 76
 ## Methods Index
76 77
 
@@ -1263,6 +1264,17 @@ Example:
1263 1264
 | ------- | -------- | -------- |
1264 1265
 | function | No       | iOS      |
1265 1266
 
1267
+### `basicAuthCredential`[⬆](#props-index)<!-- Link generated with jump2header -->
1268
+
1269
+An object that specifies the credentials of a user to be used for basic authentication.
1270
+
1271
+- `username` (string) - A username used for basic authentication.
1272
+- `password` (string) - A password used for basic authentication.
1273
+
1274
+| Type   | Required |
1275
+| ------ | -------- |
1276
+| object | No       |
1277
+
1266 1278
 ## Methods
1267 1279
 
1268 1280
 ### `extraNativeComponentConfig()`[⬆](#methods-index)<!-- Link generated with jump2header -->

+ 18
- 0
src/WebViewTypes.ts Vedi File

@@ -249,6 +249,18 @@ export type OnShouldStartLoadWithRequest = (
249 249
   event: ShouldStartLoadRequest,
250 250
 ) => boolean;
251 251
 
252
+export interface BasicAuthCredential {
253
+  /**
254
+   * A username used for basic authentication.
255
+   */
256
+  username: string;
257
+
258
+  /**
259
+   * A password used for basic authentication.
260
+   */
261
+  password: string;
262
+}
263
+
252 264
 export interface CommonNativeWebViewProps extends ViewProps {
253 265
   cacheEnabled?: boolean;
254 266
   incognito?: boolean;
@@ -277,6 +289,7 @@ export interface CommonNativeWebViewProps extends ViewProps {
277 289
    * Append to the existing user-agent. Overridden if `userAgent` is set.
278 290
    */
279 291
   applicationNameForUserAgent?: string;
292
+  basicAuthCredential?: BasicAuthCredential;
280 293
 }
281 294
 
282 295
 export interface AndroidNativeWebViewProps extends CommonNativeWebViewProps {
@@ -1031,4 +1044,9 @@ export interface WebViewSharedProps extends ViewProps {
1031 1044
    * Append to the existing user-agent. Overridden if `userAgent` is set.
1032 1045
    */
1033 1046
   applicationNameForUserAgent?: string;
1047
+
1048
+  /**
1049
+   * An object that specifies the credentials of a user to be used for basic authentication.
1050
+   */
1051
+  basicAuthCredential?: BasicAuthCredential;
1034 1052
 }