Class: OAuth::Signature::Base

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

Direct Known Subclasses

HMAC::SHA1, HMAC::SHA256, PLAINTEXT, RSA::SHA1

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 = {}, &block) ⇒ Base

Returns a new instance of Base.

Raises:

  • (TypeError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/oauth/signature/base.rb', line 23

def initialize(request, options = {}, &block)
  raise TypeError unless request.is_a?(OAuth::RequestProxy::Base)

  @request = request
  @options = options

  ## consumer secret was determined beforehand

  @consumer_secret = options[:consumer].secret if options[:consumer]

  # presence of :consumer_secret option will override any Consumer that's provided
  @consumer_secret = options[:consumer_secret] if options[:consumer_secret]

  ## token secret was determined beforehand

  @token_secret = options[:token].secret if options[:token]

  # presence of :token_secret option will override any Token that's provided
  @token_secret = options[:token_secret] if options[:token_secret]

  # override secrets based on the values returned from the block (if any)
  if block
    # consumer secret and token secret need to be looked up based on pieces of the request
    secrets = yield block.arity == 1 ? request : [token, consumer_key, nonce, request.timestamp]
    if secrets.is_a?(Array) && secrets.size == 2
      @token_secret = secrets[0]
      @consumer_secret = secrets[1]
    end
  end
end

Instance Attribute Details

#consumer_secretObject (readonly)

Returns the value of attribute consumer_secret.



14
15
16
# File 'lib/oauth/signature/base.rb', line 14

def consumer_secret
  @consumer_secret
end

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/oauth/signature/base.rb', line 13

def options
  @options
end

#requestObject (readonly)

Returns the value of attribute request.



14
15
16
# File 'lib/oauth/signature/base.rb', line 14

def request
  @request
end

#token_secretObject (readonly)

Returns the value of attribute token_secret.



14
15
16
# File 'lib/oauth/signature/base.rb', line 14

def token_secret
  @token_secret
end

Class Method Details

.implements(signature_method = nil) ⇒ Object



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

def self.implements(signature_method = nil)
  return @implements if signature_method.nil?

  @implements = signature_method
  OAuth::Signature.available_methods[@implements] = self
end

Instance Method Details

#==(other) ⇒ Object



58
59
60
61
62
# File 'lib/oauth/signature/base.rb', line 58

def ==(other)
  check = signature.bytesize ^ other.bytesize
  signature.bytes.zip(other.bytes) { |x, y| check |= x ^ y.to_i }
  check.zero?
end

#body_hashObject



72
73
74
# File 'lib/oauth/signature/base.rb', line 72

def body_hash
  raise_instantiation_error
end

#signatureObject



54
55
56
# File 'lib/oauth/signature/base.rb', line 54

def signature
  Base64.encode64(digest).chomp.delete("\n")
end

#signature_base_stringObject



68
69
70
# File 'lib/oauth/signature/base.rb', line 68

def signature_base_string
  request.signature_base_string
end

#verifyObject



64
65
66
# File 'lib/oauth/signature/base.rb', line 64

def verify
  self == request.signature
end