motionbox-oauth 0.4.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +46 -0
- data/HISTORY +160 -0
- data/LICENSE +20 -0
- data/README.rdoc +75 -0
- data/Rakefile +37 -0
- data/TODO +32 -0
- data/bin/oauth +5 -0
- data/examples/yql.rb +44 -0
- data/lib/digest/hmac.rb +104 -0
- data/lib/oauth.rb +13 -0
- data/lib/oauth/cli.rb +378 -0
- data/lib/oauth/client.rb +4 -0
- data/lib/oauth/client/action_controller_request.rb +65 -0
- data/lib/oauth/client/em_http.rb +124 -0
- data/lib/oauth/client/helper.rb +91 -0
- data/lib/oauth/client/net_http.rb +120 -0
- data/lib/oauth/consumer.rb +382 -0
- data/lib/oauth/core_ext.rb +31 -0
- data/lib/oauth/errors.rb +3 -0
- data/lib/oauth/errors/error.rb +4 -0
- data/lib/oauth/errors/problem.rb +14 -0
- data/lib/oauth/errors/unauthorized.rb +12 -0
- data/lib/oauth/helper.rb +88 -0
- data/lib/oauth/oauth.rb +13 -0
- data/lib/oauth/oauth_test_helper.rb +25 -0
- data/lib/oauth/request_proxy.rb +24 -0
- data/lib/oauth/request_proxy/action_controller_request.rb +62 -0
- data/lib/oauth/request_proxy/base.rb +174 -0
- data/lib/oauth/request_proxy/curb_request.rb +55 -0
- data/lib/oauth/request_proxy/em_http_request.rb +74 -0
- data/lib/oauth/request_proxy/jabber_request.rb +41 -0
- data/lib/oauth/request_proxy/mock_request.rb +44 -0
- data/lib/oauth/request_proxy/net_http.rb +72 -0
- data/lib/oauth/request_proxy/rack_request.rb +44 -0
- data/lib/oauth/request_proxy/typhoeus_request.rb +53 -0
- data/lib/oauth/server.rb +66 -0
- data/lib/oauth/signature.rb +45 -0
- data/lib/oauth/signature/base.rb +110 -0
- data/lib/oauth/signature/hmac/base.rb +15 -0
- data/lib/oauth/signature/hmac/md5.rb +8 -0
- data/lib/oauth/signature/hmac/rmd160.rb +8 -0
- data/lib/oauth/signature/hmac/sha1.rb +9 -0
- data/lib/oauth/signature/hmac/sha2.rb +8 -0
- data/lib/oauth/signature/md5.rb +13 -0
- data/lib/oauth/signature/plaintext.rb +23 -0
- data/lib/oauth/signature/rsa/sha1.rb +46 -0
- data/lib/oauth/signature/sha1.rb +13 -0
- data/lib/oauth/token.rb +7 -0
- data/lib/oauth/tokens/access_token.rb +71 -0
- data/lib/oauth/tokens/consumer_token.rb +33 -0
- data/lib/oauth/tokens/request_token.rb +32 -0
- data/lib/oauth/tokens/server_token.rb +9 -0
- data/lib/oauth/tokens/token.rb +17 -0
- data/oauth.gemspec +150 -0
- data/tasks/deployment.rake +34 -0
- data/tasks/environment.rake +7 -0
- data/tasks/website.rake +17 -0
- data/test/cases/oauth_case.rb +19 -0
- data/test/cases/spec/1_0-final/test_construct_request_url.rb +62 -0
- data/test/cases/spec/1_0-final/test_normalize_request_parameters.rb +88 -0
- data/test/cases/spec/1_0-final/test_parameter_encodings.rb +86 -0
- data/test/cases/spec/1_0-final/test_signature_base_strings.rb +77 -0
- data/test/integration/consumer_test.rb +307 -0
- data/test/keys/rsa.cert +11 -0
- data/test/keys/rsa.pem +16 -0
- data/test/test_access_token.rb +26 -0
- data/test/test_action_controller_request_proxy.rb +133 -0
- data/test/test_consumer.rb +171 -0
- data/test/test_curb_request_proxy.rb +77 -0
- data/test/test_em_http_client.rb +80 -0
- data/test/test_em_http_request_proxy.rb +115 -0
- data/test/test_helper.rb +26 -0
- data/test/test_hmac_sha1.rb +20 -0
- data/test/test_net_http_client.rb +280 -0
- data/test/test_net_http_request_proxy.rb +72 -0
- data/test/test_oauth_helper.rb +71 -0
- data/test/test_rack_request_proxy.rb +40 -0
- data/test/test_request_token.rb +51 -0
- data/test/test_rsa_sha1.rb +59 -0
- data/test/test_server.rb +40 -0
- data/test/test_signature.rb +22 -0
- data/test/test_signature_base.rb +32 -0
- data/test/test_signature_plain_text.rb +31 -0
- data/test/test_token.rb +14 -0
- data/test/test_typhoeus_request_proxy.rb +80 -0
- metadata +284 -0
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'oauth/request_proxy/base'
|
2
|
+
require 'curb'
|
3
|
+
require 'uri'
|
4
|
+
require 'cgi'
|
5
|
+
|
6
|
+
module OAuth::RequestProxy::Curl
|
7
|
+
class Easy < OAuth::RequestProxy::Base
|
8
|
+
# Proxy for signing Curl::Easy requests
|
9
|
+
# Usage example:
|
10
|
+
# oauth_params = {:consumer => oauth_consumer, :token => access_token}
|
11
|
+
# req = Curl::Easy.new(uri)
|
12
|
+
# oauth_helper = OAuth::Client::Helper.new(req, oauth_params.merge(:request_uri => uri))
|
13
|
+
# req.headers.merge!({"Authorization" => oauth_helper.header})
|
14
|
+
# req.http_get
|
15
|
+
# response = req.body_str
|
16
|
+
proxies ::Curl::Easy
|
17
|
+
|
18
|
+
def method
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
|
22
|
+
def uri
|
23
|
+
options[:uri].to_s
|
24
|
+
end
|
25
|
+
|
26
|
+
def parameters
|
27
|
+
if options[:clobber_request]
|
28
|
+
options[:parameters]
|
29
|
+
else
|
30
|
+
post_parameters.merge(query_parameters).merge(options[:parameters] || {})
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def query_parameters
|
37
|
+
query = URI.parse(request.url).query
|
38
|
+
return(query ? CGI.parse(query) : {})
|
39
|
+
end
|
40
|
+
|
41
|
+
def post_parameters
|
42
|
+
post_body = {}
|
43
|
+
|
44
|
+
# Post params are only used if posting form data
|
45
|
+
if (request.headers['Content-Type'] && request.headers['Content-Type'].to_s.downcase.start_with?("application/x-www-form-urlencoded"))
|
46
|
+
|
47
|
+
request.post_body.split("&").each do |str|
|
48
|
+
param = str.split("=")
|
49
|
+
post_body[param[0]] = param[1]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
post_body
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'oauth/request_proxy/base'
|
2
|
+
# em-http also uses adddressable so there is no need to require uri.
|
3
|
+
require 'em-http'
|
4
|
+
require 'cgi'
|
5
|
+
|
6
|
+
module OAuth::RequestProxy::EventMachine
|
7
|
+
class HttpRequest < OAuth::RequestProxy::Base
|
8
|
+
|
9
|
+
# A Proxy for use when you need to sign EventMachine::HttpClient instances.
|
10
|
+
# It needs to be called once the client is construct but before data is sent.
|
11
|
+
# Also see oauth/client/em-http
|
12
|
+
proxies ::EventMachine::HttpClient
|
13
|
+
|
14
|
+
# Request in this con
|
15
|
+
|
16
|
+
def method
|
17
|
+
request.respond_to?(:req) ? request.req.method : request.method
|
18
|
+
end
|
19
|
+
|
20
|
+
def uri
|
21
|
+
request.normalize_uri.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
def parameters
|
25
|
+
if options[:clobber_request]
|
26
|
+
options[:parameters]
|
27
|
+
else
|
28
|
+
all_parameters
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
def all_parameters
|
35
|
+
merged_parameters({}, post_parameters, query_parameters, options[:parameters])
|
36
|
+
end
|
37
|
+
|
38
|
+
def query_parameters
|
39
|
+
CGI.parse(request.normalize_uri.query.to_s)
|
40
|
+
end
|
41
|
+
|
42
|
+
def post_parameters
|
43
|
+
headers = begin
|
44
|
+
if request.respond_to? :options
|
45
|
+
request.options[:head]
|
46
|
+
elsif request.respond_to? :req
|
47
|
+
request.req.headers
|
48
|
+
else
|
49
|
+
{}
|
50
|
+
end
|
51
|
+
end
|
52
|
+
form_encoded = headers['Content-Type'].to_s.downcase.start_with?("application/x-www-form-urlencoded")
|
53
|
+
if ['POST', 'PUT'].include?(method) && form_encoded
|
54
|
+
CGI.parse(request.normalize_body.to_s)
|
55
|
+
else
|
56
|
+
{}
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def merged_parameters(params, *extra_params)
|
61
|
+
extra_params.compact.each do |params_pairs|
|
62
|
+
params_pairs.each_pair do |key, value|
|
63
|
+
if params.has_key?(key)
|
64
|
+
params[key] += value
|
65
|
+
else
|
66
|
+
params[key] = [value].flatten
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
params
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'xmpp4r'
|
2
|
+
require 'oauth/request_proxy/base'
|
3
|
+
|
4
|
+
module OAuth
|
5
|
+
module RequestProxy
|
6
|
+
class JabberRequest < OAuth::RequestProxy::Base
|
7
|
+
proxies Jabber::Iq
|
8
|
+
proxies Jabber::Presence
|
9
|
+
proxies Jabber::Message
|
10
|
+
|
11
|
+
def parameters
|
12
|
+
return @params if @params
|
13
|
+
|
14
|
+
@params = {}
|
15
|
+
|
16
|
+
oauth = @request.get_elements('//oauth').first
|
17
|
+
return @params unless oauth
|
18
|
+
|
19
|
+
%w( oauth_token oauth_consumer_key oauth_signature_method oauth_signature
|
20
|
+
oauth_timestamp oauth_nonce oauth_version ).each do |param|
|
21
|
+
next unless element = oauth.first_element(param)
|
22
|
+
@params[param] = element.text
|
23
|
+
end
|
24
|
+
|
25
|
+
@params
|
26
|
+
end
|
27
|
+
|
28
|
+
def method
|
29
|
+
@request.name
|
30
|
+
end
|
31
|
+
|
32
|
+
def uri
|
33
|
+
[@request.from.strip.to_s, @request.to.strip.to_s].join("&")
|
34
|
+
end
|
35
|
+
|
36
|
+
def normalized_uri
|
37
|
+
uri
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'oauth/request_proxy/base'
|
2
|
+
|
3
|
+
module OAuth
|
4
|
+
module RequestProxy
|
5
|
+
# RequestProxy for Hashes to facilitate simpler signature creation.
|
6
|
+
# Usage:
|
7
|
+
# request = OAuth::RequestProxy.proxy \
|
8
|
+
# "method" => "iq",
|
9
|
+
# "uri" => [from, to] * "&",
|
10
|
+
# "parameters" => {
|
11
|
+
# "oauth_consumer_key" => oauth_consumer_key,
|
12
|
+
# "oauth_token" => oauth_token,
|
13
|
+
# "oauth_signature_method" => "HMAC-SHA1"
|
14
|
+
# }
|
15
|
+
#
|
16
|
+
# signature = OAuth::Signature.sign \
|
17
|
+
# request,
|
18
|
+
# :consumer_secret => oauth_consumer_secret,
|
19
|
+
# :token_secret => oauth_token_secret,
|
20
|
+
class MockRequest < OAuth::RequestProxy::Base
|
21
|
+
proxies Hash
|
22
|
+
|
23
|
+
def parameters
|
24
|
+
@request["parameters"]
|
25
|
+
end
|
26
|
+
|
27
|
+
def method
|
28
|
+
@request["method"]
|
29
|
+
end
|
30
|
+
|
31
|
+
def normalized_uri
|
32
|
+
super
|
33
|
+
rescue
|
34
|
+
# if this is a non-standard URI, it may not parse properly
|
35
|
+
# in that case, assume that it's already been normalized
|
36
|
+
uri
|
37
|
+
end
|
38
|
+
|
39
|
+
def uri
|
40
|
+
@request["uri"]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'oauth/request_proxy/base'
|
2
|
+
require 'net/http'
|
3
|
+
require 'uri'
|
4
|
+
require 'cgi'
|
5
|
+
|
6
|
+
module OAuth::RequestProxy::Net
|
7
|
+
module HTTP
|
8
|
+
class HTTPRequest < OAuth::RequestProxy::Base
|
9
|
+
proxies ::Net::HTTPGenericRequest
|
10
|
+
|
11
|
+
def method
|
12
|
+
request.method
|
13
|
+
end
|
14
|
+
|
15
|
+
def uri
|
16
|
+
options[:uri].to_s
|
17
|
+
end
|
18
|
+
|
19
|
+
def parameters
|
20
|
+
if options[:clobber_request]
|
21
|
+
options[:parameters]
|
22
|
+
else
|
23
|
+
all_parameters
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def body
|
28
|
+
request.body
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def all_parameters
|
34
|
+
request_params = CGI.parse(query_string)
|
35
|
+
|
36
|
+
if options[:parameters]
|
37
|
+
options[:parameters].each do |k,v|
|
38
|
+
if request_params.has_key?(k) && v
|
39
|
+
request_params[k] << v
|
40
|
+
else
|
41
|
+
request_params[k] = [v]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
request_params
|
46
|
+
end
|
47
|
+
|
48
|
+
def query_string
|
49
|
+
params = [ query_params, auth_header_params ]
|
50
|
+
params << post_params if method.to_s.upcase == 'POST' && form_url_encoded?
|
51
|
+
params.compact.join('&')
|
52
|
+
end
|
53
|
+
|
54
|
+
def form_url_encoded?
|
55
|
+
request['Content-Type'] != nil && request['Content-Type'].to_s.downcase.start_with?('application/x-www-form-urlencoded')
|
56
|
+
end
|
57
|
+
|
58
|
+
def query_params
|
59
|
+
URI.parse(request.path).query
|
60
|
+
end
|
61
|
+
|
62
|
+
def post_params
|
63
|
+
request.body
|
64
|
+
end
|
65
|
+
|
66
|
+
def auth_header_params
|
67
|
+
return nil unless request['Authorization'] && request['Authorization'][0,5] == 'OAuth'
|
68
|
+
auth_params = request['Authorization']
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'oauth/request_proxy/base'
|
2
|
+
require 'uri'
|
3
|
+
require 'rack'
|
4
|
+
|
5
|
+
module OAuth::RequestProxy
|
6
|
+
class RackRequest < OAuth::RequestProxy::Base
|
7
|
+
proxies Rack::Request
|
8
|
+
|
9
|
+
def method
|
10
|
+
request.env["rack.methodoverride.original_method"] || request.request_method
|
11
|
+
end
|
12
|
+
|
13
|
+
def uri
|
14
|
+
request.url
|
15
|
+
end
|
16
|
+
|
17
|
+
def parameters
|
18
|
+
if options[:clobber_request]
|
19
|
+
options[:parameters] || {}
|
20
|
+
else
|
21
|
+
params = request_params.merge(query_params).merge(header_params)
|
22
|
+
params.merge(options[:parameters] || {})
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def signature
|
27
|
+
parameters['oauth_signature']
|
28
|
+
end
|
29
|
+
|
30
|
+
protected
|
31
|
+
|
32
|
+
def query_params
|
33
|
+
request.GET
|
34
|
+
end
|
35
|
+
|
36
|
+
def request_params
|
37
|
+
if request.content_type and request.content_type.to_s.downcase.start_with?("application/x-www-form-urlencoded")
|
38
|
+
request.POST
|
39
|
+
else
|
40
|
+
{}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'oauth/request_proxy/base'
|
2
|
+
require 'typhoeus'
|
3
|
+
require 'typhoeus/request'
|
4
|
+
require 'uri'
|
5
|
+
require 'cgi'
|
6
|
+
|
7
|
+
module OAuth::RequestProxy::Typhoeus
|
8
|
+
class Request < OAuth::RequestProxy::Base
|
9
|
+
# Proxy for signing Typhoeus::Request requests
|
10
|
+
# Usage example:
|
11
|
+
# oauth_params = {:consumer => oauth_consumer, :token => access_token}
|
12
|
+
# req = Typhoeus::Request.new(uri, options)
|
13
|
+
# oauth_helper = OAuth::Client::Helper.new(req, oauth_params.merge(:request_uri => uri))
|
14
|
+
# req.headers.merge!({"Authorization" => oauth_helper.header})
|
15
|
+
# hydra = Typhoeus::Hydra.new()
|
16
|
+
# hydra.queue(req)
|
17
|
+
# hydra.run
|
18
|
+
# response = req.response
|
19
|
+
proxies Typhoeus::Request
|
20
|
+
|
21
|
+
def method
|
22
|
+
request.method.to_s.upcase
|
23
|
+
end
|
24
|
+
|
25
|
+
def uri
|
26
|
+
options[:uri].to_s
|
27
|
+
end
|
28
|
+
|
29
|
+
def parameters
|
30
|
+
if options[:clobber_request]
|
31
|
+
options[:parameters]
|
32
|
+
else
|
33
|
+
post_parameters.merge(query_parameters).merge(options[:parameters] || {})
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def query_parameters
|
40
|
+
query = URI.parse(request.url).query
|
41
|
+
query ? CGI.parse(query) : {}
|
42
|
+
end
|
43
|
+
|
44
|
+
def post_parameters
|
45
|
+
# Post params are only used if posting form data
|
46
|
+
if method == 'POST'
|
47
|
+
OAuth::Helper.stringify_keys(request.params || {})
|
48
|
+
else
|
49
|
+
{}
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/oauth/server.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'oauth/helper'
|
2
|
+
require 'oauth/consumer'
|
3
|
+
|
4
|
+
module OAuth
|
5
|
+
# This is mainly used to create consumer credentials and can pretty much be ignored if you want to create your own
|
6
|
+
class Server
|
7
|
+
include OAuth::Helper
|
8
|
+
attr_accessor :base_url
|
9
|
+
|
10
|
+
@@server_paths = {
|
11
|
+
:request_token_path => "/oauth/request_token",
|
12
|
+
:authorize_path => "/oauth/authorize",
|
13
|
+
:access_token_path => "/oauth/access_token"
|
14
|
+
}
|
15
|
+
|
16
|
+
# Create a new server instance
|
17
|
+
def initialize(base_url, paths = {})
|
18
|
+
@base_url = base_url
|
19
|
+
@paths = @@server_paths.merge(paths)
|
20
|
+
end
|
21
|
+
|
22
|
+
def generate_credentials
|
23
|
+
[generate_key(16), generate_key]
|
24
|
+
end
|
25
|
+
|
26
|
+
def generate_consumer_credentials(params = {})
|
27
|
+
Consumer.new(*generate_credentials)
|
28
|
+
end
|
29
|
+
|
30
|
+
# mainly for testing purposes
|
31
|
+
def create_consumer
|
32
|
+
creds = generate_credentials
|
33
|
+
Consumer.new(creds[0], creds[1],
|
34
|
+
{
|
35
|
+
:site => base_url,
|
36
|
+
:request_token_path => request_token_path,
|
37
|
+
:authorize_path => authorize_path,
|
38
|
+
:access_token_path => access_token_path
|
39
|
+
})
|
40
|
+
end
|
41
|
+
|
42
|
+
def request_token_path
|
43
|
+
@paths[:request_token_path]
|
44
|
+
end
|
45
|
+
|
46
|
+
def request_token_url
|
47
|
+
base_url + request_token_path
|
48
|
+
end
|
49
|
+
|
50
|
+
def authorize_path
|
51
|
+
@paths[:authorize_path]
|
52
|
+
end
|
53
|
+
|
54
|
+
def authorize_url
|
55
|
+
base_url + authorize_path
|
56
|
+
end
|
57
|
+
|
58
|
+
def access_token_path
|
59
|
+
@paths[:access_token_path]
|
60
|
+
end
|
61
|
+
|
62
|
+
def access_token_url
|
63
|
+
base_url + access_token_path
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|