Class: OAuth::RequestProxy::Base

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/oauth/request_proxy/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

_escape, escape, generate_key, generate_timestamp, normalize, normalize_nested_query, parse_header, stringify_keys, unescape

Constructor Details

#initialize(request, options = {}) ⇒ Base

Returns a new instance of Base.



17
18
19
20
21
# File 'lib/oauth/request_proxy/base.rb', line 17

def initialize(request, options = {})
  @request = request
  @unsigned_parameters = (options[:unsigned_parameters] || []).map(&:to_s)
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



15
16
17
# File 'lib/oauth/request_proxy/base.rb', line 15

def options
  @options
end

#requestObject

Returns the value of attribute request.



15
16
17
# File 'lib/oauth/request_proxy/base.rb', line 15

def request
  @request
end

#unsigned_parametersObject

Returns the value of attribute unsigned_parameters.



15
16
17
# File 'lib/oauth/request_proxy/base.rb', line 15

def unsigned_parameters
  @unsigned_parameters
end

Class Method Details

.proxies(klass) ⇒ Object



11
12
13
# File 'lib/oauth/request_proxy/base.rb', line 11

def self.proxies(klass)
  OAuth::RequestProxy.available_proxies[klass] = self
end

Instance Method Details

#non_oauth_parametersObject



89
90
91
# File 'lib/oauth/request_proxy/base.rb', line 89

def non_oauth_parameters
  parameters.select { |k, _v| !OAuth::PARAMETERS.include?(k) }
end

#normalized_parametersObject

See 9.1.1. in specs Normalize Request Parameters



104
105
106
# File 'lib/oauth/request_proxy/base.rb', line 104

def normalized_parameters
  normalize(parameters_for_signature)
end

#normalized_uriObject

See 9.1.2 in specs



98
99
100
101
# File 'lib/oauth/request_proxy/base.rb', line 98

def normalized_uri
  u = URI.parse(uri)
  "#{u.scheme.downcase}://#{u.host.downcase}#{(u.scheme.casecmp("http").zero? && u.port != 80) || (u.scheme.casecmp("https").zero? && u.port != 443) ? ":#{u.port}" : ""}#{u.path && u.path != "" ? u.path : "/"}"
end

#oauth_callbackObject

OAuth parameters



25
26
27
# File 'lib/oauth/request_proxy/base.rb', line 25

def oauth_callback
  parameters["oauth_callback"]
end

#oauth_consumer_keyObject Also known as: consumer_key



29
30
31
# File 'lib/oauth/request_proxy/base.rb', line 29

def oauth_consumer_key
  parameters["oauth_consumer_key"]
end

#oauth_header(options = {}) ⇒ Object

Authorization header for OAuth



145
146
147
148
149
150
# File 'lib/oauth/request_proxy/base.rb', line 145

def oauth_header(options = {})
  header_params_str = oauth_parameters.map { |k, v| "#{k}=\"#{escape(v)}\"" }.join(", ")

  realm = "realm=\"#{options[:realm]}\", " if options[:realm]
  "OAuth #{realm}#{header_params_str}"
end

#oauth_nonceObject Also known as: nonce



33
34
35
# File 'lib/oauth/request_proxy/base.rb', line 33

def oauth_nonce
  parameters["oauth_nonce"]
end

#oauth_parametersObject



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

def oauth_parameters
  parameters.select { |k, v| OAuth::PARAMETERS.include?(k) && !v.nil? && v != "" }
end

#oauth_signatureObject Also known as: signature



37
38
39
40
# File 'lib/oauth/request_proxy/base.rb', line 37

def oauth_signature
  # TODO: can this be nil?
  [parameters["oauth_signature"]].flatten.first || ""
end

#oauth_signature_methodObject Also known as: signature_method



42
43
44
45
46
47
48
49
# File 'lib/oauth/request_proxy/base.rb', line 42

def oauth_signature_method
  case parameters["oauth_signature_method"]
  when Array
    parameters["oauth_signature_method"].first
  else
    parameters["oauth_signature_method"]
  end
end

#oauth_timestampObject Also known as: timestamp



51
52
53
# File 'lib/oauth/request_proxy/base.rb', line 51

def oauth_timestamp
  parameters["oauth_timestamp"]
end

#oauth_tokenObject Also known as: token



55
56
57
# File 'lib/oauth/request_proxy/base.rb', line 55

def oauth_token
  parameters["oauth_token"]
end

#oauth_verifierObject



59
60
61
# File 'lib/oauth/request_proxy/base.rb', line 59

def oauth_verifier
  parameters["oauth_verifier"]
end

#oauth_versionObject



63
64
65
# File 'lib/oauth/request_proxy/base.rb', line 63

def oauth_version
  parameters["oauth_version"]
end

#parametersObject

Parameter accessors

Raises:

  • (NotImplementedError)


77
78
79
# File 'lib/oauth/request_proxy/base.rb', line 77

def parameters
  raise NotImplementedError, "Must be implemented by subclasses"
end

#parameters_for_signatureObject



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

def parameters_for_signature
  parameters.select { |k, _v| !signature_and_unsigned_parameters.include?(k) }
end

#query_string_blank?Boolean

Returns:

  • (Boolean)


152
153
154
155
156
157
158
# File 'lib/oauth/request_proxy/base.rb', line 152

def query_string_blank?
  if (uri = request.env["REQUEST_URI"])
    uri.split("?", 2)[1].nil?
  else
    request.query_string.match(/\A\s*\z/)
  end
end

#sign(options = {}) ⇒ Object



108
109
110
# File 'lib/oauth/request_proxy/base.rb', line 108

def sign(options = {})
  OAuth::Signature.sign(self, options)
end

#sign!(options = {}) ⇒ Object



112
113
114
115
116
# File 'lib/oauth/request_proxy/base.rb', line 112

def sign!(options = {})
  parameters["oauth_signature"] = sign(options)
  @signed = true
  signature
end

#signature_and_unsigned_parametersObject



93
94
95
# File 'lib/oauth/request_proxy/base.rb', line 93

def signature_and_unsigned_parameters
  unsigned_parameters + ["oauth_signature"]
end

#signature_base_stringObject

See 9.1 in specs



119
120
121
122
# File 'lib/oauth/request_proxy/base.rb', line 119

def signature_base_string
  base = [method, normalized_uri, normalized_parameters]
  base.map { |v| escape(v) }.join("&")
end

#signed?Boolean

Has this request been signed yet?

Returns:

  • (Boolean)


125
126
127
# File 'lib/oauth/request_proxy/base.rb', line 125

def signed?
  @signed
end

#signed_uri(with_oauth: true) ⇒ Object

URI, including OAuth parameters



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/oauth/request_proxy/base.rb', line 130

def signed_uri(with_oauth: true)
  if signed?
    params = if with_oauth
               parameters
             else
               non_oauth_parameters
             end

    [uri, normalize(params)].join("?")
  else
    warn "This request has not yet been signed!"
  end
end