Sha256: 04eb50602425426421c632c84f8bc0e3fe12a235fbd4d3e8a506466e86ecfbe2

Contents?: true

Size: 1.55 KB

Versions: 2

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

require 'g5_authenticatable_api/services/token_validator'
require 'g5_authenticatable_api/services/user_fetcher'

module G5AuthenticatableApi
  module Helpers
    # Helper methods for securing a Grape API
    module Grape
      def authenticate_user!
        raise_auth_error unless token_validator.valid?
      end

      def token_data
        @token_data ||= token_info.token_data
      end

      def current_user
        @current_user ||= user_fetcher.current_user
      end

      def access_token
        @access_token ||= token_info.access_token
      end

      def warden
        env['warden']
      end

      def request
        Rack::Request.new(env)
      end

      protected

      def token_info
        @token_info ||= Services::TokenInfo.new(request.params, headers, warden)
      end

      def token_validator
        @token_validator ||= Services::TokenValidator.new(request.params,
                                                          headers,
                                                          warden)
      end

      def user_fetcher
        @user_fetcher ||= Services::UserFetcher.new(request.params,
                                                    headers,
                                                    warden)
      end

      def raise_auth_error
        auth_header = {
          'WWW-Authenticate' => token_validator.auth_response_header
        }
        throw :error, message: 'Unauthorized',
                      status: 401,
                      headers: auth_header
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
g5_authenticatable_api-1.0.0 lib/g5_authenticatable_api/helpers/grape.rb
g5_authenticatable_api-1.0.0.pre.1 lib/g5_authenticatable_api/helpers/grape.rb