1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-27 06:41:15 +00:00

Support HTTP Auth in Webview

This commit is contained in:
Lode Hoste 2015-04-12 22:33:07 +02:00
parent 4146970e64
commit dccbbbbd41
3 changed files with 32 additions and 10 deletions

View file

@ -9,6 +9,7 @@ import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.webkit.HttpAuthHandler;
import android.webkit.SslErrorHandler; import android.webkit.SslErrorHandler;
import android.webkit.WebView; import android.webkit.WebView;
import android.webkit.WebViewClient; 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 @Override
public void onPageFinished(WebView view, String url) { public void onPageFinished(WebView view, String url) {
mWebView.setVisibility(View.VISIBLE); mWebView.setVisibility(View.VISIBLE);
@ -115,16 +120,9 @@ public class WebGuiActivity extends SyncthingActivity
getService().registerOnWebGuiAvailableListener(WebGuiActivity.this); getService().registerOnWebGuiAvailableListener(WebGuiActivity.this);
} }
/**
* Loads and shows WebView, hides loading view.
*
* Sets the X-API-Key (HEADER_API_KEY) header for authorization
*/
@Override @Override
public void onWebGuiAvailable() { public void onWebGuiAvailable() {
Map<String, String> extraHeaders = new HashMap<>(); mWebView.loadUrl(getService().getWebGuiUrl());
extraHeaders.put(RestApi.HEADER_API_KEY, getService().getApi().getApiKey());
mWebView.loadUrl(getService().getWebGuiUrl(), extraHeaders);
} }
/** /**

View file

@ -149,6 +149,10 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
private final String mApiKey; private final String mApiKey;
private final String mGuiUser;
private final String mGuiPassword;
private final String mHttpsCertPath; private final String mHttpsCertPath;
private JSONObject mConfig; private JSONObject mConfig;
@ -174,10 +178,13 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
*/ */
private HashMap<String, Model> mCachedModelInfo = new HashMap<>(); private HashMap<String, Model> 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; mContext = context;
mUrl = url; mUrl = url;
mApiKey = apiKey; mApiKey = apiKey;
mGuiUser = guiUser;
mGuiPassword = guiPassword;
mHttpsCertPath = mContext.getFilesDir() + "/" + SyncthingService.HTTPS_CERT_FILE; mHttpsCertPath = mContext.getFilesDir() + "/" + SyncthingService.HTTPS_CERT_FILE;
mOnApiAvailableListener = listener; mOnApiAvailableListener = listener;
} }
@ -961,4 +968,12 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
return mApiKey; return mApiKey;
} }
public String getGuiUser() {
return mGuiUser;
}
public String getGuiPassword() {
return mGuiPassword;
}
} }

View file

@ -260,7 +260,7 @@ public class SyncthingService extends Service {
mDeviceStateHolder = new DeviceStateHolder(SyncthingService.this); mDeviceStateHolder = new DeviceStateHolder(SyncthingService.this);
registerReceiver(mDeviceStateHolder, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 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<String, String>}. * {@code Pair<String, String>}.
*/ */
private class StartupTask extends AsyncTask<Void, Void, Pair<String, String>> { private class StartupTask extends AsyncTask<Void, Void, Pair<String, String>> {
String mGuiUser;
String mGuiPassword;
public StartupTask(String guiUser, String guiPassword) {
mGuiUser = guiUser;
mGuiPassword = guiPassword;
}
@Override @Override
protected Pair<String, String> doInBackground(Void... voids) { protected Pair<String, String> doInBackground(Void... voids) {
mConfig = new ConfigXml(SyncthingService.this); mConfig = new ConfigXml(SyncthingService.this);
@ -278,6 +286,7 @@ public class SyncthingService extends Service {
@Override @Override
protected void onPostExecute(Pair<String, String> urlAndKey) { protected void onPostExecute(Pair<String, String> urlAndKey) {
mApi = new RestApi(SyncthingService.this, urlAndKey.first, urlAndKey.second, mApi = new RestApi(SyncthingService.this, urlAndKey.first, urlAndKey.second,
mGuiUser, mGuiPassword,
new RestApi.OnApiAvailableListener() { new RestApi.OnApiAvailableListener() {
@Override @Override
public void onApiAvailable() { public void onApiAvailable() {