Updated OAuth to actually perform authorization stuff.
git-svn-id: https://google-api-ruby-client.googlecode.com/svn/trunk@8 c1d61fac-ed7f-fcc1-18f7-ff78120a04ef
This commit is contained in:
parent
10f9db8ac6
commit
e4b6f8528e
|
@ -26,6 +26,9 @@ module Google #:nodoc:
|
||||||
'https://www.google.com/accounts/OAuthAuthorizeToken',
|
'https://www.google.com/accounts/OAuthAuthorizeToken',
|
||||||
:access_token_uri =>
|
:access_token_uri =>
|
||||||
'https://www.google.com/accounts/OAuthGetAccessToken',
|
'https://www.google.com/accounts/OAuthGetAccessToken',
|
||||||
|
:scopes => [],
|
||||||
|
:callback => OAuth::OUT_OF_BAND,
|
||||||
|
:displayname => nil,
|
||||||
:consumer_key => "anonymous",
|
:consumer_key => "anonymous",
|
||||||
:consumer_secret => "anonymous"
|
:consumer_secret => "anonymous"
|
||||||
}.merge(options)
|
}.merge(options)
|
||||||
|
@ -55,6 +58,49 @@ module Google #:nodoc:
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def request_token
|
||||||
|
oauth_parameters = {
|
||||||
|
:oauth_callback => @options[:callback]
|
||||||
|
}
|
||||||
|
app_parameters = {
|
||||||
|
:scope => @options[:scopes].join(" ")
|
||||||
|
}
|
||||||
|
if @options[:displayname]
|
||||||
|
app_parameters[:xoauth_displayname] = @options[:displayname]
|
||||||
|
end
|
||||||
|
return @request_token ||= @oauth_consumer.get_request_token(
|
||||||
|
oauth_parameters,
|
||||||
|
app_parameters
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def request_token=(new_request_token)
|
||||||
|
if new_request_token.kind_of?(OAuth::RequestToken)
|
||||||
|
@request_token = new_request_token
|
||||||
|
else
|
||||||
|
raise TypeError,
|
||||||
|
"Expected OAuth::RequestToken, got #{new_request_token.class}."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def access_token
|
||||||
|
return @access_token ||=
|
||||||
|
@oauth_consumer.get_access_token(self.request_token)
|
||||||
|
end
|
||||||
|
|
||||||
|
def access_token=(new_access_token)
|
||||||
|
if new_access_token.kind_of?(OAuth::AccessToken)
|
||||||
|
@access_token = new_access_token
|
||||||
|
else
|
||||||
|
raise TypeError,
|
||||||
|
"Expected OAuth::AccessToken, got #{new_access_token.class}."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def scopes
|
||||||
|
return @options[:scopes]
|
||||||
|
end
|
||||||
|
|
||||||
def consumer_key
|
def consumer_key
|
||||||
return @oauth_consumer.key
|
return @oauth_consumer.key
|
||||||
end
|
end
|
||||||
|
@ -67,10 +113,14 @@ module Google #:nodoc:
|
||||||
return @oauth_consumer.request_token_url
|
return @oauth_consumer.request_token_url
|
||||||
end
|
end
|
||||||
|
|
||||||
def authorization_uri
|
def authorization_endpoint_uri
|
||||||
return @oauth_consumer.authorize_url
|
return @oauth_consumer.authorize_url
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def authorization_uri(parameters={})
|
||||||
|
return self.request_token.authorize_url(parameters)
|
||||||
|
end
|
||||||
|
|
||||||
def access_token_uri
|
def access_token_uri
|
||||||
return @oauth_consumer.access_token_url
|
return @oauth_consumer.access_token_url
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,6 +12,9 @@
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
require "spec_helper"
|
||||||
|
|
||||||
|
require "oauth"
|
||||||
require "google/api_client/auth/oauth_1"
|
require "google/api_client/auth/oauth_1"
|
||||||
|
|
||||||
describe Google::APIClient::OAuth1, "in the default configuration" do
|
describe Google::APIClient::OAuth1, "in the default configuration" do
|
||||||
|
@ -25,7 +28,7 @@ describe Google::APIClient::OAuth1, "in the default configuration" do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should have the correct authorization_uri" do
|
it "should have the correct authorization_uri" do
|
||||||
@oauth.authorization_uri.should ==
|
@oauth.authorization_endpoint_uri.should ==
|
||||||
"https://www.google.com/accounts/OAuthAuthorizeToken"
|
"https://www.google.com/accounts/OAuthAuthorizeToken"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -41,4 +44,16 @@ describe Google::APIClient::OAuth1, "in the default configuration" do
|
||||||
it "should have the correct consumer_secret" do
|
it "should have the correct consumer_secret" do
|
||||||
@oauth.consumer_secret.should == "anonymous"
|
@oauth.consumer_secret.should == "anonymous"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should allow the request_token to be set manually" do
|
||||||
|
@oauth.request_token = OAuth::RequestToken.new(@oauth, "key", "secret")
|
||||||
|
@oauth.request_token.token.should == "key"
|
||||||
|
@oauth.request_token.secret.should == "secret"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not allow the request_token to be set to bogus value" do
|
||||||
|
(lambda do
|
||||||
|
@oauth.request_token = 42
|
||||||
|
end).should raise_error(TypeError)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
# Copyright 2010 Google Inc.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
require "spec_helper"
|
||||||
|
|
||||||
|
require "oauth"
|
||||||
|
require "google/api_client/auth/oauth_1"
|
||||||
|
require "addressable/uri"
|
||||||
|
|
||||||
|
describe Google::APIClient::OAuth1, "configured for use with Buzz" do
|
||||||
|
before do
|
||||||
|
@oauth = Google::APIClient::OAuth1.new(
|
||||||
|
:authorization_uri =>
|
||||||
|
"https://www.google.com/buzz/api/auth/OAuthAuthorizeToken",
|
||||||
|
:scopes => ["https://www.googleapis.com/auth/buzz"]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be able to get a request token" do
|
||||||
|
@oauth.request_token.token.should =~ /^[a-zA-Z0-9\/\-\_\+]+$/
|
||||||
|
@oauth.request_token.secret.should =~ /^[a-zA-Z0-9\/\-\_\+]+$/
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should issue only a single request token" do
|
||||||
|
@oauth.request_token.token.should == @oauth.request_token.token
|
||||||
|
@oauth.request_token.secret.should == @oauth.request_token.secret
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should build the correct authorization URI" do
|
||||||
|
icon_uri = "http://www.google.com/images/icons/feature/padlock-g128.png"
|
||||||
|
uri = @oauth.authorization_uri(
|
||||||
|
:domain => @oauth.consumer_key,
|
||||||
|
:iconUrl => icon_uri,
|
||||||
|
:scope => @oauth.scopes.join(" ")
|
||||||
|
)
|
||||||
|
uri.should =~
|
||||||
|
/^https:\/\/www.google.com\/buzz\/api\/auth\/OAuthAuthorizeToken/
|
||||||
|
Addressable::URI.unencode(uri).should =~
|
||||||
|
Regexp.new(Regexp.escape(@oauth.request_token.token))
|
||||||
|
Addressable::URI.unencode(uri).should =~
|
||||||
|
Regexp.new(Regexp.escape(icon_uri))
|
||||||
|
for scope in @oauth.scopes
|
||||||
|
Addressable::URI.unencode(uri).should =~
|
||||||
|
Regexp.new(Regexp.escape(scope))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Not much we can do to test any further into the OAuth flow
|
||||||
|
end
|
|
@ -3,5 +3,3 @@ lib_dir = File.expand_path(File.join(spec_dir, "../lib"))
|
||||||
|
|
||||||
$:.unshift(lib_dir)
|
$:.unshift(lib_dir)
|
||||||
$:.uniq!
|
$:.uniq!
|
||||||
|
|
||||||
require "api_client"
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ require 'spec/rake/verify_rcov'
|
||||||
namespace :spec do
|
namespace :spec do
|
||||||
Spec::Rake::SpecTask.new(:rcov) do |t|
|
Spec::Rake::SpecTask.new(:rcov) do |t|
|
||||||
t.spec_files = FileList['spec/**/*_spec.rb']
|
t.spec_files = FileList['spec/**/*_spec.rb']
|
||||||
t.spec_opts = ['--color', '--format', 'specdoc']
|
t.spec_opts = ['--require', 'rubygems', '--color', '--format', 'specdoc']
|
||||||
if RCOV_ENABLED
|
if RCOV_ENABLED
|
||||||
t.rcov = true
|
t.rcov = true
|
||||||
else
|
else
|
||||||
|
@ -16,9 +16,17 @@ namespace :spec do
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
Spec::Rake::SpecTask.new(:normal) do |t|
|
Spec::Rake::SpecTask.new(:all) do |t|
|
||||||
t.spec_files = FileList['spec/**/*_spec.rb']
|
t.spec_files = FileList['spec/**/*_spec.rb']
|
||||||
t.spec_opts = ['--color', '--format', 'specdoc']
|
t.spec_opts = ['--require', 'rubygems', '--color', '--format', 'specdoc']
|
||||||
|
t.rcov = false
|
||||||
|
end
|
||||||
|
|
||||||
|
Spec::Rake::SpecTask.new(:fast) do |t|
|
||||||
|
t.spec_files = FileList['spec/**/*_spec.rb'].exclude(
|
||||||
|
'spec/**/*_slow_spec.rb'
|
||||||
|
)
|
||||||
|
t.spec_opts = ['--require', 'rubygems', '--color', '--format', 'specdoc']
|
||||||
t.rcov = false
|
t.rcov = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -56,8 +64,8 @@ if RCOV_ENABLED
|
||||||
desc "Alias to spec:verify"
|
desc "Alias to spec:verify"
|
||||||
task "spec" => "spec:verify"
|
task "spec" => "spec:verify"
|
||||||
else
|
else
|
||||||
desc "Alias to spec:normal"
|
desc "Alias to spec:all"
|
||||||
task "spec" => "spec:normal"
|
task "spec" => "spec:all"
|
||||||
end
|
end
|
||||||
|
|
||||||
task "clobber" => ["spec:clobber_rcov"]
|
task "clobber" => ["spec:clobber_rcov"]
|
||||||
|
|
Loading…
Reference in New Issue