Class: SimpleOAuth::Header

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_oauth/header.rb

Constant Summary collapse

ATTRIBUTE_KEYS =
[:callback, :consumer_key, :nonce, :signature_method, :timestamp, :token, :verifier, :version]
IGNORED_KEYS =
[:consumer_secret, :token_secret, :signature]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, url, params, oauth = {}) ⇒ Header

Returns a new instance of Header


48
49
50
51
52
53
54
55
56
# 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

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method


12
13
14
# File 'lib/simple_oauth/header.rb', line 12

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options


12
13
14
# File 'lib/simple_oauth/header.rb', line 12

def options
  @options
end

#paramsObject (readonly)

Returns the value of attribute params


12
13
14
# File 'lib/simple_oauth/header.rb', line 12

def params
  @params
end

Class Method Details

.default_optionsObject


15
16
17
18
19
20
21
22
# 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

.escape(value) ⇒ Object Also known as: encode


31
32
33
# File 'lib/simple_oauth/header.rb', line 31

def escape(value)
  uri_parser.escape(value.to_s, /[^a-z0-9\-\.\_\~]/i)
end

.parse(header) ⇒ Object


24
25
26
27
28
29
# 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) ⇒ Object Also known as: decode


36
37
38
# File 'lib/simple_oauth/header.rb', line 36

def unescape(value)
  uri_parser.unescape(value.to_s)
end

Instance Method Details

#signed_attributesObject


76
77
78
# File 'lib/simple_oauth/header.rb', line 76

def signed_attributes
  attributes.merge(:oauth_signature => signature)
end

#to_sObject


64
65
66
# File 'lib/simple_oauth/header.rb', line 64

def to_s
  "OAuth #{normalized_attributes}"
end

#urlObject


58
59
60
61
62
# File 'lib/simple_oauth/header.rb', line 58

def url
  uri = @uri.dup
  uri.query = nil
  uri.to_s
end

#valid?(secrets = {}) ⇒ Boolean

Returns:

  • (Boolean)

68
69
70
71
72
73
74
# 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