Class: CloudKit::Request
- Includes:
- Util
- Defined in:
- lib/cloudkit/request.rb
Overview
A subclass of Rack::Request providing CloudKit-specific features.
Instance Method Summary collapse
-
#announce_auth(via) ⇒ Object
Report to downstream middleware that authentication is in use.
- #cloudkit_params ⇒ Object
-
#current_user ⇒ Object
Return the current user URI.
-
#current_user=(user) ⇒ Object
Set the current user URI.
-
#domain_root ⇒ Object
Return the host and scheme.
-
#flash ⇒ Object
Return the flash session for this request.
-
#if_match ⇒ Object
Return parsed contents of an If-Match header.
-
#initialize(env) ⇒ Request
constructor
A new instance of Request.
-
#inject_via(key) ⇒ Object
Add a via entry to the Rack environment.
-
#json ⇒ Object
Return the JSON content from the request body.
-
#last_path_element ⇒ Object
Return the last path element in the request URI.
-
#login_url ⇒ Object
Return the login URL for this request.
-
#login_url=(url) ⇒ Object
Set the login url for this request.
-
#logout_url ⇒ Object
Return the logout URL for this request.
-
#logout_url=(url) ⇒ Object
Set the logout URL for this request.
-
#match?(method, path, required_params = []) ⇒ Boolean
Return true if method, path, and required_params match.
-
#oauth_header_params ⇒ Object
Return OAuth header params in a hash.
-
#param_match?(required_params) ⇒ Boolean
Return true of the array of required params match the request params.
-
#params ⇒ Object
Return a merged set of both standard params and OAuth header params.
-
#path_element(index) ⇒ Object
Return a specific path element.
-
#session ⇒ Object
Return the session associated with this request.
-
#unescape(value) ⇒ Object
Unescape a value according to the OAuth spec.
-
#uri ⇒ Object
Return a CloudKit::URI instance representing the rack request’s path info.
-
#using_auth? ⇒ Boolean
Return true if authentication is being used.
-
#via ⇒ Object
Return an array containing one entry for each piece of upstream middleware.
Methods included from Util
Constructor Details
#initialize(env) ⇒ Request
Returns a new instance of Request.
8 9 10 |
# File 'lib/cloudkit/request.rb', line 8 def initialize(env) super(env) end |
Instance Method Details
#announce_auth(via) ⇒ Object
Report to downstream middleware that authentication is in use.
135 136 137 138 |
# File 'lib/cloudkit/request.rb', line 135 def announce_auth(via) inject_via(via) @env[CLOUDKIT_AUTH_PRESENCE] = 1 end |
#cloudkit_params ⇒ Object
6 |
# File 'lib/cloudkit/request.rb', line 6 alias_method :cloudkit_params, :params |
#current_user ⇒ Object
Return the current user URI.
119 120 121 122 |
# File 'lib/cloudkit/request.rb', line 119 def current_user return nil unless @env[CLOUDKIT_AUTH_KEY] && @env[CLOUDKIT_AUTH_KEY] != '' @env[CLOUDKIT_AUTH_KEY] end |
#current_user=(user) ⇒ Object
Set the current user URI.
125 126 127 |
# File 'lib/cloudkit/request.rb', line 125 def current_user=(user) @env[CLOUDKIT_AUTH_KEY] = user end |
#domain_root ⇒ Object
Return the host and scheme
173 174 175 |
# File 'lib/cloudkit/request.rb', line 173 def domain_root "#{scheme}://#{@env['HTTP_HOST']}" end |
#flash ⇒ Object
Return the flash session for this request.
168 169 170 |
# File 'lib/cloudkit/request.rb', line 168 def flash session[CLOUDKIT_FLASH] ||= CloudKit::FlashSession.new end |
#if_match ⇒ Object
Return parsed contents of an If-Match header.
Note: Only a single ETag is useful in the context of CloudKit, so a list is treated as one ETag; the result of using the wrong ETag or a list of ETags is the same in the context of PUT and DELETE where If-Match headers are required.
103 104 105 106 107 108 109 110 |
# File 'lib/cloudkit/request.rb', line 103 def if_match etag = @env['HTTP_IF_MATCH'] return nil unless etag etag.strip! etag = unquote(etag) return nil if etag == '*' etag end |
#inject_via(key) ⇒ Object
Add a via entry to the Rack environment.
113 114 115 116 |
# File 'lib/cloudkit/request.rb', line 113 def inject_via(key) items = via << key @env[CLOUDKIT_VIA] = items.join(', ') end |
#json ⇒ Object
Return the JSON content from the request body
18 19 20 21 22 23 |
# File 'lib/cloudkit/request.rb', line 18 def json self.body.rewind raw = self.body.read # extract the json from the body to avoid tunneled _method param from being parsed as json (matches = raw.match(/(\{.*\})/)) ? matches[1] : raw end |
#last_path_element ⇒ Object
Return the last path element in the request URI.
80 81 82 |
# File 'lib/cloudkit/request.rb', line 80 def last_path_element path_element(-1) end |
#login_url ⇒ Object
Return the login URL for this request. This is stashed in the Rack environment so the OpenID and OAuth middleware can cooperate during the token authorization step in the OAuth flow.
148 149 150 |
# File 'lib/cloudkit/request.rb', line 148 def login_url @env[CLOUDKIT_LOGIN_URL] || '/login' end |
#login_url=(url) ⇒ Object
Set the login url for this request.
153 154 155 |
# File 'lib/cloudkit/request.rb', line 153 def login_url=(url) @env[CLOUDKIT_LOGIN_URL] = url end |
#logout_url ⇒ Object
Return the logout URL for this request.
158 159 160 |
# File 'lib/cloudkit/request.rb', line 158 def logout_url @env[CLOUDKIT_LOGOUT_URL] || '/logout' end |
#logout_url=(url) ⇒ Object
Set the logout URL for this request.
163 164 165 |
# File 'lib/cloudkit/request.rb', line 163 def logout_url=(url) @env[CLOUDKIT_LOGOUT_URL] = url end |
#match?(method, path, required_params = []) ⇒ Boolean
Return true if method, path, and required_params match.
31 32 33 34 35 |
# File 'lib/cloudkit/request.rb', line 31 def match?(method, path, required_params=[]) (request_method == method) && path_info.match(path.gsub(':id', '*')) && # just enough to work for now param_match?(required_params) end |
#oauth_header_params ⇒ Object
Return OAuth header params in a hash.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cloudkit/request.rb', line 56 def oauth_header_params # This is a copy of the same method from the OAuth gem. # TODO: Refactor the OAuth gem so that this method is available via a # mixin, outside of the request proxy context. %w( X-HTTP_AUTHORIZATION Authorization HTTP_AUTHORIZATION ).each do |header| next unless @env.include?(header) header = @env[header] next unless header[0,6] == 'OAuth ' oauth_param_string = header[6,header.length].split(/[,=]/) oauth_param_string.map!{|v| unescape(v.strip)} oauth_param_string.map!{|v| v =~ /^\".*\"$/ ? v[1..-2] : v} oauth_params = Hash[*oauth_param_string.flatten] oauth_params.reject!{|k,v| k !~ /^oauth_/} return oauth_params end return {} end |
#param_match?(required_params) ⇒ Boolean
Return true of the array of required params match the request params. If a hash in passed in for a param, its value is also used in the match.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/cloudkit/request.rb', line 39 def param_match?(required_params) required_params.all? do |required_param| case required_param when Hash key = required_param.keys.first return false unless params.has_key? key return false unless params[key] == required_param[key] when String return false unless params.has_key? required_param else false end true end end |
#params ⇒ Object
Return a merged set of both standard params and OAuth header params.
13 14 15 |
# File 'lib/cloudkit/request.rb', line 13 def params @cloudkit_params ||= cloudkit_params.merge(oauth_header_params) end |
#path_element(index) ⇒ Object
Return a specific path element
85 86 87 |
# File 'lib/cloudkit/request.rb', line 85 def path_element(index) path_info.split('/')[index] rescue nil end |
#session ⇒ Object
Return the session associated with this request.
141 142 143 |
# File 'lib/cloudkit/request.rb', line 141 def session @env['rack.session'] end |
#unescape(value) ⇒ Object
Unescape a value according to the OAuth spec.
75 76 77 |
# File 'lib/cloudkit/request.rb', line 75 def unescape(value) ::URI.unescape(value.gsub('+', '%2B')) end |
#uri ⇒ Object
Return a CloudKit::URI instance representing the rack request’s path info.
26 27 28 |
# File 'lib/cloudkit/request.rb', line 26 def uri @uri ||= CloudKit::URI.new(self.path_info) end |
#using_auth? ⇒ Boolean
Return true if authentication is being used.
130 131 132 |
# File 'lib/cloudkit/request.rb', line 130 def using_auth? @env[CLOUDKIT_AUTH_PRESENCE] != nil end |
#via ⇒ Object
Return an array containing one entry for each piece of upstream middleware. This is in the same spirit as Via headers in HTTP, but does not use the header because the transition from one piece of middleware to the next does not use HTTP.
93 94 95 |
# File 'lib/cloudkit/request.rb', line 93 def via @env[CLOUDKIT_VIA].split(', ') rescue [] end |