mirror of https://github.com/beemdevelopment/Aegis
Start hooking up the intro to the database code
Fingerprint stuff has been left out for nowpull/41/head
parent
f1b499f101
commit
53e86db187
@ -0,0 +1,89 @@
|
||||
package me.impy.aegis.db;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import me.impy.aegis.KeyProfile;
|
||||
import me.impy.aegis.crypto.CryptParameters;
|
||||
import me.impy.aegis.crypto.CryptResult;
|
||||
import me.impy.aegis.crypto.MasterKey;
|
||||
|
||||
public class DatabaseManager {
|
||||
private MasterKey _key;
|
||||
private DatabaseFile _file;
|
||||
private Database _db;
|
||||
private Context _context;
|
||||
|
||||
public DatabaseManager(Context context) {
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public void load() throws Exception {
|
||||
_file = DatabaseFile.load(_context);
|
||||
if (!_file.isEncrypted()) {
|
||||
byte[] bytes = _file.getContent();
|
||||
_db = new Database();
|
||||
_db.deserialize(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
public void setMasterKey(MasterKey key) throws Exception {
|
||||
byte[] encrypted = _file.getContent();
|
||||
CryptParameters params = _file.getCryptParameters();
|
||||
CryptResult result = key.decrypt(encrypted, params);
|
||||
_db = new Database();
|
||||
_db.deserialize(result.Data);
|
||||
_key = key;
|
||||
}
|
||||
|
||||
public void save() throws Exception {
|
||||
assertDecrypted();
|
||||
byte[] bytes = _db.serialize();
|
||||
CryptResult result = _key.encrypt(bytes);
|
||||
_file.setContent(result.Data);
|
||||
_file.setCryptParameters(result.Parameters);
|
||||
_file.save(_context);
|
||||
}
|
||||
|
||||
public void addKey(KeyProfile profile) throws Exception {
|
||||
assertDecrypted();
|
||||
_db.addKey(profile);
|
||||
}
|
||||
|
||||
public void updateKey(KeyProfile profile) throws Exception {
|
||||
assertDecrypted();
|
||||
_db.updateKey(profile);
|
||||
}
|
||||
|
||||
public void removeKey(KeyProfile profile) throws Exception {
|
||||
assertDecrypted();
|
||||
_db.removeKey(profile);
|
||||
}
|
||||
|
||||
public List<KeyProfile> getKeys() throws Exception {
|
||||
assertDecrypted();
|
||||
return _db.getKeys();
|
||||
}
|
||||
|
||||
public boolean isLoaded() {
|
||||
return _file != null;
|
||||
}
|
||||
|
||||
public boolean isDecrypted() {
|
||||
return _db != null;
|
||||
}
|
||||
|
||||
private void assertLoaded() throws Exception {
|
||||
if (!isLoaded()) {
|
||||
throw new Exception("database file has not been loaded yet");
|
||||
}
|
||||
}
|
||||
|
||||
private void assertDecrypted() throws Exception {
|
||||
assertLoaded();
|
||||
if (!isDecrypted()) {
|
||||
throw new Exception("database file has not been decrypted yet");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,277 +0,0 @@
|
||||
package me.impy.aegis.finger;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License
|
||||
*/
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.DialogFragment;
|
||||
import android.content.SharedPreferences;
|
||||
import android.hardware.fingerprint.FingerprintManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.RequiresApi;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.inputmethod.EditorInfo;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.Button;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import me.impy.aegis.MainActivity;
|
||||
import me.impy.aegis.R;
|
||||
|
||||
/**
|
||||
* A dialog which uses fingerprint APIs to authenticate the user, and falls back to password
|
||||
* authentication if fingerprint is not available.
|
||||
*/
|
||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||
public class FingerprintAuthenticationDialogFragment extends DialogFragment
|
||||
implements TextView.OnEditorActionListener, FingerprintUiHelper.Callback {
|
||||
|
||||
private Button mCancelButton;
|
||||
private Button mSecondDialogButton;
|
||||
private View mFingerprintContent;
|
||||
private View mBackupContent;
|
||||
private EditText mPassword;
|
||||
private CheckBox mUseFingerprintFutureCheckBox;
|
||||
private TextView mPasswordDescriptionTextView;
|
||||
private TextView mNewFingerprintEnrolledTextView;
|
||||
|
||||
private Stage mStage = Stage.FINGERPRINT;
|
||||
private Action mAction;
|
||||
|
||||
private FingerprintManager.CryptoObject mCryptoObject;
|
||||
private FingerprintUiHelper mFingerprintUiHelper;
|
||||
private MainActivity mActivity;
|
||||
|
||||
private InputMethodManager mInputMethodManager;
|
||||
private SharedPreferences mSharedPreferences;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
// Do not create a new Fragment when the Activity is re-created such as orientation changes.
|
||||
setRetainInstance(true);
|
||||
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Material_Light_Dialog);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
getDialog().setTitle(getString(R.string.sign_in));
|
||||
View v = inflater.inflate(R.layout.fingerprint_dialog_container, container, false);
|
||||
mCancelButton = (Button) v.findViewById(R.id.cancel_button);
|
||||
mCancelButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
dismiss();
|
||||
}
|
||||
});
|
||||
|
||||
mSecondDialogButton = (Button) v.findViewById(R.id.second_dialog_button);
|
||||
mSecondDialogButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
if (mStage == Stage.FINGERPRINT) {
|
||||
goToBackup();
|
||||
} else {
|
||||
verifyPassword();
|
||||
}
|
||||
}
|
||||
});
|
||||
mFingerprintContent = v.findViewById(R.id.fingerprint_container);
|
||||
mBackupContent = v.findViewById(R.id.backup_container);
|
||||
mPassword = (EditText) v.findViewById(R.id.password);
|
||||
mPassword.setOnEditorActionListener(this);
|
||||
mPasswordDescriptionTextView = (TextView) v.findViewById(R.id.password_description);
|
||||
mUseFingerprintFutureCheckBox = (CheckBox)
|
||||
v.findViewById(R.id.use_fingerprint_in_future_check);
|
||||
mNewFingerprintEnrolledTextView = (TextView)
|
||||
v.findViewById(R.id.new_fingerprint_enrolled_description);
|
||||
mFingerprintUiHelper = new FingerprintUiHelper(
|
||||
mActivity.getSystemService(FingerprintManager.class),
|
||||
(ImageView) v.findViewById(R.id.fingerprint_icon),
|
||||
(TextView) v.findViewById(R.id.fingerprint_status), this);
|
||||
updateStage();
|
||||
|
||||
// If fingerprint authentication is not available, switch immediately to the backup
|
||||
// (password) screen.
|
||||
if (!mFingerprintUiHelper.isFingerprintAuthAvailable()) {
|
||||
goToBackup();
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
if (mStage == Stage.FINGERPRINT) {
|
||||
mFingerprintUiHelper.startListening(mCryptoObject);
|
||||
}
|
||||
}
|
||||
|
||||
public void setStage(Stage stage) {
|
||||
mStage = stage;
|
||||
}
|
||||
|
||||
public void setAction(Action action) { mAction = action; }
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
mFingerprintUiHelper.stopListening();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Activity activity) {
|
||||
super.onAttach(activity);
|
||||
mActivity = (MainActivity) activity;
|
||||
mInputMethodManager = mActivity.getSystemService(InputMethodManager.class);
|
||||
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mActivity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the crypto object to be passed in when authenticating with fingerprint.
|
||||
*/
|
||||
public void setCryptoObject(FingerprintManager.CryptoObject cryptoObject) {
|
||||
mCryptoObject = cryptoObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Switches to backup (password) screen. This either can happen when fingerprint is not
|
||||
* available or the user chooses to use the password authentication method by pressing the
|
||||
* button. This can also happen when the user had too many fingerprint attempts.
|
||||
*/
|
||||
private void goToBackup() {
|
||||
mStage = Stage.PASSWORD;
|
||||
updateStage();
|
||||
mPassword.requestFocus();
|
||||
|
||||
// Show the keyboard.
|
||||
mPassword.postDelayed(mShowKeyboardRunnable, 500);
|
||||
|
||||
// Fingerprint is not used anymore. Stop listening for it.
|
||||
mFingerprintUiHelper.stopListening();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the current entered password is correct, and dismisses the the dialog and
|
||||
* let's the activity know about the result.
|
||||
*/
|
||||
private void verifyPassword() {
|
||||
if (!checkPassword(mPassword.getText().toString())) {
|
||||
return;
|
||||
}
|
||||
if (mStage == Stage.NEW_FINGERPRINT_ENROLLED) {
|
||||
SharedPreferences.Editor editor = mSharedPreferences.edit();
|
||||
editor.putBoolean(getString(R.string.use_fingerprint_to_authenticate_key),
|
||||
mUseFingerprintFutureCheckBox.isChecked());
|
||||
editor.apply();
|
||||
|
||||
if (mUseFingerprintFutureCheckBox.isChecked()) {
|
||||
// Re-create the key so that fingerprints including new ones are validated.
|
||||
//mActivity.createKey(MainActivity.DEFAULT_KEY_NAME, true);
|
||||
mStage = Stage.FINGERPRINT;
|
||||
}
|
||||
}
|
||||
mPassword.setText("");
|
||||
//mActivity.onPurchased(, null);
|
||||
dismiss();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if {@code password} is correct, false otherwise
|
||||
*/
|
||||
private boolean checkPassword(String password) {
|
||||
// Assume the password is always correct.
|
||||
// In the real world situation, the password needs to be verified in the server side.
|
||||
return password.length() > 0;
|
||||
}
|
||||
|
||||
private final Runnable mShowKeyboardRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mInputMethodManager.showSoftInput(mPassword, 0);
|
||||
}
|
||||
};
|
||||
|
||||
private void updateStage() {
|
||||
switch (mStage) {
|
||||
case FINGERPRINT:
|
||||
mCancelButton.setText(R.string.cancel);
|
||||
mSecondDialogButton.setText(R.string.use_password);
|
||||
mFingerprintContent.setVisibility(View.VISIBLE);
|
||||
mBackupContent.setVisibility(View.GONE);
|
||||
break;
|
||||
case NEW_FINGERPRINT_ENROLLED:
|
||||
// Intentional fall through
|
||||
case PASSWORD:
|
||||
mCancelButton.setText(R.string.cancel);
|
||||
mSecondDialogButton.setText(R.string.ok);
|
||||
mFingerprintContent.setVisibility(View.GONE);
|
||||
mBackupContent.setVisibility(View.VISIBLE);
|
||||
if (mStage == Stage.NEW_FINGERPRINT_ENROLLED) {
|
||||
mPasswordDescriptionTextView.setVisibility(View.GONE);
|
||||
mNewFingerprintEnrolledTextView.setVisibility(View.VISIBLE);
|
||||
mUseFingerprintFutureCheckBox.setVisibility(View.VISIBLE);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
||||
if (actionId == EditorInfo.IME_ACTION_GO) {
|
||||
verifyPassword();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthenticated() {
|
||||
// Callback from FingerprintUiHelper. Let the activity know that authentication was
|
||||
// successful.
|
||||
mActivity.onAuthenticated(mAction, mCryptoObject);
|
||||
dismiss();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError() {
|
||||
goToBackup();
|
||||
}
|
||||
|
||||
/**
|
||||
* Enumeration to indicate which authentication method the user is trying to authenticate with.
|
||||
*/
|
||||
public enum Stage {
|
||||
FINGERPRINT,
|
||||
NEW_FINGERPRINT_ENROLLED,
|
||||
PASSWORD
|
||||
}
|
||||
|
||||
public enum Action {
|
||||
LOAD,
|
||||
SAVE
|
||||
}
|
||||
}
|
@ -1,172 +0,0 @@
|
||||
package me.impy.aegis.finger;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License
|
||||
*/
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.DialogFragment;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.SharedPreferences;
|
||||
import android.hardware.fingerprint.FingerprintManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.RequiresApi;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.inputmethod.EditorInfo;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.Button;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import me.impy.aegis.CustomAuthenticationSlide;
|
||||
import me.impy.aegis.IntroActivity;
|
||||
import me.impy.aegis.MainActivity;
|
||||
import me.impy.aegis.R;
|
||||
|
||||
/**
|
||||
* A dialog which uses fingerprint APIs to authenticate the user, and falls back to password
|
||||
* authentication if fingerprint is not available.
|
||||
*/
|
||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||
public class SetFingerprintAuthenticationDialog extends DialogFragment implements FingerprintUiHelper.Callback {
|
||||
|
||||
private Button mCancelButton;
|
||||
private Button mSecondDialogButton;
|
||||
private View mFingerprintContent;
|
||||
private View mBackupContent;
|
||||
private EditText mPassword;
|
||||
private CheckBox mUseFingerprintFutureCheckBox;
|
||||
private TextView mPasswordDescriptionTextView;
|
||||
private TextView mNewFingerprintEnrolledTextView;
|
||||
|
||||
private Stage mStage = Stage.FINGERPRINT;
|
||||
private Action mAction;
|
||||
|
||||
private FingerprintManager.CryptoObject mCryptoObject;
|
||||
private FingerprintUiHelper mFingerprintUiHelper;
|
||||
private IntroActivity mIntroActivity;
|
||||
public InterfaceCommunicator interfaceCommunicator;
|
||||
|
||||
private InputMethodManager mInputMethodManager;
|
||||
private SharedPreferences mSharedPreferences;
|
||||
private CustomAuthenticationSlide customAuthenticationSlide;
|
||||
|
||||
public void setCaller(CustomAuthenticationSlide customAuthenticationSlide)
|
||||
{
|
||||
this.customAuthenticationSlide = customAuthenticationSlide;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
setRetainInstance(true);
|
||||
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Material_Light_Dialog);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
getDialog().setTitle(getString(R.string.sign_in));
|
||||
View v = inflater.inflate(R.layout.fingerprint_dialog_container, container, false);
|
||||
mCancelButton = (Button) v.findViewById(R.id.cancel_button);
|
||||
mCancelButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
customAuthenticationSlide.sendRequestCode(0);
|
||||
dismiss();
|
||||
}
|
||||
});
|
||||
|
||||
mFingerprintContent = v.findViewById(R.id.fingerprint_container);
|
||||
mFingerprintUiHelper = new FingerprintUiHelper(
|
||||
mIntroActivity.getSystemService(FingerprintManager.class),
|
||||
(ImageView) v.findViewById(R.id.fingerprint_icon),
|
||||
(TextView) v.findViewById(R.id.fingerprint_status), this);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
if (mStage == Stage.FINGERPRINT) {
|
||||
mFingerprintUiHelper.startListening(mCryptoObject);
|
||||
}
|
||||
}
|
||||
|
||||
public void setStage(Stage stage) {
|
||||
mStage = stage;
|
||||
}
|
||||
|
||||
public void setAction(Action action) { mAction = action; }
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
mFingerprintUiHelper.stopListening();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDismiss(DialogInterface dialog) {
|
||||
super.onDismiss(dialog);
|
||||
customAuthenticationSlide.sendRequestCode(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Activity activity) {
|
||||
super.onAttach(activity);
|
||||
mIntroActivity = (IntroActivity) activity;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthenticated() {
|
||||
// Callback from FingerprintUiHelper. Let the activity know that authentication was
|
||||
// successful.
|
||||
customAuthenticationSlide.sendRequestCode(1);
|
||||
dismiss();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError() {
|
||||
customAuthenticationSlide.sendRequestCode(0);
|
||||
}
|
||||
|
||||
public interface InterfaceCommunicator {
|
||||
void sendRequestCode(int code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enumeration to indicate which authentication method the user is trying to authenticate with.
|
||||
*/
|
||||
public enum Stage {
|
||||
FINGERPRINT,
|
||||
NEW_FINGERPRINT_ENROLLED,
|
||||
PASSWORD
|
||||
}
|
||||
|
||||
public enum Action {
|
||||
LOAD,
|
||||
SAVE
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue