feat(auth): Basic Authentication Support (#1467)
Some people prefer using this new prop over the http://username:password@example.com syntax, now webview supports both
This commit is contained in:
parent
c74c1fd113
commit
0c42a9812e
|
@ -28,6 +28,7 @@ import android.webkit.ConsoleMessage;
|
|||
import android.webkit.CookieManager;
|
||||
import android.webkit.DownloadListener;
|
||||
import android.webkit.GeolocationPermissions;
|
||||
import android.webkit.HttpAuthHandler;
|
||||
import android.webkit.JavascriptInterface;
|
||||
import android.webkit.RenderProcessGoneDetail;
|
||||
import android.webkit.SslErrorHandler;
|
||||
|
@ -556,6 +557,19 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
|||
view.loadUrl(BLANK_URL);
|
||||
}
|
||||
|
||||
@ReactProp(name = "basicAuthCredential")
|
||||
public void setBasicAuthCredential(WebView view, @Nullable ReadableMap credential) {
|
||||
@Nullable BasicAuthCredential basicAuthCredential = null;
|
||||
if (credential != null) {
|
||||
if (credential.hasKey("username") && credential.hasKey("password")) {
|
||||
String username = credential.getString("username");
|
||||
String password = credential.getString("password");
|
||||
basicAuthCredential = new BasicAuthCredential(username, password);
|
||||
}
|
||||
}
|
||||
((RNCWebView) view).setBasicAuthCredential(basicAuthCredential);
|
||||
}
|
||||
|
||||
@ReactProp(name = "onContentSizeChange")
|
||||
public void setOnContentSizeChange(WebView view, boolean sendContentSizeChangeEvents) {
|
||||
((RNCWebView) view).setSendContentSizeChangeEvents(sendContentSizeChangeEvents);
|
||||
|
@ -849,11 +863,16 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
|||
ReadableArray mUrlPrefixesForDefaultIntent;
|
||||
protected RNCWebView.ProgressChangedFilter progressChangedFilter = null;
|
||||
protected @Nullable String ignoreErrFailedForThisURL = null;
|
||||
protected @Nullable BasicAuthCredential basicAuthCredential = null;
|
||||
|
||||
public void setIgnoreErrFailedForThisURL(@Nullable String url) {
|
||||
ignoreErrFailedForThisURL = url;
|
||||
}
|
||||
|
||||
public void setBasicAuthCredential(@Nullable BasicAuthCredential credential) {
|
||||
basicAuthCredential = credential;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageFinished(WebView webView, String url) {
|
||||
super.onPageFinished(webView, url);
|
||||
|
@ -938,6 +957,15 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
|||
return this.shouldOverrideUrlLoading(view, url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
|
||||
if (basicAuthCredential != null) {
|
||||
handler.proceed(basicAuthCredential.username, basicAuthCredential.password);
|
||||
return;
|
||||
}
|
||||
super.onReceivedHttpAuthRequest(view, handler, host, realm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceivedSslError(final WebView webView, final SslErrorHandler handler, final SslError error) {
|
||||
// onReceivedSslError is called for most requests, per Android docs: https://developer.android.com/reference/android/webkit/WebViewClient#onReceivedSslError(android.webkit.WebView,%2520android.webkit.SslErrorHandler,%2520android.net.http.SslError)
|
||||
|
@ -1463,6 +1491,10 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
|||
mRNCWebViewClient.setIgnoreErrFailedForThisURL(url);
|
||||
}
|
||||
|
||||
public void setBasicAuthCredential(BasicAuthCredential credential) {
|
||||
mRNCWebViewClient.setBasicAuthCredential(credential);
|
||||
}
|
||||
|
||||
public void setSendContentSizeChangeEvents(boolean sendContentSizeChangeEvents) {
|
||||
this.sendContentSizeChangeEvents = sendContentSizeChangeEvents;
|
||||
}
|
||||
|
@ -1737,3 +1769,13 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
class BasicAuthCredential {
|
||||
String username;
|
||||
String password;
|
||||
|
||||
BasicAuthCredential(String username, String password) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -67,6 +67,7 @@
|
|||
@property (nonatomic, assign) BOOL directionalLockEnabled;
|
||||
@property (nonatomic, assign) BOOL ignoreSilentHardwareSwitch;
|
||||
@property (nonatomic, copy) NSString * _Nullable allowingReadAccessToURL;
|
||||
@property (nonatomic, copy) NSDictionary * _Nullable basicAuthCredential;
|
||||
@property (nonatomic, assign) BOOL pullToRefreshEnabled;
|
||||
@property (nonatomic, assign) BOOL enableApplePay;
|
||||
#if !TARGET_OS_OSX
|
||||
|
|
|
@ -840,6 +840,15 @@ static NSDictionary* customCertificatesForHost;
|
|||
}
|
||||
}
|
||||
}
|
||||
if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodHTTPBasic) {
|
||||
NSString *username = [_basicAuthCredential valueForKey:@"username"];
|
||||
NSString *password = [_basicAuthCredential valueForKey:@"password"];
|
||||
if (username && password) {
|
||||
NSURLCredential *credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceNone];
|
||||
completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
|
||||
return;
|
||||
}
|
||||
}
|
||||
completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
|
||||
}
|
||||
|
||||
|
|
|
@ -76,6 +76,7 @@ RCT_EXPORT_VIEW_PROPERTY(applicationNameForUserAgent, NSString)
|
|||
RCT_EXPORT_VIEW_PROPERTY(cacheEnabled, BOOL)
|
||||
RCT_EXPORT_VIEW_PROPERTY(allowsLinkPreview, BOOL)
|
||||
RCT_EXPORT_VIEW_PROPERTY(allowingReadAccessToURL, NSString)
|
||||
RCT_EXPORT_VIEW_PROPERTY(basicAuthCredential, NSDictionary)
|
||||
|
||||
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
|
||||
RCT_EXPORT_VIEW_PROPERTY(contentInsetAdjustmentBehavior, UIScrollViewContentInsetAdjustmentBehavior)
|
||||
|
|
|
@ -79,6 +79,7 @@ This document lays out the current public properties and methods for the React N
|
|||
- [`limitsNavigationsToAppBoundDomains`](Reference.md#limitsNavigationsToAppBoundDomains)
|
||||
- [`autoManageStatusBarEnabled`](Reference.md#autoManageStatusBarEnabled)
|
||||
- [`setSupportMultipleWindows`](Reference.md#setSupportMultipleWindows)
|
||||
- [`basicAuthCredential`](Reference.md#basicAuthCredential)
|
||||
- [`enableApplePay`](Reference.md#enableApplePay)
|
||||
- [`forceDarkOn`](Reference.md#forceDarkOn)
|
||||
|
||||
|
@ -1440,6 +1441,17 @@ Example:
|
|||
<WebView forceDarkOn={false} />
|
||||
```
|
||||
|
||||
### `basicAuthCredential`
|
||||
|
||||
An object that specifies the credentials of a user to be used for basic authentication.
|
||||
|
||||
- `username` (string) - A username used for basic authentication.
|
||||
- `password` (string) - A password used for basic authentication.
|
||||
|
||||
| Type | Required |
|
||||
| ------ | -------- |
|
||||
| object | No |
|
||||
|
||||
## Methods
|
||||
|
||||
### `goForward()`[⬆](#methods-index)<!-- Link generated with jump2header -->
|
||||
|
|
|
@ -251,6 +251,18 @@ export type OnShouldStartLoadWithRequest = (
|
|||
event: ShouldStartLoadRequest,
|
||||
) => boolean;
|
||||
|
||||
export interface BasicAuthCredential {
|
||||
/**
|
||||
* A username used for basic authentication.
|
||||
*/
|
||||
username: string;
|
||||
|
||||
/**
|
||||
* A password used for basic authentication.
|
||||
*/
|
||||
password: string;
|
||||
}
|
||||
|
||||
export interface CommonNativeWebViewProps extends ViewProps {
|
||||
cacheEnabled?: boolean;
|
||||
incognito?: boolean;
|
||||
|
@ -279,6 +291,7 @@ export interface CommonNativeWebViewProps extends ViewProps {
|
|||
* Append to the existing user-agent. Overridden if `userAgent` is set.
|
||||
*/
|
||||
applicationNameForUserAgent?: string;
|
||||
basicAuthCredential?: BasicAuthCredential;
|
||||
}
|
||||
|
||||
export interface AndroidNativeWebViewProps extends CommonNativeWebViewProps {
|
||||
|
@ -1170,4 +1183,9 @@ export interface WebViewSharedProps extends ViewProps {
|
|||
* Append to the existing user-agent. Overridden if `userAgent` is set.
|
||||
*/
|
||||
applicationNameForUserAgent?: string;
|
||||
|
||||
/**
|
||||
* An object that specifies the credentials of a user to be used for basic authentication.
|
||||
*/
|
||||
basicAuthCredential?: BasicAuthCredential;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue