]> git.openstreetmap.org Git - rails.git/blob - config/initializers/oauth.rb
Merge remote-tracking branch 'upstream/pull/3017'
[rails.git] / config / initializers / oauth.rb
1 require "oauth/controllers/provider_controller"
2 require "oauth/rack/oauth_filter"
3
4 Rails.configuration.middleware.use OAuth::Rack::OAuthFilter
5
6 module OAuth
7   module RequestProxy
8     class RackRequest
9       def method
10         request.request_method
11       end
12     end
13   end
14 end
15
16 module OpenStreetMap
17   module ProviderController
18     def self.prepended(mod)
19       super
20       mod.singleton_class.prepend(OpenStreetMap::ProviderController::ClassMethods)
21     end
22
23     def render(options = {})
24       text = options.delete(:text)
25       if text
26         super options.merge(:plain => text)
27       elsif options.delete(:nothing)
28         status = options.delete(:status) || :ok
29         head status, options
30       else
31         super options
32       end
33     end
34
35     module ClassMethods
36       def included(controller)
37         controller.class_eval do
38           def self.before_filter(*names, &blk)
39             before_action(*names, &blk)
40           end
41
42           def self.skip_before_filter(*names, &blk)
43             skip_before_action(*names, &blk)
44           end
45         end
46
47         super controller
48       end
49     end
50   end
51
52   module OAuthFilter
53     def oauth1_verify(request, options = {}, &block)
54       signature = OAuth::Signature.build(request, options, &block)
55       return false unless OauthNonce.remember(signature.request.nonce, signature.request.timestamp)
56
57       value = signature.verify
58       if request.ssl? && !value
59         http_request = request.dup
60         http_request.define_singleton_method(:scheme) { "http" }
61         http_request.define_singleton_method(:port) { 80 }
62         signature = OAuth::Signature.build(http_request, options, &block)
63         value = signature.verify
64       end
65       value
66     rescue OAuth::Signature::UnknownSignatureMethod
67       false
68     end
69   end
70 end
71
72 OAuth::Controllers::ProviderController.prepend(OpenStreetMap::ProviderController)
73 OAuth::Rack::OAuthFilter.prepend(OpenStreetMap::OAuthFilter)