Skip to content

Commit

Permalink
Load UI with Authorization header (#1982)
Browse files Browse the repository at this point in the history
As preparation for syncthing/syncthing#8757. See
syncthing/syncthing#8757 (comment)

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.
  • Loading branch information
bt90 authored Oct 14, 2023
1 parent 2ff421a commit e8ef250
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.nutomic.syncthingandroid.activities;

import static java.nio.charset.StandardCharsets.UTF_8;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
Expand All @@ -13,6 +15,7 @@
import android.os.IBinder;
import android.os.Parcelable;
import android.util.ArrayMap;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.webkit.HttpAuthHandler;
Expand Down Expand Up @@ -43,6 +46,8 @@
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
Expand Down Expand Up @@ -99,10 +104,6 @@ public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError e
}
}

public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
handler.proceed(mConfig.getUserName(), mConfig.getApiKey());
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Uri uri = Uri.parse(url);
Expand Down Expand Up @@ -171,7 +172,11 @@ public void onServiceStateChange(SyncthingService.State newState) {
if (mWebView.getUrl() == null) {
mWebView.stopLoading();
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);
}
}
}
Expand Down

0 comments on commit e8ef250

Please sign in to comment.