Imported Upstream version 1.0.1
Praveen Arimbrathodiyil
9 years ago
15 | 15 | |
16 | 16 | First start by adding this gem to your Gemfile: |
17 | 17 | |
18 | gem 'omniauth-twitter' | |
18 | ```ruby | |
19 | gem 'omniauth-twitter' | |
20 | ``` | |
19 | 21 | |
20 | 22 | If you need to use the latest HEAD version, you can do so with: |
21 | 23 | |
22 | gem 'omniauth-twitter', :github => 'arunagw/omniauth-twitter' | |
24 | ```ruby | |
25 | gem 'omniauth-twitter', :github => 'arunagw/omniauth-twitter' | |
26 | ``` | |
23 | 27 | |
24 | 28 | Next, tell OmniAuth about this provider. For a Rails app, your `config/initializers/omniauth.rb` file should look like this: |
25 | 29 | |
26 | Rails.application.config.middleware.use OmniAuth::Builder do | |
27 | provider :twitter, "CONSUMER_KEY", "CONSUMER_SECRET" | |
28 | end | |
30 | ```ruby | |
31 | Rails.application.config.middleware.use OmniAuth::Builder do | |
32 | provider :twitter, "CONSUMER_KEY", "CONSUMER_SECRET" | |
33 | end | |
34 | ``` | |
29 | 35 | |
30 | 36 | Replace CONSUMER_KEY and CONSUMER_SECRET with the appropriate values you obtained from dev.twitter.com earlier. |
31 | 37 | |
39 | 45 | |
40 | 46 | * **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` |
41 | 47 | |
48 | * **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` | |
49 | ||
50 | * **secure_image_url** - Set to `true` to use https for the user's image url. Default is `false`. | |
51 | ||
52 | * **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`. | |
53 | ||
42 | 54 | * **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` |
43 | 55 | |
44 | * **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` | |
56 | * **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. | |
57 | ||
58 | 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: | |
59 | ||
60 | ```ruby | |
61 | Rails.application.config.middleware.use OmniAuth::Builder do | |
62 | provider :twitter, ENV["TWITTER_KEY"], ENV["TWITTER_SECRET"], | |
63 | { | |
64 | :secure_image_url => 'true', | |
65 | :image_size => 'original', | |
66 | :authorize_params => { | |
67 | :force_login => 'true', | |
68 | :lang => 'pt' | |
69 | } | |
70 | } | |
71 | end | |
72 | ``` | |
73 | ||
74 | ## Authentication Hash | |
75 | An example auth hash available in `request.env['omniauth.auth']`: | |
76 | ||
77 | ```ruby | |
78 | { | |
79 | :provider => "twitter", | |
80 | :uid => "123456", | |
81 | :info => { | |
82 | :nickname => "johnqpublic", | |
83 | :name => "John Q Public", | |
84 | :location => "Anytown, USA", | |
85 | :image => "http://si0.twimg.com/sticky/default_profile_images/default_profile_2_normal.png", | |
86 | :description => "a very normal guy.", | |
87 | :urls => { | |
88 | :Website => nil, | |
89 | :Twitter => "https://twitter.com/johnqpublic" | |
90 | } | |
91 | }, | |
92 | :credentials => { | |
93 | :token => "a1b2c3d4...", # The OAuth 2.0 access token | |
94 | :secret => "abcdef1234" | |
95 | }, | |
96 | :extra => { | |
97 | :access_token => "", # An OAuth::AccessToken object | |
98 | :raw_info => { | |
99 | :name => "John Q Public", | |
100 | :listed_count" => 0, | |
101 | :profile_sidebar_border_color" => "181A1E", | |
102 | :url => nil, | |
103 | :lang => "en", | |
104 | :statuses_count => 129, | |
105 | :profile_image_url => "http://si0.twimg.com/sticky/default_profile_images/default_profile_2_normal.png", | |
106 | :profile_background_image_url_https => "https://twimg0-a.akamaihd.net/profile_background_images/229171796/pattern_036.gif", | |
107 | :location => "Anytown, USA", | |
108 | :time_zone => "Chicago", | |
109 | :follow_request_sent => false, | |
110 | :id => 123456, | |
111 | :profile_background_tile => true, | |
112 | :profile_sidebar_fill_color => "666666", | |
113 | :followers_count => 1, | |
114 | :default_profile_image => false, | |
115 | :screen_name => "", | |
116 | :following => false, | |
117 | :utc_offset => -3600, | |
118 | :verified => false, | |
119 | :favourites_count => 0, | |
120 | :profile_background_color => "1A1B1F", | |
121 | :is_translator => false, | |
122 | :friends_count => 1, | |
123 | :notifications => false, | |
124 | :geo_enabled => true, | |
125 | :profile_background_image_url => "http://twimg0-a.akamaihd.net/profile_background_images/229171796/pattern_036.gif", | |
126 | :protected => false, | |
127 | :description => "a very normal guy.", | |
128 | :profile_link_color => "2FC2EF", | |
129 | :created_at => "Thu Jul 4 00:00:00 +0000 2013", | |
130 | :id_str => "123456", | |
131 | :profile_image_url_https => "https://si0.twimg.com/sticky/default_profile_images/default_profile_2_normal.png", | |
132 | :default_profile => false, | |
133 | :profile_use_background_image => false, | |
134 | :entities => { | |
135 | :description => { | |
136 | :urls => [] | |
137 | } | |
138 | }, | |
139 | :profile_text_color => "666666", | |
140 | :contributors_enabled => false | |
141 | } | |
142 | } | |
143 | } | |
144 | ``` | |
45 | 145 | |
46 | 146 | ## Watch the RailsCast |
47 | 147 | |
48 | 148 | Ryan Bates has put together an excellent RailsCast on OmniAuth: |
49 | 149 | |
50 | [")](http://railscasts.com/episodes/241-simple-omniauth-revised) | |
150 | [")](http://railscasts.com/episodes/241-simple-omniauth-revised) | |
51 | 151 | |
52 | 152 | ## Supported Rubies |
53 | 153 |
Binary diff not shown
4 | 4 | module Strategies |
5 | 5 | class Twitter < OmniAuth::Strategies::OAuth |
6 | 6 | option :name, 'twitter' |
7 | ||
7 | 8 | option :client_options, {:authorize_path => '/oauth/authenticate', |
8 | 9 | :site => 'https://api.twitter.com', |
9 | 10 | :proxy => ENV['http_proxy'] ? URI(ENV['http_proxy']) : nil} |
15 | 16 | :nickname => raw_info['screen_name'], |
16 | 17 | :name => raw_info['name'], |
17 | 18 | :location => raw_info['location'], |
18 | :image => options[:secure_image_url] ? raw_info['profile_image_url_https'] : raw_info['profile_image_url'], | |
19 | :image => image_url, | |
19 | 20 | :description => raw_info['description'], |
20 | 21 | :urls => { |
21 | 22 | 'Website' => raw_info['url'], |
37 | 38 | alias :old_request_phase :request_phase |
38 | 39 | |
39 | 40 | def request_phase |
40 | force_login = session['omniauth.params'] ? session['omniauth.params']['force_login'] : nil | |
41 | screen_name = session['omniauth.params'] ? session['omniauth.params']['screen_name'] : nil | |
42 | x_auth_access_type = session['omniauth.params'] ? session['omniauth.params']['x_auth_access_type'] : nil | |
43 | if force_login && !force_login.empty? | |
44 | options[:authorize_params] ||= {} | |
45 | options[:authorize_params].merge!(:force_login => 'true') | |
46 | end | |
47 | if screen_name && !screen_name.empty? | |
48 | options[:authorize_params] ||= {} | |
49 | options[:authorize_params].merge!(:force_login => 'true', :screen_name => screen_name) | |
50 | end | |
51 | if x_auth_access_type | |
52 | options[:request_params] ||= {} | |
53 | options[:request_params].merge!(:x_auth_access_type => x_auth_access_type) | |
41 | %w[force_login lang screen_name].each do |v| | |
42 | if request.params[v] | |
43 | options[:authorize_params][v.to_sym] = request.params[v] | |
44 | end | |
54 | 45 | end |
55 | 46 | |
56 | if session['omniauth.params'] && session['omniauth.params']["use_authorize"] == "true" | |
57 | options.client_options.authorize_path = '/oauth/authorize' | |
47 | %w[x_auth_access_type].each do |v| | |
48 | if request.params[v] | |
49 | options[:request_params][v.to_sym] = request.params[v] | |
50 | end | |
51 | end | |
52 | ||
53 | if request.params['use_authorize'] == 'true' | |
54 | options[:client_options][:authorize_path] = '/oauth/authorize' | |
58 | 55 | else |
59 | options.client_options.authorize_path = '/oauth/authenticate' | |
56 | options[:client_options][:authorize_path] = '/oauth/authenticate' | |
60 | 57 | end |
61 | 58 | |
62 | 59 | old_request_phase |
63 | 60 | end |
64 | 61 | |
62 | private | |
63 | ||
64 | def image_url | |
65 | original_url = options[:secure_image_url] ? raw_info['profile_image_url_https'] : raw_info['profile_image_url'] | |
66 | case options[:image_size] | |
67 | when 'mini' | |
68 | original_url.sub('normal', 'mini') | |
69 | when 'bigger' | |
70 | original_url.sub('normal', 'bigger') | |
71 | when 'original' | |
72 | original_url.sub('_normal', '') | |
73 | else | |
74 | original_url | |
75 | end | |
76 | end | |
77 | ||
65 | 78 | end |
66 | 79 | end |
67 | 80 | end |
0 | 0 | --- !ruby/object:Gem::Specification |
1 | 1 | name: omniauth-twitter |
2 | 2 | version: !ruby/object:Gem::Version |
3 | version: 0.0.16 | |
4 | prerelease: | |
3 | version: 1.0.1 | |
5 | 4 | platform: ruby |
6 | 5 | authors: |
7 | 6 | - Arun Agrawal |
8 | 7 | autorequire: |
9 | 8 | bindir: bin |
10 | 9 | cert_chain: [] |
11 | date: 2013-04-03 00:00:00.000000000 Z | |
10 | date: 2013-10-04 00:00:00.000000000 Z | |
12 | 11 | dependencies: |
13 | 12 | - !ruby/object:Gem::Dependency |
14 | 13 | name: multi_json |
15 | 14 | requirement: !ruby/object:Gem::Requirement |
16 | none: false | |
17 | 15 | requirements: |
18 | 16 | - - ~> |
19 | 17 | - !ruby/object:Gem::Version |
21 | 19 | type: :runtime |
22 | 20 | prerelease: false |
23 | 21 | version_requirements: !ruby/object:Gem::Requirement |
24 | none: false | |
25 | 22 | requirements: |
26 | 23 | - - ~> |
27 | 24 | - !ruby/object:Gem::Version |
29 | 26 | - !ruby/object:Gem::Dependency |
30 | 27 | name: omniauth-oauth |
31 | 28 | requirement: !ruby/object:Gem::Requirement |
32 | none: false | |
33 | 29 | requirements: |
34 | 30 | - - ~> |
35 | 31 | - !ruby/object:Gem::Version |
37 | 33 | type: :runtime |
38 | 34 | prerelease: false |
39 | 35 | version_requirements: !ruby/object:Gem::Requirement |
40 | none: false | |
41 | 36 | requirements: |
42 | 37 | - - ~> |
43 | 38 | - !ruby/object:Gem::Version |
45 | 40 | - !ruby/object:Gem::Dependency |
46 | 41 | name: rspec |
47 | 42 | requirement: !ruby/object:Gem::Requirement |
48 | none: false | |
49 | 43 | requirements: |
50 | 44 | - - ~> |
51 | 45 | - !ruby/object:Gem::Version |
53 | 47 | type: :development |
54 | 48 | prerelease: false |
55 | 49 | version_requirements: !ruby/object:Gem::Requirement |
56 | none: false | |
57 | 50 | requirements: |
58 | 51 | - - ~> |
59 | 52 | - !ruby/object:Gem::Version |
61 | 54 | - !ruby/object:Gem::Dependency |
62 | 55 | name: rack-test |
63 | 56 | requirement: !ruby/object:Gem::Requirement |
64 | none: false | |
65 | 57 | requirements: |
66 | - - ! '>=' | |
58 | - - '>=' | |
67 | 59 | - !ruby/object:Gem::Version |
68 | 60 | version: '0' |
69 | 61 | type: :development |
70 | 62 | prerelease: false |
71 | 63 | version_requirements: !ruby/object:Gem::Requirement |
72 | none: false | |
73 | 64 | requirements: |
74 | - - ! '>=' | |
65 | - - '>=' | |
75 | 66 | - !ruby/object:Gem::Version |
76 | 67 | version: '0' |
77 | 68 | - !ruby/object:Gem::Dependency |
78 | 69 | name: simplecov |
79 | 70 | requirement: !ruby/object:Gem::Requirement |
80 | none: false | |
81 | 71 | requirements: |
82 | - - ! '>=' | |
72 | - - '>=' | |
83 | 73 | - !ruby/object:Gem::Version |
84 | 74 | version: '0' |
85 | 75 | type: :development |
86 | 76 | prerelease: false |
87 | 77 | version_requirements: !ruby/object:Gem::Requirement |
88 | none: false | |
89 | 78 | requirements: |
90 | - - ! '>=' | |
79 | - - '>=' | |
91 | 80 | - !ruby/object:Gem::Version |
92 | 81 | version: '0' |
93 | 82 | - !ruby/object:Gem::Dependency |
94 | 83 | name: webmock |
95 | 84 | requirement: !ruby/object:Gem::Requirement |
96 | none: false | |
97 | 85 | requirements: |
98 | - - ! '>=' | |
86 | - - '>=' | |
99 | 87 | - !ruby/object:Gem::Version |
100 | 88 | version: '0' |
101 | 89 | type: :development |
102 | 90 | prerelease: false |
103 | 91 | version_requirements: !ruby/object:Gem::Requirement |
104 | none: false | |
105 | 92 | requirements: |
106 | - - ! '>=' | |
93 | - - '>=' | |
107 | 94 | - !ruby/object:Gem::Version |
108 | 95 | version: '0' |
109 | 96 | description: OmniAuth strategy for Twitter |
128 | 115 | homepage: https://github.com/arunagw/omniauth-twitter |
129 | 116 | licenses: |
130 | 117 | - MIT |
118 | metadata: {} | |
131 | 119 | post_install_message: |
132 | 120 | rdoc_options: [] |
133 | 121 | require_paths: |
134 | 122 | - lib |
135 | 123 | required_ruby_version: !ruby/object:Gem::Requirement |
136 | none: false | |
137 | 124 | requirements: |
138 | - - ! '>=' | |
125 | - - '>=' | |
139 | 126 | - !ruby/object:Gem::Version |
140 | 127 | version: '0' |
141 | 128 | required_rubygems_version: !ruby/object:Gem::Requirement |
142 | none: false | |
143 | 129 | requirements: |
144 | - - ! '>=' | |
130 | - - '>=' | |
145 | 131 | - !ruby/object:Gem::Version |
146 | 132 | version: '0' |
147 | 133 | requirements: [] |
148 | 134 | rubyforge_project: omniauth-twitter |
149 | rubygems_version: 1.8.25 | |
135 | rubygems_version: 2.0.6 | |
150 | 136 | signing_key: |
151 | specification_version: 3 | |
137 | specification_version: 4 | |
152 | 138 | summary: OmniAuth strategy for Twitter |
153 | 139 | test_files: |
154 | 140 | - spec/omniauth/strategies/twitter_spec.rb |
0 | 0 | require 'spec_helper' |
1 | 1 | |
2 | 2 | describe OmniAuth::Strategies::Twitter do |
3 | let(:request) { double('Request', :params => {}, :cookies => {}, :env => {}) } | |
4 | ||
3 | 5 | subject do |
4 | OmniAuth::Strategies::Twitter.new({}) | |
6 | args = ['appid', 'secret', @options || {}].compact | |
7 | OmniAuth::Strategies::Twitter.new(*args).tap do |strategy| | |
8 | strategy.stub(:request) { | |
9 | request | |
10 | } | |
11 | end | |
5 | 12 | end |
6 | 13 | |
7 | context 'client options' do | |
14 | describe 'client options' do | |
8 | 15 | it 'should have correct name' do |
9 | 16 | expect(subject.options.name).to eq('twitter') |
10 | 17 | end |
18 | 25 | end |
19 | 26 | end |
20 | 27 | |
28 | describe 'image_size option' do | |
29 | context 'when user has an image' do | |
30 | it 'should return image with size specified' do | |
31 | @options = { :image_size => 'original' } | |
32 | subject.stub(:raw_info).and_return( | |
33 | { 'profile_image_url' => 'http://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_0_normal.png' } | |
34 | ) | |
35 | expect(subject.info[:image]).to eq('http://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_0.png') | |
36 | end | |
37 | ||
38 | it 'should return secure image with size specified' do | |
39 | @options = { :secure_image_url => 'true', :image_size => 'mini' } | |
40 | subject.stub(:raw_info).and_return( | |
41 | { 'profile_image_url_https' => 'https://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_0_normal.png' } | |
42 | ) | |
43 | expect(subject.info[:image]).to eq('https://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_0_mini.png') | |
44 | end | |
45 | ||
46 | it 'should return normal image by default' do | |
47 | subject.stub(:raw_info).and_return( | |
48 | { 'profile_image_url' => 'http://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_0_normal.png' } | |
49 | ) | |
50 | expect(subject.info[:image]).to eq('http://twimg0-a.akamaihd.net/sticky/default_profile_images/default_profile_0_normal.png') | |
51 | end | |
52 | end | |
53 | end | |
54 | ||
21 | 55 | describe 'request_phase' do |
22 | context 'no request params set and x_auth_access_type specified' do | |
56 | context 'with no request params set and x_auth_access_type specified' do | |
23 | 57 | before do |
24 | subject.options[:request_params] = nil | |
25 | subject.stub(:session).and_return( | |
26 | {'omniauth.params' => {'x_auth_access_type' => 'read'}}) | |
58 | subject.stub(:request).and_return( | |
59 | double('Request', {:params => {'x_auth_access_type' => 'read'}}) | |
60 | ) | |
27 | 61 | subject.stub(:old_request_phase).and_return(:whatever) |
28 | 62 | end |
29 | 63 |