class SimpleOAuth::Header

Constants

ATTRIBUTE_KEYS
IGNORED_KEYS

Attributes

method[R]
options[R]
params[R]

Public Class Methods

decode(value)
Alias for: unescape
default_options() click to toggle source
# File lib/simple_oauth/header.rb, line 15
def default_options
  {
    :nonce => OpenSSL::Random.random_bytes(16).unpack('H*')[0],
    :signature_method => 'HMAC-SHA1',
    :timestamp => Time.now.to_i.to_s,
    :version => '1.0',
  }
end
encode(value)
Alias for: escape
escape(value) click to toggle source
# File lib/simple_oauth/header.rb, line 31
def escape(value)
  uri_parser.escape(value.to_s, /[^a-z0-9\-\.\_\~]/i)
end
Also aliased as: encode
new(method, url, params, oauth = {}) click to toggle source
# File lib/simple_oauth/header.rb, line 48
def initialize(method, url, params, oauth = {})
  @method = method.to_s.upcase
  @uri = URI.parse(url.to_s)
  @uri.scheme = @uri.scheme.downcase
  @uri.normalize!
  @uri.fragment = nil
  @params = params
  @options = oauth.is_a?(Hash) ? self.class.default_options.merge(oauth) : self.class.parse(oauth)
end
parse(header) click to toggle source
# File lib/simple_oauth/header.rb, line 24
def parse(header)
  header.to_s.sub(/^OAuth\s/, '').split(/,\s*/).inject({}) do |attributes, pair|
    match = pair.match(/^(\w+)\=\"([^\"]*)\"$/)
    attributes.merge(match[1].sub(/^oauth_/, '').to_sym => unescape(match[2]))
  end
end
unescape(value) click to toggle source
# File lib/simple_oauth/header.rb, line 36
def unescape(value)
  uri_parser.unescape(value.to_s)
end
Also aliased as: decode

Private Class Methods

uri_parser() click to toggle source
# File lib/simple_oauth/header.rb, line 43
def uri_parser
  @uri_parser ||= URI.const_defined?(:Parser) ? URI::Parser.new : URI
end

Public Instance Methods

signed_attributes() click to toggle source
# File lib/simple_oauth/header.rb, line 76
def signed_attributes
  attributes.merge(:oauth_signature => signature)
end
to_s() click to toggle source
# File lib/simple_oauth/header.rb, line 64
def to_s
  "OAuth #{normalized_attributes}"
end
url() click to toggle source
# File lib/simple_oauth/header.rb, line 58
def url
  uri = @uri.dup
  uri.query = nil
  uri.to_s
end
valid?(secrets = {}) click to toggle source
# File lib/simple_oauth/header.rb, line 68
def valid?(secrets = {})
  original_options = options.dup
  options.merge!(secrets)
  valid = options[:signature] == signature
  options.replace(original_options)
  valid
end

Private Instance Methods

attributes() click to toggle source
# File lib/simple_oauth/header.rb, line 86
def attributes
  matching_keys, extra_keys = options.keys.partition { |key| ATTRIBUTE_KEYS.include?(key) }
  extra_keys -= IGNORED_KEYS
  if options[:ignore_extra_keys] || extra_keys.empty?
    Hash[options.select { |key, _| matching_keys.include?(key) }.collect { |key, value| [:"oauth_#{key}", value] }]
  else
    fail "SimpleOAuth: Found extra option keys not matching ATTRIBUTE_KEYS:\n  [#{extra_keys.collect(&:inspect).join(', ')}]"
  end
end
hmac_sha1_signature() click to toggle source
# File lib/simple_oauth/header.rb, line 100
def hmac_sha1_signature
  Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new, secret, signature_base)).chomp.gsub(/\n/, '')
end
normalized_attributes() click to toggle source
# File lib/simple_oauth/header.rb, line 82
def normalized_attributes
  signed_attributes.sort_by { |k, _| k.to_s }.collect { |k, v| %Q(#{k}="#{self.class.escape(v)}") }.join(', ')
end
normalized_params() click to toggle source
# File lib/simple_oauth/header.rb, line 113
def normalized_params
  signature_params.collect { |p| p.collect { |v| self.class.escape(v) } }.sort.collect { |p| p.join('=') }.join('&')
end
plaintext_signature()
Alias for: secret
private_key() click to toggle source
# File lib/simple_oauth/header.rb, line 129
def private_key
  OpenSSL::PKey::RSA.new(options[:consumer_secret])
end
rsa_sha1_signature() click to toggle source
# File lib/simple_oauth/header.rb, line 125
def rsa_sha1_signature
  Base64.encode64(private_key.sign(OpenSSL::Digest::SHA1.new, signature_base)).chomp.gsub(/\n/, '')
end
secret() click to toggle source
# File lib/simple_oauth/header.rb, line 104
def secret
  options.values_at(:consumer_secret, :token_secret).collect { |v| self.class.escape(v) }.join('&')
end
Also aliased as: plaintext_signature
signature() click to toggle source
# File lib/simple_oauth/header.rb, line 96
def signature
  send(options[:signature_method].downcase.tr('-', '_') + '_signature')
end
signature_base() click to toggle source
# File lib/simple_oauth/header.rb, line 109
def signature_base
  [method, url, normalized_params].collect { |v| self.class.escape(v) }.join('&')
end
signature_params() click to toggle source
# File lib/simple_oauth/header.rb, line 117
def signature_params
  attributes.to_a + params.to_a + url_params
end
url_params() click to toggle source
# File lib/simple_oauth/header.rb, line 121
def url_params
  CGI.parse(@uri.query || '').inject([]) { |p, (k, vs)| p + vs.sort.collect { |v| [k, v] } }
end