Sha256: 6a68e9ce9b6bc36a8c0cd597d379eda00d2429dd027679b8e067c4ef5463bad0

Contents?: true

Size: 1.49 KB

Versions: 12

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module Auther
  # Manages account authentication.
  class Authenticator
    # rubocop:disable Metrics/ParameterLists
    def initialize secret, account_model, account_presenter, logger: LOGGER
      @cipher = Auther::Cipher.new secret
      @account_model = account_model
      @account_presenter = account_presenter
      @logger = logger
    end
    # rubocop:enable Metrics/ParameterLists

    def authenticated?
      account_model.valid? &&
        account_presenter.valid? &&
        authentic_name? &&
        authentic_login? &&
        authentic_password?
    end

    private

    attr_reader :cipher, :account_model, :account_presenter, :logger

    def log_info message
      id = "[#{Auther::Keymaster.namespace}]"
      logger.info [id, message].join(": ")
    end

    def authentic? encrypted_value, value, error_name
      if cipher.decrypt(encrypted_value) == value
        true
      else
        account_presenter.errors.add error_name, "is invalid"
        false
      end
    rescue ActiveSupport::MessageEncryptor::InvalidMessage
      log_info %(Authentication failed! Invalid credential(s) for "#{account_model.name}" account.)
      false
    end

    def authentic_name? = account_presenter.name == account_model.name

    def authentic_login?
      authentic? account_model.encrypted_login, account_presenter.login, "login"
    end

    def authentic_password?
      authentic? account_model.encrypted_password, account_presenter.password, "password"
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
auther-17.0.0 lib/auther/authenticator.rb
auther-16.10.0 lib/auther/authenticator.rb
auther-16.9.0 lib/auther/authenticator.rb
auther-16.8.0 lib/auther/authenticator.rb
auther-16.7.0 lib/auther/authenticator.rb
auther-16.6.0 lib/auther/authenticator.rb
auther-16.5.0 lib/auther/authenticator.rb
auther-16.4.0 lib/auther/authenticator.rb
auther-16.3.0 lib/auther/authenticator.rb
auther-16.2.0 lib/auther/authenticator.rb
auther-16.1.0 lib/auther/authenticator.rb
auther-16.0.0 lib/auther/authenticator.rb