예제 #1
0
        private async void AddDialogPositive(object sender, EventArgs e)
        {
            var error = false;

            if (_addDialog.Issuer.Trim() == "")
            {
                _addDialog.IssuerError = GetString(Resource.String.noIssuer);
                error = true;
            }

            var secret = Authenticator.CleanSecret(_addDialog.Secret);

            if (secret == "")
            {
                _addDialog.SecretError = GetString(Resource.String.noSecret);
                error = true;
            }
            else if (!Authenticator.IsValidSecret(secret))
            {
                _addDialog.SecretError = GetString(Resource.String.secretInvalid);
                error = true;
            }

            if (_addDialog.Digits < 6 || _addDialog.Digits > 10)
            {
                _addDialog.DigitsError = GetString(Resource.String.digitsInvalid);
                error = true;
            }

            if (_addDialog.Period <= 0)
            {
                _addDialog.PeriodError = GetString(Resource.String.periodToShort);
                error = true;
            }

            if (error)
            {
                return;
            }

            var issuer   = _addDialog.Issuer.Trim().Truncate(32);
            var username = _addDialog.Username.Trim().Truncate(32);

            var algorithm = _addDialog.Algorithm switch {
                1 => OtpHashMode.Sha256,
                2 => OtpHashMode.Sha512,
                _ => OtpHashMode.Sha1
            };

            var type = _addDialog.Type == 0 ? AuthenticatorType.Totp : AuthenticatorType.Hotp;

            var code = "";

            for (var i = 0; i < _addDialog.Digits; code += "-", i++)
            {
                ;
            }

            var auth = new Authenticator {
                Issuer    = issuer,
                Username  = username,
                Type      = type,
                Icon      = Icons.FindServiceKeyByName(issuer),
                Algorithm = algorithm,
                Counter   = 0,
                Secret    = secret,
                Digits    = _addDialog.Digits,
                Period    = _addDialog.Period,
                Code      = code
            };

            if (_authSource.IsDuplicate(auth))
            {
                _addDialog.SecretError = GetString(Resource.String.duplicateAuthenticator);
                return;
            }

            await _connection.InsertAsync(auth);

            await _authSource.UpdateSource();

            CheckEmptyState();
            _authAdapter.NotifyItemInserted(_authSource.GetPosition(auth.Secret));

            _addDialog.Dismiss();
        }
예제 #2
0
        private async void AddDialogPositive(object sender, EventArgs e)
        {
            var error = false;

            if (_addDialog.Issuer.Trim() == "")
            {
                _addDialog.IssuerError = GetString(Resource.String.noIssuer);
                error = true;
            }

            if (_addDialog.Secret.Trim() == "")
            {
                _addDialog.SecretError = GetString(Resource.String.noSecret);
                error = true;
            }

            var secret = _addDialog.Secret.Trim().ToUpper();

            if (secret.Length < 16)
            {
                _addDialog.SecretError = GetString(Resource.String.secretTooShort);
                error = true;
            }

            if (!Authenticator.IsValidSecret(secret))
            {
                _addDialog.SecretError = GetString(Resource.String.secretInvalidChars);
                error = true;
            }

            if (_addDialog.Digits < 6)
            {
                _addDialog.DigitsError = GetString(Resource.String.digitsToSmall);
                error = true;
            }

            if (_addDialog.Period < 10)
            {
                _addDialog.PeriodError = GetString(Resource.String.periodToShort);
                error = true;
            }

            if (error)
            {
                return;
            }

            var issuer   = _addDialog.Issuer.Trim().Truncate(32);
            var username = _addDialog.Username.Trim().Truncate(32);

            var algorithm = OtpHashMode.Sha1;

            switch (_addDialog.Algorithm)
            {
            case 1:
                algorithm = OtpHashMode.Sha256;
                break;

            case 2:
                algorithm = OtpHashMode.Sha512;
                break;
            }

            var type = _addDialog.Type == 0 ? OtpType.Totp : OtpType.Hotp;

            var code = "";

            for (var i = 0; i < _addDialog.Digits; code += "-", i++)
            {
                ;
            }

            var auth = new Authenticator {
                Issuer    = issuer,
                Username  = username,
                Type      = type,
                Icon      = Icons.FindServiceKeyByName(issuer),
                Algorithm = algorithm,
                Counter   = 0,
                Secret    = secret,
                Digits    = _addDialog.Digits,
                Period    = _addDialog.Period,
                Code      = code
            };

            if (_authSource.IsDuplicate(auth))
            {
                Toast.MakeText(this, Resource.String.duplicateAuthenticator, ToastLength.Short).Show();
                return;
            }

            await _connection.InsertAsync(auth);

            await _authSource.UpdateSource();

            CheckEmptyState();
            _authAdapter.NotifyDataSetChanged();

            _addDialog.Dismiss();
        }