Skip to content

Commit

Permalink
Added java overload for reset without password, added hasLocalPrivate…
Browse files Browse the repository at this point in the history
…Key to coroutines package
  • Loading branch information
BuddahLD committed Apr 4, 2019
1 parent a24b47e commit c534ff7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion ethree-kotlin-coroutines/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ android {
}

group 'com.virgilsecurity'
version '0.2.4'
version '0.2.5'

dependencies {
// Inner dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class EThree

private val virgilCrypto = VirgilCrypto()
private val cardManager: CardManager
private val localKeyStorage: KeyManagerLocal
private val keyManagerLocal: KeyManagerLocal
private val keyManagerCloud: KeyManagerCloud

init {
Expand All @@ -92,7 +92,7 @@ class EThree
VirgilCardVerifier(cardCrypto, false, false),
VirgilCardClient(VIRGIL_BASE_URL + VIRGIL_CARDS_SERVICE_PATH))
}
localKeyStorage = KeyManagerLocal(tokenProvider.getToken(NO_CONTEXT).identity, context)
keyManagerLocal = KeyManagerLocal(tokenProvider.getToken(NO_CONTEXT).identity, context)
keyManagerCloud = KeyManagerCloud(currentIdentity(), tokenProvider)
}

Expand All @@ -109,7 +109,7 @@ class EThree
throw RegistrationException("Card with identity " +
"${currentIdentity()} already exists")

if (localKeyStorage.exists())
if (keyManagerLocal.exists())
throw PrivateKeyExistsException("You already have a Private Key on this " +
"device for identity: ${currentIdentity()}. " +
"Please, use \'cleanup()\' function first.")
Expand All @@ -119,7 +119,7 @@ class EThree
this.publicKey,
currentIdentity())

localKeyStorage.store(this.privateKey.rawKey)
keyManagerLocal.store(this.privateKey.rawKey)
}
}

Expand All @@ -139,9 +139,15 @@ class EThree
fun cleanup() {
checkPrivateKeyOrThrow()

localKeyStorage.delete()
keyManagerLocal.delete()
}

/**
* Checks whether the private key is present in the local storage of current device.
* Returns *true* if the key is present in the local key storage otherwise *false*.
*/
fun hasLocalPrivateKey() = keyManagerLocal.exists()

/**
* Encrypts the user's private key using the user's [password] and backs up the encrypted
* private key to Virgil's cloud. This enables users to log in from other devices and have
Expand All @@ -164,7 +170,7 @@ class EThree
if (password.isBlank())
throw IllegalArgumentException("\'password\' should not be empty")

with(localKeyStorage.load()) {
with(keyManagerLocal.load()) {
keyManagerCloud.store(password,
this.value,
this.meta)
Expand Down Expand Up @@ -222,7 +228,7 @@ class EThree
*/
fun restorePrivateKey(password: String): Deferred<Unit> = GlobalScope.asyncWithCatch(
{
if (localKeyStorage.exists())
if (keyManagerLocal.exists())
throw RestoreKeyException("You already have a Private Key on this device" +
"for identity: ${currentIdentity()}. Please, use" +
"\'cleanup()\' function first.")
Expand All @@ -233,7 +239,7 @@ class EThree

val keyEntry = keyManagerCloud.retrieve(password)

localKeyStorage.store(keyEntry.data)
keyManagerLocal.store(keyEntry.data)
} else {
throw RestoreKeyException("There is no key backup with " +
"identity: ${currentIdentity()}")
Expand All @@ -256,7 +262,7 @@ class EThree
* @throws CryptoException
*/
fun rotatePrivateKey(): Deferred<Unit> = GlobalScope.async {
if (localKeyStorage.exists())
if (keyManagerLocal.exists())
throw PrivateKeyExistsException("You already have a Private Key on this device" +
"for identity: ${currentIdentity()}. Please, use" +
"\'cleanup()\' function first.")
Expand All @@ -278,7 +284,7 @@ class EThree
this.first.identifier)
cardManager.publishCard(rawCard)

localKeyStorage.store(this.second.privateKey.rawKey)
keyManagerLocal.store(this.second.privateKey.rawKey)
}
}

Expand Down Expand Up @@ -486,7 +492,7 @@ class EThree
* from [tokenProvider].
*/
private fun loadCurrentPrivateKey(): PrivateKey =
localKeyStorage.load().let {
keyManagerLocal.load().let {
virgilCrypto.importPrivateKey(it.value)
}

Expand All @@ -507,7 +513,7 @@ class EThree
* [PrivateKeyNotFoundException] exception.
*/
private fun checkPrivateKeyOrThrow() {
if (!localKeyStorage.exists())
if (!keyManagerLocal.exists())
throw PrivateKeyNotFoundException("You have to get private key first. Use " +
"\'register\' or \'restorePrivateKey\' functions.")
}
Expand Down
2 changes: 1 addition & 1 deletion ethree-kotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ android {
}

group 'com.virgilsecurity'
version '0.3.6'
version '0.3.7'

dependencies {
// Inner dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class EThree
* @throws PrivateKeyNotFoundException
* @throws WrongPasswordException
*/
fun resetPrivateKeyBackup(password: String? = null, onCompleteListener: OnCompleteListener) {
@JvmOverloads fun resetPrivateKeyBackup(password: String? = null, onCompleteListener: OnCompleteListener) {
GlobalScope.launch {
try {
checkPrivateKeyOrThrow()
Expand Down

0 comments on commit c534ff7

Please sign in to comment.