|
| 1 | +/* |
| 2 | + * Copyright (c) 2015-2021, Virgil Security, Inc. |
| 3 | + * |
| 4 | + * Lead Maintainer: Virgil Security Inc. <[email protected]> |
| 5 | + * |
| 6 | + * All rights reserved. |
| 7 | + * |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions are met: |
| 10 | + * |
| 11 | + * (1) Redistributions of source code must retain the above copyright notice, this |
| 12 | + * list of conditions and the following disclaimer. |
| 13 | + * |
| 14 | + * (2) Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * (3) Neither the name of virgil nor the names of its |
| 19 | + * contributors may be used to endorse or promote products derived from |
| 20 | + * this software without specific prior written permission. |
| 21 | + * |
| 22 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 23 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 25 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 26 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 27 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 28 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 30 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | + */ |
| 33 | + |
| 34 | +package com.virgilsecurity.android.common.snippet; |
| 35 | + |
| 36 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 37 | + |
| 38 | +import com.virgilsecurity.android.common.callback.OnGetTokenCallback; |
| 39 | +import com.virgilsecurity.android.common.model.DerivedPasswords; |
| 40 | +import com.virgilsecurity.android.common.model.java.EThreeParams; |
| 41 | +import com.virgilsecurity.android.common.utils.TestConfig; |
| 42 | +import com.virgilsecurity.android.common.utils.TestUtils; |
| 43 | +import com.virgilsecurity.android.ethree.interaction.EThree; |
| 44 | +import com.virgilsecurity.common.callback.OnCompleteListener; |
| 45 | +import com.virgilsecurity.sdk.crypto.VirgilCrypto; |
| 46 | + |
| 47 | +import org.jetbrains.annotations.NotNull; |
| 48 | +import org.junit.Before; |
| 49 | +import org.junit.Test; |
| 50 | +import org.junit.runner.RunWith; |
| 51 | + |
| 52 | +import java.util.UUID; |
| 53 | + |
| 54 | +import static org.junit.Assert.assertNotNull; |
| 55 | + |
| 56 | +/** |
| 57 | + * This test covers snippets that used in documentation. |
| 58 | + */ |
| 59 | +@RunWith(AndroidJUnit4.class) |
| 60 | +public class SnippetsJavaTest { |
| 61 | + private String aliceIdentity; |
| 62 | + private String bobIdentity; |
| 63 | + private EThree aliceEthree; |
| 64 | + private EThree bobEthree; |
| 65 | + |
| 66 | + @Before |
| 67 | + public void setup() { |
| 68 | + this.aliceIdentity = UUID.randomUUID().toString(); |
| 69 | + OnGetTokenCallback aliceCallback = new OnGetTokenCallback() { |
| 70 | + |
| 71 | + @NotNull |
| 72 | + @Override |
| 73 | + public String onGetToken() { |
| 74 | + return TestUtils.Companion.generateTokenString(aliceIdentity); |
| 75 | + } |
| 76 | + }; |
| 77 | + this.aliceEthree = new EThree(aliceIdentity, aliceCallback, TestConfig.Companion.getContext()); |
| 78 | + assertNotNull(this.aliceEthree); |
| 79 | + this.aliceEthree.register().execute(); |
| 80 | + |
| 81 | + this.bobIdentity = UUID.randomUUID().toString(); |
| 82 | + OnGetTokenCallback bobCallback = new OnGetTokenCallback() { |
| 83 | + |
| 84 | + @NotNull |
| 85 | + @Override |
| 86 | + public String onGetToken() { |
| 87 | + return TestUtils.Companion.generateTokenString(bobIdentity); |
| 88 | + } |
| 89 | + }; |
| 90 | + this.bobEthree = new EThree(bobIdentity, bobCallback, TestConfig.Companion.getContext()); |
| 91 | + assertNotNull(this.bobEthree); |
| 92 | + this.bobEthree.register().execute(); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void backup_restore_key() { |
| 97 | + String identity = UUID.randomUUID().toString(); |
| 98 | + String keyPassword = UUID.randomUUID().toString(); |
| 99 | + String userPassword = UUID.randomUUID().toString(); |
| 100 | + OnGetTokenCallback getTokenCallback = new OnGetTokenCallback() { |
| 101 | + |
| 102 | + @NotNull |
| 103 | + @Override |
| 104 | + public String onGetToken() { |
| 105 | + return TestUtils.Companion.generateTokenString(bobIdentity); |
| 106 | + } |
| 107 | + }; |
| 108 | + EThreeParams params = new EThreeParams(identity, getTokenCallback, TestConfig.Companion.getContext()); |
| 109 | + EThree eThree = new EThree(params); |
| 110 | + |
| 111 | + |
| 112 | + // Kotlin (Back up key) >> |
| 113 | + |
| 114 | + OnCompleteListener backupListener = new OnCompleteListener() { |
| 115 | + @Override public void onSuccess() { |
| 116 | + // private key backup success |
| 117 | + } |
| 118 | + |
| 119 | + @Override public void onError(@NotNull Throwable throwable) { |
| 120 | + // Error handling |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + // Backup user's private key to the cloud (encrypted using her password). |
| 125 | + // This will enable your user to log in from another device and have access |
| 126 | + // to the same private key there. |
| 127 | + eThree.backupPrivateKey(keyPassword).addCallback(backupListener); |
| 128 | + |
| 129 | + // << Kotlin (Back up key) |
| 130 | + |
| 131 | + |
| 132 | + // Kotlin (Make user's password the backup password) >> |
| 133 | + |
| 134 | + DerivedPasswords derivedPasswords = EThree.derivePasswords(userPassword); |
| 135 | + |
| 136 | + // This password should be used for backup/restore PrivateKey |
| 137 | + String backupPassword = derivedPasswords.getBackupPassword(); |
| 138 | + |
| 139 | + // This password should be used for other purposes, e.g user authorization |
| 140 | + String loginPassword = derivedPasswords.getLoginPassword(); |
| 141 | + |
| 142 | + // << Kotlin (Make user's password the backup password) |
| 143 | + |
| 144 | + |
| 145 | + assertNotNull(backupPassword); |
| 146 | + assertNotNull(loginPassword); |
| 147 | + |
| 148 | + |
| 149 | + // Kotlin (Restore key) >> |
| 150 | + |
| 151 | + OnCompleteListener restoreListener = new OnCompleteListener() { |
| 152 | + @Override public void onSuccess() { |
| 153 | + // You're done |
| 154 | + } |
| 155 | + |
| 156 | + @Override public void onError(@NotNull Throwable throwable) { |
| 157 | + // Error handling |
| 158 | + } |
| 159 | + }; |
| 160 | + |
| 161 | + // If user wants to restore her private key from backup in Virgil Cloud. |
| 162 | + // While user in session - key can be removed and restore multiply times (via cleanup/restorePrivateKey functions). |
| 163 | + // To know whether private key is present on device now use hasLocalPrivateKey() function: |
| 164 | + if (!eThree.hasLocalPrivateKey()) { |
| 165 | + eThree.restorePrivateKey(keyPassword).addCallback(restoreListener); |
| 166 | + } |
| 167 | + |
| 168 | + // << Kotlin (Restore key) |
| 169 | + |
| 170 | + |
| 171 | + String oldPassword = keyPassword; |
| 172 | + String newPassword = UUID.randomUUID().toString(); |
| 173 | + |
| 174 | + |
| 175 | + // Kotlin (Change backup password) >> |
| 176 | + |
| 177 | + OnCompleteListener changeListener = new OnCompleteListener() { |
| 178 | + @Override public void onSuccess() { |
| 179 | + // You're done |
| 180 | + } |
| 181 | + |
| 182 | + @Override public void onError(@NotNull Throwable throwable) { |
| 183 | + // Error handling |
| 184 | + } |
| 185 | + }; |
| 186 | + |
| 187 | + // If the user wants to change his password for private key backup |
| 188 | + eThree.changePassword(oldPassword, newPassword).addCallback(changeListener); |
| 189 | + |
| 190 | + // << Kotlin (Change backup password) |
| 191 | + |
| 192 | + |
| 193 | + // Kotlin (Delete backup) >> |
| 194 | + |
| 195 | + OnCompleteListener resetListener = new OnCompleteListener() { |
| 196 | + @Override public void onSuccess() { |
| 197 | + // You're done |
| 198 | + } |
| 199 | + |
| 200 | + @Override public void onError(@NotNull Throwable throwable) { |
| 201 | + // Error handling |
| 202 | + } |
| 203 | + }; |
| 204 | + |
| 205 | + // If user wants to delete their account, use the following function |
| 206 | + // to delete their private key |
| 207 | + eThree.resetPrivateKeyBackup().addCallback(resetListener); |
| 208 | + |
| 209 | + // << Kotlin (Delete backup) |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + public void backup_restore_key_with_keyName() { |
| 214 | + String identity = UUID.randomUUID().toString(); |
| 215 | + String keyName = UUID.randomUUID().toString(); |
| 216 | + String keyPassword = UUID.randomUUID().toString(); |
| 217 | + String userPassword = UUID.randomUUID().toString(); |
| 218 | + OnGetTokenCallback getTokenCallback = new OnGetTokenCallback() { |
| 219 | + |
| 220 | + @NotNull |
| 221 | + @Override |
| 222 | + public String onGetToken() { |
| 223 | + return TestUtils.Companion.generateTokenString(bobIdentity); |
| 224 | + } |
| 225 | + }; |
| 226 | + EThreeParams params = new EThreeParams(identity, getTokenCallback, TestConfig.Companion.getContext()); |
| 227 | + EThree eThree = new EThree(params); |
| 228 | + |
| 229 | + |
| 230 | + // Kotlin (Back up key) >> |
| 231 | + |
| 232 | + OnCompleteListener backupListener = new OnCompleteListener() { |
| 233 | + @Override public void onSuccess() { |
| 234 | + // private key backup success |
| 235 | + } |
| 236 | + |
| 237 | + @Override public void onError(@NotNull Throwable throwable) { |
| 238 | + // Error handling |
| 239 | + } |
| 240 | + }; |
| 241 | + |
| 242 | + // Backup user's private key to the cloud (encrypted using her password). |
| 243 | + // This will enable your user to log in from another device and have access |
| 244 | + // to the same private key there. |
| 245 | + eThree.backupPrivateKey(keyName, keyPassword).addCallback(backupListener); |
| 246 | + |
| 247 | + // << Kotlin (Back up key) |
| 248 | + |
| 249 | + |
| 250 | + // Kotlin (Restore key) >> |
| 251 | + |
| 252 | + OnCompleteListener restoreListener = new OnCompleteListener() { |
| 253 | + @Override public void onSuccess() { |
| 254 | + // You're done |
| 255 | + } |
| 256 | + |
| 257 | + @Override public void onError(@NotNull Throwable throwable) { |
| 258 | + // Error handling |
| 259 | + } |
| 260 | + }; |
| 261 | + |
| 262 | + // If user wants to restore her private key from backup in Virgil Cloud. |
| 263 | + // While user in session - key can be removed and restore multiply times (via cleanup/restorePrivateKey functions). |
| 264 | + // To know whether private key is present on device now use hasLocalPrivateKey() function: |
| 265 | + if (!eThree.hasLocalPrivateKey()) { |
| 266 | + eThree.restorePrivateKey(keyName, keyPassword).addCallback(restoreListener); |
| 267 | + } |
| 268 | + |
| 269 | + // << Kotlin (Restore key) |
| 270 | + |
| 271 | + |
| 272 | + String oldPassword = keyPassword; |
| 273 | + String newPassword = UUID.randomUUID().toString(); |
| 274 | + |
| 275 | + |
| 276 | + // Kotlin (Change backup password) >> |
| 277 | + |
| 278 | + OnCompleteListener changeListener = new OnCompleteListener() { |
| 279 | + @Override public void onSuccess() { |
| 280 | + // You're done |
| 281 | + } |
| 282 | + |
| 283 | + @Override public void onError(@NotNull Throwable throwable) { |
| 284 | + // Error handling |
| 285 | + } |
| 286 | + }; |
| 287 | + |
| 288 | + // If the user wants to change his password for private key backup |
| 289 | + eThree.changePassword(oldPassword, newPassword).addCallback(changeListener); |
| 290 | + |
| 291 | + // << Kotlin (Change backup password) |
| 292 | + |
| 293 | + |
| 294 | + // Kotlin (Delete backup) >> |
| 295 | + |
| 296 | + OnCompleteListener resetListener = new OnCompleteListener() { |
| 297 | + @Override public void onSuccess() { |
| 298 | + // You're done |
| 299 | + } |
| 300 | + |
| 301 | + @Override public void onError(@NotNull Throwable throwable) { |
| 302 | + // Error handling |
| 303 | + } |
| 304 | + }; |
| 305 | + |
| 306 | + // If user wants to delete their account, use the following function |
| 307 | + // to delete their private key |
| 308 | + eThree.resetPrivateKeyBackup().addCallback(resetListener); |
| 309 | + |
| 310 | + // << Kotlin (Delete backup) |
| 311 | + } |
| 312 | + |
| 313 | +} |
0 commit comments