Sha256: 66ebc41f775ae98e68b8c1087bf1379867de67ddb58623fee685cf642298159b

Contents?: true

Size: 1.53 KB

Versions: 10

Compression:

Stored size: 1.53 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: Auther::NullLogger.new(STDOUT)
      @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::MessageVerifier::InvalidSignature
      log_info %(Authentication failed! Invalid credential(s) for "#{account_model.name}" account.)
      false
    end

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

    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

10 entries across 10 versions & 1 rubygems

Version Path
auther-10.2.3 lib/auther/authenticator.rb
auther-10.2.2 lib/auther/authenticator.rb
auther-10.2.1 lib/auther/authenticator.rb
auther-10.2.0 lib/auther/authenticator.rb
auther-10.1.0 lib/auther/authenticator.rb
auther-10.0.0 lib/auther/authenticator.rb
auther-9.3.0 lib/auther/authenticator.rb
auther-9.2.0 lib/auther/authenticator.rb
auther-9.1.0 lib/auther/authenticator.rb
auther-9.0.0 lib/auther/authenticator.rb