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:
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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<JSONObject>() {
|
||||
StringRequest req = new StringRequest(Request.Method.POST, urlStr,
|
||||
new Response.Listener<String>() {
|
||||
@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<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);
|
||||
return (true);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user