Class: CloudKit::Request

Inherits:
Rack::Request
  • Object
show all
Includes:
Util
Defined in:
lib/cloudkit/request.rb

Overview

A subclass of Rack::Request providing CloudKit-specific features.

Instance Method Summary collapse

Methods included from Util

#erb, #r, #unquote

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.



128
129
130
131
# File 'lib/cloudkit/request.rb', line 128

def announce_auth(via)
  inject_via(via)
  @env[CLOUDKIT_AUTH_PRESENCE] = 1
end

#cloudkit_paramsObject



6
# File 'lib/cloudkit/request.rb', line 6

alias_method :cloudkit_params, :params

#current_userObject

Return the current user URI.



112
113
114
115
# File 'lib/cloudkit/request.rb', line 112

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.



118
119
120
# File 'lib/cloudkit/request.rb', line 118

def current_user=(user)
  @env[CLOUDKIT_AUTH_KEY] = user
end

#flashObject

Return the flash session for this request.



161
162
163
# File 'lib/cloudkit/request.rb', line 161

def flash
  session[CLOUDKIT_FLASH] ||= CloudKit::FlashSession.new
end

#if_matchObject

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.



96
97
98
99
100
101
102
103
# File 'lib/cloudkit/request.rb', line 96

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.



106
107
108
109
# File 'lib/cloudkit/request.rb', line 106

def inject_via(key)
  items = via << key
  @env[CLOUDKIT_VIA] = items.join(', ')
end

#jsonObject

Return the JSON content from the request body



18
19
20
21
# File 'lib/cloudkit/request.rb', line 18

def json
  self.body.rewind
  self.body.read
end

#last_path_elementObject

Return the last path element in the request URI.



73
74
75
# File 'lib/cloudkit/request.rb', line 73

def last_path_element
  path_element(-1)
end

#login_urlObject

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.



141
142
143
# File 'lib/cloudkit/request.rb', line 141

def 
  @env[CLOUDKIT_LOGIN_URL] || '/login'
end

#login_url=(url) ⇒ Object

Set the login url for this request.



146
147
148
# File 'lib/cloudkit/request.rb', line 146

def (url)
  @env[CLOUDKIT_LOGIN_URL] = url
end

#logout_urlObject

Return the logout URL for this request.



151
152
153
# File 'lib/cloudkit/request.rb', line 151

def logout_url
  @env[CLOUDKIT_LOGOUT_URL] || '/logout'
end

#logout_url=(url) ⇒ Object

Set the logout URL for this request.



156
157
158
# File 'lib/cloudkit/request.rb', line 156

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.

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/cloudkit/request.rb', line 24

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_paramsObject

Return OAuth header params in a hash.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cloudkit/request.rb', line 49

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.

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cloudkit/request.rb', line 32

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

#paramsObject

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



78
79
80
# File 'lib/cloudkit/request.rb', line 78

def path_element(index)
  path_info.split('/')[index] rescue nil
end

#sessionObject

Return the session associated with this request.



134
135
136
# File 'lib/cloudkit/request.rb', line 134

def session
  @env['rack.session']
end

#unescape(value) ⇒ Object

Unescape a value according to the OAuth spec.



68
69
70
# File 'lib/cloudkit/request.rb', line 68

def unescape(value)
  URI.unescape(value.gsub('+', '%2B'))
end

#using_auth?Boolean

Return true if authentication is being used.

Returns:

  • (Boolean)


123
124
125
# File 'lib/cloudkit/request.rb', line 123

def using_auth?
  @env[CLOUDKIT_AUTH_PRESENCE] != nil
end

#viaObject

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.



86
87
88
# File 'lib/cloudkit/request.rb', line 86

def via
  @env[CLOUDKIT_VIA].split(', ') rescue []
end