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.

This commit is contained in:
Graham Jones
2021-12-12 15:05:24 +00:00
parent 0a405af092
commit 32ab7d4229
3 changed files with 51 additions and 21 deletions

View File

@@ -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);
}

View File

@@ -13,7 +13,7 @@ import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
public class AuthenticateActivity extends AppCompatActivity { public class AuthenticateActivity extends AppCompatActivity implements AuthCallbackInterface {
private String TAG = "AuthenticateActivity"; private String TAG = "AuthenticateActivity";
private Context mContext; private Context mContext;
private EditText mUnameEt; private EditText mUnameEt;
@@ -34,7 +34,7 @@ public class AuthenticateActivity extends AppCompatActivity {
mUnameEt = (EditText) findViewById(R.id.username); mUnameEt = (EditText) findViewById(R.id.username);
mPasswdEt = (EditText) findViewById(R.id.password); mPasswdEt = (EditText) findViewById(R.id.password);
mWac = new WebApiConnection(this); mWac = new WebApiConnection(this, this);
} }
@Override @Override
@@ -43,6 +43,11 @@ public class AuthenticateActivity extends AppCompatActivity {
switchUi(); switchUi();
} }
public void authCallback(boolean authSuccess, String tokenStr) {
Log.v(TAG,"authCallback");
switchUi();
}
private void switchUi() { private void switchUi() {
SharedPreferences prefs; SharedPreferences prefs;
String storedAuthToken; String storedAuthToken;

View File

@@ -1,6 +1,9 @@
package uk.org.openseizuredetector; package uk.org.openseizuredetector;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.icu.text.RelativeDateTimeFormatter;
import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
@@ -15,50 +18,66 @@ import com.android.volley.toolbox.Volley;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
// This class is intended to handle all interactions with the OSD WebAPI // This class is intended to handle all interactions with the OSD WebAPI
public class WebApiConnection { public class WebApiConnection {
public String retVal; public String retVal;
public int retCode; public int retCode;
private String mUrlBase = "https://osdApi.ddns.net"; private String mUrlBase = "https://osdApi.ddns.net";
private String TAG = "WebApiConnection"; private String TAG = "WebApiConnection";
private AuthCallbackInterface mAuthCallback;
private Context mContext;
RequestQueue mQueue; RequestQueue mQueue;
public WebApiConnection(Context context) { public WebApiConnection(Context context, AuthCallbackInterface authCallback) {
mContext = context;
mAuthCallback = authCallback;
mQueue = Volley.newRequestQueue(context); 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: // 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/ // 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/"; String urlStr = mUrlBase + "/api/accounts/login/";
Log.v(TAG, "urlStr=" + urlStr); Log.v(TAG, "urlStr=" + urlStr);
JSONObject postData = new JSONObject(); StringRequest req = new StringRequest(Request.Method.POST, urlStr,
try { new Response.Listener<String>() {
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<JSONObject>() {
@Override @Override
public void onResponse(JSONObject response) { public void onResponse(String response) {
// Display the first 500 characters of the response string.
Log.v(TAG, "Response is: " + 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() { new Response.ErrorListener() {
@Override @Override
public void onErrorResponse(VolleyError error) { public void onErrorResponse(VolleyError error) {
String responseBody = new String(error.networkResponse.data); 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<String, String> getParams() {
Map<String, String> params = new HashMap<>();
// params.put("name",sname); // passing parameters to server
params.put("login", uname);
params.put("password", passwd);
return params;
} }
); };
mQueue.add(req); mQueue.add(req);
return (true); return (true);
} }