1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-12-23 11:21:29 +00:00

Load UI with Authorization header (#1982)

As preparation for https://github.com/syncthing/syncthing/pull/8757. See
https://github.com/syncthing/syncthing/pull/8757#issuecomment-1750372399

The app sends the `Authorization: Basic <base64-ui-credentials>` HTTP
header in the initial request when loading the Web UI to skip the login
form.
This commit is contained in:
bt90 2023-10-14 12:04:36 +02:00 committed by GitHub
parent 2ff421a166
commit e8ef250150
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,7 @@
package com.nutomic.syncthingandroid.activities; package com.nutomic.syncthingandroid.activities;
import static java.nio.charset.StandardCharsets.UTF_8;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
@ -13,6 +15,7 @@ import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.os.Parcelable; import android.os.Parcelable;
import android.util.ArrayMap; import android.util.ArrayMap;
import android.util.Base64;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.webkit.HttpAuthHandler; import android.webkit.HttpAuthHandler;
@ -43,6 +46,8 @@ import java.security.SignatureException;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory; import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
/** /**
@ -99,10 +104,6 @@ public class WebGuiActivity extends StateDialogActivity
} }
} }
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
handler.proceed(mConfig.getUserName(), mConfig.getApiKey());
}
@Override @Override
public boolean shouldOverrideUrlLoading(WebView view, String url) { public boolean shouldOverrideUrlLoading(WebView view, String url) {
Uri uri = Uri.parse(url); Uri uri = Uri.parse(url);
@ -171,7 +172,11 @@ public class WebGuiActivity extends StateDialogActivity
if (mWebView.getUrl() == null) { if (mWebView.getUrl() == null) {
mWebView.stopLoading(); mWebView.stopLoading();
setWebViewProxy(mWebView.getContext().getApplicationContext(), "", 0, "localhost|0.0.0.0|127.*|[::1]"); setWebViewProxy(mWebView.getContext().getApplicationContext(), "", 0, "localhost|0.0.0.0|127.*|[::1]");
mWebView.loadUrl(getService().getWebGuiUrl().toString()); String credentials = mConfig.getUserName() + ":" + mConfig.getApiKey();
String b64Credentials = Base64.encodeToString(credentials.getBytes(UTF_8), Base64.NO_WRAP);
Map<String,String> headers = new HashMap<>();
headers.put("Authorization", "Basic " + b64Credentials);
mWebView.loadUrl(getService().getWebGuiUrl().toString(), headers);
} }
} }
} }