From 0d45cbe27b77a6641fc5c0ea2d3fe5de93c8c6af Mon Sep 17 00:00:00 2001 From: "bobaman@google.com" Date: Sat, 14 Aug 2010 00:53:57 +0000 Subject: [PATCH] Updated OAuth1 configuration code. Added docs. git-svn-id: https://google-api-ruby-client.googlecode.com/svn/trunk@9 c1d61fac-ed7f-fcc1-18f7-ff78120a04ef --- lib/google/api_client/auth/oauth_1.rb | 141 ++++++++++++++++-- .../auth/services/buzz_slow_spec.rb | 15 +- 2 files changed, 136 insertions(+), 20 deletions(-) diff --git a/lib/google/api_client/auth/oauth_1.rb b/lib/google/api_client/auth/oauth_1.rb index 4b52a9556..36271718c 100644 --- a/lib/google/api_client/auth/oauth_1.rb +++ b/lib/google/api_client/auth/oauth_1.rb @@ -18,20 +18,70 @@ require "oauth" module Google #:nodoc: class APIClient #:nodoc: class OAuth1 - def initialize(options={}) - @options = { - :request_token_uri => - 'https://www.google.com/accounts/OAuthGetRequestToken', + ## + # The default OAuth 1.0a configuration values. These may be overrided + # simply by passing in the same key to the constructor. + DEFAULTS = { + :request_token_uri => + 'https://www.google.com/accounts/OAuthGetRequestToken', + :authorization_uri => + 'https://www.google.com/accounts/OAuthAuthorizeToken', + :access_token_uri => + 'https://www.google.com/accounts/OAuthGetAccessToken', + :scopes => [], + :callback => OAuth::OUT_OF_BAND, + :display_name => nil, + :consumer_key => "anonymous", + :consumer_secret => "anonymous" + } + + ## + # A set of default configuration values specific to each service. These + # may be overrided simply by passing in the same key to the constructor. + SERVICE_DEFAULTS = { + :buzz => { :authorization_uri => - 'https://www.google.com/accounts/OAuthAuthorizeToken', - :access_token_uri => - 'https://www.google.com/accounts/OAuthGetAccessToken', - :scopes => [], - :callback => OAuth::OUT_OF_BAND, - :displayname => nil, - :consumer_key => "anonymous", - :consumer_secret => "anonymous" - }.merge(options) + 'https://www.google.com/buzz/api/auth/OAuthAuthorizeToken', + :scopes => ["https://www.googleapis.com/auth/buzz"] + } + } + + ## + # Creates a new OAuth 1.0a handler. This object obtains the tokens from + # the provider and handles signing any requests to the API. + # + # @param [Hash] options + # The configuration options. + # :request_token_uri:: + # The OAuth endpoint for obtaining a request token. + # :authorization_uri:: + # The OAuth endpoint for obtaining user permission. + # :access_token_uri:: + # The OAuth endpoint for obtaining an access token. + # :scopes:: + # An Array of scopes that define the access being + # requested to the API. + # :callback:: + # The URI the user will be redirected to if access is granted to the + # API. For development purposes, the special value + # OAuth::OUT_OF_BAND may also be used. + # :display_name:: + # A human-readable service name to present to the user when they + # visit the :authorization_uri. + # :consumer_key:: + # The consumer key you registered with the Google Accounts API. + # :consumer_secret:: + # The consumer secret issued to you when you registered with the + # Google Accounts API. + # + # @return [Google::APIClient::OAuth1] The OAuth 1.0a handler. + def initialize(options={}) + if options[:service] + @options = DEFAULTS.merge(SERVICE_DEFAULTS[options[:service]]) + else + @options = DEFAULTS.clone + end + @options.merge!(options) @options[:request_token_uri] = Addressable::URI.parse(@options[:request_token_uri]) @options[:authorization_uri] = @@ -58,6 +108,11 @@ module Google #:nodoc: ) end + ## + # Returns the current request token. Obtains a new request token if + # one hasn't already been obtained. + # + # @return [OAuth::RequestToken] The request token. def request_token oauth_parameters = { :oauth_callback => @options[:callback] @@ -65,8 +120,8 @@ module Google #:nodoc: app_parameters = { :scope => @options[:scopes].join(" ") } - if @options[:displayname] - app_parameters[:xoauth_displayname] = @options[:displayname] + if @options[:display_name] + app_parameters[:xoauth_displayname] = @options[:display_name] end return @request_token ||= @oauth_consumer.get_request_token( oauth_parameters, @@ -74,6 +129,10 @@ module Google #:nodoc: ) end + ## + # Sets the request token for the handler. + # + # @param [OAuth::RequestToken] new_request_token The request token. def request_token=(new_request_token) if new_request_token.kind_of?(OAuth::RequestToken) @request_token = new_request_token @@ -83,11 +142,21 @@ module Google #:nodoc: end end + ## + # Returns the current access token. Obtains a new access token if + # one hasn't already been obtained. An request token must have already + # been obtained and authorized or this method will fail. + # + # @return [OAuth::AccessToken] The access token. def access_token return @access_token ||= @oauth_consumer.get_access_token(self.request_token) end + ## + # Sets the access token for the handler. + # + # @param [OAuth::AccessToken] new_access_token The access token. def access_token=(new_access_token) if new_access_token.kind_of?(OAuth::AccessToken) @access_token = new_access_token @@ -97,30 +166,72 @@ module Google #:nodoc: end end + ## + # Returns the list of scopes for the handler. + # + # @return [Array] An Array of access scopes. def scopes return @options[:scopes] end + ## + # Returns the consumer key. + # + # @return [String] + # The consumer key you registered with the Google Accounts API. def consumer_key return @oauth_consumer.key end + ## + # Returns the consumer key. + # + # @return [String] + # The consumer secret issued to you when you registered with the + # Google Accounts API. def consumer_secret return @oauth_consumer.secret end + ## + # Returns the request token URI. + # + # @return [String] + # The OAuth endpoint for obtaining a request token. def request_token_uri return @oauth_consumer.request_token_url end + ## + # Returns the authorization endpoint URI. This URI is used to construct + # the {#authorization_uri}. + # + # @return [String] + # The OAuth endpoint for obtaining user permission. def authorization_endpoint_uri return @oauth_consumer.authorize_url end + ## + # Builds the authorization URI that the user will be redirected to. + # Note that this value is derived from the + # {#authorization_endpoint_uri}. + # + # @param [Hash] parameters + # The extra URI query parameters appended to the + # {#authorization_endpoint_uri}. + # + # @return [String] + # The URI to redirect the user to to obtain permission. def authorization_uri(parameters={}) return self.request_token.authorize_url(parameters) end + ## + # Returns the access token URI. + # + # @return [String] + # The OAuth endpoint for obtaining an access token. def access_token_uri return @oauth_consumer.access_token_url end diff --git a/spec/google/api_client/auth/services/buzz_slow_spec.rb b/spec/google/api_client/auth/services/buzz_slow_spec.rb index 51e6050f4..903f5d502 100644 --- a/spec/google/api_client/auth/services/buzz_slow_spec.rb +++ b/spec/google/api_client/auth/services/buzz_slow_spec.rb @@ -20,11 +20,16 @@ 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"] - ) + @oauth = Google::APIClient::OAuth1.new(:service => :buzz) + end + + it "should have the correct authorization_uri" do + @oauth.authorization_endpoint_uri.should == + "https://www.google.com/buzz/api/auth/OAuthAuthorizeToken" + end + + it "should have the correct scope" do + @oauth.scopes.should include("https://www.googleapis.com/auth/buzz") end it "should be able to get a request token" do