Refactor login logic to make request to server and parse result
This commit is contained in:
parent
a4017c303e
commit
d274d47c6c
@ -89,6 +89,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
} while ( cursor.moveToNext() );
|
} while ( cursor.moveToNext() );
|
||||||
}
|
}
|
||||||
|
cursor.close();
|
||||||
|
|
||||||
serviceIntent = new Intent(this, MessagingService.class);
|
serviceIntent = new Intent(this, MessagingService.class);
|
||||||
ContextCompat.startForegroundService(this, serviceIntent);
|
ContextCompat.startForegroundService(this, serviceIntent);
|
||||||
@ -103,6 +104,13 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
serverAddressView = null;
|
serverAddressView = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void switchToLoggingInMode() {
|
||||||
|
if ( serviceIntent != null ) stopService(serviceIntent);
|
||||||
|
|
||||||
|
setContentView(R.layout.activity_main_authenticating);
|
||||||
|
serverAddressView = null;
|
||||||
|
}
|
||||||
|
|
||||||
protected void requestSMSRead() {
|
protected void requestSMSRead() {
|
||||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED) {
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED) {
|
||||||
// switchToStatusMode();
|
// switchToStatusMode();
|
||||||
@ -137,9 +145,13 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
switchToLoggingInMode();
|
||||||
|
|
||||||
LoginToken login = new LoginToken(data.getExtras().getString(Hyperlink.EXTRA_LOGIN_TOKEN));
|
LoginToken login = new LoginToken(data.getExtras().getString(Hyperlink.EXTRA_LOGIN_TOKEN));
|
||||||
APIv1.login(login);
|
APIv1.login(login, () -> {
|
||||||
switchToStatusMode();
|
if ( APIv1.isAuthenticated() ) switchToStatusMode();
|
||||||
|
else switchToLoginMode();
|
||||||
|
});
|
||||||
} catch (InvalidLoginTokenException e) {
|
} catch (InvalidLoginTokenException e) {
|
||||||
Toast.makeText(getApplicationContext(), "Invalid login token!", Toast.LENGTH_SHORT).show();
|
Toast.makeText(getApplicationContext(), "Invalid login token!", Toast.LENGTH_SHORT).show();
|
||||||
switchToLoginMode();
|
switchToLoginMode();
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
package dev.garrettmills.starship.hyperlink.relay;
|
package dev.garrettmills.starship.hyperlink.relay;
|
||||||
|
|
||||||
public class ServerSentRequest {
|
public class ServerSentRequest {
|
||||||
private String _uuid;
|
private String server_request_id;
|
||||||
private ServerRequestEndpoint _endpoint;
|
private ServerRequestEndpoint endpoint;
|
||||||
|
|
||||||
public String getUUID() {
|
public String getUUID() {
|
||||||
return _uuid;
|
return server_request_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ServerRequestEndpoint getEndpoint() {
|
public ServerRequestEndpoint getEndpoint() {
|
||||||
return _endpoint;
|
return endpoint;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,16 @@
|
|||||||
package dev.garrettmills.starship.hyperlink.util;
|
package dev.garrettmills.starship.hyperlink.util;
|
||||||
|
|
||||||
import android.content.SharedPreferences;
|
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;
|
import dev.garrettmills.starship.hyperlink.Hyperlink;
|
||||||
|
|
||||||
@ -48,21 +58,34 @@ public class APIv1 {
|
|||||||
editor.apply();
|
editor.apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static AccessToken login(LoginToken login) {
|
public static void login(LoginToken login, Runnable callback) {
|
||||||
AccessToken token = redeemLoginToken(login);
|
Gson gson = new Gson();
|
||||||
SharedPreferences.Editor editor = Hyperlink.preferences.edit();
|
String tokenJson = gson.toJson(login);
|
||||||
editor.putString(Hyperlink.SERVER_ADDR, token.getServer());
|
|
||||||
editor.putString(Hyperlink.ACCESS_TOKEN, token.getToken());
|
|
||||||
editor.apply();
|
|
||||||
return token;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
try {
|
||||||
* @fixme this is a stub placeholder. Replace with actual implementation
|
JsonObjectRequest request = new JsonObjectRequest(
|
||||||
* @param login the LoginToken from the user
|
Request.Method.POST,
|
||||||
* @return the AccessToken redeemed from the server
|
resolveEndpoint("/login/redeem"),
|
||||||
*/
|
new JSONObject(tokenJson),
|
||||||
protected static AccessToken redeemLoginToken(LoginToken login) {
|
response -> {
|
||||||
return new AccessToken(login.getServer(), login.getToken());
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,8 +14,8 @@ public class LoginToken {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String _server;
|
private String server;
|
||||||
private String _token;
|
private String token;
|
||||||
|
|
||||||
public LoginToken(String token) throws InvalidLoginTokenException {
|
public LoginToken(String token) throws InvalidLoginTokenException {
|
||||||
String[] parts = token.split("\\|");
|
String[] parts = token.split("\\|");
|
||||||
@ -23,15 +23,15 @@ public class LoginToken {
|
|||||||
throw new InvalidLoginTokenException();
|
throw new InvalidLoginTokenException();
|
||||||
}
|
}
|
||||||
|
|
||||||
this._server = parts[1];
|
this.server = parts[1];
|
||||||
this._token = parts[2];
|
this.token = parts[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getServer() {
|
public String getServer() {
|
||||||
return _server;
|
return server;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getToken() {
|
public String getToken() {
|
||||||
return _token;
|
return token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
20
app/src/main/res/layout/activity_main_authenticating.xml
Normal file
20
app/src/main/res/layout/activity_main_authenticating.xml
Normal 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>
|
Loading…
Reference in New Issue
Block a user