restfull_oauth 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +9 -5
- data/lib/restfull_oauth/version.rb +1 -1
- data/lib/restfull_oauth.rb +86 -64
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: adff2d769488a9b7c5d78441db5a39a6ca03e0b5
|
4
|
+
data.tar.gz: df0c13f39fe659544008d71bdc22b088fb6307e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0a3b23e46e9d8c1dd363052b00a92354ef818c9863d18e21b7816def9d47e5ec5bcf860d8eeda50853cbaf0a036572acf3528df3f1c205ffd7a79321cb443ab3
|
7
|
+
data.tar.gz: c9310088344850749e4c4fe13be33cb2fbd0430435c4297bd1c67ad5294b8564aded8e15e8ae622509a1133d229dc15770ed022436cf0179ce7e5b767abace91
|
data/README.md
CHANGED
@@ -30,19 +30,23 @@ GET request (get product information):
|
|
30
30
|
|
31
31
|
```irb
|
32
32
|
% irb
|
33
|
-
>>
|
34
|
-
>>
|
33
|
+
>> require 'restfull_oauth'
|
34
|
+
>> service = RestfullOauth::Connection.new({"consumer_key"=>"your_consumer_key", "consumer_secret"=>"your_consumer_secret", "token"=>"your_token", "token_secret"=>"your_token_secret"})
|
35
|
+
>> response = service.connect('GET','https://your server/rest/api', keys)
|
35
36
|
=> #<Net::HTTPOK 200 OK readbody=true>
|
37
|
+
>> puts JSON.parse(response.body).to_yaml
|
36
38
|
```
|
37
39
|
|
38
40
|
POST request (assign product to website):
|
39
41
|
|
40
42
|
```irb
|
41
43
|
% irb
|
42
|
-
>>
|
43
|
-
>>
|
44
|
-
>>
|
44
|
+
>> require 'restfull_oauth'
|
45
|
+
>> service = RestfullOauth::Connection.new({"consumer_key"=>"your_consumer_key", "consumer_secret"=>"your_consumer_secret", "token"=>"your_token", "token_secret"=>"your_token_secret"})
|
46
|
+
>> post_data = {'foo' => 'bar' }.to_json
|
47
|
+
>> response = RestfullOauth::connect('POST','https://your server/rest/api', post_data)
|
45
48
|
=> #<Net::HTTPOK 200 OK readbody=true>
|
49
|
+
>> puts JSON.parse(response.body).to_yaml
|
46
50
|
```
|
47
51
|
|
48
52
|
PUT requests work in the same way as POST requests
|
data/lib/restfull_oauth.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'restfull_oauth/version'
|
2
2
|
require 'oauth'
|
3
3
|
require 'uri'
|
4
4
|
require 'cgi'
|
@@ -16,79 +16,101 @@ module RestfullOauth
|
|
16
16
|
# % authenticate
|
17
17
|
#
|
18
18
|
# Example:
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
19
|
+
#
|
20
|
+
#% irb
|
21
|
+
# >> require 'restfull_oauth'
|
22
|
+
# >> service = RestfullOauth::Connection.new({"consumer_key"=>"your_consumer_key", "consumer_secret"=>"your_consumer_secret", "token"=>"your_token", "token_secret"=>"your_token_secret"})
|
23
|
+
# >> post_data = {'foo' => 'bar' }.to_json
|
24
|
+
# >> response = RestfullOauth::connect('POST','https://your server/rest/api', post_data)
|
25
|
+
# => #<Net::HTTPOK 200 OK readbody=true>
|
26
|
+
# >> puts JSON.parse(response.body).to_yaml
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
end
|
28
|
+
class Connection
|
29
|
+
attr_accessor :session, :config, :logger
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
def initialize(config = {})
|
32
|
+
@config = config
|
33
|
+
self
|
34
|
+
end
|
34
35
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
'
|
39
|
-
'
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
}
|
44
|
-
end
|
36
|
+
def connect(method, uri, post_data=nil)
|
37
|
+
params = params(@config['consumer_key'], @config['token'] )
|
38
|
+
signature_base_string = signature_base_string(method, uri.to_s, params)
|
39
|
+
signing_key = @config['consumer_secret'] + '&' + @config['token_secret']
|
40
|
+
params['oauth_signature'] = url_encode(sign(signing_key, signature_base_string))
|
41
|
+
header_string = create_header(params)
|
42
|
+
json_response = request_data(header_string, uri, method, post_data)
|
43
|
+
end
|
45
44
|
|
46
|
-
|
47
|
-
encoded_params = url_encode("oauth_consumer_key=#{params['oauth_consumer_key']}&oauth_nonce=#{params['oauth_nonce']}&oauth_signature_method=#{params['oauth_signature_method']}&oauth_timestamp=#{params['oauth_timestamp']}&oauth_token=#{params['oauth_token']}&oauth_version=#{params['oauth_version']}")
|
48
|
-
encoded = method + '&' + url_encode(uri) + '&' + encoded_params
|
49
|
-
end
|
45
|
+
private
|
50
46
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
Base64.encode64(hmac).chomp.gsub(/\n/, '')
|
55
|
-
end
|
47
|
+
def generate_nonce(size=6)
|
48
|
+
Base64.encode64(OpenSSL::Random.random_bytes(size)).gsub(/\W/, '')
|
49
|
+
end
|
56
50
|
|
57
|
-
|
58
|
-
|
59
|
-
params.each do |k, v|
|
60
|
-
header += "#{k}=\"#{v}\","
|
51
|
+
def url_encode(string)
|
52
|
+
CGI::escape(string)
|
61
53
|
end
|
62
|
-
header.slice(0..-2)
|
63
|
-
end
|
64
54
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
55
|
+
def params(consumer_key, token)
|
56
|
+
params = {'oauth_consumer_key' => consumer_key,
|
57
|
+
'oauth_nonce' => generate_nonce,
|
58
|
+
'oauth_signature_method' => 'HMAC-SHA1',
|
59
|
+
'oauth_token' => token,
|
60
|
+
'oauth_version' => '1.0',
|
61
|
+
'oauth_timestamp' => Time.now.to_i.to_s
|
62
|
+
}
|
63
|
+
end
|
64
|
+
|
65
|
+
def signature_base_string(method, uri, params)
|
66
|
+
encoded_params = url_encode("oauth_consumer_key=#{params['oauth_consumer_key']}&oauth_nonce=#{params['oauth_nonce']}&oauth_signature_method=#{params['oauth_signature_method']}&oauth_timestamp=#{params['oauth_timestamp']}&oauth_token=#{params['oauth_token']}&oauth_version=#{params['oauth_version']}")
|
67
|
+
encoded = method + '&' + url_encode(uri) + '&' + encoded_params
|
68
|
+
end
|
69
|
+
|
70
|
+
def sign(key, base_string)
|
71
|
+
digest = OpenSSL::Digest.new('sha1')
|
72
|
+
hmac = OpenSSL::HMAC.digest(digest, key, base_string)
|
73
|
+
Base64.encode64(hmac).chomp.gsub(/\n/, '')
|
74
|
+
end
|
75
|
+
|
76
|
+
def create_header(params)
|
77
|
+
header = "OAuth "
|
78
|
+
params.each do |k, v|
|
79
|
+
header += "#{k}=\"#{v}\","
|
77
80
|
end
|
78
|
-
|
79
|
-
resp, data = http.request(request)
|
80
|
-
else
|
81
|
-
resp, data = http.get(uri.path, { 'Authorization' => header, "Content-Type" => "application/json; charset=utf-8" })
|
81
|
+
header.slice(0..-2)
|
82
82
|
end
|
83
|
-
resp
|
84
|
-
end
|
85
83
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
84
|
+
def request_data(header, uri, method, post_data=nil)
|
85
|
+
uri = URI(uri)
|
86
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
87
|
+
if (uri.port == 443)
|
88
|
+
http.use_ssl = true
|
89
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
90
|
+
end
|
91
|
+
if method == 'POST'
|
92
|
+
resp, data = http.post(uri.path,
|
93
|
+
post_data,
|
94
|
+
{ 'Authorization' => header,
|
95
|
+
'Content-Type' => 'application/json; charset=utf-8'
|
96
|
+
})
|
97
|
+
elsif method == 'PUT'
|
98
|
+
request = Net::HTTP::Put.new(uri.path)
|
99
|
+
headers = { 'Authorization' => header,
|
100
|
+
'Content-Type' => 'application/json'
|
101
|
+
}
|
102
|
+
headers.each do |key|
|
103
|
+
request[key] = headers[key]
|
104
|
+
end
|
105
|
+
request.body = post_data
|
106
|
+
resp, data = http.request(request)
|
107
|
+
else
|
108
|
+
resp, data = http.get(uri.path,
|
109
|
+
{ 'Authorization' => header,
|
110
|
+
'Content-Type' => 'application/json; charset=utf-8'
|
111
|
+
})
|
112
|
+
end
|
113
|
+
resp
|
114
|
+
end
|
93
115
|
end
|
94
116
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restfull_oauth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel.van.den.Oord
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|