2016-09-29 12:31:55 +02:00
|
|
|
package me.impy.aegis;
|
|
|
|
|
|
|
|
import android.Manifest;
|
|
|
|
import android.content.Context;
|
2017-08-06 16:03:36 +02:00
|
|
|
import android.content.Intent;
|
2016-09-29 12:31:55 +02:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.os.Bundle;
|
2017-08-13 19:51:54 +02:00
|
|
|
import android.support.v4.app.Fragment;
|
2016-09-29 12:31:55 +02:00
|
|
|
|
2017-08-13 19:51:54 +02:00
|
|
|
import com.github.paolorotolo.appintro.AppIntro;
|
|
|
|
import com.github.paolorotolo.appintro.AppIntroFragment;
|
|
|
|
import com.github.paolorotolo.appintro.model.SliderPage;
|
2017-08-06 16:03:36 +02:00
|
|
|
|
|
|
|
import javax.crypto.Cipher;
|
|
|
|
|
|
|
|
import me.impy.aegis.crypto.CryptResult;
|
|
|
|
import me.impy.aegis.crypto.MasterKey;
|
|
|
|
import me.impy.aegis.crypto.slots.PasswordSlot;
|
|
|
|
import me.impy.aegis.crypto.slots.SlotCollection;
|
|
|
|
import me.impy.aegis.db.Database;
|
|
|
|
import me.impy.aegis.db.DatabaseFile;
|
2016-09-29 12:31:55 +02:00
|
|
|
|
2017-08-13 19:51:54 +02:00
|
|
|
public class IntroActivity extends AppIntro {
|
2017-08-06 16:03:36 +02:00
|
|
|
public static final int RESULT_OK = 0;
|
|
|
|
public static final int RESULT_EXCEPTION = 1;
|
|
|
|
|
|
|
|
private CustomAuthenticatedSlide authenticatedSlide;
|
2017-08-13 19:51:54 +02:00
|
|
|
private CustomAuthenticationSlide authenticationSlide;
|
|
|
|
private Fragment endSlide;
|
2016-09-29 12:31:55 +02:00
|
|
|
|
|
|
|
@Override
|
2017-08-13 19:51:54 +02:00
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
2016-09-29 12:31:55 +02:00
|
|
|
super.onCreate(savedInstanceState);
|
2017-08-13 19:51:54 +02:00
|
|
|
showSkipButton(false);
|
|
|
|
//showPagerIndicator(false);
|
|
|
|
setGoBackLock(true);
|
|
|
|
|
|
|
|
SliderPage homeSliderPage = new SliderPage();
|
|
|
|
homeSliderPage.setTitle("Welcome");
|
|
|
|
homeSliderPage.setDescription("Aegis is a secure, free and open source 2FA app");
|
|
|
|
homeSliderPage.setImageDrawable(R.drawable.intro_shield);
|
|
|
|
homeSliderPage.setBgColor(getResources().getColor(R.color.colorPrimary));
|
|
|
|
addSlide(AppIntroFragment.newInstance(homeSliderPage));
|
|
|
|
|
|
|
|
SliderPage permSliderPage = new SliderPage();
|
|
|
|
permSliderPage.setTitle("Permissions");
|
|
|
|
permSliderPage.setDescription("Aegis needs permission to use your camera in order to scan QR codes.");
|
|
|
|
permSliderPage.setImageDrawable(R.drawable.intro_scanner);
|
|
|
|
permSliderPage.setBgColor(getResources().getColor(R.color.colorAccent));
|
|
|
|
addSlide(AppIntroFragment.newInstance(permSliderPage));
|
|
|
|
askForPermissions(new String[]{Manifest.permission.CAMERA}, 2);
|
|
|
|
|
|
|
|
authenticationSlide = new CustomAuthenticationSlide();
|
|
|
|
authenticationSlide.setBgColor(getResources().getColor(R.color.colorHeaderSuccess));
|
|
|
|
addSlide(authenticationSlide);
|
2017-08-06 16:03:36 +02:00
|
|
|
authenticatedSlide = new CustomAuthenticatedSlide();
|
2017-08-13 19:51:54 +02:00
|
|
|
authenticatedSlide.setBgColor(getResources().getColor(R.color.colorPrimary));
|
2017-08-06 16:03:36 +02:00
|
|
|
addSlide(authenticatedSlide);
|
|
|
|
|
2017-08-13 19:51:54 +02:00
|
|
|
SliderPage endSliderPage = new SliderPage();
|
|
|
|
endSliderPage.setTitle("All done!");
|
|
|
|
endSliderPage.setDescription("Aegis has been set up and is ready to go.");
|
|
|
|
endSliderPage.setImageDrawable(R.drawable.intro_shield);
|
|
|
|
endSliderPage.setBgColor(getResources().getColor(R.color.colorPrimary));
|
|
|
|
endSlide = AppIntroFragment.newInstance(endSliderPage);
|
|
|
|
addSlide(endSlide);
|
2017-08-06 16:03:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setException(Exception e) {
|
|
|
|
Intent result = new Intent();
|
|
|
|
result.putExtra("exception", e);
|
2017-08-07 22:38:36 +02:00
|
|
|
setResult(RESULT_EXCEPTION, result);
|
2017-08-13 19:51:54 +02:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSlideChanged(Fragment oldFragment, Fragment newFragment) {
|
|
|
|
// skip to the last slide if no encryption will be used
|
|
|
|
if (oldFragment == authenticationSlide && newFragment != endSlide) {
|
|
|
|
Intent intent = getIntent();
|
|
|
|
int cryptType = intent.getIntExtra("cryptType", CustomAuthenticationSlide.CRYPT_TYPE_INVALID);
|
|
|
|
if (cryptType == CustomAuthenticationSlide.CRYPT_TYPE_NONE) {
|
|
|
|
// TODO: no magic indices
|
|
|
|
getPager().setCurrentItem(5);
|
|
|
|
}
|
|
|
|
}
|
2016-09-29 12:31:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-13 19:51:54 +02:00
|
|
|
public void onDonePressed(Fragment currentFragment) {
|
|
|
|
super.onDonePressed(currentFragment);
|
2017-08-06 16:03:36 +02:00
|
|
|
|
|
|
|
// create the database and database file
|
|
|
|
Database database = new Database();
|
|
|
|
DatabaseFile databaseFile = new DatabaseFile();
|
|
|
|
|
2017-08-06 21:45:27 +02:00
|
|
|
int cryptType = authenticatedSlide.getCryptType();
|
|
|
|
|
|
|
|
// generate the master key
|
|
|
|
MasterKey masterKey = null;
|
|
|
|
if (cryptType != CustomAuthenticationSlide.CRYPT_TYPE_NONE) {
|
|
|
|
try {
|
|
|
|
masterKey = MasterKey.generate();
|
|
|
|
} catch (Exception e) {
|
|
|
|
setException(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cryptType != CustomAuthenticationSlide.CRYPT_TYPE_NONE) {
|
|
|
|
try {
|
|
|
|
// encrypt the master key with a key derived from the user's password
|
|
|
|
// and add it to the list of slots
|
|
|
|
SlotCollection slots = databaseFile.getSlots();
|
|
|
|
PasswordSlot slot = new PasswordSlot();
|
|
|
|
Cipher cipher = authenticatedSlide.getCipher(slot, Cipher.ENCRYPT_MODE);
|
|
|
|
masterKey.encryptSlot(slot, cipher);
|
|
|
|
slots.add(slot);
|
|
|
|
} catch (Exception e) {
|
|
|
|
setException(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cryptType != CustomAuthenticationSlide.CRYPT_TYPE_FINGER) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally, save the database
|
2017-08-06 16:03:36 +02:00
|
|
|
try {
|
|
|
|
byte[] bytes = database.serialize();
|
2017-08-06 21:45:27 +02:00
|
|
|
if (cryptType == CustomAuthenticationSlide.CRYPT_TYPE_NONE) {
|
|
|
|
databaseFile.setContent(bytes);
|
|
|
|
} else {
|
|
|
|
CryptResult result = masterKey.encrypt(bytes);
|
|
|
|
databaseFile.setContent(result.Data);
|
|
|
|
databaseFile.setCryptParameters(result.Parameters);
|
|
|
|
}
|
2017-08-06 16:03:36 +02:00
|
|
|
databaseFile.save(getApplicationContext());
|
|
|
|
} catch (Exception e) {
|
|
|
|
setException(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send the master key back to the main activity
|
|
|
|
Intent result = new Intent();
|
|
|
|
result.putExtra("key", masterKey);
|
|
|
|
setResult(RESULT_OK, result);
|
|
|
|
|
|
|
|
// skip the intro from now on
|
|
|
|
// TODO: show the intro if we can't find any database files
|
2016-09-29 12:31:55 +02:00
|
|
|
SharedPreferences prefs = this.getSharedPreferences("me.impy.aegis", Context.MODE_PRIVATE);
|
|
|
|
prefs.edit().putBoolean("passedIntro", true).apply();
|
2017-08-13 19:51:54 +02:00
|
|
|
finish();
|
2016-09-29 12:31:55 +02:00
|
|
|
}
|
|
|
|
}
|