Added support for two-legged OAuth.

git-svn-id: https://google-api-ruby-client.googlecode.com/svn/trunk@92 c1d61fac-ed7f-fcc1-18f7-ff78120a04ef
This commit is contained in:
Bob Aman 2010-10-22 00:36:23 +00:00
parent d346426d4d
commit 20eff1df7d
4 changed files with 185 additions and 96 deletions

View File

@ -91,6 +91,16 @@ HTML
"--scope <scope>", String, "Set the OAuth scope") do |s| "--scope <scope>", String, "Set the OAuth scope") do |s|
options[:scope] = s options[:scope] = s
end end
opts.on(
"--client-key <key>", String,
"Set the 2-legged OAuth key") do |k|
options[:client_credential_key] = k
end
opts.on(
"--client-secret <secret>", String,
"Set the 2-legged OAuth secret") do |s|
options[:client_credential_secret] = s
end
opts.on( opts.on(
"-s", "--service <name>", String, "-s", "--service <name>", String,
"Perform discovery on service") do |s| "Perform discovery on service") do |s|
@ -117,6 +127,16 @@ HTML
end end
options[:content_type] = f options[:content_type] = f
end end
opts.on(
"-u", "--uri <uri>", String,
"Sets the URI to perform a request against") do |u|
options[:uri] = u
end
opts.on(
"-m", "--method <method>", String,
"Sets the HTTP method to use for the request") do |m|
options[:http_method] = m
end
opts.on("-v", "--verbose", "Run verbosely") do |v| opts.on("-v", "--verbose", "Run verbosely") do |v|
options[:verbose] = v options[:verbose] = v
@ -142,15 +162,43 @@ HTML
def parse! def parse!
self.parser.parse!(self.argv) self.parser.parse!(self.argv)
self.send(self.command.gsub(/-/, "_").to_sym) symbol = self.command.gsub(/-/, "_").to_sym
if !COMMANDS.include?(symbol)
STDERR.puts("Invalid command: #{self.command}")
exit(1)
end end
self.send(symbol)
end
COMMANDS = [
:oauth_login,
:list,
:execute,
:irb,
:fuzz
]
def oauth_login def oauth_login
require 'signet/oauth_1/client' require 'signet/oauth_1/client'
require 'launchy' require 'launchy'
require 'yaml' require 'yaml'
if options[:client_credential_key] &&
options[:client_credential_secret]
scope = options[:scope]
config = {
"scope" => nil,
"client_credential_key" => options[:client_credential_key],
"client_credential_secret" => options[:client_credential_secret],
"token_credential_key" => nil,
"token_credential_secret" => nil
}
config_file = File.expand_path('~/.google-api.yaml')
open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
exit(0)
else
$verifier = nil $verifier = nil
logger = WEBrick::Log.new('/dev/null') # TODO(bobaman): Cross-platform? # TODO(bobaman): Cross-platform?
logger = WEBrick::Log.new('/dev/null')
server = WEBrick::HTTPServer.new( server = WEBrick::HTTPServer.new(
:Port => OAUTH_SERVER_PORT, :Port => OAUTH_SERVER_PORT,
:Logger => logger, :Logger => logger,
@ -194,15 +242,20 @@ HTML
oauth_client.fetch_token_credential!(:verifier => $verifier) oauth_client.fetch_token_credential!(:verifier => $verifier)
config = { config = {
"scope" => scope, "scope" => scope,
"client_credential_key" => oauth_client.client_credential_key, "client_credential_key" =>
"client_credential_secret" => oauth_client.client_credential_secret, oauth_client.client_credential_key,
"token_credential_key" => oauth_client.token_credential_key, "client_credential_secret" =>
"token_credential_secret" => oauth_client.token_credential_secret oauth_client.client_credential_secret,
"token_credential_key" =>
oauth_client.token_credential_key,
"token_credential_secret" =>
oauth_client.token_credential_secret
} }
config_file = File.expand_path('~/.google-api.yaml') config_file = File.expand_path('~/.google-api.yaml')
open(config_file, 'w') { |file| file.write(YAML.dump(config)) } open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
exit(0) exit(0)
end end
end
def list def list
service_name = options[:service_name] service_name = options[:service_name]
@ -224,16 +277,20 @@ HTML
require 'yaml' require 'yaml'
config_file = File.expand_path('~/.google-api.yaml') config_file = File.expand_path('~/.google-api.yaml')
signed = File.exist?(config_file) signed = File.exist?(config_file)
if !self.rpcname
STDERR.puts('No rpcname supplied.') # Setup HTTP request data
exit(1) request_body = ''
input_streams, _, _ = IO.select([STDIN], [], [], 0)
request_body = STDIN.read || '' if input_streams
headers = []
if options[:content_type]
headers << ['Content-Type', options[:content_type]]
elsif request_body
# Default to JSON
headers << ['Content-Type', 'application/json']
end end
service_name = options[:service_name] || self.rpcname[/^([^\.]+)\./, 1]
client = Google::APIClient.new( configure_authorization = lambda do |client|
:service => service_name,
:authorization => :oauth_1
)
if signed
if !client.authorization.kind_of?(Signet::OAuth1::Client) if !client.authorization.kind_of?(Signet::OAuth1::Client)
STDERR.puts( STDERR.puts(
"Unexpected authorization mechanism: " + "Unexpected authorization mechanism: " +
@ -251,6 +308,38 @@ HTML
client.authorization.token_credential_secret = client.authorization.token_credential_secret =
config["token_credential_secret"] config["token_credential_secret"]
end end
if options[:uri]
# Make request with URI manually specified
uri = Addressable::URI.parse(options[:uri])
if uri.relative?
STDERR.puts('URI may not be relative.')
exit(1)
end
method = options[:http_method]
method ||= request_body == '' ? 'GET' : 'POST'
method.upcase!
client = Google::APIClient.new(:authorization => :two_legged_oauth_1)
configure_authorization.call(client) if signed
request = [method, uri.to_str, headers, [request_body]]
request = client.sign_request(request)
response = client.transmit_request(request)
status, headers, body = response
puts body
exit(0)
else
# Make request with URI generated from template and parameters
if !self.rpcname
STDERR.puts('No rpcname supplied.')
exit(1)
end
service_name =
options[:service_name] || self.rpcname[/^([^\.]+)\./, 1]
client = Google::APIClient.new(
:service => service_name,
:authorization => :oauth_1
)
configure_authorization.call(client) if signed
service_version = service_version =
options[:service_version] || options[:service_version] ||
client.latest_service_version(service_name).version client.latest_service_version(service_name).version
@ -268,16 +357,6 @@ HTML
accu[name] = value accu[name] = value
accu accu
end end
request_body = ''
input_streams, _, _ = IO.select([STDIN], [], [], 0)
request_body = STDIN.read || '' if input_streams
headers = []
if options[:content_type]
headers << ['Content-Type', options[:content_type]]
elsif request_body
# Default to JSON
headers << ['Content-Type', 'application/json']
end
begin begin
response = client.execute( response = client.execute(
method, parameters, request_body, headers, {:signed => signed} method, parameters, request_body, headers, {:signed => signed}
@ -290,6 +369,7 @@ HTML
exit(1) exit(1)
end end
end end
end
def irb def irb
require 'signet/oauth_1/client' require 'signet/oauth_1/client'

View File

@ -71,6 +71,14 @@ module Google
:client_credential_key => 'anonymous', :client_credential_key => 'anonymous',
:client_credential_secret => 'anonymous' :client_credential_secret => 'anonymous'
) )
when :two_legged_oauth_1, :two_legged_oauth
require 'signet/oauth_1/client'
# NOTE: Do not rely on this default value, as it may change
@options[:authorization] = Signet::OAuth1::Client.new(
:client_credential_key => nil,
:client_credential_secret => nil,
:two_legged => true
)
when nil when nil
# No authorization mechanism # No authorization mechanism
else else

View File

@ -19,7 +19,7 @@ namespace :gem do
s.extra_rdoc_files = %w( README ) s.extra_rdoc_files = %w( README )
s.rdoc_options.concat ['--main', 'README'] s.rdoc_options.concat ['--main', 'README']
s.add_runtime_dependency('signet', '>= 0.1.3') s.add_runtime_dependency('signet', '>= 0.1.4')
s.add_runtime_dependency('addressable', '>= 2.2.2') s.add_runtime_dependency('addressable', '>= 2.2.2')
s.add_runtime_dependency('httpadapter', '>= 0.2.0') s.add_runtime_dependency('httpadapter', '>= 0.2.0')
s.add_runtime_dependency('json', '>= 1.1.9') s.add_runtime_dependency('json', '>= 1.1.9')

View File

@ -20,7 +20,8 @@ namespace :spec do
'--exclude', 'spec', '--exclude', 'spec',
'--exclude', '\\.rvm\\/gems', '--exclude', '\\.rvm\\/gems',
'--exclude', '1\\.8\\/gems', '--exclude', '1\\.8\\/gems',
'--exclude', '1\\.9\\/gems' '--exclude', '1\\.9\\/gems',
'--exclude', '\\.rvm'
] ]
end end