Sha256: 0cd1320406f6dd4406ca3411ca1e75ee10de468f82b105fe3d350525062801d8

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

module Oauth2
  module Auth
    module Server
      module Authentication

        def self.included(base)
          base.send(:include, Authentication::HelperMethods)
          base.send(:include, Authentication::ControllerMethods)
        end

        def self.extended(base)
          base.send(:extend, Authentication::ClassMethods)
        end

        module HelperMethods
          def current_token
            @current_token
          end

          def current_client
            @current_client
          end
        end

        module ControllerMethods
          def require_oauth_token(options = {})
            @current_token = request.env[Rack::OAuth2::Server::Resource::ACCESS_TOKEN]
            raise Rack::OAuth2::Server::Resource::Bearer::Unauthorized unless @current_token
            raise Rack::OAuth2::Server::Resource::Bearer::Forbidden.new(:insufficient_scope) unless @current_token.has_scope?(options[:scope])
          end

          def require_oauth_client_token(options = {})
            require_oauth_token(options)
            raise Rack::OAuth2::Server::Resource::Bearer::Unauthorized.new(:invalid_token, 'Client token is required') if @current_token.user
            @current_client = @current_token.client
          end
        end

        module ClassMethods
          def oauth_required(options = {})
            scope = options.delete(:scope)
            before_filter options do |controller|
              controller.require_oauth_token(:scope => scope)
            end
          end

          def oauth_client_required(options = {})
            scope = options.delete(:scope)
            before_filter options do |controller|
              controller.require_oauth_client_token(:scope => scope)
            end
          end
        end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
oauth2-auth-server-0.0.1 lib/oauth2-auth-server/authentication.rb