Sha256: 7225734025a20a37721b0e11db84063b464d1eeaf3af22556299b3551c0ee15e

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

module ApiWarden
  module Helpers
    module Accessable
      # @return [Boolean] whether or not authenticated
      def ward_by(scope)
        current_authentication_for(scope).authenticated?
      end

      # If not authenticated, an unauthorized response is rendered.
      #
      # @return [Boolean] whether or not authenticated
      def ward_by!(scope)
        scope = validate_scope(scope)
        
        authentication = current_authentication_for(scope)
        unless authentication.authenticated?
          if (block = scope.on_authenticate_failed) && block.respond_to?(:call)
            instance_exec(authentication, &block)
          else
            render json: { err_msg: 'Unauthorized' }, status: 401
          end
          false
        else
          if (block = scope.on_authenticate_success) && block.respond_to?(:call)
            instance_exec(authentication, &block)
          end
          true
        end
      end

      def current_authentication_for(scope)
        scope = validate_scope(scope)

        ivar_authentication = "@current_#{scope.name}_authentication"
        unless authentication = instance_variable_get(ivar_authentication)
          authentication = Authentication.new(scope, request)
          instance_variable_set(ivar_authentication, authentication)
        else
          authentication
        end
      end

      def generate_access_token_for(scope, id, *args)
        scope = validate_scope(scope)

        access_token = ApiWarden.friendly_token(20)

        ApiWarden.redis do |conn|
          conn.set(scope.key_for_access_token(id, access_token), 
            scope.value_for_access_token(access_token, *args), 
            ex: scope.expire_time_for_access_token
          )
        end

        access_token
      end

      private
        def validate_scope(scope)
          scope.is_a?(String) ? ApiWarden.find_scope(scope) : scope
        end          
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
api_warden-0.3.0 lib/api_warden/helpers/accessable.rb
api_warden-0.2.0 lib/api_warden/helpers/accessable.rb