Search code examples
encryptionprivate-key

iOS Swift - Virgil Security user is already registered - private key not found


Context

When using the Virgil Security SDK for iOS swift after authenticating "Alice" and fetching a JWT token, upon registration an error always states that a user is already registered. This happens on first login even after clearing a device or using a new device. Further more when trying the eThree.authEncrypt() method for text an error always states that a private key is not found on the device, even though eThree.hasPrivateKey() is returning true.

Code Tried

        do {
            let params = try EThreeParams(identity: "Alice", tokenCallback: self.virgil.authWithVirgil)
            let ethree = try EThree(params: params)

            ethree.register { error in
                guard error == nil else {
                    // Error handling here
                    print(error?.localizedDescription) //User is already registered
                    return
                }
                print("New Registration")
                // User private key loaded, ready to end-to-end encrypt!
            }

        } catch {
            print(error.localizedDescription)
        }

Questions

  • Why after clearing a device or using a new device does the code tell me that a user is already registered?
  • Why does hasPrivateKey return true but then when using authEncrypt there is an error saying there it no private key in the device?

Solution

  • Q1

    Why after clearing a device or using a new device does the code tell me that a user is already registered?

    Short answer: There is no local private key within a cleared device or within a new device.

    From the official documentation.

    The EThree.register() function checks whether a user already has a private key saved in local storage, and a published public key on the Virgil Cloud. If the user doesn't have them, the function generates a new keypair for the user, saves the private key locally...

    Q2

    Why does hasPrivateKey return true but then when using authEncrypt there is an error saying there it no private key in the device?

    LocalKeyStorage.retrieveKeyPair() can be used to define private key presence.

    Best practice

    To support login across multiple-devices Key Backup mechanism can be used.