Skip to content

Commit

Permalink
Allow Hash objects to be used to initialize the authorization URI.
Browse files Browse the repository at this point in the history
  • Loading branch information
sporkmonger authored and Bob Aman committed Sep 28, 2013
1 parent f6f146b commit c3efb8b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 39 deletions.
18 changes: 10 additions & 8 deletions lib/signet/oauth_1/client.rb
Expand Up @@ -127,8 +127,10 @@ def authorization_uri(options={})
# The authorization URI.
def authorization_uri=(new_authorization_uri)
if new_authorization_uri != nil
new_authorization_uri =
Addressable::URI.parse(new_authorization_uri)
new_authorization_uri = Addressable::URI.send(
new_authorization_uri.kind_of?(Hash) ? :new : :parse,
new_authorization_uri
)
@authorization_uri = new_authorization_uri
else
@authorization_uri = nil
Expand Down Expand Up @@ -859,7 +861,7 @@ def generate_authenticated_request(options={})
:realm => nil,
:connection => Faraday.default_connection
}.merge(options)

if options[:request].kind_of?(Faraday::Request)
request = options[:request]
else
Expand Down Expand Up @@ -902,16 +904,16 @@ def generate_authenticated_request(options={})
req.body = body
end
end

parameters = ::Signet::OAuth1.unsigned_resource_parameters(
:client_credential_key => self.client_credential_key,
:token_credential_key => self.token_credential_key,
:signature_method => options[:signature_method],
:two_legged => self.two_legged
)

env = request.to_env(options[:connection])

content_type = request['Content-Type'].to_s
content_type = content_type.split(';', 2).first if content_type.index(';')
if request.method == :post && content_type == 'application/x-www-form-urlencoded'
Expand All @@ -923,7 +925,7 @@ def generate_authenticated_request(options={})
post_parameters = Addressable::URI.form_unencode(env[:body])
parameters = parameters.concat(post_parameters)
end

# No need to attach URI query parameters, the .sign_parameters
# method takes care of that automatically.
signature = ::Signet::OAuth1.sign_parameters(
Expand All @@ -933,7 +935,7 @@ def generate_authenticated_request(options={})
self.client_credential_secret,
self.token_credential_secret
)

parameters << ['oauth_signature', signature]
request['Authorization'] = ::Signet::OAuth1.generate_authorization_header(
parameters, options[:realm])
Expand Down
50 changes: 26 additions & 24 deletions lib/signet/oauth_2/client.rb
Expand Up @@ -210,12 +210,12 @@ def update_token!(options={})

self.expires_in = options["expires_in"] if options.has_key?("expires_in")
self.expires_at = options["expires_at"] if options.has_key?("expires_at")

# By default, the token is issued at `Time.now` when `expires_in` is
# set, but this can be used to supply a more precise time.
self.issued_at = options["issued_at"] if options.has_key?("issued_at")
self.access_token = options["access_token"] if options.has_key?("access_token")
self.issued_at = options["issued_at"] if options.has_key?("issued_at")

self.access_token = options["access_token"] if options.has_key?("access_token")
self.refresh_token = options["refresh_token"] if options.has_key?("refresh_token")
self.id_token = options["id_token"] if options.has_key?("id_token")

Expand Down Expand Up @@ -268,12 +268,14 @@ def authorization_uri(options={})
##
# Sets the authorization URI for this client.
#
# @param [Addressable::URI, String, #to_str] new_authorization_uri
# @param [Addressable::URI, Hash, String, #to_str] new_authorization_uri
# The authorization URI.
def authorization_uri=(new_authorization_uri)
if new_authorization_uri != nil
new_authorization_uri =
Addressable::URI.parse(new_authorization_uri)
new_authorization_uri = Addressable::URI.send(
new_authorization_uri.kind_of?(Hash) ? :new : :parse,
new_authorization_uri
)
@authorization_uri = new_authorization_uri
else
@authorization_uri = nil
Expand Down Expand Up @@ -423,7 +425,7 @@ def redirect_uri
# The redirect URI.
def redirect_uri=(new_redirect_uri)
new_redirect_uri = Addressable::URI.parse(new_redirect_uri)
#TODO - Better solution to allow google postmessage flow. For now, make an exception to the spec.
#TODO - Better solution to allow google postmessage flow. For now, make an exception to the spec.
if new_redirect_uri == nil|| new_redirect_uri.absolute? || uri_is_postmessage?(new_redirect_uri)
@redirect_uri = new_redirect_uri
else
Expand Down Expand Up @@ -525,10 +527,10 @@ def principal
def principal=(new_person)
@principal = new_person
end

alias_method :person, :principal
alias_method :person=, :principal=

##
# Returns the number of seconds assertions are valid for
# Used only by the assertion grant type.
Expand All @@ -547,8 +549,8 @@ def expiry
def expiry=(new_expiry)
@expiry = new_expiry
end


##
# Returns the signing key associated with this client.
# Used only by the assertion grant type.
Expand All @@ -567,14 +569,14 @@ def signing_key
def signing_key=(new_key)
@signing_key = new_key
end

##
# Algorithm used for signing JWTs
# @return [String] Signing algorithm
def signing_algorithm
self.signing_key.is_a?(String) ? "HS256" : "RS256"
end

##
# Returns the set of extension parameters used by the client.
# Used only by extension access grant types.
Expand Down Expand Up @@ -760,8 +762,8 @@ def expires_at=(new_expires_at)
def expired?
return self.expires_at != nil && Time.now >= self.expires_at
end


##
# Removes all credentials from the client.
def clear_credentials!
Expand Down Expand Up @@ -816,7 +818,7 @@ def grant_type=(new_grant_type)
end

def to_jwt(options={})
now = Time.new
now = Time.new
skew = options[:skew] || 60
assertion = {
"iss" => self.issuer,
Expand All @@ -828,7 +830,7 @@ def to_jwt(options={})
assertion['prn'] = self.person unless self.person.nil?
JWT.encode(assertion, self.signing_key, self.signing_algorithm)
end

##
# Generates a request for token credentials.
#
Expand Down Expand Up @@ -923,12 +925,12 @@ def fetch_access_token!(options={})
return token_hash
end

##
##
# Refresh the access token, if possible
def refresh!
self.fetch_access_token!
end

##
# Generates an authenticated request for protected resources.
#
Expand Down Expand Up @@ -989,7 +991,7 @@ def generate_authenticated_request(options={})
req.body = body
end
end

request['Authorization'] = ::Signet::OAuth2.generate_bearer_authorization_header(
self.access_token,
options[:realm] ? [['realm', options[:realm]]] : nil
Expand Down Expand Up @@ -1058,16 +1060,16 @@ def fetch_protected_resource(options={})
return response
end
end

private

##
# Check if URI is Google's postmessage flow (not a valid redirect_uri by spec, but allowed)
# @private
def uri_is_postmessage?(uri)
return uri.to_s.casecmp('postmessage') == 0
end

end
end
end
9 changes: 9 additions & 0 deletions spec/signet/oauth_1/client_spec.rb
Expand Up @@ -66,6 +66,15 @@ def merge_body(chunked_body)
)
end

it 'should allow the authorization_uri to be set to a Hash' do
@client.authorization_uri = {
:scheme => 'http', :host => 'example.com', :path => '/authorize'
}
@client.authorization_uri.to_s.should include(
'http://example.com/authorize'
)
end

it 'should allow the authorization_uri to be set to a URI' do
@client.authorization_uri =
Addressable::URI.parse('http://example.com/authorize')
Expand Down
29 changes: 22 additions & 7 deletions spec/signet/oauth_2/client_spec.rb
Expand Up @@ -74,14 +74,14 @@
@client = Signet::OAuth2::Client.new(:redirect_uri => '/relative/path')
end).should raise_error(ArgumentError)
end

it 'should allow "postmessage" as a redirect URI (Google hack)' do
@client.authorization_uri = 'https://example.com/authorize'
@client.client_id = 's6BhdRkqt3'
@client.redirect_uri = 'postmessage'
@client.authorization_uri.query_values['redirect_uri'].should == 'postmessage'
end

it 'should have no authorization_uri' do
@client.authorization_uri.should == nil
end
Expand All @@ -99,6 +99,21 @@
)
end

it 'should allow the authorization_uri to be set to a Hash' do
@client.authorization_uri = {
:scheme => 'https', :host => 'example.com', :path => '/authorize'
}
@client.client_id = 's6BhdRkqt3'
@client.redirect_uri = 'https://example.client.com/callback'
@client.authorization_uri.to_s.should include(
'https://example.com/authorize'
)
@client.authorization_uri.query_values['client_id'].should == 's6BhdRkqt3'
@client.authorization_uri.query_values['redirect_uri'].should == (
'https://example.client.com/callback'
)
end

it 'should allow the authorization_uri to be set to a URI' do
@client.authorization_uri =
Addressable::URI.parse('https://example.com/authorize')
Expand Down Expand Up @@ -150,7 +165,7 @@
end

describe Signet::OAuth2::Client, 'configured for assertions profile' do

describe 'when using RSA keys' do
before do
@key = OpenSSL::PKey::RSA.new 2048
Expand Down Expand Up @@ -218,10 +233,10 @@

@client.fetch_access_token!(:connection => connection)
@client.access_token.should == "1/abcdef1234567890"
stubs.verify_stubbed_calls
stubs.verify_stubbed_calls
end
end

describe 'when using shared secrets' do
before do
@key = 'my secret key'
Expand Down Expand Up @@ -309,12 +324,12 @@
@client.grant_type = 'urn:ietf:params:oauth:grant-type:saml2-bearer'
@client.extension_parameters['assertion'] =
'PEFzc2VydGlvbiBJc3N1ZUluc3RhbnQ9IjIwMTEtMDU'

request = @client.generate_access_token_request
params = Addressable::URI.form_unencode(request.body)
params.should include(['assertion', 'PEFzc2VydGlvbiBJc3N1ZUluc3RhbnQ9IjIwMTEtMDU'])
end

it 'should allow the token to be updated' do
issued_at = Time.now
@client.update_token!(
Expand Down

6 comments on commit c3efb8b

@alauper
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When do you plan on cutting a new gem version? the latest on RubyGems (0.5.0) does not include these changes.

@sporkmonger
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sqrrrl Anything else pending on signet you want me to get in before I cut a release?

@sqrrrl
Copy link
Contributor

@sqrrrl sqrrrl commented on c3efb8b Oct 18, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at the moment :)

@jagwire
Copy link

@jagwire jagwire commented on c3efb8b Jun 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not to be annoying, but did a release ever get cut to include these changes? I still see 0.5.0 on RubyGems...

@taka-oyama
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. still seeing this error.

@sporkmonger
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jagwire I just pushed a release, but didn't see #41 until after I'd done so.

Please sign in to comment.