Class: OoAuth::RequestProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/oo_auth/request_proxy.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RequestProxy

Returns a new instance of RequestProxy.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/oo_auth/request_proxy.rb', line 31

def initialize(*args)
  case args.size
  when 1 # ActionDispatch request
    request = args[0]
    @port = request.port
    @ssl = request.ssl?
    @path = request.fullpath
    @host = request.host
    @headers = request.headers
  when 2 # Net:HTTP request
    http, request = args[0], args[1]
    @port = http.port
    @ssl = http.use_ssl?
    @path = request.path
    @host = http.address
    @headers = request
  else
    raise ArgumentError, 'wrong number of arguments'
  end
  @method = request.method
  @body = request.body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



4
5
6
# File 'lib/oo_auth/request_proxy.rb', line 4

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



4
5
6
# File 'lib/oo_auth/request_proxy.rb', line 4

def headers
  @headers
end

#hostObject (readonly)

Returns the value of attribute host.



4
5
6
# File 'lib/oo_auth/request_proxy.rb', line 4

def host
  @host
end

#methodObject (readonly)

Returns the value of attribute method.



4
5
6
# File 'lib/oo_auth/request_proxy.rb', line 4

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/oo_auth/request_proxy.rb', line 4

def path
  @path
end

#portObject (readonly)

Returns the value of attribute port.



4
5
6
# File 'lib/oo_auth/request_proxy.rb', line 4

def port
  @port
end

#sslObject (readonly)

Returns the value of attribute ssl.



4
5
6
# File 'lib/oo_auth/request_proxy.rb', line 4

def ssl
  @ssl
end

Class Method Details

.parse(header) ⇒ Object

Parse an Authorization / WWW-Authenticate header into a hash. Takes care of unescaping and removing surrounding quotes. Raises a OAuth::Problem if the header is not parsable into a valid hash. Does not validate the keys or values.

hash = parse(headers['Authorization'] || headers['WWW-Authenticate'])
hash['oauth_timestamp']
  #=>"1234567890"


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/oo_auth/request_proxy.rb', line 17

def parse(header)
  header = header.to_s
  return unless header.start_with?('OAuth ')
  # decompose
  params = header[6, header.length].split(',').inject({}) do |hsh, str|
    key, value = str.split('=').map { |s| OoAuth.unescape(s.strip) }
    if PARAMETERS.include?(key)
      hsh[key] = value.sub(/^\"(.*)\"$/, '\1')
    end
    hsh
  end
end

Instance Method Details

#authorizationObject



81
82
83
# File 'lib/oo_auth/request_proxy.rb', line 81

def authorization
  headers['Authorization']
end

#authorization=(header) ⇒ Object



85
86
87
# File 'lib/oo_auth/request_proxy.rb', line 85

def authorization=(header)
  headers['Authorization'] = header
end

#normalized_request_uriObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/oo_auth/request_proxy.rb', line 54

def normalized_request_uri
  if self.port == Net::HTTP.default_port
    scheme, port = :http, nil
  elsif self.port == Net::HTTP.https_default_port
    scheme, port = :https, nil
  elsif ssl
    scheme, port = :https, self.port
  else
    scheme, port = :http, self.port
  end

  uri = "#{scheme}://#{host.downcase}"
  uri += ":#{port}" if port
  uri += path.split('?').first
  uri
end

#oauth_paramsObject



71
72
73
# File 'lib/oo_auth/request_proxy.rb', line 71

def oauth_params
  self.class.parse(authorization)
end

#oauth_params_without_signatureObject



75
76
77
78
79
# File 'lib/oo_auth/request_proxy.rb', line 75

def oauth_params_without_signature
  params = oauth_params
  params.delete('oauth_signature')
  params
end

#params_array(object) ⇒ Object

FIXME: cf nested params implementation in oauth gem TODO: support oauth body signature for non-formencoded content types



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/oo_auth/request_proxy.rb', line 107

def params_array(object)
  case object
  when Array then object
  when Hash then object.to_a
  when RequestProxy
    tmp = object.path.split('?')
    params = tmp[1] ? params_decode(tmp[1]) : []
    if object.post? && object.headers['Content-Type'].to_s.start_with?('application/x-www-form-urlencoded')
      params.concat params_decode(object.body)
    end
    params
  else
    raise "error: cannot convert #{object.class} object to params array"
  end
end

#params_decode(string) ⇒ Object



123
124
125
126
127
128
# File 'lib/oo_auth/request_proxy.rb', line 123

def params_decode(string)
  string.split('&').each_with_object([]) do |param, array|
    k, v = *param.split('=')
    array << [OoAuth.unescape(k), v && OoAuth.unescape(v)]
  end
end

#params_encode(params) ⇒ Object



131
132
133
# File 'lib/oo_auth/request_proxy.rb', line 131

def params_encode(params)
  params.map { |k, v| [OoAuth.escape(k), OoAuth.escape(v)] }.sort.map { |k, v| "#{k}=#{v}" }.join('&')
end

#post?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/oo_auth/request_proxy.rb', line 96

def post?
  'POST' == method
end

#signature_base_string(params = {}) ⇒ Object



100
101
102
103
# File 'lib/oo_auth/request_proxy.rb', line 100

def signature_base_string(params = {})
  encoded_params = params_encode(params_array(self) + params_array(params))
  OoAuth.encode(method, normalized_request_uri, encoded_params)
end