Class: Libgss::HttpClientWithSignatureKey

Inherits:
Object
  • Object
show all
Defined in:
lib/libgss/http_client_with_signature_key.rb

Defined Under Namespace

Classes: Signature

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(impl, network) ⇒ HttpClientWithSignatureKey

Returns a new instance of HttpClientWithSignatureKey.



17
18
19
# File 'lib/libgss/http_client_with_signature_key.rb', line 17

def initialize(impl, network)
  @impl, @network = impl, network
end

Instance Attribute Details

#implObject (readonly)

Returns the value of attribute impl.



16
17
18
# File 'lib/libgss/http_client_with_signature_key.rb', line 16

def impl
  @impl
end

#networkObject (readonly)

Returns the value of attribute network.



16
17
18
# File 'lib/libgss/http_client_with_signature_key.rb', line 16

def network
  @network
end

Instance Method Details

#get(uri, query = {}, original_headers = {}, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/libgss/http_client_with_signature_key.rb', line 59

def get(uri, query={}, original_headers = {}, &block)
  headers = {
    "oauth_consumer_key" => network.consumer_key || "",
    "oauth_token"        => network.auth_token,
  }
  unless @network.ignore_oauth_nonce
    headers.update({
        "oauth_nonce"     => oauth_nonce,
        "oauth_timestamp" => oauth_timestamp,
      })
  end
  oauth_params = {
    "body" => '',
    "oauth_signature_method" => "HMAC-SHA1"
  }.update(headers)

  encoded_query = ''
  query.each do |key, val|
    key = key.to_s
    if val.is_a?(::Array)
      k = CGI.escape("#{key}[]")
      val.each do |v|
        encoded_query << "&#{k}=#{URI.escape(v.to_s)}"
      end
    elsif val.is_a?(::Hash)
      val.each do |k, v|
        k = URI.escape("#{key}[#{k}]")
        encoded_query << "&#{k}=#{URI.escape(v.to_s)}"
      end
    else
      encoded_query << "&#{URI.escape(key)}=#{URI.escape(val.to_s)}"
    end
  end
  if uri =~ /\?/
    uri += encoded_query
  else
    uri += encoded_query.gsub(/^&/, '?')
  end
  req_hash = {
    "method" => "GET",
    "uri"    => uri,
    "parameters" => oauth_params
  }

  options = {
    :consumer_secret => network.consumer_secret,
    :token_secret    => network.signature_key
  }

  signature_class = Libgss.use_oauth_gem ? ::OAuth::Signature : Signature
  if ENV["VERBOSE"]
    $stdout.puts("signature_class: #{signature_class.name}")
    $stdout.puts("       req_hash: #{req_hash.inspect}")
  end

  headers["oauth_signature"] = signature_class.sign(req_hash, options)

  res = @impl.get(uri, nil, headers.update(original_headers))
end

#oauth_nonceObject



119
120
121
# File 'lib/libgss/http_client_with_signature_key.rb', line 119

def oauth_nonce
  @network.oauth_nonce || SecureRandom.uuid.to_s
end

#oauth_timestampObject



123
124
125
# File 'lib/libgss/http_client_with_signature_key.rb', line 123

def oauth_timestamp
  @network.oauth_timestamp || Time.now.utc.to_i
end

#post(uri, body, original_headers = {}, &block) ⇒ Object



21
22
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
53
54
55
56
57
# File 'lib/libgss/http_client_with_signature_key.rb', line 21

def post(uri, body, original_headers = {}, &block)
  headers = {
    "oauth_consumer_key" => network.consumer_key || "",
    "oauth_token"        => network.auth_token,
  }
  unless @network.ignore_oauth_nonce
    headers.update({
        "oauth_nonce"     => oauth_nonce,
        "oauth_timestamp" => oauth_timestamp,
      })
  end
  oauth_params = {
    "body" => body,
    "oauth_signature_method" => "HMAC-SHA1"
  }.update(headers)

  req_hash = {
    "method" => "POST",
    "uri"    => uri,
    "parameters" => oauth_params
  }

  options = {
    :consumer_secret => network.consumer_secret,
    :token_secret    => network.signature_key
  }

  signature_class = Libgss.use_oauth_gem ? ::OAuth::Signature : Signature
  if ENV["VERBOSE"]
    $stdout.puts("signature_class: #{signature_class.name}")
    $stdout.puts("       req_hash: #{req_hash.inspect}")
  end

  headers["oauth_signature"] = signature_class.sign(req_hash, options)

  res = @impl.post(uri, body, headers.update(original_headers))
end