Permalink
Browse files

Add GitHub authentication support

  • Loading branch information...
radar committed Oct 17, 2012
1 parent e64d6a0 commit ecffdb9dc9ffa59bea5b6e4c94bebbe85e3ed35b
View
@@ -6,7 +6,9 @@ gem 'devise', '2.1.0.rc'
gem 'cancan', '1.6.7'
gem 'paperclip', '2.8.0'
gem 'searcher', :git => "git://github.com/radar/searcher"
-gem 'omniauth-twitter', :git => 'https://github.com/arunagw/omniauth-twitter.git'
+gem 'omniauth-twitter', :github => 'arunagw/omniauth-twitter'
+gem 'omniauth-github', :github => "intridea/omniauth-github"
+
group :assets do
gem 'sass-rails', '~> 3.2.3'
View
@@ -1,18 +1,26 @@
GIT
- remote: git://github.com/radar/searcher
- revision: 56463b662cc98b63fd5d4e627acbd66531297990
- specs:
- searcher (0.0.6)
- activerecord (~> 3.0)
-
-GIT
- remote: https://github.com/arunagw/omniauth-twitter.git
+ remote: git://github.com/arunagw/omniauth-twitter.git
revision: efa0dc10728ec41dd2297b10441146615caa4807
specs:
omniauth-twitter (0.0.13)
multi_json (~> 1.3)
omniauth-oauth (~> 1.0)
+GIT
+ remote: git://github.com/intridea/omniauth-github.git
+ revision: 4c9caf9875360c93349d872e4697323bdf3e0e2d
+ specs:
+ omniauth-github (1.0.3)
+ omniauth (~> 1.0)
+ omniauth-oauth2 (~> 1.1)
+
+GIT
+ remote: git://github.com/radar/searcher
+ revision: 56463b662cc98b63fd5d4e627acbd66531297990
+ specs:
+ searcher (0.0.6)
+ activerecord (~> 3.0)
+
GEM
remote: https://rubygems.org/
specs:
@@ -95,6 +103,8 @@ GEM
multi_json (~> 1.0)
factory_girl (2.6.4)
activesupport (>= 2.3.9)
+ faraday (0.8.4)
+ multipart-post (~> 1.1)
ffi (1.1.5)
gmail (0.4.0)
gmail_xoauth (>= 0.3.0)
@@ -105,12 +115,15 @@ GEM
hashie (1.2.0)
highline (1.6.13)
hike (1.2.1)
+ httpauth (0.2.0)
i18n (0.6.0)
journey (1.0.4)
jquery-rails (2.1.0)
railties (>= 3.1.0, < 5.0)
thor (~> 0.14)
json (1.7.5)
+ jwt (0.1.5)
+ multi_json (>= 1.0)
launchy (2.1.2)
addressable (~> 2.3)
libv8 (3.3.10.4)
@@ -123,6 +136,7 @@ GEM
mime (0.1)
mime-types (1.19)
multi_json (1.3.6)
+ multipart-post (1.1.5)
net-scp (1.0.4)
net-ssh (>= 1.99.1)
net-sftp (2.0.5)
@@ -132,12 +146,21 @@ GEM
net-ssh (>= 1.99.1)
nokogiri (1.5.5)
oauth (0.4.6)
+ oauth2 (0.8.0)
+ faraday (~> 0.8)
+ httpauth (~> 0.1)
+ jwt (~> 0.1.4)
+ multi_json (~> 1.0)
+ rack (~> 1.2)
omniauth (1.1.1)
hashie (~> 1.2)
rack
omniauth-oauth (1.0.1)
oauth
omniauth (~> 1.0)
+ omniauth-oauth2 (1.1.1)
+ oauth2 (~> 0.8.0)
+ omniauth (~> 1.0)
orm_adapter (0.0.7)
paperclip (2.8.0)
activerecord (>= 2.3.0)
@@ -233,6 +256,7 @@ DEPENDENCIES
gmail (= 0.4.0)
jquery-rails
launchy
+ omniauth-github!
omniauth-twitter!
paperclip (= 2.8.0)
pg
@@ -5,6 +5,12 @@ def twitter
flash[:notice] = "Signed in with Twitter successfully."
sign_in_and_redirect @user, :event => :authentication
end
+
+ def github
+ @user = User.find_or_create_for_github(env["omniauth.auth"])
+ flash[:notice] = "Signed in with GitHub successfully."
+ sign_in_and_redirect @user, :event => :authentication
+ end
end
end
@@ -0,0 +1,10 @@
+module OauthHelper
+ def auth_providers(*names)
+ names.each do |name|
+ concat(link_to(image_tag("icons/#{name}_32.png"),
+ user_omniauth_authorize_path(name),
+ :id => "sign_in_with_#{name}"))
+ end
+ nil
+ end
+end
View
@@ -1,4 +1,5 @@
class User < ActiveRecord::Base
+ extend User::OmniauthCallbacks
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
@@ -14,21 +15,6 @@ class User < ActiveRecord::Base
has_many :permissions
- def self.find_or_create_for_twitter(response)
- data = response['extra']['user_hash']
- if user = User.find_by_twitter_id(data["id"])
- user
- else # Create a user with a stub password.
- user = User.new(:email => "twitter+#{data["id"]}@example.com",
- :password => Devise.friendly_token[0,20])
- user.twitter_id = data["id"]
- user.twitter_screen_name = data["screen_name"]
- user.twitter_display_name = data["display_name"]
- user.confirm!
- user
- end
- end
-
def self.reset_request_count!
update_all("request_count = 0", "request_count > 0")
end
@@ -40,6 +26,8 @@ def to_s
def display_name
if twitter_id
"#{twitter_display_name} (@#{twitter_screen_name})"
+ elsif github_id
+ "#{github_display_name} (#{github_user_name})"
else
email
end
@@ -0,0 +1,34 @@
+class User < ActiveRecord::Base
+ module OmniauthCallbacks
+ def find_or_create_for_twitter(response)
+ data = response['extra']['user_hash']
+ if user = User.find_by_twitter_id(data["id"])
+ user
+ else # Create a user with a stub password.
+ user = User.new(:email => "twitter+#{data["id"]}@example.com",
+ :password => Devise.friendly_token[0,20])
+ user.twitter_id = data["id"]
+ user.twitter_screen_name = data["screen_name"]
+ user.twitter_display_name = data["display_name"]
+ user.confirm!
+ user
+ end
+ end
+
+ def find_or_create_for_github(response)
+ data = response['extra']['user_hash']
+ if user = User.find_by_github_id(data["id"])
+ user
+ else # Create a user with a stub password.
+ user = User.new(:email => data["email"],
+ :password => Devise.friendly_token[0,20])
+ user.github_id = data["id"]
+ user.github_user_name = data["login"]
+ user.github_display_name = data["name"]
+ user.confirm!
+ user
+ end
+ end
+ end
+end
+
@@ -27,10 +27,7 @@
<% else %>
<%= link_to "Sign up", new_user_registration_path %>
<%= link_to "Sign in", new_user_session_path %>
- Or use <%= link_to image_tag("icons/twitter_32.png"),
- user_omniauth_authorize_path(:twitter),
- :id => "sign_in_with_twitter" %>
-
+ Or use <%= auth_providers(:twitter, :github) %>
<% end %>
</li>
</ul>
@@ -212,6 +212,7 @@
# up on your models and hooks.
# config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo'
config.omniauth :twitter, 'K7h3TU2ODm0gYQIMwEu3dg', 'fCgqHFEep1aEyYwmG6WW0e3SregFWguDJGTPcSGJaw'
+ config.omniauth :github, '06a293bad9e2942ca063', '417bba1b425ce16a2683b5b3926b6005f8254c47'
# ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or
@@ -0,0 +1,7 @@
+class AddGithubFieldsToUsers < ActiveRecord::Migration
+ def change
+ add_column :users, :github_id, :integer
+ add_column :users, :github_user_name, :string
+ add_column :users, :github_display_name, :string
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20121006065210) do
+ActiveRecord::Schema.define(:version => 20121017201805) do
create_table "assets", :force => true do |t|
t.string "asset_file_name"
@@ -108,6 +108,9 @@
t.string "twitter_id"
t.string "twitter_screen_name"
t.string "twitter_display_name"
+ t.integer "github_id"
+ t.string "github_user_name"
+ t.string "github_display_name"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
@@ -0,0 +1,23 @@
+require 'spec_helper'
+feature "GitHub Auth" do
+ before do
+ OmniAuth.config.mock_auth[:github] = {
+ "extra" => {
+ "user_hash" => {
+ "id" => '12345',
+ "email" => 'githubber@example.com',
+ "login" => "githubber",
+ "name" => "A GitHubber"
+ }
+ }
+ }
+ end
+
+ it "can sign in withn GitHub" do
+ visit '/'
+ click_link "sign_in_with_github"
+ page.should have_content "Signed in with GitHub successfully."
+ page.should have_content "Signed in as A GitHubber"
+ end
+end
+

0 comments on commit ecffdb9

Please sign in to comment.