Sha256: 0b0bc11c0b9d22820faaf1600c3b3fa815bdd71001e5ecf265304cfc7fb28db8

Contents?: true

Size: 1.53 KB

Versions: 4

Compression:

Stored size: 1.53 KB

Contents

module Unimatrix::Authorization

  class RequiresResourceOwner

    def before( controller )
      access_token = controller.params[ 'access_token' ]

      if access_token.present?
        resource_owner = controller.retrieve_resource_owner( access_token )

        if resource_owner.present? && resource_owner.is_a?( Array ) &&
           resource_owner.first.type_name == 'resource_owner'
          controller.resource_owner = resource_owner
        else
          controller.render_error(
            ::ForbiddenError,
            "The requested resource_owner could not be retrieved."
          )
        end
      else
        controller.render_error(
          ::MissingParameterError,
          "The parameter 'access_token' is required."
        )
      end
    end
  end

  module ClassMethods
    def requires_resource_owner( options = {} )
      before_action(
        RequiresResourceOwner.new,
        options
      )
    end
  end

  def self.included( controller )
    controller.extend( ClassMethods )
  end

  def resource_owner=( attributes )
    @resource_owner = attributes
  end

  def resource_owner
    @resource_owner ||= begin
      retrieve_resource_owner( params[ :access_token ] )
    end
  end

  # In Rails app, this is overwritten by #retrieve_resource_owner in railtie.rb
  def retrieve_resource_owner( access_token )
    if access_token
      request_resource_owner( access_token )
    end
  end

  def request_resource_owner( access_token )
    Operation.new( '/resource_owner' ).where( access_token: access_token ).read
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
unimatrix-2.9.0 lib/unimatrix/authorization/filters/requires_resource_owner.rb
unimatrix-2.8.0 lib/unimatrix/authorization/filters/requires_resource_owner.rb
unimatrix-2.7.1 lib/unimatrix/authorization/filters/requires_resource_owner.rb
unimatrix-2.7.0 lib/unimatrix/authorization/filters/requires_resource_owner.rb