From dccbbbbd41b53e7c342effaf94c74045d15b0b8f Mon Sep 17 00:00:00 2001 From: Lode Hoste Date: Sun, 12 Apr 2015 22:33:07 +0200 Subject: [PATCH] Support HTTP Auth in Webview --- .../activities/WebGuiActivity.java | 14 ++++++-------- .../syncthingandroid/syncthing/RestApi.java | 17 ++++++++++++++++- .../syncthing/SyncthingService.java | 11 ++++++++++- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/WebGuiActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/WebGuiActivity.java index 0f5e4b81..f0714e8c 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/WebGuiActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/WebGuiActivity.java @@ -9,6 +9,7 @@ import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.view.View; +import android.webkit.HttpAuthHandler; import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.webkit.WebViewClient; @@ -78,6 +79,10 @@ public class WebGuiActivity extends SyncthingActivity } } + public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) { + handler.proceed(getService().getApi().getGuiUser(), getService().getApi().getGuiPassword()); + } + @Override public void onPageFinished(WebView view, String url) { mWebView.setVisibility(View.VISIBLE); @@ -115,16 +120,9 @@ public class WebGuiActivity extends SyncthingActivity getService().registerOnWebGuiAvailableListener(WebGuiActivity.this); } - /** - * Loads and shows WebView, hides loading view. - * - * Sets the X-API-Key (HEADER_API_KEY) header for authorization - */ @Override public void onWebGuiAvailable() { - Map extraHeaders = new HashMap<>(); - extraHeaders.put(RestApi.HEADER_API_KEY, getService().getApi().getApiKey()); - mWebView.loadUrl(getService().getWebGuiUrl(), extraHeaders); + mWebView.loadUrl(getService().getWebGuiUrl()); } /** diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java index b7fe7022..74f7c57d 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java @@ -149,6 +149,10 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, private final String mApiKey; + private final String mGuiUser; + + private final String mGuiPassword; + private final String mHttpsCertPath; private JSONObject mConfig; @@ -174,10 +178,13 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, */ private HashMap mCachedModelInfo = new HashMap<>(); - public RestApi(Context context, String url, String apiKey, OnApiAvailableListener listener) { + public RestApi(Context context, String url, String apiKey, String guiUser, String guiPassword, + OnApiAvailableListener listener) { mContext = context; mUrl = url; mApiKey = apiKey; + mGuiUser = guiUser; + mGuiPassword = guiPassword; mHttpsCertPath = mContext.getFilesDir() + "/" + SyncthingService.HTTPS_CERT_FILE; mOnApiAvailableListener = listener; } @@ -961,4 +968,12 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, return mApiKey; } + public String getGuiUser() { + return mGuiUser; + } + + public String getGuiPassword() { + return mGuiPassword; + } + } diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java index d15ff648..35ba0c07 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java @@ -260,7 +260,7 @@ public class SyncthingService extends Service { mDeviceStateHolder = new DeviceStateHolder(SyncthingService.this); registerReceiver(mDeviceStateHolder, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); - new StartupTask().execute(); + new StartupTask(sp.getString("gui_user",""), sp.getString("gui_password","")).execute(); } /** @@ -269,6 +269,14 @@ public class SyncthingService extends Service { * {@code Pair}. */ private class StartupTask extends AsyncTask> { + String mGuiUser; + String mGuiPassword; + + public StartupTask(String guiUser, String guiPassword) { + mGuiUser = guiUser; + mGuiPassword = guiPassword; + } + @Override protected Pair doInBackground(Void... voids) { mConfig = new ConfigXml(SyncthingService.this); @@ -278,6 +286,7 @@ public class SyncthingService extends Service { @Override protected void onPostExecute(Pair urlAndKey) { mApi = new RestApi(SyncthingService.this, urlAndKey.first, urlAndKey.second, + mGuiUser, mGuiPassword, new RestApi.OnApiAvailableListener() { @Override public void onApiAvailable() {