Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
Rename CredentialStorage to PasscodeDataStorage. Implement RealmPassc…
Browse files Browse the repository at this point in the history
…odeDataStorage in separate module.
  • Loading branch information
hluhovskyi committed Oct 25, 2017
1 parent 0fa0468 commit f928337
Show file tree
Hide file tree
Showing 17 changed files with 330 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
*/
public class CustomApplication extends Application {

@SuppressWarnings("unchecked")
@Override
public void onCreate() {
super.onCreate();

LockManager<CustomPinActivity> lockManager = LockManager.getInstance();
LockManager lockManager = LockManager.getInstance();
lockManager.enableAppLock(this, CustomPinActivity.class);
lockManager.getAppLock().setLogoId(R.drawable.security_lock);
}
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath "io.realm:realm-gradle-plugin:3.1.4"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import com.github.orangegangsters.lollipin.lib.enums.Algorithm;

public interface CredentialStorage {
public interface PasscodeDataStorage {

int readAttemptsCount();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import android.support.annotation.Nullable;

import com.github.orangegangsters.lollipin.lib.interfaces.ConfigurationStorage;
import com.github.orangegangsters.lollipin.lib.interfaces.CredentialStorage;
import com.github.orangegangsters.lollipin.lib.interfaces.PasscodeDataStorage;

import java.util.HashSet;

Expand Down Expand Up @@ -235,7 +235,7 @@ public static class Builder {
private final Class<? extends AppLockActivity> mActivityClass;

private ConfigurationStorage mConfigurationStorage;
private CredentialStorage mCredentialStorage;
private PasscodeDataStorage mPasscodeDataStorage;

public Builder(Context context, Class<? extends AppLockActivity> activityClass) {
mContext = context;
Expand Down Expand Up @@ -263,12 +263,12 @@ public Builder setConfigurationStorage(@Nullable ConfigurationStorage configurat
}

@Nullable
CredentialStorage getCredentialStorage() {
return mCredentialStorage;
PasscodeDataStorage getPasscodeDataStorage() {
return mPasscodeDataStorage;
}

public Builder setCredentialStorage(@Nullable CredentialStorage credentialStorage) {
mCredentialStorage = credentialStorage;
public Builder setPasscodeDataStorage(@Nullable PasscodeDataStorage passcodeDataStorage) {
mPasscodeDataStorage = passcodeDataStorage;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
import com.github.orangegangsters.lollipin.lib.encryption.SaltGenerator;
import com.github.orangegangsters.lollipin.lib.enums.Algorithm;
import com.github.orangegangsters.lollipin.lib.interfaces.ConfigurationStorage;
import com.github.orangegangsters.lollipin.lib.interfaces.CredentialStorage;
import com.github.orangegangsters.lollipin.lib.interfaces.PasscodeDataStorage;
import com.github.orangegangsters.lollipin.lib.interfaces.LifeCycleInterface;
import com.github.orangegangsters.lollipin.lib.storage.DefaultPreferencesConfigurationStorage;
import com.github.orangegangsters.lollipin.lib.storage.DefaultPreferencesCredentialStorage;
import com.github.orangegangsters.lollipin.lib.storage.DefaultPreferencesPasscodeDataStorage;

public class AppLockImpl extends AppLock implements LifeCycleInterface {

public static final String TAG = "AppLockImpl";

private final CredentialStorage mCredentialStorage;
private final PasscodeDataStorage mPasscodeDataStorage;
private final ConfigurationStorage mConfigurationStorage;

/**
Expand Down Expand Up @@ -53,14 +53,14 @@ public static AppLockImpl getInstance(Context context, Class<? extends AppLockAc

AppLockImpl(Context context, Class<? extends AppLockActivity> activityClass) {
mActivityClass = activityClass;
mCredentialStorage = new DefaultPreferencesCredentialStorage(context);
mPasscodeDataStorage = new DefaultPreferencesPasscodeDataStorage(context);
mConfigurationStorage = new DefaultPreferencesConfigurationStorage(context);
}

AppLockImpl(Builder builder) {
mActivityClass = builder.getActivityClass();
mConfigurationStorage = getConfigurationStorageOrDefault(builder);
mCredentialStorage = getCredentialStorageOrDefault(builder);
mPasscodeDataStorage = getPasscodeDataStorageOrDefault(builder);
}

private static ConfigurationStorage getConfigurationStorageOrDefault(Builder builder) {
Expand All @@ -72,10 +72,10 @@ private static ConfigurationStorage getConfigurationStorageOrDefault(Builder bui
}
}

private static CredentialStorage getCredentialStorageOrDefault(Builder builder) {
CredentialStorage storage = builder.getCredentialStorage();
private static PasscodeDataStorage getPasscodeDataStorageOrDefault(Builder builder) {
PasscodeDataStorage storage = builder.getPasscodeDataStorage();
if (storage == null) {
return new DefaultPreferencesCredentialStorage(builder.getContext());
return new DefaultPreferencesPasscodeDataStorage(builder.getContext());
} else {
return storage;
}
Expand All @@ -92,7 +92,7 @@ public void setTimeout(long timeout) {
}

public String getSalt() {
String salt = mCredentialStorage.readSalt();
String salt = mPasscodeDataStorage.readSalt();
if (salt == null) {
salt = SaltGenerator.generate();
setSalt(salt);
Expand All @@ -101,7 +101,7 @@ public String getSalt() {
}

private void setSalt(String salt) {
mCredentialStorage.writeSalt(salt);
mPasscodeDataStorage.writeSalt(salt);
}

@Override
Expand Down Expand Up @@ -166,7 +166,7 @@ public void disableAndRemoveConfiguration() {
PinCompatActivity.clearListeners();
PinFragmentActivity.clearListeners();
mConfigurationStorage.clear();
mCredentialStorage.clear();
mPasscodeDataStorage.clear();
}

@Override
Expand All @@ -191,32 +191,32 @@ public void setLastActiveMillis() {

@Override
public int getAttemptsCount() {
return mCredentialStorage.readAttemptsCount();
return mPasscodeDataStorage.readAttemptsCount();
}

@Override
public int incrementAttemptsCountAndGet() {
int attempts = mCredentialStorage.readAttemptsCount() + 1;
mCredentialStorage.writeAttemptsCount(attempts);
int attempts = mPasscodeDataStorage.readAttemptsCount() + 1;
mPasscodeDataStorage.writeAttemptsCount(attempts);
return attempts;
}

@Override
public void resetAttemptsCount() {
mCredentialStorage.writeAttemptsCount(0);
mPasscodeDataStorage.writeAttemptsCount(0);
}

@Override
public boolean checkPasscode(String passcode) {
Algorithm algorithm = mCredentialStorage.readCurrentAlgorithm();
Algorithm algorithm = mPasscodeDataStorage.readCurrentAlgorithm();

String salt = getSalt();
passcode = salt + passcode + salt;
passcode = Encryptor.getSHA(passcode, algorithm);
String storedPasscode = "";

if (isPasscodeSet()) {
storedPasscode = mCredentialStorage.readPasscode();
storedPasscode = mPasscodeDataStorage.readPasscode();
}

return storedPasscode.equalsIgnoreCase(passcode);
Expand All @@ -226,12 +226,12 @@ public boolean checkPasscode(String passcode) {
public boolean setPasscode(String passcode) {
String salt = getSalt();
if (passcode == null) {
mCredentialStorage.clearPasscode();
mPasscodeDataStorage.clearPasscode();
this.disable();
} else {
setAlgorithm(Algorithm.SHA256);
passcode = Encryptor.getSHA(salt + passcode + salt, Algorithm.SHA256);
mCredentialStorage.writePasscode(passcode);
mPasscodeDataStorage.writePasscode(passcode);
this.enable();
}
return true;
Expand All @@ -241,12 +241,12 @@ public boolean setPasscode(String passcode) {
* Set the algorithm used in {@link #setPasscode(String)}
*/
private void setAlgorithm(Algorithm algorithm) {
mCredentialStorage.writeCurrentAlgorithm(algorithm);
mPasscodeDataStorage.writeCurrentAlgorithm(algorithm);
}

@Override
public boolean isPasscodeSet() {
return mCredentialStorage.hasPasscode();
return mPasscodeDataStorage.hasPasscode();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
* the actual app calling the library.
* You must get this static instance by calling {@link #getInstance()}
*/
public class LockManager<T extends AppLockActivity> {
public class LockManager {

/**
* The static singleton instance
*/
private static LockManager mInstance;
private static final LockManager INSTANCE = new LockManager();
/**
* The static singleton instance of {@link com.github.orangegangsters.lollipin.lib.managers.AppLock}
*/
Expand All @@ -27,19 +27,14 @@ public class LockManager<T extends AppLockActivity> {
* Used to retrieve the static instance
*/
public static LockManager getInstance() {
synchronized (LockManager.class) {
if (mInstance == null) {
mInstance = new LockManager<>();
}
}
return mInstance;
return INSTANCE;
}

/**
* You must call that into your custom {@link android.app.Application} to enable the
* {@link com.github.orangegangsters.lollipin.lib.PinActivity}
*/
public void enableAppLock(Context context, Class<T> activityClass) {
public void enableAppLock(Context context, Class<? extends AppLockActivity> activityClass) {
if (mAppLocker != null) {
mAppLocker.disable();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import android.support.annotation.Nullable;

import com.github.orangegangsters.lollipin.lib.enums.Algorithm;
import com.github.orangegangsters.lollipin.lib.interfaces.CredentialStorage;
import com.github.orangegangsters.lollipin.lib.interfaces.PasscodeDataStorage;

public class DefaultPreferencesCredentialStorage implements CredentialStorage {
public class DefaultPreferencesPasscodeDataStorage implements PasscodeDataStorage {

private static final String ATTEMPTS_COUNT_PREFERENCE_KEY = "ATTEMPTS_COUNT_PREFERENCE_KEY";
/**
Expand All @@ -27,7 +27,7 @@ public class DefaultPreferencesCredentialStorage implements CredentialStorage {

private final SharedPreferences mPreferences;

public DefaultPreferencesCredentialStorage(Context context) {
public DefaultPreferencesPasscodeDataStorage(Context context) {
mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
}

Expand Down
1 change: 1 addition & 0 deletions realm-storage/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
26 changes: 26 additions & 0 deletions realm-storage/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apply plugin: 'com.android.library'
apply plugin: 'realm-android'

android {
compileSdkVersion 26
buildToolsVersion "26.0.1"

defaultConfig {
minSdkVersion 14
targetSdkVersion 26
versionCode 1
versionName "1.0"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(":lib")
}
25 changes: 25 additions & 0 deletions realm-storage/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Users\ahluh\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
1 change: 1 addition & 0 deletions realm-storage/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="com.dewarder.realm_storage"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.dewarder.realmstorage;

import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;

public class PasscodeData extends RealmObject {

/**
* Ensure that only one object of PasscodeData will be created in Realm
*/
private static final long SINGLE_PRIMARY_KEY = 0L;

@PrimaryKey
private long id = SINGLE_PRIMARY_KEY;
private int attempts;
private String salt;
private String algorithm;
private String passcode;

public int getAttempts() {
return attempts;
}

public void setAttempts(int attempts) {
this.attempts = attempts;
}

public String getSalt() {
return salt;
}

public void setSalt(String salt) {
this.salt = salt;
}

public String getAlgorithm() {
return algorithm;
}

public void setAlgorithm(String algorithm) {
this.algorithm = algorithm;
}

public String getPasscode() {
return passcode;
}

public void setPasscode(String passcode) {
this.passcode = passcode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.dewarder.realmstorage;

import io.realm.annotations.RealmModule;

@RealmModule(library = true, classes = {PasscodeData.class})
public class PasscodeDataModule {
}
Loading

0 comments on commit f928337

Please sign in to comment.