File: resource.rb

package info (click to toggle)
ruby-rack-oauth2 2.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 560 kB
  • sloc: ruby: 4,013; makefile: 4
file content (54 lines) | stat: -rw-r--r-- 1,259 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
module Rack
  module OAuth2
    module Server
      class Resource < Abstract::Handler
        ACCESS_TOKEN = 'rack.oauth2.access_token'
        DEFAULT_REALM = 'Protected by OAuth 2.0'
        attr_accessor :realm, :request

        def initialize(app, realm = nil, &authenticator)
          @app = app
          @realm = realm
          super(&authenticator)
        end

        def _call(env)
          if request.oauth2?
            access_token = authenticate! request.setup!
            env[ACCESS_TOKEN] = access_token
          end
          @app.call(env)
        rescue Rack::OAuth2::Server::Abstract::Error => e
          e.realm ||= realm
          e.finish
        end

        private

        def authenticate!(request)
          @authenticator.call(request)
        end

        class Request < Rack::Request
          attr_reader :access_token

          def initialize(env)
            @env = env
            @auth_header = Rack::Auth::AbstractRequest.new(env)
          end

          def setup!
            raise 'Define me!'
          end

          def oauth2?
            raise 'Define me!'
          end
        end
      end
    end
  end
end

require 'rack/oauth2/server/resource/error'
require 'rack/oauth2/server/resource/bearer'