diff --git a/README.md b/README.md index 0f1863f..88b221a 100644 --- a/README.md +++ b/README.md @@ -16,17 +16,23 @@ First start by adding this gem to your Gemfile: - gem 'omniauth-twitter' +```ruby +gem 'omniauth-twitter' +``` If you need to use the latest HEAD version, you can do so with: - gem 'omniauth-twitter', :github => 'arunagw/omniauth-twitter' +```ruby +gem 'omniauth-twitter', :github => 'arunagw/omniauth-twitter' +``` Next, tell OmniAuth about this provider. For a Rails app, your `config/initializers/omniauth.rb` file should look like this: - Rails.application.config.middleware.use OmniAuth::Builder do - provider :twitter, "CONSUMER_KEY", "CONSUMER_SECRET" - end +```ruby +Rails.application.config.middleware.use OmniAuth::Builder do + provider :twitter, "CONSUMER_KEY", "CONSUMER_SECRET" +end +``` Replace CONSUMER_KEY and CONSUMER_SECRET with the appropriate values you obtained from dev.twitter.com earlier. @@ -40,15 +46,109 @@ * **screen_name** - This option implies **force_login**, except the screen name field is pre-filled with a particular value. *Example:* `http://yoursite.com/auth/twitter?screen_name=jim` +* **lang** - The language used in the Twitter prompt. This is useful for adding i18n support since the language of the prompt can be dynamically set for each user. *Example:* `http://yoursite.com/auth/twitter?lang=pt` + +* **secure_image_url** - Set to `true` to use https for the user's image url. Default is `false`. + +* **image_size**: This option defines the size of the user's image. Valid options include `mini` (24x24), `normal` (48x48), `bigger` (73x73) and `original` (the size of the image originally uploaded). Default is `normal`. + * **x_auth_access_type** - This option (described [here](https://dev.twitter.com/docs/api/1/post/oauth/request_token)) lets you request the level of access that your app will have to the Twitter account in question. *Example:* `http://yoursite.com/auth/twitter?x_auth_access_type=read` -* **use_authorize** - There are actually two URLs you can use against the Twitter API. As mentioned, the default is `https://api.twitter.com/oauth/authenticate`, but you also have `https://api.twitter.com/oauth/authorize`. Passing this option as `true` will use the second URL rather than the first. What's the difference? As described [here](https://dev.twitter.com/docs/api/1/get/oauth/authenticate), with `authenticate`, if your user has already granted permission to your application, Twitter will redirect straight back to your application, whereas `authorize` forces the user to go through the "grant permission" screen again. For certain use cases this may be necessary. *Example:* `http://yoursite.com/auth/twitter?use_authorize=true` +* **use_authorize** - There are actually two URLs you can use against the Twitter API. As mentioned, the default is `https://api.twitter.com/oauth/authenticate`, but you also have `https://api.twitter.com/oauth/authorize`. Passing this option as `true` will use the second URL rather than the first. What's the difference? As described [here](https://dev.twitter.com/docs/api/1/get/oauth/authenticate), with `authenticate`, if your user has already granted permission to your application, Twitter will redirect straight back to your application, whereas `authorize` forces the user to go through the "grant permission" screen again. For certain use cases this may be necessary. *Example:* `http://yoursite.com/auth/twitter?use_authorize=true`. *Note:* You must have "Allow this application to be used to Sign in with Twitter" checked in [your application's settings](https://dev.twitter.com/apps) - without it your user will be asked to authorize your application each time they log in. + +Here's an example of a possible configuration where the the user's original profile picture is returned over https, the user is always prompted to sign-in and the default language of the Twitter prompt is changed: + +```ruby +Rails.application.config.middleware.use OmniAuth::Builder do + provider :twitter, ENV["TWITTER_KEY"], ENV["TWITTER_SECRET"], + { + :secure_image_url => 'true', + :image_size => 'original', + :authorize_params => { + :force_login => 'true', + :lang => 'pt' + } + } +end +``` + +## Authentication Hash +An example auth hash available in `request.env['omniauth.auth']`: + +```ruby +{ + :provider => "twitter", + :uid => "123456", + :info => { + :nickname => "johnqpublic", + :name => "John Q Public", + :location => "Anytown, USA", + :image => "http://si0.twimg.com/sticky/default_profile_images/default_profile_2_normal.png", + :description => "a very normal guy.", + :urls => { + :Website => nil, + :Twitter => "https://twitter.com/johnqpublic" + } + }, + :credentials => { + :token => "a1b2c3d4...", # The OAuth 2.0 access token + :secret => "abcdef1234" + }, + :extra => { + :access_token => "", # An OAuth::AccessToken object + :raw_info => { + :name => "John Q Public", + :listed_count" => 0, + :profile_sidebar_border_color" => "181A1E", + :url => nil, + :lang => "en", + :statuses_count => 129, + :profile_image_url => "http://si0.twimg.com/sticky/default_profile_images/default_profile_2_normal.png", + :profile_background_image_url_https => "https://twimg0-a.akamaihd.net/profile_background_images/229171796/pattern_036.gif", + :location => "Anytown, USA", + :time_zone => "Chicago", + :follow_request_sent => false, + :id => 123456, + :profile_background_tile => true, + :profile_sidebar_fill_color => "666666", + :followers_count => 1, + :default_profile_image => false, + :screen_name => "", + :following => false, + :utc_offset => -3600, + :verified => false, + :favourites_count => 0, + :profile_background_color => "1A1B1F", + :is_translator => false, + :friends_count => 1, + :notifications => false, + :geo_enabled => true, + :profile_background_image_url => "http://twimg0-a.akamaihd.net/profile_background_images/229171796/pattern_036.gif", + :protected => false, + :description => "a very normal guy.", + :profile_link_color => "2FC2EF", + :created_at => "Thu Jul 4 00:00:00 +0000 2013", + :id_str => "123456", + :profile_image_url_https => "https://si0.twimg.com/sticky/default_profile_images/default_profile_2_normal.png", + :default_profile => false, + :profile_use_background_image => false, + :entities => { + :description => { + :urls => [] + } + }, + :profile_text_color => "666666", + :contributors_enabled => false + } + } +} +``` ## Watch the RailsCast Ryan Bates has put together an excellent RailsCast on OmniAuth: -[![RailsCast #241](https://www.evernote.com/shard/s35/sh/479f2503-aefa-4542-a7b4-8f84fd22eafc/0571f5a3795a0be3d0b0814312a8d5b7/res/49b5478a-657c-4aff-ae58-dae08b9a46d5/Screen_Shot_2012-07-15_at_12.41.15_PM-20120715-125424.jpg "RailsCast #241 - Simple OmniAuth (revised)")](http://railscasts.com/episodes/241-simple-omniauth-revised) +[![RailsCast #241](http://railscasts.com/static/episodes/stills/241-simple-omniauth-revised.png "RailsCast #241 - Simple OmniAuth (revised)")](http://railscasts.com/episodes/241-simple-omniauth-revised) ## Supported Rubies diff --git a/checksums.yaml.gz b/checksums.yaml.gz new file mode 100644 index 0000000..a35ac4f Binary files /dev/null and b/checksums.yaml.gz differ diff --git a/lib/omniauth/strategies/twitter.rb b/lib/omniauth/strategies/twitter.rb index b836b30..d29c548 100644 --- a/lib/omniauth/strategies/twitter.rb +++ b/lib/omniauth/strategies/twitter.rb @@ -5,6 +5,7 @@ module Strategies class Twitter < OmniAuth::Strategies::OAuth option :name, 'twitter' + option :client_options, {:authorize_path => '/oauth/authenticate', :site => 'https://api.twitter.com', :proxy => ENV['http_proxy'] ? URI(ENV['http_proxy']) : nil} @@ -16,7 +17,7 @@ :nickname => raw_info['screen_name'], :name => raw_info['name'], :location => raw_info['location'], - :image => options[:secure_image_url] ? raw_info['profile_image_url_https'] : raw_info['profile_image_url'], + :image => image_url, :description => raw_info['description'], :urls => { 'Website' => raw_info['url'], @@ -38,31 +39,43 @@ alias :old_request_phase :request_phase def request_phase - force_login = session['omniauth.params'] ? session['omniauth.params']['force_login'] : nil - screen_name = session['omniauth.params'] ? session['omniauth.params']['screen_name'] : nil - x_auth_access_type = session['omniauth.params'] ? session['omniauth.params']['x_auth_access_type'] : nil - if force_login && !force_login.empty? - options[:authorize_params] ||= {} - options[:authorize_params].merge!(:force_login => 'true') - end - if screen_name && !screen_name.empty? - options[:authorize_params] ||= {} - options[:authorize_params].merge!(:force_login => 'true', :screen_name => screen_name) - end - if x_auth_access_type - options[:request_params] ||= {} - options[:request_params].merge!(:x_auth_access_type => x_auth_access_type) + %w[force_login lang screen_name].each do |v| + if request.params[v] + options[:authorize_params][v.to_sym] = request.params[v] + end end - if session['omniauth.params'] && session['omniauth.params']["use_authorize"] == "true" - options.client_options.authorize_path = '/oauth/authorize' + %w[x_auth_access_type].each do |v| + if request.params[v] + options[:request_params][v.to_sym] = request.params[v] + end + end + + if request.params['use_authorize'] == 'true' + options[:client_options][:authorize_path] = '/oauth/authorize' else - options.client_options.authorize_path = '/oauth/authenticate' + options[:client_options][:authorize_path] = '/oauth/authenticate' end old_request_phase end + private + + def image_url + original_url = options[:secure_image_url] ? raw_info['profile_image_url_https'] : raw_info['profile_image_url'] + case options[:image_size] + when 'mini' + original_url.sub('normal', 'mini') + when 'bigger' + original_url.sub('normal', 'bigger') + when 'original' + original_url.sub('_normal', '') + else + original_url + end + end + end end end diff --git a/lib/omniauth-twitter/version.rb b/lib/omniauth-twitter/version.rb index edf9472..a060640 100644 --- a/lib/omniauth-twitter/version.rb +++ b/lib/omniauth-twitter/version.rb @@ -1,5 +1,5 @@ module OmniAuth module Twitter - VERSION = "0.0.16" + VERSION = "1.0.1" end end diff --git a/metadata.yml b/metadata.yml index d3c9008..7c4f319 100644 --- a/metadata.yml +++ b/metadata.yml @@ -1,20 +1,18 @@ --- !ruby/object:Gem::Specification name: omniauth-twitter version: !ruby/object:Gem::Version - version: 0.0.16 - prerelease: + version: 1.0.1 platform: ruby authors: - Arun Agrawal autorequire: bindir: bin cert_chain: [] -date: 2013-04-03 00:00:00.000000000 Z +date: 2013-10-04 00:00:00.000000000 Z dependencies: - !ruby/object:Gem::Dependency name: multi_json requirement: !ruby/object:Gem::Requirement - none: false requirements: - - ~> - !ruby/object:Gem::Version @@ -22,7 +20,6 @@ type: :runtime prerelease: false version_requirements: !ruby/object:Gem::Requirement - none: false requirements: - - ~> - !ruby/object:Gem::Version @@ -30,7 +27,6 @@ - !ruby/object:Gem::Dependency name: omniauth-oauth requirement: !ruby/object:Gem::Requirement - none: false requirements: - - ~> - !ruby/object:Gem::Version @@ -38,7 +34,6 @@ type: :runtime prerelease: false version_requirements: !ruby/object:Gem::Requirement - none: false requirements: - - ~> - !ruby/object:Gem::Version @@ -46,7 +41,6 @@ - !ruby/object:Gem::Dependency name: rspec requirement: !ruby/object:Gem::Requirement - none: false requirements: - - ~> - !ruby/object:Gem::Version @@ -54,7 +48,6 @@ type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement - none: false requirements: - - ~> - !ruby/object:Gem::Version @@ -62,49 +55,43 @@ - !ruby/object:Gem::Dependency name: rack-test requirement: !ruby/object:Gem::Requirement - none: false requirements: - - - ! '>=' + - - '>=' - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement - none: false requirements: - - - ! '>=' + - - '>=' - !ruby/object:Gem::Version version: '0' - !ruby/object:Gem::Dependency name: simplecov requirement: !ruby/object:Gem::Requirement - none: false requirements: - - - ! '>=' + - - '>=' - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement - none: false requirements: - - - ! '>=' + - - '>=' - !ruby/object:Gem::Version version: '0' - !ruby/object:Gem::Dependency name: webmock requirement: !ruby/object:Gem::Requirement - none: false requirements: - - - ! '>=' + - - '>=' - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement - none: false requirements: - - - ! '>=' + - - '>=' - !ruby/object:Gem::Version version: '0' description: OmniAuth strategy for Twitter @@ -129,27 +116,26 @@ homepage: https://github.com/arunagw/omniauth-twitter licenses: - MIT +metadata: {} post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement - none: false requirements: - - - ! '>=' + - - '>=' - !ruby/object:Gem::Version version: '0' required_rubygems_version: !ruby/object:Gem::Requirement - none: false requirements: - - - ! '>=' + - - '>=' - !ruby/object:Gem::Version version: '0' requirements: [] rubyforge_project: omniauth-twitter -rubygems_version: 1.8.25 +rubygems_version: 2.0.6 signing_key: -specification_version: 3 +specification_version: 4 summary: OmniAuth strategy for Twitter test_files: - spec/omniauth/strategies/twitter_spec.rb diff --git a/spec/omniauth/strategies/twitter_spec.rb b/spec/omniauth/strategies/twitter_spec.rb index 19391c5..36b42a1 100644 --- a/spec/omniauth/strategies/twitter_spec.rb +++ b/spec/omniauth/strategies/twitter_spec.rb @@ -1,11 +1,18 @@ require 'spec_helper' describe OmniAuth::Strategies::Twitter do + let(:request) { double('Request', :params => {}, :cookies => {}, :env => {}) } + subject do - OmniAuth::Strategies::Twitter.new({}) + args = ['appid', 'secret', @options || {}].compact + OmniAuth::Strategies::Twitter.new(*args).tap do |strategy| + strategy.stub(:request) { + request + } + end end - context 'client options' do + describe 'client options' do it 'should have correct name' do expect(subject.options.name).to eq('twitter') end @@ -19,12 +26,39 @@ end end + describe 'image_size option' do + context 'when user has an image' do + it 'should return image with size specified' do + @options = { :image_size => 'original' } + subject.stub(:raw_info).and_return( + { 'profile_image_url' => 'http://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_0_normal.png' } + ) + expect(subject.info[:image]).to eq('http://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_0.png') + end + + it 'should return secure image with size specified' do + @options = { :secure_image_url => 'true', :image_size => 'mini' } + subject.stub(:raw_info).and_return( + { 'profile_image_url_https' => 'https://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_0_normal.png' } + ) + expect(subject.info[:image]).to eq('https://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_0_mini.png') + end + + it 'should return normal image by default' do + subject.stub(:raw_info).and_return( + { 'profile_image_url' => 'http://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_0_normal.png' } + ) + expect(subject.info[:image]).to eq('http://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_0_normal.png') + end + end + end + describe 'request_phase' do - context 'no request params set and x_auth_access_type specified' do + context 'with no request params set and x_auth_access_type specified' do before do - subject.options[:request_params] = nil - subject.stub(:session).and_return( - {'omniauth.params' => {'x_auth_access_type' => 'read'}}) + subject.stub(:request).and_return( + double('Request', {:params => {'x_auth_access_type' => 'read'}}) + ) subject.stub(:old_request_phase).and_return(:whatever) end