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