Skip to content

Commit

Permalink
authentication test for android blog client
Browse files Browse the repository at this point in the history
  • Loading branch information
fhoehnel committed Dec 21, 2016
1 parent a36db05 commit 80145f3
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 39 deletions.
Binary file modified android-blog-client/apk/webfilesys-blog.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion android-blog-client/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.webfilesys.de.webfilesysblog"
android:versionCode="7" android:versionName="0.4.2">
android:versionCode="7" android:versionName="0.4.3">

<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="18"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,18 @@ public void onClick(View v) {
startActivityForResult(pickImageIntent, REQUEST_PICK_IMAGE);
} else {
Log.e("webfilesysblog", "not connected to the internet");
Toast.makeText(getApplicationContext(), R.string.offline, Toast.LENGTH_LONG).show();
Toast toast = Toast.makeText(getApplicationContext(), R.string.offline, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
break;
case R.id.send_publish_button:
case R.id.send_post_button:
EditText descrText = (EditText) findViewById(R.id.description);
if (descrText.getText().length() == 0) {
Toast.makeText(getApplicationContext(), R.string.missingDescription, Toast.LENGTH_LONG).show();
Toast toast = Toast.makeText(getApplicationContext(), R.string.missingDescription, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
} else {
if (v.getId() == R.id.send_publish_button) {
new PostToBlogTask(v, true).execute();
Expand Down Expand Up @@ -598,6 +602,12 @@ private void showSettings() {
serverUrl = prefs.getString(PREF_SERVER_URL, SERVER_URL_DEFAULT);
userid = prefs.getString(PREF_USERID, null);

ProgressBar authProgressBar = (ProgressBar) findViewById(R.id.authProgressBar);
authProgressBar.setVisibility(View.GONE);

TextView connectingMsg = (TextView) findViewById(R.id.connecting_msg);
connectingMsg.setVisibility(View.GONE);

EditText serverUrlInput = (EditText) findViewById(R.id.server_url);
if (serverUrl != null) {
serverUrlInput.setText(serverUrl, TextView.BufferType.EDITABLE);
Expand All @@ -614,6 +624,7 @@ private void showSettings() {
}

Button saveSettingsButton = (Button) findViewById(R.id.save_settings_button);
saveSettingsButton.setVisibility(View.VISIBLE);

saveSettingsButton.setOnClickListener(new View.OnClickListener() {

Expand All @@ -622,7 +633,6 @@ public void onClick(View v) {

switch (v.getId()) {
case R.id.save_settings_button:
Log.d("webfilesysblog", "save button clicked");

EditText serverUrlInput = (EditText) findViewById(R.id.server_url);
serverUrl = serverUrlInput.getText().toString();
Expand All @@ -641,15 +651,26 @@ public void onClick(View v) {
serverUrl = serverUrl.substring(0, serverUrl.length() - 1);
}

v.setVisibility(View.GONE);

TextView connectingMsg = (TextView) findViewById(R.id.connecting_msg);
connectingMsg.setVisibility(View.VISIBLE);

ProgressBar authProgressBar = (ProgressBar) findViewById(R.id.authProgressBar);
authProgressBar.setVisibility(View.VISIBLE);

SharedPreferences.Editor prefEditor = prefs.edit();
prefEditor.putString(PREF_SERVER_URL, serverUrl);
prefEditor.putString(PREF_USERID, userid);

prefEditor.commit();

showBlogForm();
new TestAuthenticationTask(v).execute();

} else {
Toast.makeText(getApplicationContext(), R.string.missingParameters, Toast.LENGTH_LONG).show();
Toast toast = Toast.makeText(getApplicationContext(), R.string.missingParameters, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}

break;
Expand All @@ -658,6 +679,68 @@ public void onClick(View v) {
});
}

class TestAuthenticationTask extends AsyncTask<String, Void, String> {
int authResult;

private View view;

public TestAuthenticationTask(View v) {
view = v;
}

protected String doInBackground(String... params) {
authResult = checkAuthentication();

return "";
}

protected void onPostExecute(String result) {
if (authResult == 1) {
showBlogForm();
} else if (authResult == 0) {
Toast toast = Toast.makeText(view.getContext(), R.string.authenticationFailed, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
showSettings();
} else if (authResult == (-1)) {
Toast toast = Toast.makeText(view.getContext(), R.string.communicationFailure, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
showSettings();
}
}

private int checkAuthentication() {
String encodedAuthToken = createBasicAuthToken();

try {
URL url = new URL(serverUrl + "/webfilesys/blogpost/authenticate");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", encodedAuthToken);

int responseCode = conn.getResponseCode();

if (responseCode == HttpURLConnection.HTTP_OK) {
return 1;
} else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED){
return 0;
}
return (-1);
} catch (MalformedURLException urlEx) {
Log.w("webfilesysblog", "invalid server URL in authentication check", urlEx);
return (-1);
} catch (IOException ioEx) {
Log.w("webfilesysblog", "communication failure in authentication check", ioEx);
return (-1);
}
}

}

class PostToBlogTask extends AsyncTask<String, Void, String> {

private Exception exception;
Expand Down Expand Up @@ -706,7 +789,9 @@ protected void onPostExecute(String result) {
progressBar.setVisibility(View.INVISIBLE);

if (success) {
Toast.makeText(view.getContext(), R.string.postSuccess, Toast.LENGTH_LONG).show();
Toast toast = Toast.makeText(view.getContext(), R.string.postSuccess, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
EditText descriptionInput = (EditText) findViewById(R.id.description);
descriptionInput.getText().clear();
blogPicImageView.setImageDrawable(null);
Expand All @@ -718,7 +803,9 @@ protected void onPostExecute(String result) {
View selectedLocationView = (View) findViewById(R.id.selectedLocation);
selectedLocationView.setVisibility(View.GONE);
} else {
Toast.makeText(view.getContext(), R.string.postFailed, Toast.LENGTH_LONG).show();
Toast toast = Toast.makeText(view.getContext(), R.string.postFailed, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
sendPostButton.setVisibility(View.VISIBLE);
sendPublishButton.setVisibility(View.VISIBLE);
}
Expand Down Expand Up @@ -920,14 +1007,13 @@ private HttpURLConnection prepareUrlConnection(String webfilesysUrl)

return conn;
}
}

private String createBasicAuthToken() {
String authToken = userid + ":" + password;
String encodedToken = Base64.encodeToString(authToken.getBytes(), Base64.DEFAULT).replaceAll("\n", "");
String encodedAuthToken = "Basic " + encodedToken;

return encodedAuthToken;
}
private String createBasicAuthToken() {
String authToken = userid + ":" + password;
String encodedToken = Base64.encodeToString(authToken.getBytes(), Base64.DEFAULT).replaceAll("\n", "");
String encodedAuthToken = "Basic " + encodedToken;

return encodedAuthToken;
}
}
43 changes: 35 additions & 8 deletions android-blog-client/src/main/res/layout/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,41 @@
android:layout_width="match_parent"
android:layout_height="10sp"/>

<Button
android:id="@+id/save_settings_button"
android:layout_width="wrap_content"
android:layout_height="32sp"
android:textColor="#202020"
android:textSize="16sp"
android:background="@drawable/button_border"
android:text="@string/buttonSaveSettings" />
<LinearLayout
android:id="@+id/saveSettings_layout"
android:drawingCacheQuality="low"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<Button
android:id="@+id/save_settings_button"
android:layout_width="wrap_content"
android:layout_height="32sp"
android:textColor="#202020"
android:textSize="16sp"
android:background="@drawable/button_border"
android:text="@string/buttonSaveSettings" />

<TextView
android:id="@+id/connecting_msg"
android:layout_width="wrap_content"
android:layout_height="32sp"
android:paddingTop="6sp"
android:text="@string/connecting"
android:visibility="gone" />

<ProgressBar
android:id="@+id/authProgressBar"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:indeterminate="true"
android:progress="1"
android:visibility="gone" />
</LinearLayout>

</LinearLayout>

Expand Down
4 changes: 4 additions & 0 deletions android-blog-client/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@

<string name="buttonMapSelectionOk">OK</string>

<string name="authenticationFailed">Das Login am Server ist fehlgeschlagen wegen ungültigem Nutzer oder Passwort.</string>
<string name="communicationFailure">Das Login am Server ist fehlgeschlagen wegen Kommunikations-Problemen. Bitte überprüfen Sie die URL des WebFileSys Servers!</string>
<string name="connecting">verbinde zum Server ...</string>

</resources>
6 changes: 5 additions & 1 deletion android-blog-client/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources>
<string name="app_name">WebFileSys Blog</string>
<string name="appVersion" translatable="false">version 0.4.2 (18 Sep 2016)</string>
<string name="appVersion" translatable="false">version 0.4.3 (18 Dec 2016)</string>
<string name="appAuthor" translatable="false">by Frank Hoehnel</string>
<string name="appHomepage" translatable="false">www.webfilesys.de</string>
<string name="buttonClose">Close</string>
Expand Down Expand Up @@ -38,4 +38,8 @@

<string name="buttonMapSelectionOk">OK</string>

<string name="authenticationFailed">The authentication with the server has failed due to invalid userid or password.</string>
<string name="communicationFailure">The authentication with the server has failed due to communication problems. Please check the URL of the WebFileSys server!</string>
<string name="connecting">connecting to server ...</string>

</resources>
2 changes: 1 addition & 1 deletion src/main/java/de/webfilesys/WebFileSys.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class WebFileSys
{
private static WebFileSys instance = null;

public static final String VERSION = "Version 2.15.2-beta2 (28 Nov 2016)";
public static final String VERSION = "Version 2.15.2-beta3 (18 Dec 2016)";

public static final String THUMB_DIR = "thumbnails";

Expand Down
49 changes: 35 additions & 14 deletions src/main/java/de/webfilesys/servlet/BlogPostServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,22 @@ public class BlogPostServlet extends WebFileSysServlet {

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException {
LOG.error("method GET not supported by BlogPostServlet");
if (LOG.isDebugEnabled()) {
LOG.debug("GET request");
}

resp.setDateHeader("expires", 0l);

String userid = authenticateUser(req, resp);

if (userid == null) {
resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
resp.setHeader(BASIC_HTTP_AUTH_PROMPT, "Basic realm=\"webfilesys\"");
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("successful authentication by blog client");
}
}
}

public void doPost(HttpServletRequest req, HttpServletResponse resp)
Expand All @@ -53,29 +68,35 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp)
return;
}

String currentPath = WebFileSys.getInstance().getUserMgr().getDocumentRoot(userid);

if (currentPath == null) {
LOG.error("current working directory unknown");
throw new ServletException("current working directory unknown");
}

String requestPath = req.getRequestURI();

int lastPathDelimiterIdx = requestPath.lastIndexOf('/');

String fileName = UTF8URLDecoder.decode(requestPath.substring(lastPathDelimiterIdx + 1));

fileName = replaceIllegalChars(fileName);

int delIdx = requestPath.substring(0, lastPathDelimiterIdx).lastIndexOf('/');

if (delIdx < 0) {
LOG.error("invalid parameters for BlogPostServlet: " + requestPath);
throw new ServletException("invalid parameters for BlogPostServlet");
}

String command = requestPath.substring(delIdx + 1, lastPathDelimiterIdx);

/*
if (command.equals("authenticate")) {
return;
}
*/

String currentPath = WebFileSys.getInstance().getUserMgr().getDocumentRoot(userid);

if (currentPath == null) {
LOG.error("current working directory unknown");
throw new ServletException("current working directory unknown");
}

String fileName = UTF8URLDecoder.decode(requestPath.substring(lastPathDelimiterIdx + 1));

fileName = replaceIllegalChars(fileName);

if (command.equals("picture")) {
reveicePicture(req, resp, userid, currentPath, fileName);
Expand Down Expand Up @@ -356,7 +377,7 @@ private String authenticateUser(HttpServletRequest req, HttpServletResponse resp
}

if (!WebFileSys.getInstance().getUserMgr().checkPassword(userid, password)) {
LOG.warn("invalid credentials");
LOG.warn("invalid credentials in BlogPostServlet");
return null;
}

Expand Down

0 comments on commit 80145f3

Please sign in to comment.