Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Replace oauth gem with omniauth for Twitter authentication.

OmniAuth has mocking built in to make testing easier.
  • Loading branch information...
commit 4e2da24dd17205350a52c31046ed0bb651d94a50 1 parent f7329a0
@twe4ked authored
Showing with 29 additions and 37 deletions.
  1. +2 −1  Gemfile
  2. +12 −1 Gemfile.lock
  3. +14 −34 tmpfollow.rb
  4. +1 −1  views/index.haml
View
3  Gemfile
@@ -5,7 +5,8 @@ gem 'rack-flash3', :require => 'rack-flash'
gem 'sinatra', :require => 'sinatra/base'
gem 'compass'
gem 'twitter'
-gem 'oauth'
+gem 'omniauth'
+gem 'omniauth-twitter'
gem 'sass'
gem 'thin'
gem 'haml'
View
13 Gemfile.lock
@@ -37,11 +37,21 @@ GEM
multipart-post (~> 1.1)
fssm (0.2.9)
haml (3.1.4)
+ hashie (1.2.0)
i18n (0.6.0)
method_source (0.7.1)
multi_json (1.3.2)
multipart-post (1.1.5)
oauth (0.4.6)
+ omniauth (1.1.0)
+ hashie (~> 1.2)
+ rack
+ omniauth-oauth (1.0.1)
+ oauth
+ omniauth (~> 1.0)
+ omniauth-twitter (0.0.11)
+ multi_json (~> 1.3)
+ omniauth-oauth (~> 1.0)
pry (0.9.8.4)
coderay (~> 1.0.5)
method_source (~> 0.7.1)
@@ -105,7 +115,8 @@ DEPENDENCIES
dm-timestamps
dm-validations
haml
- oauth
+ omniauth
+ omniauth-twitter
pry
rack-flash3
rake
View
48 tmpfollow.rb
@@ -31,6 +31,10 @@ class TmpFollow < Sinatra::Base
set :haml, {format: :html5}
end
+ use OmniAuth::Builder do
+ provider :twitter, ENV['TWITTER_CONSUMER_KEY'], ENV['TWITTER_CONSUMER_SECRET']
+ end
+
helpers do
def flashes
[:notice, :alert].map do |type|
@@ -81,34 +85,19 @@ def flashes
redirect to '/'
end
- # redirects the user to Twitter for authentication
- get '/twitter' do
- callback_url = "#{base_url}/twitter/callback"
- request_token = oauth_consumer.get_request_token(:oauth_callback => callback_url)
- session[:request_token] = request_token.token
- session[:request_token_secret] = request_token.secret
- redirect request_token.authorize_url
- end
-
- # used by Twitter as the callback URL after the user has authenticated
- get '/twitter/callback' do
- request_token = OAuth::RequestToken.new(oauth_consumer, session[:request_token], session[:request_token_secret])
- begin
- @oauth_tokens = request_token.get_access_token(
- {},
- :oauth_token => params[:oauth_token],
- :oauth_verifier => params[:oauth_verifier]
- )
- rescue OAuth::Unauthorized => exception
- flash[:alert] = 'Authenticating with Twitter failed.'
- redirect to '/'
- # exception.message
- end
+ get '/auth/:provider/callback' do
+ auth_hash = env['omniauth.auth'] # => OmniAuth::AuthHash
# store oauth_token and oauth_token_secret in the session
- session[:oauth_token] = @oauth_tokens.token
- session[:oauth_token_secret] = @oauth_tokens.secret
+ session[:oauth_token] = auth_hash[:credentials][:token]
+ session[:oauth_token_secret] = auth_hash[:credentials][:secret]
+ flash[:notice] = 'Authenticated successfully.'
+ redirect to '/'
+ end
+
+ get '/auth/failure' do
+ flash[:alert] = 'Authentication failed.'
redirect to '/'
end
@@ -126,15 +115,6 @@ def flashes
private
- def base_url
- port = (request.port == 80) ? "" : ":#{request.port.to_s}"
- "http://#{request.host}#{port}"
- end
-
- def oauth_consumer
- OAuth::Consumer.new(ENV['TWITTER_CONSUMER_KEY'], ENV['TWITTER_CONSUMER_SECRET'], :site => 'https://twitter.com')
- end
-
def is_numeric?(str)
!!str.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/)
end
View
2  views/index.haml
@@ -1,5 +1,5 @@
- if session[:oauth_token] == nil && session[:oauth_token_secret] == nil
- %a{:href => '/twitter'} Authenticate with Twitter
+ %a{:href => '/auth/twitter'} Authenticate with Twitter
- else
%form{action: '/follow', method: 'post'}
Follow
Please sign in to comment.
Something went wrong with that request. Please try again.