Codebase list ruby-omniauth-twitter / 252bfe6
Imported Upstream version 1.0.1 Praveen Arimbrathodiyil 9 years ago
6 changed file(s) with 192 addition(s) and 59 deletion(s). Raw diff Collapse all Expand all
1515
1616 First start by adding this gem to your Gemfile:
1717
18 gem 'omniauth-twitter'
18 ```ruby
19 gem 'omniauth-twitter'
20 ```
1921
2022 If you need to use the latest HEAD version, you can do so with:
2123
22 gem 'omniauth-twitter', :github => 'arunagw/omniauth-twitter'
24 ```ruby
25 gem 'omniauth-twitter', :github => 'arunagw/omniauth-twitter'
26 ```
2327
2428 Next, tell OmniAuth about this provider. For a Rails app, your `config/initializers/omniauth.rb` file should look like this:
2529
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 ```
2935
3036 Replace CONSUMER_KEY and CONSUMER_SECRET with the appropriate values you obtained from dev.twitter.com earlier.
3137
3945
4046 * **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`
4147
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
4254 * **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`
4355
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 ```
45145
46146 ## Watch the RailsCast
47147
48148 Ryan Bates has put together an excellent RailsCast on OmniAuth:
49149
50 [![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)
150 [![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)
51151
52152 ## Supported Rubies
53153
Binary diff not shown
44 module Strategies
55 class Twitter < OmniAuth::Strategies::OAuth
66 option :name, 'twitter'
7
78 option :client_options, {:authorize_path => '/oauth/authenticate',
89 :site => 'https://api.twitter.com',
910 :proxy => ENV['http_proxy'] ? URI(ENV['http_proxy']) : nil}
1516 :nickname => raw_info['screen_name'],
1617 :name => raw_info['name'],
1718 :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,
1920 :description => raw_info['description'],
2021 :urls => {
2122 'Website' => raw_info['url'],
3738 alias :old_request_phase :request_phase
3839
3940 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
5445 end
5546
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'
5855 else
59 options.client_options.authorize_path = '/oauth/authenticate'
56 options[:client_options][:authorize_path] = '/oauth/authenticate'
6057 end
6158
6259 old_request_phase
6360 end
6461
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
6578 end
6679 end
6780 end
00 module OmniAuth
11 module Twitter
2 VERSION = "0.0.16"
2 VERSION = "1.0.1"
33 end
44 end
00 --- !ruby/object:Gem::Specification
11 name: omniauth-twitter
22 version: !ruby/object:Gem::Version
3 version: 0.0.16
4 prerelease:
3 version: 1.0.1
54 platform: ruby
65 authors:
76 - Arun Agrawal
87 autorequire:
98 bindir: bin
109 cert_chain: []
11 date: 2013-04-03 00:00:00.000000000 Z
10 date: 2013-10-04 00:00:00.000000000 Z
1211 dependencies:
1312 - !ruby/object:Gem::Dependency
1413 name: multi_json
1514 requirement: !ruby/object:Gem::Requirement
16 none: false
1715 requirements:
1816 - - ~>
1917 - !ruby/object:Gem::Version
2119 type: :runtime
2220 prerelease: false
2321 version_requirements: !ruby/object:Gem::Requirement
24 none: false
2522 requirements:
2623 - - ~>
2724 - !ruby/object:Gem::Version
2926 - !ruby/object:Gem::Dependency
3027 name: omniauth-oauth
3128 requirement: !ruby/object:Gem::Requirement
32 none: false
3329 requirements:
3430 - - ~>
3531 - !ruby/object:Gem::Version
3733 type: :runtime
3834 prerelease: false
3935 version_requirements: !ruby/object:Gem::Requirement
40 none: false
4136 requirements:
4237 - - ~>
4338 - !ruby/object:Gem::Version
4540 - !ruby/object:Gem::Dependency
4641 name: rspec
4742 requirement: !ruby/object:Gem::Requirement
48 none: false
4943 requirements:
5044 - - ~>
5145 - !ruby/object:Gem::Version
5347 type: :development
5448 prerelease: false
5549 version_requirements: !ruby/object:Gem::Requirement
56 none: false
5750 requirements:
5851 - - ~>
5952 - !ruby/object:Gem::Version
6154 - !ruby/object:Gem::Dependency
6255 name: rack-test
6356 requirement: !ruby/object:Gem::Requirement
64 none: false
6557 requirements:
66 - - ! '>='
58 - - '>='
6759 - !ruby/object:Gem::Version
6860 version: '0'
6961 type: :development
7062 prerelease: false
7163 version_requirements: !ruby/object:Gem::Requirement
72 none: false
7364 requirements:
74 - - ! '>='
65 - - '>='
7566 - !ruby/object:Gem::Version
7667 version: '0'
7768 - !ruby/object:Gem::Dependency
7869 name: simplecov
7970 requirement: !ruby/object:Gem::Requirement
80 none: false
8171 requirements:
82 - - ! '>='
72 - - '>='
8373 - !ruby/object:Gem::Version
8474 version: '0'
8575 type: :development
8676 prerelease: false
8777 version_requirements: !ruby/object:Gem::Requirement
88 none: false
8978 requirements:
90 - - ! '>='
79 - - '>='
9180 - !ruby/object:Gem::Version
9281 version: '0'
9382 - !ruby/object:Gem::Dependency
9483 name: webmock
9584 requirement: !ruby/object:Gem::Requirement
96 none: false
9785 requirements:
98 - - ! '>='
86 - - '>='
9987 - !ruby/object:Gem::Version
10088 version: '0'
10189 type: :development
10290 prerelease: false
10391 version_requirements: !ruby/object:Gem::Requirement
104 none: false
10592 requirements:
106 - - ! '>='
93 - - '>='
10794 - !ruby/object:Gem::Version
10895 version: '0'
10996 description: OmniAuth strategy for Twitter
128115 homepage: https://github.com/arunagw/omniauth-twitter
129116 licenses:
130117 - MIT
118 metadata: {}
131119 post_install_message:
132120 rdoc_options: []
133121 require_paths:
134122 - lib
135123 required_ruby_version: !ruby/object:Gem::Requirement
136 none: false
137124 requirements:
138 - - ! '>='
125 - - '>='
139126 - !ruby/object:Gem::Version
140127 version: '0'
141128 required_rubygems_version: !ruby/object:Gem::Requirement
142 none: false
143129 requirements:
144 - - ! '>='
130 - - '>='
145131 - !ruby/object:Gem::Version
146132 version: '0'
147133 requirements: []
148134 rubyforge_project: omniauth-twitter
149 rubygems_version: 1.8.25
135 rubygems_version: 2.0.6
150136 signing_key:
151 specification_version: 3
137 specification_version: 4
152138 summary: OmniAuth strategy for Twitter
153139 test_files:
154140 - spec/omniauth/strategies/twitter_spec.rb
00 require 'spec_helper'
11
22 describe OmniAuth::Strategies::Twitter do
3 let(:request) { double('Request', :params => {}, :cookies => {}, :env => {}) }
4
35 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
512 end
613
7 context 'client options' do
14 describe 'client options' do
815 it 'should have correct name' do
916 expect(subject.options.name).to eq('twitter')
1017 end
1825 end
1926 end
2027
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
2155 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
2357 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 )
2761 subject.stub(:old_request_phase).and_return(:whatever)
2862 end
2963