private async Task ProvideCredentialAsync()
        {
            var cipherService = ServiceContainer.Resolve <ICipherService>("cipherService", true);

            Bit.Core.Models.Domain.Cipher cipher = null;
            var cancel = cipherService == null || _context.CredentialIdentity?.RecordIdentifier == null;

            if (!cancel)
            {
                cipher = await cipherService.GetAsync(_context.CredentialIdentity.RecordIdentifier);

                cancel = cipher == null || cipher.Type != Bit.Core.Enums.CipherType.Login || cipher.Login == null;
            }
            if (cancel)
            {
                var err = new NSError(new NSString("ASExtensionErrorDomain"),
                                      Convert.ToInt32(ASExtensionErrorCode.CredentialIdentityNotFound), null);
                ExtensionContext?.CancelRequest(err);
                return;
            }

            var storageService = ServiceContainer.Resolve <IStorageService>("storageService");
            var decCipher      = await cipher.DecryptAsync();

            string totpCode        = null;
            var    disableTotpCopy = await storageService.GetAsync <bool?>(Bit.Core.Constants.DisableAutoTotpCopyKey);

            if (!disableTotpCopy.GetValueOrDefault(false))
            {
                var userService           = ServiceContainer.Resolve <IUserService>("userService");
                var canAccessPremiumAsync = await userService.CanAccessPremiumAsync();

                if (!string.IsNullOrWhiteSpace(decCipher.Login.Totp) &&
                    (canAccessPremiumAsync || cipher.OrganizationUseTotp))
                {
                    var totpService = ServiceContainer.Resolve <ITotpService>("totpService");
                    totpCode = await totpService.GetCodeAsync(decCipher.Login.Totp);
                }
            }

            CompleteRequest(decCipher.Id, decCipher.Login.Username, decCipher.Login.Password, totpCode);
        }
Exemplo n.º 2
0
        private async Task ProvideCredentialAsync(bool userInteraction = true)
        {
            var cipherService = ServiceContainer.Resolve <ICipherService>("cipherService", true);

            Bit.Core.Models.Domain.Cipher cipher = null;
            var cancel = cipherService == null || _context.CredentialIdentity?.RecordIdentifier == null;

            if (!cancel)
            {
                cipher = await cipherService.GetAsync(_context.CredentialIdentity.RecordIdentifier);

                cancel = cipher == null || cipher.Type != Bit.Core.Enums.CipherType.Login || cipher.Login == null;
            }
            if (cancel)
            {
                var err = new NSError(new NSString("ASExtensionErrorDomain"),
                                      Convert.ToInt32(ASExtensionErrorCode.CredentialIdentityNotFound), null);
                ExtensionContext?.CancelRequest(err);
                return;
            }

            var storageService = ServiceContainer.Resolve <IStorageService>("storageService");
            var decCipher      = await cipher.DecryptAsync();

            if (decCipher.Reprompt != Bit.Core.Enums.CipherRepromptType.None)
            {
                // Prompt for password using either the lock screen or dialog unless
                // already verified the password.
                if (!userInteraction)
                {
                    await storageService.SaveAsync(Bit.Core.Constants.PasswordRepromptAutofillKey, true);

                    var err = new NSError(new NSString("ASExtensionErrorDomain"),
                                          Convert.ToInt32(ASExtensionErrorCode.UserInteractionRequired), null);
                    ExtensionContext?.CancelRequest(err);
                    return;
                }
                else if (!await storageService.GetAsync <bool>(Bit.Core.Constants.PasswordVerifiedAutofillKey))
                {
                    // Add a timeout to resolve keyboard not always showing up.
                    await Task.Delay(250);

                    var passwordRepromptService = ServiceContainer.Resolve <IPasswordRepromptService>("passwordRepromptService");
                    if (!await passwordRepromptService.ShowPasswordPromptAsync())
                    {
                        var err = new NSError(new NSString("ASExtensionErrorDomain"),
                                              Convert.ToInt32(ASExtensionErrorCode.UserCanceled), null);
                        ExtensionContext?.CancelRequest(err);
                        return;
                    }
                }
            }
            string totpCode        = null;
            var    disableTotpCopy = await storageService.GetAsync <bool?>(Bit.Core.Constants.DisableAutoTotpCopyKey);

            if (!disableTotpCopy.GetValueOrDefault(false))
            {
                var userService           = ServiceContainer.Resolve <IUserService>("userService");
                var canAccessPremiumAsync = await userService.CanAccessPremiumAsync();

                if (!string.IsNullOrWhiteSpace(decCipher.Login.Totp) &&
                    (canAccessPremiumAsync || cipher.OrganizationUseTotp))
                {
                    var totpService = ServiceContainer.Resolve <ITotpService>("totpService");
                    totpCode = await totpService.GetCodeAsync(decCipher.Login.Totp);
                }
            }

            CompleteRequest(decCipher.Id, decCipher.Login.Username, decCipher.Login.Password, totpCode);
        }