From 32ab7d42299e33bb6ecb0bd51c5219d6b5772b9c Mon Sep 17 00:00:00 2001 From: Graham Jones Date: Sun, 12 Dec 2021 15:05:24 +0000 Subject: [PATCH] I think we have authentication working - need to test to make sure it saves the token and not a load of extra http response stuff though. --- .../AuthCallbackInterface.java | 6 ++ .../AuthenticateActivity.java | 9 ++- .../openseizuredetector/WebApiConnection.java | 57 ++++++++++++------- 3 files changed, 51 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/uk/org/openseizuredetector/AuthCallbackInterface.java diff --git a/app/src/main/java/uk/org/openseizuredetector/AuthCallbackInterface.java b/app/src/main/java/uk/org/openseizuredetector/AuthCallbackInterface.java new file mode 100644 index 0000000..8422ffe --- /dev/null +++ b/app/src/main/java/uk/org/openseizuredetector/AuthCallbackInterface.java @@ -0,0 +1,6 @@ +package uk.org.openseizuredetector; + +// Interface used by the authentication part of WebApi to send back the authentication token +public interface AuthCallbackInterface { + void authCallback(boolean success, String tokenStr); +} diff --git a/app/src/main/java/uk/org/openseizuredetector/AuthenticateActivity.java b/app/src/main/java/uk/org/openseizuredetector/AuthenticateActivity.java index 0781164..5ac970d 100644 --- a/app/src/main/java/uk/org/openseizuredetector/AuthenticateActivity.java +++ b/app/src/main/java/uk/org/openseizuredetector/AuthenticateActivity.java @@ -13,7 +13,7 @@ import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; -public class AuthenticateActivity extends AppCompatActivity { +public class AuthenticateActivity extends AppCompatActivity implements AuthCallbackInterface { private String TAG = "AuthenticateActivity"; private Context mContext; private EditText mUnameEt; @@ -34,7 +34,7 @@ public class AuthenticateActivity extends AppCompatActivity { mUnameEt = (EditText) findViewById(R.id.username); mPasswdEt = (EditText) findViewById(R.id.password); - mWac = new WebApiConnection(this); + mWac = new WebApiConnection(this, this); } @Override @@ -43,6 +43,11 @@ public class AuthenticateActivity extends AppCompatActivity { switchUi(); } + public void authCallback(boolean authSuccess, String tokenStr) { + Log.v(TAG,"authCallback"); + switchUi(); + } + private void switchUi() { SharedPreferences prefs; String storedAuthToken; diff --git a/app/src/main/java/uk/org/openseizuredetector/WebApiConnection.java b/app/src/main/java/uk/org/openseizuredetector/WebApiConnection.java index f953f73..9f64c2a 100644 --- a/app/src/main/java/uk/org/openseizuredetector/WebApiConnection.java +++ b/app/src/main/java/uk/org/openseizuredetector/WebApiConnection.java @@ -1,6 +1,9 @@ package uk.org.openseizuredetector; import android.content.Context; +import android.content.SharedPreferences; +import android.icu.text.RelativeDateTimeFormatter; +import android.preference.PreferenceManager; import android.util.Log; @@ -15,50 +18,66 @@ import com.android.volley.toolbox.Volley; import org.json.JSONException; import org.json.JSONObject; +import java.util.HashMap; +import java.util.Map; + + // This class is intended to handle all interactions with the OSD WebAPI public class WebApiConnection { public String retVal; public int retCode; private String mUrlBase = "https://osdApi.ddns.net"; private String TAG = "WebApiConnection"; + private AuthCallbackInterface mAuthCallback; + private Context mContext; RequestQueue mQueue; - public WebApiConnection(Context context) { + public WebApiConnection(Context context, AuthCallbackInterface authCallback) { + mContext = context; + mAuthCallback = authCallback; mQueue = Volley.newRequestQueue(context); } - public boolean authenticate(String uname, String passwd) { + public boolean authenticate(final String uname, final String passwd) { + // NOTE: the 'final' keyword is necessary for uname and passwd to be accessible to getParams below - I don't know why! // We know that this command works, so we just need the Java equivalent: // curl -X POST -d 'login=graham4&password=testpwd1' https://osdapi.ddns.net/api/accounts/login/ + // sending the credentials as a JSONObject postData did not work, so try the method from: + // https://protocoderspoint.com/login-and-registration-form-in-android-using-volley-keeping-user-logged-in/#Login_Registration_form_in_android_using_volley_library String urlStr = mUrlBase + "/api/accounts/login/"; Log.v(TAG, "urlStr=" + urlStr); - JSONObject postData = new JSONObject(); - try { - postData.put("login", uname); - postData.put("password", passwd); - - } catch (JSONException e) { - Log.e(TAG,e.getMessage()); - } - - Log.v(TAG,postData.toString()); - JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST, urlStr, postData, - new Response.Listener() { + StringRequest req = new StringRequest(Request.Method.POST, urlStr, + new Response.Listener() { @Override - public void onResponse(JSONObject response) { - // Display the first 500 characters of the response string. + public void onResponse(String response) { Log.v(TAG, "Response is: " + response); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); + prefs.edit().putString("webApiAuthToken", response).commit(); + mAuthCallback.authCallback(true, response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { String responseBody = new String(error.networkResponse.data); - Log.v(TAG, "Login Error" + error.toString()+","+error.getMessage()+error.networkResponse.statusCode+","+responseBody); + Log.v(TAG, "Login Error: " + error.toString() + ", message:" + error.getMessage()+", Response Code:" + error.networkResponse.statusCode + ", Response: " + responseBody); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); + prefs.edit().putString("webApiAuthToken", null).commit(); + mAuthCallback.authCallback(false,new String(error.networkResponse.data)); } - } - ); + }) { + // Note, this is overriding part of StringRequest, not one of the sub-classes above! + @Override + protected Map getParams() { + Map params = new HashMap<>(); + // params.put("name",sname); // passing parameters to server + params.put("login", uname); + params.put("password", passwd); + return params; + } + }; + mQueue.add(req); return (true); }