Sha256: bdc43b7bbc853be924adb3b700e0a2467b0a1a3f81ae7f424294a9a32f18e167

Contents?: true

Size: 1.79 KB

Versions: 20

Compression:

Stored size: 1.79 KB

Contents

module Grape::Middleware::Auth
  # OAuth 2.0 authorization for Grape APIs.
  class OAuth2 < Grape::Middleware::Base
    def default_options
      {
        :token_class => 'AccessToken',
        :realm => 'OAuth API',
        :parameter => %w(bearer_token oauth_token),
        :accepted_headers => %w(HTTP_AUTHORIZATION X_HTTP_AUTHORIZATION X-HTTP_AUTHORIZATION REDIRECT_X_HTTP_AUTHORIZATION),
        :header => [/Bearer (.*)/i, /OAuth (.*)/i]
      }
    end
    
    def before
      verify_token(token_parameter || token_header)
    end

    def token_parameter
      Array(options[:parameter]).each do |p|
        return request[p] if request[p]
      end
      nil
    end

    def token_header
      return false unless authorization_header
      Array(options[:header]).each do |regexp|
        if authorization_header =~ regexp
          return $1
        end
      end
      nil
    end

    def authorization_header
      options[:accepted_headers].each do |head|
        return env[head] if env[head]
      end
      nil
    end
    
    def token_class
      @klass ||= eval(options[:token_class])
    end
    
    def verify_token(token)
      if token = token_class.verify(token)
        if token.respond_to?(:expired?) && token.expired?
          error_out(401, 'expired_token')
        else
          if !token.respond_to?(:permission_for?) || token.permission_for?(env)
            env['api.token'] = token
          else
            error_out(403, 'insufficient_scope')
          end
        end
      else
        error_out(401, 'invalid_token')
      end
    end
    
    def error_out(status, error)
      throw :error,
        :message => error,
        :status => status,
        :headers => {
          'WWW-Authenticate' => "OAuth realm='#{options[:realm]}', error='#{error}'"
        }
    end
  end
end
    

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
grape-0.6.0 lib/grape/middleware/auth/oauth2.rb
grape-0.5.0 lib/grape/middleware/auth/oauth2.rb
grape-0.4.1 lib/grape/middleware/auth/oauth2.rb
grape-0.4.0 lib/grape/middleware/auth/oauth2.rb
grape-0.3.2 lib/grape/middleware/auth/oauth2.rb
grape-0.3.1 lib/grape/middleware/auth/oauth2.rb
grape-0.3.0 lib/grape/middleware/auth/oauth2.rb
grape-0.2.1.1 lib/grape/middleware/auth/oauth2.rb
grape-0.2.6 lib/grape/middleware/auth/oauth2.rb
grape-0.2.5 lib/grape/middleware/auth/oauth2.rb
grape-0.2.4 lib/grape/middleware/auth/oauth2.rb
grape-0.2.3 lib/grape/middleware/auth/oauth2.rb
fragrant-0.0.5 vendor/bundle/ruby/1.9.1/gems/grape-0.2.2/lib/grape/middleware/auth/oauth2.rb
fragrant-0.0.4 vendor/grape/lib/grape/middleware/auth/oauth2.rb
grape-0.2.2 lib/grape/middleware/auth/oauth2.rb
fragrant-0.0.3 vendor/grape/lib/grape/middleware/auth/oauth2.rb
fragrant-0.0.2 vendor/grape/lib/grape/middleware/auth/oauth2.rb
fragrant-0.0.1 vendor/grape/lib/grape/middleware/auth/oauth2.rb
grape-0.2.1 lib/grape/middleware/auth/oauth2.rb
grape-0.2.0 lib/grape/middleware/auth/oauth2.rb