Module: Riddl::Roles::OAuth

Defined in:
lib/ruby/riddl/roles/http%3A%2F%2Foauth.net%2F1.0/base.rb,
lib/ruby/riddl/roles/http%3A%2F%2Foauth.net%2F1.0%2Fon_behalf.rb,
lib/ruby/riddl/roles/http%3A%2F%2Foauth.net%2F1.0%2Faccess_token.rb,
lib/ruby/riddl/roles/http%3A%2F%2Foauth.net%2F1.0%2Frequest_token.rb

Defined Under Namespace

Modules: AccessToken, OnBehalf, RequestToken Classes: Response

Constant Summary collapse

DIGEST =
OpenSSL::Digest::SHA1.new
VERSION_MAJOR =
1
VERSION_MINOR =
0

Class Method Summary collapse

Class Method Details

.sign(fullpath, method, parameters, headers, options) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ruby/riddl/roles/http%3A%2F%2Foauth.net%2F1.0/base.rb', line 30

def self::sign(fullpath,method,parameters,headers,options)
  signature_string = method.upcase + "&" + Protocols::Utils::escape(fullpath) + "&"

  sparams = []
  if parameters.class == Array
    parameters.each do |p|
      if p.class == Riddl::Parameter::Simple
        sparams << [Protocols::Utils::escape(p.name),Protocols::Utils::escape(p.value)]
      end
    end
  end

  oparams = []
  oparams << ["oauth_consumer_key",Protocols::Utils::escape(options[:consumer_key])]
  oparams << ["oauth_signature_method","HMAC-SHA1"]
  oparams << ["oauth_timestamp",Time.new.to_i.to_s]
  oparams << ["oauth_version","1.0"]
  oparams << ["oauth_nonce",Protocols::Utils::escape(OpenSSL::Digest::SHA1.hexdigest(rand(10000).to_s)[0...32])]
  oparams << ["oauth_token",Protocols::Utils::escape(options[:token])] if options[:token]
  oparams << ["oauth_verifier",Protocols::Utils::escape(options[:verifier])] if options[:verifier]

  params = (sparams + oparams).sort{|a,b|a[0]<=>b[0]}.map{ |e| e[0] + '=' + e[1] }.join('&')
  signature_string += Protocols::Utils::escape(params)

  signature = OpenSSL::HMAC.digest(DIGEST,"#{Protocols::Utils::escape(options[:consumer_secret])}&#{Protocols::Utils::escape(options[:token_secret])}",signature_string)
  signature = [signature].pack("m").gsub(/\n/, '')

  oparams << ["oauth_signature", Protocols::Utils::escape(signature)]

  oparams.unshift(["realm", Protocols::Utils::escape(options[:realm])]) if options[:realm]

  headers['Authorization'] = 'OAuth ' + oparams.map{|e|e[0]+'='+"\"#{e[1]}\""}.join(', ')
end