class CryptIdent::SignIn

Reworked sign-in logic for `CryptIdent`, per Issue #9.

This class *is not* part of the published API. @private

Constants

LogicError

Attributes

current_user[R]

rubocop:enable Naming/RescuedExceptionsVariableName

password[R]

rubocop:enable Naming/RescuedExceptionsVariableName

user[R]

rubocop:enable Naming/RescuedExceptionsVariableName

Public Instance Methods

call(user:, password:, current_user: nil) click to toggle source

As a reminder, calling `Failure` *does not* interrupt control flow or prevent a future `Success` call from overriding the result. This is one case where raising *and catching* an exception is Useful rubocop:disable Naming/RescuedExceptionsVariableName

# File lib/crypt_ident/sign_in.rb, line 140
def call(user:, password:, current_user: nil)
  set_ivars(user, password, current_user)
  validate_call_params
  Success(user: user)
rescue LogicError => err
  Failure(code: err.message.to_sym)
end

Private Instance Methods

illegal_current_user?() click to toggle source
# File lib/crypt_ident/sign_in.rb, line 156
def illegal_current_user?
  !current_user.guest? && !same_user?
end
password_comparator() click to toggle source
# File lib/crypt_ident/sign_in.rb, line 160
def password_comparator
  BCrypt::Password.new(user.password_hash)
end
same_user?() click to toggle source
# File lib/crypt_ident/sign_in.rb, line 164
def same_user?
  current_user.name == user.name
end
set_ivars(user, password, current) click to toggle source

Reek complains about a :reek:ControlParameter for `current`. Never mind.

# File lib/crypt_ident/sign_in.rb, line 169
def set_ivars(user, password, current)
  @user = user
  @password = password
  guest_user = CryptIdent.config.guest_user
  current ||= guest_user
  @current_user = guest_user.class.new(current)
end
validate_call_params() click to toggle source
# File lib/crypt_ident/sign_in.rb, line 177
def validate_call_params
  raise LogicError, 'user_is_guest' if user.guest?
  raise LogicError, 'illegal_current_user' if illegal_current_user?

  verify_matching_password
end
verify_matching_password() click to toggle source
# File lib/crypt_ident/sign_in.rb, line 184
def verify_matching_password
  match = password_comparator == password
  raise LogicError, 'invalid_password' unless match
end