mirror of
https://github.com/syncthing/syncthing-android.git
synced 2024-11-26 22:31:16 +00:00
Support HTTP Auth in Webview
This commit is contained in:
parent
4146970e64
commit
dccbbbbd41
3 changed files with 32 additions and 10 deletions
|
@ -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<String, String> extraHeaders = new HashMap<>();
|
||||
extraHeaders.put(RestApi.HEADER_API_KEY, getService().getApi().getApiKey());
|
||||
mWebView.loadUrl(getService().getWebGuiUrl(), extraHeaders);
|
||||
mWebView.loadUrl(getService().getWebGuiUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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<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;
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<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
|
||||
protected Pair<String, String> doInBackground(Void... voids) {
|
||||
mConfig = new ConfigXml(SyncthingService.this);
|
||||
|
@ -278,6 +286,7 @@ public class SyncthingService extends Service {
|
|||
@Override
|
||||
protected void onPostExecute(Pair<String, String> urlAndKey) {
|
||||
mApi = new RestApi(SyncthingService.this, urlAndKey.first, urlAndKey.second,
|
||||
mGuiUser, mGuiPassword,
|
||||
new RestApi.OnApiAvailableListener() {
|
||||
@Override
|
||||
public void onApiAvailable() {
|
||||
|
|
Loading…
Reference in a new issue