Sha256: 7fa571d7cd1bf8a05bb81ee7292810769ec213cb3a9de22938e89c28e68711ad

Contents?: true

Size: 1.21 KB

Versions: 6

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

# В модуле сгруппированы методы отвечающие за аутентификацию
#
module Authentication
  extend ActiveSupport::Concern

  included do
    before_action :authenticate_request
    attr_reader :access_token

    private

    def authenticate_request
      raise AuthenticationError unless access_token
    end

    def access_token
      @access_token ||= decode(http_auth_header)
    end

    def http_auth_header
      request.headers["Authorization"]&.split(" ")&.last
    end

    def decode(token)
      decoded_token = JWT.decode(token, secret_key, true, algorithm: "RS256")
      HashWithIndifferentAccess.new(decoded_token[0])
    end

    def secret_key
      secret_key_file_open { |f| OpenSSL::PKey::RSA.new(f) }
    end

    def secret_key_file_open(&block)
      File.open(ENV['JWT_SECRET_PATH'], &block)
    end
  end

  #  Используется для сигнализации ошибки во время аутентификации
  #
  class AuthenticationError < StandardError
    attr_reader :param

    def initialize(param)
      @param = param
      super("Ошибка аутентификации: #{param}")
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
care-0.1.6 lib/patch/action_controller/concerns/authentication.rb
care-0.1.5 lib/patch/action_controller/concerns/authentication.rb
care-0.1.4 lib/patch/action_controller/concerns/authentication.rb
care-0.1.3 lib/patch/action_controller/concerns/authentication.rb
care-0.1.1 lib/patch/action_controller/concerns/authentication.rb
care-0.1.0 lib/patch/action_controller/concerns/authentication.rb