Add LoginToken helper class and basic validation

This commit is contained in:
Garrett Mills 2021-05-12 12:22:26 -05:00
parent d73c79f083
commit 398903588e
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246
5 changed files with 53 additions and 11 deletions

View File

@ -3,7 +3,6 @@ package dev.garrettmills.starship.hyperlink;
import android.content.SharedPreferences; import android.content.SharedPreferences;
public class Hyperlink { public class Hyperlink {
public static final int PERMISSION_REQUEST_CAMERA = 0;
public static final String SHARED_PREFERENCES_NAME = "dev.garrettmills.starship.hyperlink.main"; public static final String SHARED_PREFERENCES_NAME = "dev.garrettmills.starship.hyperlink.main";
public static final String SERVER_ADDR = "dev.garrettmills.starship.hyperlink.server"; public static final String SERVER_ADDR = "dev.garrettmills.starship.hyperlink.server";
public static final String SERVER_TOKEN = "dev.garrettmills.starship.hyperlink.token.server"; public static final String SERVER_TOKEN = "dev.garrettmills.starship.hyperlink.token.server";
@ -11,6 +10,7 @@ public class Hyperlink {
public static final String EXTRA_LOGIN_TOKEN = "dev.garrettmills.starship.hyperlink.extra.login_token"; public static final String EXTRA_LOGIN_TOKEN = "dev.garrettmills.starship.hyperlink.extra.login_token";
public static final int REQUEST_LOGIN_TOKEN = 180; public static final int REQUEST_LOGIN_TOKEN = 180;
public static final int REQUEST_PERMISSION_CAMERA = 181;
public static SharedPreferences preferences; public static SharedPreferences preferences;
} }

View File

@ -25,6 +25,8 @@ import java.util.concurrent.ExecutionException;
import dev.garrettmills.starship.hyperlink.scanner.QRCodeImageAnalyzer; import dev.garrettmills.starship.hyperlink.scanner.QRCodeImageAnalyzer;
import dev.garrettmills.starship.hyperlink.scanner.QRCodeListener; import dev.garrettmills.starship.hyperlink.scanner.QRCodeListener;
import dev.garrettmills.starship.hyperlink.util.InvalidLoginTokenException;
import dev.garrettmills.starship.hyperlink.util.LoginToken;
public class LoginTokenScannerActivity extends AppCompatActivity implements QRCodeListener { public class LoginTokenScannerActivity extends AppCompatActivity implements QRCodeListener {
private PreviewView previewView; private PreviewView previewView;
@ -45,21 +47,22 @@ public class LoginTokenScannerActivity extends AppCompatActivity implements QRCo
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
startCamera(); startCamera();
} else { } else {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, Hyperlink.REQUEST_PERMISSION_CAMERA);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, Hyperlink.PERMISSION_REQUEST_CAMERA);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, Hyperlink.PERMISSION_REQUEST_CAMERA);
}
} }
} }
@Override @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == Hyperlink.PERMISSION_REQUEST_CAMERA) { if (requestCode == Hyperlink.REQUEST_PERMISSION_CAMERA) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startCamera(); startCamera();
} else { } else {
Toast.makeText(this, "Camera Permission Denied", Toast.LENGTH_SHORT).show(); if ( BuildConfig.DEBUG ) {
Toast.makeText(this, "Camera permission denied", Toast.LENGTH_SHORT).show();
}
// If we denied, go back up to the main activity, w/o any login token
finish();
} }
} }
} }
@ -70,8 +73,12 @@ public class LoginTokenScannerActivity extends AppCompatActivity implements QRCo
ProcessCameraProvider cameraProvider = cameraProviderFuture.get(); ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
bindCameraPreview(cameraProvider); bindCameraPreview(cameraProvider);
} catch (ExecutionException | InterruptedException e) { } catch (ExecutionException | InterruptedException e) {
if ( BuildConfig.DEBUG ) {
Toast.makeText(this, "Error starting camera: " + e.getMessage(), Toast.LENGTH_LONG).show(); Toast.makeText(this, "Error starting camera: " + e.getMessage(), Toast.LENGTH_LONG).show();
} }
finish();
}
}, ContextCompat.getMainExecutor(this)); }, ContextCompat.getMainExecutor(this));
} }
@ -109,6 +116,11 @@ public class LoginTokenScannerActivity extends AppCompatActivity implements QRCo
public void onQRCodeNotFound() {} public void onQRCodeNotFound() {}
public boolean isValidLoginToken(String token) { public boolean isValidLoginToken(String token) {
try {
new LoginToken(token);
return true; return true;
} catch (InvalidLoginTokenException e) {
return false;
}
} }
} }

View File

@ -27,8 +27,10 @@ public class MainActivity extends AppCompatActivity {
if ( resultCode == RESULT_OK && requestCode == Hyperlink.REQUEST_LOGIN_TOKEN ) { if ( resultCode == RESULT_OK && requestCode == Hyperlink.REQUEST_LOGIN_TOKEN ) {
if ( data.hasExtra(Hyperlink.EXTRA_LOGIN_TOKEN) ) { if ( data.hasExtra(Hyperlink.EXTRA_LOGIN_TOKEN) ) {
if ( BuildConfig.DEBUG ) {
Toast.makeText(getApplicationContext(), data.getExtras().getString(Hyperlink.EXTRA_LOGIN_TOKEN), Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), data.getExtras().getString(Hyperlink.EXTRA_LOGIN_TOKEN), Toast.LENGTH_SHORT).show();
} }
} }
} }
} }
}

View File

@ -0,0 +1,4 @@
package dev.garrettmills.starship.hyperlink.util;
public class InvalidLoginTokenException extends Exception {
}

View File

@ -0,0 +1,24 @@
package dev.garrettmills.starship.hyperlink.util;
public class LoginToken {
private String _server;
private String _token;
public LoginToken(String token) throws InvalidLoginTokenException {
String[] parts = token.split("\\|");
if ( parts.length != 3 || !parts[0].equals("hyperlink")) {
throw new InvalidLoginTokenException();
}
this._server = parts[1];
this._token = parts[2];
}
public String getServer() {
return _server;
}
public String getToken() {
return _token;
}
}