]> git.openstreetmap.org Git - rails.git/blob - config/initializers/oauth.rb
Move more api-only methods into api_controller
[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       mod.singleton_class.prepend(OpenStreetMap::ProviderController::ClassMethods)
20     end
21
22     def render(options = {})
23       text = options.delete(:text)
24       if text
25         super options.merge(:plain => text)
26       elsif options.delete(:nothing)
27         status = options.delete(:status) || :ok
28         head status, options
29       else
30         super options
31       end
32     end
33
34     module ClassMethods
35       def included(controller)
36         controller.class_eval do
37           def self.before_filter(*names, &blk)
38             before_action(*names, &blk)
39           end
40
41           def self.skip_before_filter(*names, &blk)
42             skip_before_action(*names, &blk)
43           end
44         end
45
46         super controller
47       end
48     end
49   end
50
51   module OAuthFilter
52     def oauth1_verify(request, options = {}, &block)
53       signature = OAuth::Signature.build(request, options, &block)
54       return false unless OauthNonce.remember(signature.request.nonce, signature.request.timestamp)
55
56       value = signature.verify
57       if request.ssl? && !value
58         http_request = request.dup
59         http_request.define_singleton_method(:scheme) { "http" }
60         http_request.define_singleton_method(:port) { 80 }
61         signature = OAuth::Signature.build(http_request, options, &block)
62         value = signature.verify
63       end
64       value
65     rescue OAuth::Signature::UnknownSignatureMethod
66       false
67     end
68   end
69 end
70
71 OAuth::Controllers::ProviderController.prepend(OpenStreetMap::ProviderController)
72 OAuth::Rack::OAuthFilter.prepend(OpenStreetMap::OAuthFilter)