rares-oauth 0.2.7
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.
- data/History.txt +33 -0
- data/License.txt +20 -0
- data/Manifest.txt +55 -0
- data/Rakefile +4 -0
- data/config/hoe.rb +71 -0
- data/config/requirements.rb +17 -0
- data/lib/oauth.rb +3 -0
- data/lib/oauth/client.rb +4 -0
- data/lib/oauth/client/action_controller_request.rb +52 -0
- data/lib/oauth/client/helper.rb +88 -0
- data/lib/oauth/client/net_http.rb +75 -0
- data/lib/oauth/consumer.rb +218 -0
- data/lib/oauth/helper.rb +14 -0
- data/lib/oauth/request_proxy.rb +24 -0
- data/lib/oauth/request_proxy/action_controller_request.rb +64 -0
- data/lib/oauth/request_proxy/base.rb +76 -0
- data/lib/oauth/request_proxy/net_http.rb +67 -0
- data/lib/oauth/request_proxy/rack_request.rb +42 -0
- data/lib/oauth/server.rb +68 -0
- data/lib/oauth/signature.rb +28 -0
- data/lib/oauth/signature/base.rb +76 -0
- data/lib/oauth/signature/hmac/base.rb +12 -0
- data/lib/oauth/signature/hmac/md5.rb +9 -0
- data/lib/oauth/signature/hmac/rmd160.rb +9 -0
- data/lib/oauth/signature/hmac/sha1.rb +10 -0
- data/lib/oauth/signature/hmac/sha2.rb +9 -0
- data/lib/oauth/signature/md5.rb +13 -0
- data/lib/oauth/signature/plaintext.rb +23 -0
- data/lib/oauth/signature/rsa/sha1.rb +44 -0
- data/lib/oauth/signature/sha1.rb +13 -0
- data/lib/oauth/token.rb +163 -0
- data/lib/oauth/version.rb +9 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/script/txt2html +74 -0
- data/setup.rb +1585 -0
- data/tasks/deployment.rake +34 -0
- data/tasks/environment.rake +7 -0
- data/tasks/website.rake +17 -0
- data/test/test_action_controller_request_proxy.rb +27 -0
- data/test/test_consumer.rb +285 -0
- data/test/test_helper.rb +7 -0
- data/test/test_hmac_sha1.rb +21 -0
- data/test/test_net_http_client.rb +169 -0
- data/test/test_net_http_request_proxy.rb +38 -0
- data/test/test_rack_request_proxy.rb +40 -0
- data/test/test_server.rb +40 -0
- data/test/test_signature.rb +11 -0
- data/test/test_signature_base.rb +32 -0
- data/test/test_token.rb +15 -0
- data/website/index.html +87 -0
- data/website/index.txt +73 -0
- data/website/javascripts/rounded_corners_lite.inc.js +285 -0
- data/website/stylesheets/screen.css +138 -0
- data/website/template.rhtml +48 -0
- metadata +137 -0
data/History.txt
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
Fix in plain text signatures to bug found by Andrew Arrow. Who contributed new new unit tests for plain text sigs.
|
2
|
+
|
3
|
+
== 0.2.7 2008-9-10 The lets fix the last release release
|
4
|
+
|
5
|
+
There was an error in the RSA requests using oauth tokens. Thanks to Philip Lipu Tsai for noticing this.
|
6
|
+
|
7
|
+
== 0.2.6 2008-9-9 The lets RSA release
|
8
|
+
|
9
|
+
- Bill Kocik's fix for Ruby 1.8.7
|
10
|
+
- Fixed rsa verification, so you can actually create an OAuth server yourself now using Ruby and RSA
|
11
|
+
- Added better testing for RSA
|
12
|
+
- Fixed issue where token was being included for rsa signatures
|
13
|
+
- Chris Mear added support for a private_key_file option for rsa signatures
|
14
|
+
- Scott Hill fixed several edge cases where parameters were incorrectly being signed
|
15
|
+
- Patch from choonkeat fixing a problem with rsa signing.
|
16
|
+
|
17
|
+
== 0.2.2 2008-2-22 Lets actually support SSL release
|
18
|
+
|
19
|
+
It didn't actually use https when required.
|
20
|
+
|
21
|
+
== 0.2 2008-1-19 All together now release
|
22
|
+
|
23
|
+
This is a big release, where we have merged the efforts of various parties into one common library. This means there are definitely some API changes you should be aware of. They should be minimal but please have a look at the unit tests.
|
24
|
+
|
25
|
+
== 0.1.2 2007-12-1
|
26
|
+
|
27
|
+
* 1 Fixed a problem where incoming request didn't check whether oauth parameters where missing. While not giving unauthorized access it did cause extra processing where not necessary.
|
28
|
+
* 2 Includes Pat's fix for getting the realm out.
|
29
|
+
|
30
|
+
== 0.1.1 2007-11-26
|
31
|
+
|
32
|
+
* 1 First release as a GEM
|
33
|
+
* Moved all non rails functions into this GEM from the Rails plugin http://code.google.com/p/oauth-plugin/
|
data/License.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2007 Blaine Cook, Larry Halff, Pelle Braendgaard
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
History.txt
|
2
|
+
License.txt
|
3
|
+
Manifest.txt
|
4
|
+
README.txt
|
5
|
+
Rakefile
|
6
|
+
config/hoe.rb
|
7
|
+
config/requirements.rb
|
8
|
+
lib/oauth.rb
|
9
|
+
lib/oauth/client.rb
|
10
|
+
lib/oauth/client/action_controller_request.rb
|
11
|
+
lib/oauth/client/helper.rb
|
12
|
+
lib/oauth/client/net_http.rb
|
13
|
+
lib/oauth/consumer.rb
|
14
|
+
lib/oauth/helper.rb
|
15
|
+
lib/oauth/request_proxy.rb
|
16
|
+
lib/oauth/request_proxy/action_controller_request.rb
|
17
|
+
lib/oauth/request_proxy/base.rb
|
18
|
+
lib/oauth/request_proxy/net_http.rb
|
19
|
+
lib/oauth/request_proxy/rack_request.rb
|
20
|
+
lib/oauth/server.rb
|
21
|
+
lib/oauth/signature.rb
|
22
|
+
lib/oauth/signature/base.rb
|
23
|
+
lib/oauth/signature/hmac/base.rb
|
24
|
+
lib/oauth/signature/hmac/md5.rb
|
25
|
+
lib/oauth/signature/hmac/rmd160.rb
|
26
|
+
lib/oauth/signature/hmac/sha1.rb
|
27
|
+
lib/oauth/signature/hmac/sha2.rb
|
28
|
+
lib/oauth/signature/md5.rb
|
29
|
+
lib/oauth/signature/plaintext.rb
|
30
|
+
lib/oauth/signature/rsa/sha1.rb
|
31
|
+
lib/oauth/signature/sha1.rb
|
32
|
+
lib/oauth/token.rb
|
33
|
+
lib/oauth/version.rb
|
34
|
+
script/destroy
|
35
|
+
script/generate
|
36
|
+
script/txt2html
|
37
|
+
setup.rb
|
38
|
+
tasks/deployment.rake
|
39
|
+
tasks/environment.rake
|
40
|
+
tasks/website.rake
|
41
|
+
test/test_action_controller_request_proxy.rb
|
42
|
+
test/test_consumer.rb
|
43
|
+
test/test_helper.rb
|
44
|
+
test/test_hmac_sha1.rb
|
45
|
+
test/test_net_http_client.rb
|
46
|
+
test/test_net_http_request_proxy.rb
|
47
|
+
test/test_rack_request_proxy.rb
|
48
|
+
test/test_signature.rb
|
49
|
+
test/test_signature_base.rb
|
50
|
+
test/test_token.rb
|
51
|
+
website/index.html
|
52
|
+
website/index.txt
|
53
|
+
website/javascripts/rounded_corners_lite.inc.js
|
54
|
+
website/stylesheets/screen.css
|
55
|
+
website/template.rhtml
|
data/Rakefile
ADDED
data/config/hoe.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'oauth/version'
|
2
|
+
|
3
|
+
AUTHOR = ['Pelle Braendgaard','Blaine Cook','Larry Halff','Jesse Clark','Jon Crosby', 'Seth Fitzsimmons'] # can also be an array of Authors
|
4
|
+
EMAIL = "pelleb@gmail.com"
|
5
|
+
DESCRIPTION = "OAuth Core Ruby implementation"
|
6
|
+
GEM_NAME = 'oauth' # what ppl will type to install your gem
|
7
|
+
RUBYFORGE_PROJECT = 'oauth' # The unix name for your project
|
8
|
+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
9
|
+
DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
|
10
|
+
|
11
|
+
@config_file = "~/.rubyforge/user-config.yml"
|
12
|
+
@config = nil
|
13
|
+
RUBYFORGE_USERNAME = "unknown"
|
14
|
+
def rubyforge_username
|
15
|
+
unless @config
|
16
|
+
begin
|
17
|
+
@config = YAML.load(File.read(File.expand_path(@config_file)))
|
18
|
+
rescue
|
19
|
+
puts <<-EOS
|
20
|
+
ERROR: No rubyforge config file found: #{@config_file}
|
21
|
+
Run 'rubyforge setup' to prepare your env for access to Rubyforge
|
22
|
+
- See http://newgem.rubyforge.org/rubyforge.html for more details
|
23
|
+
EOS
|
24
|
+
exit
|
25
|
+
end
|
26
|
+
end
|
27
|
+
RUBYFORGE_USERNAME.replace @config["username"]
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
REV = nil
|
32
|
+
# UNCOMMENT IF REQUIRED:
|
33
|
+
# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
|
34
|
+
VERS = Oauth::VERSION::STRING + (REV ? ".#{REV}" : "")
|
35
|
+
RDOC_OPTS = ['--quiet', '--title', 'oauth documentation',
|
36
|
+
"--opname", "index.html",
|
37
|
+
"--line-numbers",
|
38
|
+
"--main", "README",
|
39
|
+
"--inline-source"]
|
40
|
+
|
41
|
+
class Hoe
|
42
|
+
def extra_deps
|
43
|
+
@extra_deps.reject! { |x| Array(x).first == 'hoe' }
|
44
|
+
@extra_deps
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Generate all the Rake tasks
|
49
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
50
|
+
hoe = Hoe.new(GEM_NAME, VERS) do |p|
|
51
|
+
p.author = AUTHOR
|
52
|
+
p.description = DESCRIPTION
|
53
|
+
p.email = EMAIL
|
54
|
+
p.summary = DESCRIPTION
|
55
|
+
p.url = HOMEPATH
|
56
|
+
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
|
57
|
+
p.test_globs = ["test/**/test_*.rb"]
|
58
|
+
p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
|
59
|
+
|
60
|
+
# == Optional
|
61
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
62
|
+
p.extra_deps = [['ruby-hmac','>= 0.3.1'] ] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
|
63
|
+
|
64
|
+
#p.spec_extras = {} # A hash of extra values to set in the gemspec.
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
|
69
|
+
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
|
70
|
+
hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
|
71
|
+
hoe.rsync_args = '-av --delete --ignore-errors'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
include FileUtils
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
%w[rake hoe newgem rubigen].each do |req_gem|
|
6
|
+
begin
|
7
|
+
require req_gem
|
8
|
+
rescue LoadError
|
9
|
+
puts "This Rakefile requires the '#{req_gem}' RubyGem."
|
10
|
+
puts "Installation: gem install #{req_gem} -y"
|
11
|
+
exit
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
|
16
|
+
|
17
|
+
require 'oauth'
|
data/lib/oauth.rb
ADDED
data/lib/oauth/client.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'oauth/client/helper'
|
2
|
+
require 'oauth/request_proxy/action_controller_request'
|
3
|
+
require 'action_controller/test_process'
|
4
|
+
|
5
|
+
module ActionController
|
6
|
+
class Base
|
7
|
+
def process_with_oauth(request,response=nil)
|
8
|
+
request.apply_oauth!
|
9
|
+
process_without_oauth(request,response)
|
10
|
+
end
|
11
|
+
|
12
|
+
alias_method_chain :process, :oauth
|
13
|
+
end
|
14
|
+
|
15
|
+
class TestRequest
|
16
|
+
def self.use_oauth=(bool)
|
17
|
+
@use_oauth = bool
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.use_oauth?
|
21
|
+
@use_oauth
|
22
|
+
end
|
23
|
+
|
24
|
+
def configure_oauth(consumer = nil, token = nil, options = {})
|
25
|
+
@oauth_options = { :consumer => consumer,
|
26
|
+
:token => token,
|
27
|
+
:scheme => 'header',
|
28
|
+
:signature_method => nil,
|
29
|
+
:nonce => nil,
|
30
|
+
:timestamp => nil }.merge(options)
|
31
|
+
end
|
32
|
+
|
33
|
+
def apply_oauth!
|
34
|
+
return unless ActionController::TestRequest.use_oauth? && @oauth_options
|
35
|
+
@oauth_helper = OAuth::Client::Helper.new(self, @oauth_options.merge( { :request_uri => request_uri } ))
|
36
|
+
|
37
|
+
self.send("set_oauth_#{@oauth_options[:scheme]}")
|
38
|
+
end
|
39
|
+
|
40
|
+
def set_oauth_header
|
41
|
+
env['Authorization'] = @oauth_helper.header
|
42
|
+
end
|
43
|
+
|
44
|
+
def set_oauth_parameters
|
45
|
+
@query_parameters = @oauth_helper.parameters_with_oauth
|
46
|
+
@query_parameters.merge!( { :oauth_signature => @oauth_helper.signature } )
|
47
|
+
end
|
48
|
+
|
49
|
+
def set_oauth_query_string
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'oauth/client'
|
2
|
+
require 'oauth/consumer'
|
3
|
+
require 'oauth/helper'
|
4
|
+
require 'oauth/token'
|
5
|
+
require 'oauth/signature/hmac/sha1'
|
6
|
+
|
7
|
+
module OAuth::Client
|
8
|
+
class Helper
|
9
|
+
include OAuth::Helper
|
10
|
+
|
11
|
+
def initialize(request, options = {})
|
12
|
+
@request = request
|
13
|
+
@options = options
|
14
|
+
@options[:signature_method] ||= 'HMAC-SHA1'
|
15
|
+
end
|
16
|
+
|
17
|
+
def options
|
18
|
+
@options
|
19
|
+
end
|
20
|
+
|
21
|
+
def nonce
|
22
|
+
options[:nonce] ||= generate_key
|
23
|
+
end
|
24
|
+
|
25
|
+
def timestamp
|
26
|
+
options[:timestamp] ||= generate_timestamp
|
27
|
+
end
|
28
|
+
|
29
|
+
def generate_timestamp
|
30
|
+
Time.now.to_i.to_s
|
31
|
+
end
|
32
|
+
|
33
|
+
def oauth_parameters
|
34
|
+
params = {
|
35
|
+
'oauth_consumer_key' => options[:consumer].key,
|
36
|
+
'oauth_token' => options[:token] ? options[:token].token : '',
|
37
|
+
'oauth_signature_method' => options[:signature_method],
|
38
|
+
'oauth_timestamp' => timestamp,
|
39
|
+
'oauth_nonce' => nonce,
|
40
|
+
'oauth_version' => '1.0'
|
41
|
+
}
|
42
|
+
|
43
|
+
prune_oauth_token_if_not_present params
|
44
|
+
params
|
45
|
+
end
|
46
|
+
|
47
|
+
def signature(extra_options = {})
|
48
|
+
OAuth::Signature.sign(@request, { :uri => options[:request_uri],
|
49
|
+
:consumer => options[:consumer],
|
50
|
+
:token => options[:token] }.merge(extra_options) )
|
51
|
+
end
|
52
|
+
|
53
|
+
def signature_base_string(extra_options = {})
|
54
|
+
OAuth::Signature.signature_base_string(@request, { :uri => options[:request_uri],
|
55
|
+
:consumer => options[:consumer],
|
56
|
+
:token => options[:token],
|
57
|
+
:parameters => oauth_parameters}.merge(extra_options) )
|
58
|
+
end
|
59
|
+
|
60
|
+
def header
|
61
|
+
parameters = oauth_parameters
|
62
|
+
parameters.merge!( { 'oauth_signature' => signature( options.merge({ :parameters => parameters }) ) } )
|
63
|
+
|
64
|
+
header_params_str = parameters.map { |k,v| "#{k}=\"#{escape(v)}\"" }.join(', ')
|
65
|
+
|
66
|
+
return "OAuth realm=\"#{options[:realm]||''}\", #{header_params_str}"
|
67
|
+
end
|
68
|
+
|
69
|
+
def parameters
|
70
|
+
OAuth::RequestProxy.proxy(@request).parameters
|
71
|
+
end
|
72
|
+
|
73
|
+
def parameters_with_oauth
|
74
|
+
oauth_parameters.merge( parameters )
|
75
|
+
end
|
76
|
+
|
77
|
+
protected
|
78
|
+
|
79
|
+
# If the oauth_token key is present, strip it. This _can't_ go up in the
|
80
|
+
# RequestToken when using HMAC-SHA1. The hashing validation gets blowed up.
|
81
|
+
# HACK: the request signing in Consumer,rb and Token.rb need a little refatoring
|
82
|
+
# so this isn't needed.
|
83
|
+
def prune_oauth_token_if_not_present(params = {})
|
84
|
+
params.delete("oauth_token") if params["oauth_token"].nil? or params["oauth_token"].empty?
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'oauth/helper'
|
2
|
+
require 'oauth/client/helper'
|
3
|
+
require 'oauth/request_proxy/net_http'
|
4
|
+
|
5
|
+
class Net::HTTPRequest
|
6
|
+
include OAuth::Helper
|
7
|
+
|
8
|
+
def oauth!(http, consumer = nil, token = nil, options = {})
|
9
|
+
options = { :request_uri => oauth_full_request_uri(http),
|
10
|
+
:consumer => consumer,
|
11
|
+
:token => token,
|
12
|
+
:scheme => 'header',
|
13
|
+
:signature_method => nil,
|
14
|
+
:nonce => nil,
|
15
|
+
:timestamp => nil }.merge(options)
|
16
|
+
|
17
|
+
@oauth_helper = OAuth::Client::Helper.new(self, options)
|
18
|
+
self.send("set_oauth_#{options[:scheme]}")
|
19
|
+
end
|
20
|
+
|
21
|
+
def signature_base_string(http, consumer = nil, token = nil, options = {})
|
22
|
+
options = { :request_uri => oauth_full_request_uri(http),
|
23
|
+
:consumer => consumer,
|
24
|
+
:token => token,
|
25
|
+
:scheme => 'header',
|
26
|
+
:signature_method => nil,
|
27
|
+
:nonce => nil,
|
28
|
+
:timestamp => nil }.merge(options)
|
29
|
+
|
30
|
+
OAuth::Client::Helper.new(self, options).signature_base_string
|
31
|
+
end
|
32
|
+
|
33
|
+
def oauth_helper
|
34
|
+
@oauth_helper
|
35
|
+
end
|
36
|
+
private
|
37
|
+
|
38
|
+
def oauth_full_request_uri(http)
|
39
|
+
uri = URI.parse(self.path)
|
40
|
+
uri.host = http.address
|
41
|
+
uri.port = http.port
|
42
|
+
if http.respond_to?(:use_ssl?)
|
43
|
+
uri.scheme = http.use_ssl? ? 'https' : 'http'
|
44
|
+
end
|
45
|
+
uri.to_s
|
46
|
+
end
|
47
|
+
|
48
|
+
def set_oauth_header
|
49
|
+
self['Authorization'] = @oauth_helper.header
|
50
|
+
end
|
51
|
+
|
52
|
+
# FIXME: if you're using a POST body and query string parameters, using this
|
53
|
+
# method will convert those parameters on the query string into parameters in
|
54
|
+
# the body. this is broken, and should be fixed.
|
55
|
+
def set_oauth_body
|
56
|
+
self.set_form_data(@oauth_helper.parameters_with_oauth)
|
57
|
+
params_with_sig = @oauth_helper.parameters.merge(:oauth_signature => @oauth_helper.signature)
|
58
|
+
self.set_form_data(params_with_sig)
|
59
|
+
end
|
60
|
+
|
61
|
+
def set_oauth_query_string
|
62
|
+
oauth_params_str = @oauth_helper.oauth_parameters.map { |k,v| "#{k}=#{v}" }.join("&")
|
63
|
+
|
64
|
+
uri = URI.parse(path)
|
65
|
+
if !uri.query || uri.query == ''
|
66
|
+
uri.query = oauth_params_str
|
67
|
+
else
|
68
|
+
uri.query = uri.query + "&" + oauth_params_str
|
69
|
+
end
|
70
|
+
|
71
|
+
@path = uri.to_s
|
72
|
+
|
73
|
+
@path << "&oauth_signature=#{escape(@oauth_helper.signature)}"
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,218 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'net/https'
|
3
|
+
require 'oauth/client/net_http'
|
4
|
+
module OAuth
|
5
|
+
class Consumer
|
6
|
+
|
7
|
+
@@default_options={
|
8
|
+
# Signature method used by server. Defaults to HMAC-SHA1
|
9
|
+
:signature_method => 'HMAC-SHA1',
|
10
|
+
|
11
|
+
# default paths on site. These are the same as the defaults set up by the generators
|
12
|
+
:request_token_path=>'/oauth/request_token',
|
13
|
+
:authorize_path=>'/oauth/authorize',
|
14
|
+
:access_token_path=>'/oauth/access_token',
|
15
|
+
|
16
|
+
# How do we send the oauth values to the server see
|
17
|
+
# http://oauth.net/core/1.0/#consumer_req_param for more info
|
18
|
+
#
|
19
|
+
# Possible values:
|
20
|
+
#
|
21
|
+
# :header - via the Authorize header (Default) ( option 1. in spec)
|
22
|
+
# :body - url form encoded in body of POST request ( option 2. in spec)
|
23
|
+
# :query_string - via the query part of the url ( option 3. in spec)
|
24
|
+
:scheme=>:header,
|
25
|
+
|
26
|
+
# Default http method used for OAuth Token Requests (defaults to :post)
|
27
|
+
:http_method=>:post,
|
28
|
+
|
29
|
+
:oauth_version=>"1.0"
|
30
|
+
}
|
31
|
+
|
32
|
+
attr_accessor :site,:options, :key, :secret,:http
|
33
|
+
|
34
|
+
|
35
|
+
# Create a new consumer instance by passing it a configuration hash:
|
36
|
+
#
|
37
|
+
# @consumer=OAuth::Consumer.new( key,secret,{
|
38
|
+
# :site=>"http://term.ie",
|
39
|
+
# :scheme=>:header,
|
40
|
+
# :http_method=>:post,
|
41
|
+
# :request_token_path=>"/oauth/example/request_token.php",
|
42
|
+
# :access_token_path=>"/oauth/example/access_token.php",
|
43
|
+
# :authorize_path=>"/oauth/example/authorize.php"
|
44
|
+
# })
|
45
|
+
#
|
46
|
+
# Start the process by requesting a token
|
47
|
+
#
|
48
|
+
# @request_token=@consumer.get_request_token
|
49
|
+
# session[:request_token]=@request_token
|
50
|
+
# redirect_to @request_token.authorize_url
|
51
|
+
#
|
52
|
+
# When user returns create an access_token
|
53
|
+
#
|
54
|
+
# @access_token=@request_token.get_access_token
|
55
|
+
# @photos=@access_token.get('/photos.xml')
|
56
|
+
#
|
57
|
+
#
|
58
|
+
|
59
|
+
def initialize(consumer_key,consumer_secret,options={})
|
60
|
+
# ensure that keys are symbols
|
61
|
+
@options=@@default_options.merge( options.inject({}) do |options, (key, value)|
|
62
|
+
options[key.to_sym] = value
|
63
|
+
options
|
64
|
+
end)
|
65
|
+
@key = consumer_key
|
66
|
+
@secret = consumer_secret
|
67
|
+
end
|
68
|
+
|
69
|
+
# The default http method
|
70
|
+
def http_method
|
71
|
+
@http_method||=@options[:http_method]||:post
|
72
|
+
end
|
73
|
+
|
74
|
+
# The HTTP object for the site. The HTTP Object is what you get when you do Net::HTTP.new
|
75
|
+
def http
|
76
|
+
@http ||= create_http
|
77
|
+
end
|
78
|
+
|
79
|
+
# Contains the root URI for this site
|
80
|
+
def uri(custom_uri=nil)
|
81
|
+
if custom_uri
|
82
|
+
@uri = custom_uri
|
83
|
+
@http = create_http # yike, oh well. less intrusive this way
|
84
|
+
else # if no custom passed, we use existing, which, if unset, is set to site uri
|
85
|
+
@uri ||= URI.parse(site)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# Makes a request to the service for a new OAuth::RequestToken
|
90
|
+
#
|
91
|
+
# @request_token=@consumer.get_request_token
|
92
|
+
#
|
93
|
+
def get_request_token(request_options={}, *arguments)
|
94
|
+
response=token_request(http_method,request_token_path, nil, request_options, *arguments)
|
95
|
+
OAuth::RequestToken.new(self,response[:oauth_token],response[:oauth_token_secret])
|
96
|
+
end
|
97
|
+
|
98
|
+
# Creates, signs and performs an http request.
|
99
|
+
# It's recommended to use the OAuth::Token classes to set this up correctly.
|
100
|
+
# The arguments parameters are a hash or string encoded set of parameters if it's a post request as well as optional http headers.
|
101
|
+
#
|
102
|
+
# @consumer.request(:get,'/people',@token,{:scheme=>:query_string})
|
103
|
+
# @consumer.request(:post,'/people',@token,{},@person.to_xml,{ 'Content-Type' => 'application/xml' })
|
104
|
+
#
|
105
|
+
def request(http_method,path, token=nil,request_options={},*arguments)
|
106
|
+
http.request(create_signed_request(http_method,path,token,request_options,*arguments))
|
107
|
+
end
|
108
|
+
|
109
|
+
# Creates and signs an http request.
|
110
|
+
# It's recommended to use the Token classes to set this up correctly
|
111
|
+
def create_signed_request(http_method,path, token=nil,request_options={},*arguments)
|
112
|
+
request=create_http_request(http_method,path,*arguments)
|
113
|
+
sign!(request,token,request_options)
|
114
|
+
request
|
115
|
+
end
|
116
|
+
|
117
|
+
# Creates a request and parses the result as url_encoded. This is used internally for the RequestToken and AccessToken requests.
|
118
|
+
def token_request(http_method,path,token=nil,request_options={},*arguments)
|
119
|
+
response=request(http_method,path,token,request_options,*arguments)
|
120
|
+
if response.code=="200"
|
121
|
+
CGI.parse(response.body).inject({}){|h,(k,v)| h[k.to_sym]=v.first;h}
|
122
|
+
else
|
123
|
+
response.error!
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
# Sign the Request object. Use this if you have an externally generated http request object you want to sign.
|
128
|
+
def sign!(request,token=nil, request_options = {})
|
129
|
+
request.oauth!(http, self, token, options.merge(request_options))
|
130
|
+
end
|
131
|
+
|
132
|
+
# Return the signature_base_string
|
133
|
+
def signature_base_string(request,token=nil, request_options = {})
|
134
|
+
request.signature_base_string(http, self, token, options.merge(request_options))
|
135
|
+
end
|
136
|
+
|
137
|
+
def site
|
138
|
+
@options[:site].to_s
|
139
|
+
end
|
140
|
+
|
141
|
+
def scheme
|
142
|
+
@options[:scheme]
|
143
|
+
end
|
144
|
+
|
145
|
+
def request_token_path
|
146
|
+
@options[:request_token_path]
|
147
|
+
end
|
148
|
+
|
149
|
+
def authorize_path
|
150
|
+
@options[:authorize_path]
|
151
|
+
end
|
152
|
+
|
153
|
+
def access_token_path
|
154
|
+
@options[:access_token_path]
|
155
|
+
end
|
156
|
+
|
157
|
+
# TODO this is ugly, rewrite
|
158
|
+
def request_token_url
|
159
|
+
@options[:request_token_url]||site+request_token_path
|
160
|
+
end
|
161
|
+
|
162
|
+
def authorize_url
|
163
|
+
@options[:authorize_url]||site+authorize_path
|
164
|
+
end
|
165
|
+
|
166
|
+
def access_token_url
|
167
|
+
@options[:access_token_url]||site+access_token_path
|
168
|
+
end
|
169
|
+
|
170
|
+
protected
|
171
|
+
|
172
|
+
#Instantiates the http object
|
173
|
+
def create_http
|
174
|
+
http_object=Net::HTTP.new(uri.host, uri.port)
|
175
|
+
http_object.use_ssl = true if uri.scheme=="https"
|
176
|
+
http_object
|
177
|
+
end
|
178
|
+
|
179
|
+
# create the http request object for a given http_method and path
|
180
|
+
def create_http_request(http_method,path,*arguments)
|
181
|
+
http_method=http_method.to_sym
|
182
|
+
if [:post,:put].include?(http_method)
|
183
|
+
data=arguments.shift
|
184
|
+
end
|
185
|
+
headers=(arguments.first.is_a?(Hash) ? arguments.shift : {})
|
186
|
+
case http_method
|
187
|
+
when :post
|
188
|
+
request=Net::HTTP::Post.new(path,headers)
|
189
|
+
request["Content-Length"]=0 # Default to 0
|
190
|
+
when :put
|
191
|
+
request=Net::HTTP::Put.new(path,headers)
|
192
|
+
request["Content-Length"]=0 # Default to 0
|
193
|
+
when :get
|
194
|
+
request=Net::HTTP::Get.new(path,headers)
|
195
|
+
when :delete
|
196
|
+
request=Net::HTTP::Delete.new(path,headers)
|
197
|
+
when :head
|
198
|
+
request=Net::HTTP::Head.new(path,headers)
|
199
|
+
else
|
200
|
+
raise ArgumentError, "Don't know how to handle http_method: :#{http_method.to_s}"
|
201
|
+
end
|
202
|
+
if data.is_a?(Hash)
|
203
|
+
request.set_form_data(data)
|
204
|
+
elsif data
|
205
|
+
request.body=data.to_s
|
206
|
+
request["Content-Length"]=request.body.length
|
207
|
+
end
|
208
|
+
request
|
209
|
+
end
|
210
|
+
|
211
|
+
# Unset cached http instance because it cannot be marshalled when
|
212
|
+
# it has already been used and use_ssl is set to true
|
213
|
+
def marshal_dump(*args)
|
214
|
+
@http = nil
|
215
|
+
self
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|