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:
iwashi1t
2021-09-30 18:03:34 +02:00
committed by GitHub
parent c74c1fd113
commit 0c42a9812e
6 changed files with 83 additions and 0 deletions
@@ -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;
}
}