Example #1
0
        public CommandHandlerBuilder WithNewPin2()
        {
            _commands.Enqueue(() =>
            {
                try
                {
                    PinChanger.ChangePin2(_slot, _volumeOwnersStore.GetPin2Id());

                    _logger.LogInformation(_logMessageBuilder.WithTokenId(Resources.Pin2SetSuccess));
                }
                catch
                {
                    _logger.LogError(_logMessageBuilder.WithTokenId(Resources.Pin2SetFailed));
                    throw;
                }
            });

            return(this);
        }
Example #2
0
        public CommandHandlerBuilder WithNewLocalPin()
        {
            _prerequisites.Enqueue(_validator.ValidateNewLocalPinParams);

            _commands.Enqueue(() =>
            {
                try
                {
                    PinChanger.ChangeLocalPin(_slot, _runtimeTokenParams.NewUserPin.EnteredByUser ?
                                              _runtimeTokenParams.NewUserPin.Value : _runtimeTokenParams.OldUserPin.Value,
                                              _runtimeTokenParams.NewLocalPin, _runtimeTokenParams.LocalIdToCreate);

                    _logger.LogInformation(_logMessageBuilder.WithTokenId(Resources.NewLocalPinSetSuccess));
                }
                catch
                {
                    _logger.LogError(_logMessageBuilder.WithTokenId(Resources.NewLocalPinSetFailed));
                    throw;
                }
            });

            return(this);
        }
Example #3
0
        public CommandHandlerBuilder WithPinsChange()
        {
            void ChangeUserPin(PinCode ownerPinCode)
            {
                if (ownerPinCode.EnteredByUser)
                {
                    var changeBy = string.Empty;

                    try
                    {
                        if (ownerPinCode.Owner == PinCodeOwner.Admin)
                        {
                            changeBy = Resources.PinChangeByAdmin;

                            PinChanger.Change <UserPinByAdminChangeOperation>(_slot,
                                                                              ownerPinCode.Value, _runtimeTokenParams.NewUserPin.Value, CKU.CKU_SO);
                        }
                        else
                        {
                            PinChanger.Change <PinChangeOperation>(_slot,
                                                                   ownerPinCode.Value, _runtimeTokenParams.NewUserPin.Value, CKU.CKU_USER);
                        }

                        Console.WriteLine(Resources.PinChangedSuccess);

                        if (!_commandLineOptions.StdinPins)
                        {
                            _logger.LogInformation(_logMessageBuilder.WithTokenId(
                                                       string.Format(Resources.PinChangePassed, Resources.UserPinOwner,
                                                                     changeBy,
                                                                     ownerPinCode.Value,
                                                                     _runtimeTokenParams.NewUserPin.Value)));
                        }
                    }
                    catch
                    {
                        Console.Error.WriteLine(Resources.ChangingPinError);

                        if (!_commandLineOptions.StdinPins)
                        {
                            _logger.LogError(_logMessageBuilder.WithTokenId(
                                                 string.Format(Resources.PinChangeFailed, Resources.UserPinOwner)) +
                                             $" {changeBy} : {ownerPinCode.Value} : {_runtimeTokenParams.NewUserPin.Value}");
                        }
                        throw;
                    }
                }
                else
                {
                    _logger.LogError(_logMessageBuilder.WithTokenId(
                                         string.Format(Resources.PinChangeFailed, Resources.UserPinOwner)));
                    Console.Error.WriteLine(_logMessageBuilder.WithPolicyDescription(_runtimeTokenParams.UserPinChangePolicy));

                    throw new TokenMustBeChangedException();
                }
            }

            void ChangeAdminPin()
            {
                if (_runtimeTokenParams.OldAdminPin.EnteredByUser)
                {
                    try
                    {
                        PinChanger.Change <PinChangeOperation>(_slot,
                                                               _runtimeTokenParams.OldAdminPin.Value, _runtimeTokenParams.NewAdminPin.Value, CKU.CKU_SO);

                        Console.WriteLine(Resources.PinChangedSuccess);
                        _logger.LogInformation(_logMessageBuilder.WithTokenId(
                                                   string.Format(Resources.PinChangePassed, Resources.AdminPinOwner,
                                                                 string.Empty,
                                                                 _runtimeTokenParams.OldAdminPin.Value,
                                                                 _runtimeTokenParams.NewAdminPin.Value)));
                    }
                    catch
                    {
                        Console.WriteLine(Resources.ChangingPinError);
                        _logger.LogError(_logMessageBuilder.WithTokenId(
                                             string.Format(Resources.PinChangeFailed, Resources.AdminPinOwner)) +
                                         $" : { _runtimeTokenParams.OldAdminPin.Value} : {_runtimeTokenParams.NewAdminPin.Value}");
                        throw;
                    }
                }
                else
                {
                    _logger.LogError(_logMessageBuilder.WithTokenId(
                                         string.Format(Resources.PinChangeFailed, Resources.AdminPinOwner)));
                    throw new ArgumentException(string.Format(Resources.AdminPinChangeError, Resources.AdminPin));
                }
            }

            _prerequisites.Enqueue(() =>
            {
                ValidateNewPins(() => _validator.ValidatePinsLengthBeforePinsChange());
            });

            _commands.Enqueue(() =>
            {
                if (_runtimeTokenParams.NewUserPin.EnteredByUser)
                {
                    switch (_runtimeTokenParams.UserPinChangePolicy)
                    {
                    case UserPinChangePolicy.ByUser:
                        {
                            ChangeUserPin(_runtimeTokenParams.OldUserPin);
                            break;
                        }

                    case UserPinChangePolicy.ByAdmin:
                        {
                            ChangeUserPin(_runtimeTokenParams.OldAdminPin);
                            break;
                        }

                    case UserPinChangePolicy.ByUserOrAdmin:
                        {
                            ChangeUserPin(_runtimeTokenParams.OldAdminPin.EnteredByUser
                                ? _runtimeTokenParams.OldAdminPin
                                : _runtimeTokenParams.OldUserPin);

                            break;
                        }
                    }
                }

                if (_runtimeTokenParams.NewAdminPin.EnteredByUser)
                {
                    ChangeAdminPin();
                }

                if (_pinsStorage.Initialized && !_pinsStorage.CanGetNext)
                {
                    Console.WriteLine(Resources.PinCodesFilePinsHaveEnded);
                    throw new AppMustBeClosedException(0);
                }
            });

            return(this);
        }