Refactor login logic to make request to server and parse result

This commit is contained in:
Garrett Mills 2021-05-15 23:20:14 -05:00
parent a4017c303e
commit d274d47c6c
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246
5 changed files with 82 additions and 27 deletions

View File

@ -89,6 +89,7 @@ public class MainActivity extends AppCompatActivity {
}
} while ( cursor.moveToNext() );
}
cursor.close();
serviceIntent = new Intent(this, MessagingService.class);
ContextCompat.startForegroundService(this, serviceIntent);
@ -103,6 +104,13 @@ public class MainActivity extends AppCompatActivity {
serverAddressView = null;
}
protected void switchToLoggingInMode() {
if ( serviceIntent != null ) stopService(serviceIntent);
setContentView(R.layout.activity_main_authenticating);
serverAddressView = null;
}
protected void requestSMSRead() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED) {
// switchToStatusMode();
@ -137,9 +145,13 @@ public class MainActivity extends AppCompatActivity {
}
try {
switchToLoggingInMode();
LoginToken login = new LoginToken(data.getExtras().getString(Hyperlink.EXTRA_LOGIN_TOKEN));
APIv1.login(login);
switchToStatusMode();
APIv1.login(login, () -> {
if ( APIv1.isAuthenticated() ) switchToStatusMode();
else switchToLoginMode();
});
} catch (InvalidLoginTokenException e) {
Toast.makeText(getApplicationContext(), "Invalid login token!", Toast.LENGTH_SHORT).show();
switchToLoginMode();

View File

@ -1,14 +1,14 @@
package dev.garrettmills.starship.hyperlink.relay;
public class ServerSentRequest {
private String _uuid;
private ServerRequestEndpoint _endpoint;
private String server_request_id;
private ServerRequestEndpoint endpoint;
public String getUUID() {
return _uuid;
return server_request_id;
}
public ServerRequestEndpoint getEndpoint() {
return _endpoint;
return endpoint;
}
}

View File

@ -1,6 +1,16 @@
package dev.garrettmills.starship.hyperlink.util;
import android.content.SharedPreferences;
import android.util.Log;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;
import dev.garrettmills.starship.hyperlink.Hyperlink;
@ -48,21 +58,34 @@ public class APIv1 {
editor.apply();
}
public static AccessToken login(LoginToken login) {
AccessToken token = redeemLoginToken(login);
SharedPreferences.Editor editor = Hyperlink.preferences.edit();
editor.putString(Hyperlink.SERVER_ADDR, token.getServer());
editor.putString(Hyperlink.ACCESS_TOKEN, token.getToken());
editor.apply();
return token;
}
public static void login(LoginToken login, Runnable callback) {
Gson gson = new Gson();
String tokenJson = gson.toJson(login);
/**
* @fixme this is a stub placeholder. Replace with actual implementation
* @param login the LoginToken from the user
* @return the AccessToken redeemed from the server
*/
protected static AccessToken redeemLoginToken(LoginToken login) {
return new AccessToken(login.getServer(), login.getToken());
try {
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST,
resolveEndpoint("/login/redeem"),
new JSONObject(tokenJson),
response -> {
try {
String tokenValue = response.getString("token");
Log.d("APIv1", "Got access token: " + tokenValue);
SharedPreferences.Editor editor = Hyperlink.preferences.edit();
editor.putString(Hyperlink.SERVER_ADDR, login.getServer());
editor.putString(Hyperlink.ACCESS_TOKEN, tokenValue);
editor.apply();
} catch (JSONException e) {
callback.run();
}
},
error -> callback.run()
);
Hyperlink.httpRequestQueue.add(request);
} catch (JSONException e) {
callback.run();
}
}
}

View File

@ -14,8 +14,8 @@ public class LoginToken {
}
}
private String _server;
private String _token;
private String server;
private String token;
public LoginToken(String token) throws InvalidLoginTokenException {
String[] parts = token.split("\\|");
@ -23,15 +23,15 @@ public class LoginToken {
throw new InvalidLoginTokenException();
}
this._server = parts[1];
this._token = parts[2];
this.server = parts[1];
this.token = parts[2];
}
public String getServer() {
return _server;
return server;
}
public String getToken() {
return _token;
return token;
}
}

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Authenticating..."
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>