Skip to content

Instantly share code, notes, and snippets.

@swifthand
Last active August 29, 2015 14:18
Show Gist options
  • Save swifthand/7c35c2c56619db5abbfd to your computer and use it in GitHub Desktop.
Save swifthand/7c35c2c56619db5abbfd to your computer and use it in GitHub Desktop.
Demonstration of error in signet gem's OAuth2 Client, wherein the client cannot consume the authorization_uri values it produces. Runs against the current version of the signet gem (0.6.0 at time of writing).

Demonstration of error in signet gem's OAuth2 Client, wherein the client cannot consume the authorization_uri values it produces. Error occurs when run against the current version of the signet gem (0.6.0 at time of writing).

To reproduce error:

$ bundle install
$ bundle exec ruby signet_oauth2_client_error.rb

Fix created and pull request issued. To demonstrate fix, update Gemfile line 3 to:

gem 'signet', git: 'https://github.com/swifthand/signet.git', branch: 'oauth2-authorization-uri-error'

Then

$ bundle install
$ bundle exec ruby signet_oauth2_client_error.rb
source 'https://rubygems.org'
gem 'signet'
require 'signet'
require 'signet/oauth_2'
require 'signet/oauth_2/client'
client = Signet::OAuth2::Client.new(
authorization_uri: "https://accounts.google.com/o/oauth2/auth",
token_credential_uri: "https://accounts.google.com/o/oauth2/token",
client_id: "thisisnotarealclientid.apps.googleusercontent.com",
client_secret: "thisisnotarealclientsecret",
expiry: 60,
redirect_uri: "https://localhost:4567/oauth2callback"
)
client.authorization_uri
client.authorization_uri = client.authorization_uri
client.authorization_uri
require 'signet'
require 'signet/oauth_2'
require 'signet/oauth_2/client'
# Working with a simple OAuth2 client
puts "> Building Signet::OAuth2::Client"
client = Signet::OAuth2::Client.new(
authorization_uri: "https://accounts.google.com/o/oauth2/auth",
token_credential_uri: "https://accounts.google.com/o/oauth2/token",
client_id: "thisisnotarealclientid.apps.googleusercontent.com",
client_secret: "thisisnotarealclientsecret",
expiry: 60,
redirect_uri: "https://localhost:4567/oauth2callback"
)
puts client
# Initially accessing authorization_uri works.
puts "\n> Asking client for authorization_uri"
puts client.authorization_uri
# => #<Addressable::URI:0x13eb6f0 URI:https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=thisisnotarealclientiddonotworry.apps.googleusercontent.com&redirect_uri=https://localhost:4567/oauth2callback&response_type=code>
# Passing options to augment the URI also works.
puts "\n> Asking client for authorization_uri with additional options"
puts client.authorization_uri(approval_prompt: :force)
# => #<Addressable::URI:0x13ce794 URI:https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force&client_id=thisisnotarealclientiddonotworry.apps.googleusercontent.com&redirect_uri=https://localhost:4567/oauth2callback&response_type=code>
# Note that the previous statement did not alter or reassign the client
# instance's @authorization_uri. It was simply to demonstrate that the
# additional functionality of #authorization_uri is functioning properly.
puts "\n> Accessing client for original/unchanged authorization_uri"
puts client.authorization_uri
# => #<Addressable::URI:0x13bfe4c URI:https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=thisisnotarealclientiddonotworry.apps.googleusercontent.com&redirect_uri=https://localhost:4567/oauth2callback&response_type=code>
# Re-assigning authorization_uri to itself, via the method
# #authorization_uri= does not raise exception or otherwise indicate problem.
puts "\n> Re-assigning authorization_uri from itself"
puts client.authorization_uri = client.authorization_uri
# => #<Addressable::URI:0x1366770 URI:https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=thisisnotarealclientiddonotworry.apps.googleusercontent.com&redirect_uri=https://localhost:4567/oauth2callback&response_type=code>
# However, attempting to access the authorization_uri
# after re-assigning it, raises an exception within Addressable::URI.
puts "\n> Asking client for authorization_uri after re-assignment"
puts client.authorization_uri
# => ArgumentError: comparison of Array with Array failed
# from #{RUBY_PATH}/gems/addressable-2.3.8/lib/addressable/uri.rb:1589:in `sort!'
# from #{RUBY_PATH}/gems/addressable-2.3.8/lib/addressable/uri.rb:1589:in `query_values='
# from #{RUBY_PATH}/gems/signet-0.6.0/lib/signet/oauth_2.rb:152:in `generate_authorization_uri'
# from #{RUBY_PATH}/gems/signet-0.6.0/lib/signet/oauth_2/client.rb:263:in `authorization_uri'
# from client_error.rb:43:in `<main>'
puts "\n> If this prints, Great Success!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment