Permalink
Browse files

Merge pull request #65 from GuilhermeSimoes/i18n

Add i18n support
  • Loading branch information...
2 parents ff2546a + aa43b89 commit ece9eb9f047c476558955640ab726a9fa0cc0930 @arunagw committed Oct 4, 2013
Showing with 43 additions and 22 deletions.
  1. +18 −0 README.md
  2. +15 −18 lib/omniauth/strategies/twitter.rb
  3. +10 −4 spec/omniauth/strategies/twitter_spec.rb
View
@@ -46,6 +46,8 @@ The options are:
* **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`.
@@ -54,6 +56,22 @@ The options are:
* **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']`:
@@ -5,6 +5,7 @@ module OmniAuth
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 @@ class Twitter < OmniAuth::Strategies::OAuth
:nickname => raw_info['screen_name'],
:name => raw_info['name'],
:location => raw_info['location'],
- :image => image_url(options),
+ :image => image_url,
:description => raw_info['description'],
:urls => {
'Website' => raw_info['url'],
@@ -38,34 +39,30 @@ def raw_info
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!(:screen_name => screen_name)
+ %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 x_auth_access_type
- options[:request_params] ||= {}
- options[:request_params].merge!(:x_auth_access_type => x_auth_access_type)
+
+ %w[x_auth_access_type].each do |v|
+ if request.params[v]
+ options[:request_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'
+ 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(options)
+ 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'
@@ -1,9 +1,15 @@
require 'spec_helper'
describe OmniAuth::Strategies::Twitter do
+ let(:request) { double('Request', :params => {}, :cookies => {}, :env => {}) }
+
subject do
args = ['appid', 'secret', @options || {}].compact
- OmniAuth::Strategies::Twitter.new(*args)
+ OmniAuth::Strategies::Twitter.new(*args).tap do |strategy|
+ strategy.stub(:request) {
+ request
+ }
+ end
end
describe 'client options' do
@@ -50,9 +56,9 @@
describe 'request_phase' 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

0 comments on commit ece9eb9

Please sign in to comment.