summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2011-11-27 16:57:05 +0300
committerAndrew Dolgov <[email protected]>2011-11-27 16:57:05 +0300
commit37f4010e8f81db354085ca5b7b8fc40100169376 (patch)
tree798f23bbf8dea8c74460283932d08a4e9fa284e5
parent0dbf6d75cadc21f8b03e1a00052e066c1f555e74 (diff)
add support for http basic authorization
-rw-r--r--res/values/strings.xml10
-rw-r--r--res/xml/preferences.xml14
-rw-r--r--src/org/fox/ttrss/ApiRequest.java182
-rw-r--r--src/org/fox/ttrss/MainActivity.java6
4 files changed, 47 insertions, 165 deletions
diff --git a/res/values/strings.xml b/res/values/strings.xml
index c0615976..222e8444 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -13,15 +13,16 @@
<string name="password">Password</string>
<string name="default_url">http://example.domain/tt-rss/</string>
<string name="authentication">Authentication</string>
- <string name="look_and_feel">Look and feel</string>
+ <string name="look_and_feel">Interface</string>
<string name="pref_theme">Theme</string>
- <string name="pref_theme_long">Changes color theme of the application.</string>
+ <string name="pref_theme_long">Changes color theme of the application</string>
<string name="ttrss_url">Tiny Tiny RSS URL</string>
<string name="auto_login">Login automatically</string>
<string name="theme_dark">Dark</string>
<string name="preferences">Preferences</string>
<string name="theme_light">Light</string>
<string name="connection">Connection</string>
+ <string name="http_authentication">HTTP Authentication</string>
<string name="login_api_disabled">Login failed: API disabled.</string>
<string name="login_no_data">Login failed: no data received.</string>
<string name="login_wrong_password">Login failed: username or password incorrect.</string>
@@ -48,4 +49,9 @@
<string name="toggle_marked">Toggle starred</string>
<string name="toggle_published">Toggle published</string>
<string name="set_unread">Mark unread</string>
+ <string name="http_login_summary">Optional. Fill this if your tt-rss installation is protected by HTTP Basic authorization</string>
+ <string name="login_summary">Your tt-rss login. Not needed for single user mode</string>
+ <string name="password_summary">Your tt-rss password</string>
+ <string name="ttrss_url_summary">URL of your tt-rss installation directory, e.g. http://site.com/tt-rss/</string>
+
</resources>
diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml
index 4fb2c5ec..3bc482f3 100644
--- a/res/xml/preferences.xml
+++ b/res/xml/preferences.xml
@@ -5,12 +5,17 @@
<PreferenceCategory android:title="@string/connection">
- <EditTextPreference android:title="@string/login" android:key="login" android:singleLine="true"></EditTextPreference>
- <EditTextPreference android:title="@string/password" android:key="password" android:singleLine="true" android:password="true"></EditTextPreference>
- <EditTextPreference android:key="ttrss_url" android:title="@string/ttrss_url" android:singleLine="true" textUri="true" android:hint="@string/default_url"></EditTextPreference>
+ <EditTextPreference android:summary="@string/login_summary" android:title="@string/login" android:key="login" android:singleLine="true"></EditTextPreference>
+ <EditTextPreference android:summary="@string/password_summary" android:title="@string/password" android:key="password" android:singleLine="true" android:password="true"></EditTextPreference>
+ <EditTextPreference android:summary="@string/ttrss_url_summary" android:key="ttrss_url" android:title="@string/ttrss_url" android:singleLine="true" textUri="true" android:hint="@string/default_url"></EditTextPreference>
<CheckBoxPreference android:defaultValue="false" android:title="@string/ssl_trust_any" android:key="ssl_trust_any" />
</PreferenceCategory>
+ <PreferenceCategory android:title="@string/http_authentication">
+ <EditTextPreference android:title="@string/login" android:summary="@string/http_login_summary" android:key="http_login" android:singleLine="true"></EditTextPreference>
+ <EditTextPreference android:title="@string/password" android:key="http_password" android:singleLine="true" android:password="true"></EditTextPreference>
+ </PreferenceCategory>
+
<PreferenceCategory android:title="@string/look_and_feel">
<ListPreference
android:title="@string/pref_theme"
@@ -21,7 +26,8 @@
<CheckBoxPreference android:title="@string/sort_feeds_by_unread" android:key="sort_feeds_by_unread"/>
</PreferenceCategory>
-
+
+
<PreferenceCategory android:title="@string/debugging">
<CheckBoxPreference android:defaultValue="false" android:title="@string/transport_debugging" android:key="transport_debugging" />
</PreferenceCategory>
diff --git a/src/org/fox/ttrss/ApiRequest.java b/src/org/fox/ttrss/ApiRequest.java
index a33f07da..a27b3dbb 100644
--- a/src/org/fox/ttrss/ApiRequest.java
+++ b/src/org/fox/ttrss/ApiRequest.java
@@ -3,9 +3,14 @@ package org.fox.ttrss;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.net.MalformedURLException;
+import java.net.URL;
import java.util.HashMap;
+import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
@@ -38,15 +43,16 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
private boolean m_trustAny = false;
private boolean m_transportDebugging = false;
private Context m_context;
+ private SharedPreferences m_prefs;
public ApiRequest(Context context) {
m_context = context;
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(m_context);
+ m_prefs = PreferenceManager.getDefaultSharedPreferences(m_context);
- m_api = prefs.getString("ttrss_url", null);
- m_trustAny = prefs.getBoolean("ssl_trust_any", false);
- m_transportDebugging = prefs.getBoolean("transport_debugging", false);
+ m_api = m_prefs.getString("ttrss_url", null);
+ m_trustAny = m_prefs.getBoolean("ssl_trust_any", false);
+ m_transportDebugging = m_prefs.getBoolean("transport_debugging", false);
}
@Override
@@ -73,129 +79,29 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
}
HttpPost httpPost = new HttpPost(m_api + "/api/");
-
- try {
- httpPost.setEntity(new StringEntity(requestStr, "utf-8"));
- HttpResponse execute = client.execute(httpPost);
-
- InputStream content = execute.getEntity().getContent();
-
- BufferedReader buffer = new BufferedReader(
- new InputStreamReader(content));
- String s = "";
- String response = "";
-
- while ((s = buffer.readLine()) != null) {
- response += s;
+ String httpLogin = m_prefs.getString("http_login", "");
+ String httpPassword = m_prefs.getString("http_password", "");
+
+ if (httpLogin.length() > 0) {
+ if (m_transportDebugging) Log.d(TAG, "Using HTTP Basic authentication.");
+
+ URL targetUrl;
+ try {
+ targetUrl = new URL(m_api);
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ return null;
}
-
- if (m_transportDebugging) Log.d(TAG, "<<< " + response);
-
- JsonParser parser = new JsonParser();
- return parser.parse(response);
+ HttpHost targetHost = new HttpHost(targetUrl.getHost(), targetUrl.getPort(), targetUrl.getProtocol());
- } catch (Exception e) {
- e.printStackTrace();
+ client.getCredentialsProvider().setCredentials(
+ new AuthScope(targetHost.getHostName(), targetHost.getPort()),
+ new UsernamePasswordCredentials(httpLogin, httpPassword));
}
- return null;
- }
-
- /* protected String m_sessionId;
- protected String m_apiEndpoint;
- protected String m_login;
- protected String m_password;
- protected int m_authStatus;
- protected Gson m_gson = new Gson();
-
- protected static final int STATUS_LOGIN_FAILED = 0;
- protected static final int STATUS_OK = 1;
- protected static final int STATUS_API_DISABLED = 2;
- protected static final int STATUS_OTHER_ERROR = 3;
-
- protected ApiRequest(String sessionId, String apiEndpoint, String login, String password) {
- super();
- m_sessionId = sessionId;
- m_apiEndpoint = apiEndpoint;
- m_authStatus = STATUS_OK;
- m_login = login;
- m_password = password;
-
- //Log.d(TAG, "initial SID=" + sessionId);
- }
-
- protected int tryAuthenticate() {
- JsonElement result = _sendRequest(new HashMap<String,String>() {
- {
- put("op", "login");
- put("user", m_login);
- put("password", m_password);
- }
- });
-
- if (result != null) {
- try {
- JsonObject rv = result.getAsJsonObject();
-
- int status = rv.get("status").getAsInt();
-
- if (status == 0) {
- JsonObject content = rv.get("content").getAsJsonObject();
- if (content != null) {
- m_sessionId = content.get("session_id").getAsString();
-
- Log.d(TAG, "<<< Authentified, sessionId=" + m_sessionId);
-
- return STATUS_OK;
- }
- } else {
- JsonObject content = rv.get("content").getAsJsonObject();
-
- if (content != null) {
- String error = content.get("error").getAsString();
-
- if (error.equals("LOGIN_ERROR")) {
- m_sessionId = null;
- return STATUS_LOGIN_FAILED;
- } else if (error.equals("API_DISABLED")) {
- m_sessionId = null;
- return STATUS_API_DISABLED;
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- m_sessionId = null;
- return STATUS_OTHER_ERROR;
- }
-
- protected String getSessionId() {
- return m_sessionId;
- }
-
- protected int getAuthStatus() {
- return m_authStatus;
- }
-
- protected JsonElement _sendRequest(HashMap<String,String> param) {
-
- HashMap<String,String> tmp = new HashMap<String,String>(param);
-
- if (m_sessionId != null)
- tmp.put("sid", m_sessionId);
-
- String requestStr = m_gson.toJson(tmp);
-
- Log.d(TAG, ">>> (" + requestStr + ") " + m_apiEndpoint);
-
- DefaultHttpClient client = new DefaultHttpClient();
- HttpPost httpPost = new HttpPost(m_apiEndpoint + "/api/");
-
try {
httpPost.setEntity(new StringEntity(requestStr, "utf-8"));
HttpResponse execute = client.execute(httpPost);
@@ -212,7 +118,7 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
response += s;
}
- Log.d(TAG, "<<< " + response);
+ if (m_transportDebugging) Log.d(TAG, "<<< " + response);
JsonParser parser = new JsonParser();
@@ -224,38 +130,4 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
return null;
}
-
- public JsonElement sendRequest(HashMap<String, String> params) {
-
- JsonElement result = _sendRequest(params);
-
- try {
- JsonElement content = result.getAsJsonObject().get("content");
- int status = result.getAsJsonObject().get("status").getAsInt();
-
- if (status == 1) {
- String error = content.getAsJsonObject().get("error").getAsString();
-
- if (error.equals("NOT_LOGGED_IN")) {
- Log.d(TAG, "<<< Session invalid, trying to authenticate...");
-
- m_sessionId = null;
- m_authStatus = tryAuthenticate();
-
- if (m_authStatus == STATUS_OK) {
- result = _sendRequest(params);
-
- return result.getAsJsonObject().get("content");
- }
- }
- } else {
- return content;
- }
-
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- return null;
- } */
}
diff --git a/src/org/fox/ttrss/MainActivity.java b/src/org/fox/ttrss/MainActivity.java
index 10eabc28..49b3db03 100644
--- a/src/org/fox/ttrss/MainActivity.java
+++ b/src/org/fox/ttrss/MainActivity.java
@@ -692,10 +692,8 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
logout();
- if (m_prefs.getString("ttrss_url", null) == null ||
- m_prefs.getString("login", null) == null ||
- m_prefs.getString("password", null) == null) {
-
+ if (m_prefs.getString("ttrss_url", "").length() == 0) {
+
setLoadingStatus(R.string.login_need_configure, false);
} else {