]> git.openstreetmap.org Git - rails.git/blob - config/initializers/oauth.rb
bad01dd3dc0fa097431b928e877480490283ee52
[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 Helper
8     def escape(value)
9       value.to_s.gsub(OAuth::RESERVED_CHARACTERS) do |c|
10         format("%%%02X", c.ord)
11       end
12     end
13   end
14
15   module RequestProxy
16     class RackRequest
17       def method
18         request.request_method
19       end
20     end
21   end
22 end
23
24 module OpenStreetMap
25   module ProviderController
26     def self.prepended(mod)
27       super
28       mod.singleton_class.prepend(OpenStreetMap::ProviderController::ClassMethods)
29     end
30
31     def render(options = {})
32       text = options.delete(:text)
33       if text
34         super options.merge(:plain => text)
35       elsif options.delete(:nothing)
36         status = options.delete(:status) || :ok
37         head status, options
38       else
39         super options
40       end
41     end
42
43     module ClassMethods
44       def included(controller)
45         controller.class_eval do
46           def self.before_filter(*names, &blk)
47             before_action(*names, &blk)
48           end
49
50           def self.skip_before_filter(*names, &blk)
51             skip_before_action(*names, &blk)
52           end
53         end
54
55         super controller
56       end
57     end
58   end
59
60   module OAuthFilter
61     def oauth1_verify(request, options = {}, &block)
62       signature = OAuth::Signature.build(request, options, &block)
63       return false unless OauthNonce.remember(signature.request.nonce, signature.request.timestamp)
64
65       value = signature.verify
66       if request.ssl? && !value
67         http_request = request.dup
68         http_request.define_singleton_method(:scheme) { "http" }
69         http_request.define_singleton_method(:port) { 80 }
70         signature = OAuth::Signature.build(http_request, options, &block)
71         value = signature.verify
72       end
73       value
74     rescue OAuth::Signature::UnknownSignatureMethod
75       false
76     end
77   end
78 end
79
80 OAuth::Controllers::ProviderController.prepend(OpenStreetMap::ProviderController)
81 OAuth::Rack::OAuthFilter.prepend(OpenStreetMap::OAuthFilter)