#30: use omniauth for registration through Facebook, GitHub, Google

This commit is contained in:
Vokhmin Alexey V 2013-03-26 03:13:15 +04:00
parent da77a20c32
commit c2e89f8781
10 changed files with 154 additions and 55 deletions

11
Gemfile
View File

@ -5,10 +5,15 @@ gem 'redhillonrails_core', :git => 'git://github.com/warpc/redhillonrails_core.g
gem 'pg', '~> 0.14.0' gem 'pg', '~> 0.14.0'
gem 'devise', '~> 2.1.2' gem 'devise', '~> 2.2.3'
gem 'omniauth', '~> 1.1.0' gem 'omniauth'
gem 'omniauth-openid', '~> 1.0.1' # gem 'oa-oauth', :require => 'omniauth/oauth'
gem 'omniauth-facebook'
gem 'omniauth-google-oauth2'
gem 'omniauth-github'
# gem 'omniauth-openid', '~> 1.0.1'
gem 'cancan', '1.6.7' # 1.6.8 fail specs with strange error gem 'cancan', '1.6.7' # 1.6.8 fail specs with strange error
# gem 'uuidtools'
gem 'ancestry', '~> 1.3.0' gem 'ancestry', '~> 1.3.0'
gem 'paperclip', '~> 3.3.1' gem 'paperclip', '~> 3.3.1'

View File

@ -52,7 +52,7 @@ GEM
activesupport (3.2.13) activesupport (3.2.13)
i18n (= 0.6.1) i18n (= 0.6.1)
multi_json (~> 1.0) multi_json (~> 1.0)
airbrake (3.1.8) airbrake (3.1.9)
activesupport activesupport
builder builder
json json
@ -68,8 +68,8 @@ GEM
daemons (~> 1.1.4) daemons (~> 1.1.4)
i18n (>= 0.5.0) i18n (>= 0.5.0)
state_machine (~> 1.1.0) state_machine (~> 1.1.0)
bourne (1.2.1) bourne (1.4.0)
mocha (= 0.12.7) mocha (~> 0.13.2)
builder (3.0.4) builder (3.0.4)
cancan (1.6.7) cancan (1.6.7)
cape (1.7.0) cape (1.7.0)
@ -80,7 +80,7 @@ GEM
net-ssh (>= 2.0.14) net-ssh (>= 2.0.14)
net-ssh-gateway (>= 1.1.0) net-ssh-gateway (>= 1.1.0)
capistrano_colors (0.5.5) capistrano_colors (0.5.5)
charlock_holmes (0.6.9.1) charlock_holmes (0.6.9.2)
chronic (0.6.7) chronic (0.6.7)
chunky_png (1.2.7) chunky_png (1.2.7)
cocaine (0.4.2) cocaine (0.4.2)
@ -90,7 +90,7 @@ GEM
coffee-script (2.2.0) coffee-script (2.2.0)
coffee-script-source coffee-script-source
execjs execjs
coffee-script-source (1.6.1) coffee-script-source (1.6.2)
compass (0.12.2) compass (0.12.2)
chunky_png (~> 1.2) chunky_png (~> 1.2)
fssm (>= 0.2.7) fssm (>= 0.2.7)
@ -99,7 +99,7 @@ GEM
compass (>= 0.12.2, < 0.14) compass (>= 0.12.2, < 0.14)
creole (0.5.0) creole (0.5.0)
daemons (1.1.9) daemons (1.1.9)
devise (2.1.3) devise (2.2.3)
bcrypt-ruby (~> 3.0) bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.1) orm_adapter (~> 0.1)
railties (~> 3.1) railties (~> 3.1)
@ -119,6 +119,8 @@ GEM
factory_girl_rails (4.0.0) factory_girl_rails (4.0.0)
factory_girl (~> 4.0.0) factory_girl (~> 4.0.0)
railties (>= 3.0.0) railties (>= 3.0.0)
faraday (0.8.7)
multipart-post (~> 1.1)
ffi (1.0.11) ffi (1.0.11)
fssm (0.2.10) fssm (0.2.10)
github-linguist (2.2.1) github-linguist (2.2.1)
@ -147,9 +149,10 @@ GEM
haml (~> 3.1) haml (~> 3.1)
railties (>= 3.1, < 4.1) railties (>= 3.1, < 4.1)
hashie (1.2.0) hashie (1.2.0)
highline (1.6.16) highline (1.6.15)
hike (1.2.1) hike (1.2.1)
hirb (0.7.1) hirb (0.7.1)
httpauth (0.2.0)
i18n (0.6.1) i18n (0.6.1)
jbuilder (0.8.3) jbuilder (0.8.3)
activesupport (>= 3.0.0) activesupport (>= 3.0.0)
@ -158,6 +161,8 @@ GEM
railties (>= 3.1.0, < 5.0) railties (>= 3.1.0, < 5.0)
thor (~> 0.14) thor (~> 0.14)
json (1.7.7) json (1.7.7)
jwt (0.1.8)
multi_json (>= 1.5)
kgio (2.8.0) kgio (2.8.0)
libv8 (3.3.10.4) libv8 (3.3.10.4)
macaddr (1.6.1) macaddr (1.6.1)
@ -179,10 +184,11 @@ GEM
actionpack actionpack
metaclass (0.0.1) metaclass (0.0.1)
mime-types (1.21) mime-types (1.21)
mocha (0.12.7) mocha (0.13.3)
metaclass (~> 0.0.1) metaclass (~> 0.0.1)
mock_redis (0.6.2) mock_redis (0.6.2)
multi_json (1.7.1) multi_json (1.7.2)
multipart-post (1.2.0)
mustache (0.99.4) mustache (0.99.4)
net-scp (1.1.0) net-scp (1.1.0)
net-ssh (>= 2.6.5) net-ssh (>= 2.6.5)
@ -192,13 +198,27 @@ GEM
net-ssh-gateway (1.2.0) net-ssh-gateway (1.2.0)
net-ssh (>= 2.6.5) net-ssh (>= 2.6.5)
newrelic_rpm (3.5.5.38) newrelic_rpm (3.5.5.38)
nokogiri (1.5.7) nokogiri (1.5.9)
oauth2 (0.8.1)
faraday (~> 0.8)
httpauth (~> 0.1)
jwt (~> 0.1.4)
multi_json (~> 1.0)
rack (~> 1.2)
omniauth (1.1.3) omniauth (1.1.3)
hashie (~> 1.2) hashie (~> 1.2)
rack rack
omniauth-openid (1.0.1) omniauth-facebook (1.4.1)
omniauth-oauth2 (~> 1.1.0)
omniauth-github (1.1.0)
omniauth (~> 1.0)
omniauth-oauth2 (~> 1.1)
omniauth-google-oauth2 (0.1.13)
omniauth (~> 1.0)
omniauth-oauth2
omniauth-oauth2 (1.1.1)
oauth2 (~> 0.8.0)
omniauth (~> 1.0) omniauth (~> 1.0)
rack-openid (~> 1.3.1)
orm_adapter (0.4.0) orm_adapter (0.4.0)
paperclip (3.3.1) paperclip (3.3.1)
activemodel (>= 3.0.0) activemodel (>= 3.0.0)
@ -218,9 +238,6 @@ GEM
rack (1.4.5) rack (1.4.5)
rack-cache (1.2) rack-cache (1.2)
rack (>= 0.4) rack (>= 0.4)
rack-openid (1.3.1)
rack (>= 1.1.0)
ruby-openid (>= 2.1.8)
rack-protection (1.5.0) rack-protection (1.5.0)
rack rack
rack-ssl (1.3.3) rack-ssl (1.3.3)
@ -253,7 +270,7 @@ GEM
rdoc (~> 3.4) rdoc (~> 3.4)
thor (>= 0.14.6, < 2.0) thor (>= 0.14.6, < 2.0)
raindrops (0.10.0) raindrops (0.10.0)
rake (10.0.3) rake (10.0.4)
rdiscount (2.0.7.1) rdiscount (2.0.7.1)
rdoc (3.12.2) rdoc (3.12.2)
json (~> 1.4) json (~> 1.4)
@ -294,7 +311,6 @@ GEM
ruby-haml-js (0.0.3) ruby-haml-js (0.0.3)
execjs execjs
sprockets (>= 2.0.0) sprockets (>= 2.0.0)
ruby-openid (2.2.3)
rubypython (0.5.3) rubypython (0.5.3)
blankslate (>= 2.1.2.3) blankslate (>= 2.1.2.3)
ffi (~> 1.0.7) ffi (~> 1.0.7)
@ -315,9 +331,9 @@ GEM
shoulda-context (~> 1.0, >= 1.0.1) shoulda-context (~> 1.0, >= 1.0.1)
shoulda-matchers (~> 1.0, >= 1.4.1) shoulda-matchers (~> 1.0, >= 1.4.1)
shoulda-context (1.0.2) shoulda-context (1.0.2)
shoulda-matchers (1.5.0) shoulda-matchers (1.5.4)
activesupport (>= 3.0.0) activesupport (>= 3.0.0)
bourne (~> 1.2.0) bourne (~> 1.3)
sinatra (1.3.6) sinatra (1.3.6)
rack (~> 1.4) rack (~> 1.4)
rack-protection (~> 1.3) rack-protection (~> 1.3)
@ -341,7 +357,7 @@ GEM
eventmachine (>= 0.12.6) eventmachine (>= 0.12.6)
rack (>= 1.0.0) rack (>= 1.0.0)
thor (0.17.0) thor (0.17.0)
tilt (1.3.5) tilt (1.3.6)
treetop (1.4.12) treetop (1.4.12)
polyglot polyglot
polyglot (>= 0.3.1) polyglot (>= 0.3.1)
@ -388,7 +404,7 @@ DEPENDENCIES
coffee-rails (~> 3.2.2) coffee-rails (~> 3.2.2)
compass-rails (~> 1.0.3) compass-rails (~> 1.0.3)
creole creole
devise (~> 2.1.2) devise (~> 2.2.3)
diff-display (~> 0.0.1) diff-display (~> 0.0.1)
factory_girl_rails (~> 4.0.0) factory_girl_rails (~> 4.0.0)
github-linguist (~> 2.2.1) github-linguist (~> 2.2.1)
@ -404,8 +420,10 @@ DEPENDENCIES
meta-tags (~> 1.2.5) meta-tags (~> 1.2.5)
mock_redis (= 0.6.2) mock_redis (= 0.6.2)
newrelic_rpm (~> 3.5.5.38) newrelic_rpm (~> 3.5.5.38)
omniauth (~> 1.1.0) omniauth
omniauth-openid (~> 1.0.1) omniauth-facebook
omniauth-github
omniauth-google-oauth2
paperclip (~> 3.3.1) paperclip (~> 3.3.1)
perform_later (~> 1.3.0) perform_later (~> 1.3.0)
pg (~> 0.14.0) pg (~> 0.14.0)

View File

@ -1,30 +1,69 @@
# -*- encoding : utf-8 -*- # -*- encoding : utf-8 -*-
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def open_id require 'uuidtools'
# raise env['omniauth.auth'].inspect
generic # def facebook
# generic
# end
def facebook
oauthorize 'Facebook'
end
def google_oauth2
oauthorize 'google_oauth2'
end
def github
oauthorize 'GitHub'
end end
def passthru def passthru
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end end
protected private
def generic def oauthorize(kind)
authentication = Authentication.find_or_initialize_by_provider_and_uid(env['omniauth.auth']['provider'], env['omniauth.auth']['uid']) provider = kind.downcase
if authentication.new_record? @user = find_for_ouath(env["omniauth.auth"], current_user)
if user_signed_in? # New authentication method for current_user if @user.persisted?
authentication.user = current_user flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => action_name.classify
authentication.save sign_in_and_redirect @user, :event => :authentication
else # Register new user from session else
session["devise.omniauth_data"] = env["omniauth.auth"].except('extra') session["devise.#{provider}_data"] = env["omniauth.auth"]
flash[:notice] = I18n.t "devise.omniauth_callbacks.register"
redirect_to new_user_registration_url redirect_to new_user_registration_url
end end
end
def find_for_ouath(auth, resource=nil)
provider, uid = auth['provider'], auth['uid']
authentication = Authentication.find_or_initialize_by_provider_and_uid(provider, uid)
if authentication.new_record?
unless user_signed_in? # Register new user from session
case provider
when 'facebook'
name = auth['extra']['raw_info']['name']
email = auth['info']['email']
when 'google_oauth2'
name = auth['info']['nickname']
email = auth['info']['email']
when 'github'
else else
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => action_name.classify raise 'Provider #{provider} not handled'
sign_in_and_redirect authentication.user, :event => :authentication
end end
user = User.create(
:uname => "#{provider}-#{uid}",
:name => name,
:email => email,
:password => Devise.friendly_token[0,20]
)
end end
authentication.user = current_user
authentication.save
end
return user
end
end end

View File

@ -4,7 +4,11 @@ class Users::RegisterRequestsController < ApplicationController
layout 'invite' layout 'invite'
def new def new
if APP_CONFIG['preregistration']
render :invite render :invite
else
redirect_to new_user_registration_path
end
end end
def create def create

View File

@ -7,6 +7,7 @@ class User < Avatar
devise :database_authenticatable, :registerable, :omniauthable, :token_authenticatable,# :encryptable, :timeoutable devise :database_authenticatable, :registerable, :omniauthable, :token_authenticatable,# :encryptable, :timeoutable
:recoverable, :rememberable, :validatable, :lockable, :confirmable#, :reconfirmable, :trackable :recoverable, :rememberable, :validatable, :lockable, :confirmable#, :reconfirmable, :trackable
devise :omniauthable, :omniauth_providers => [:facebook, :google_oauth2, :github]
has_one :notifier, :class_name => 'SettingsNotifier', :dependent => :destroy #:notifier has_one :notifier, :class_name => 'SettingsNotifier', :dependent => :destroy #:notifier

View File

@ -17,7 +17,10 @@
.both .both
.left=t('activerecord.attributes.user.email') .left=t('activerecord.attributes.user.email')
.right .right
- if APP_CONFIG['preregistration']
= f.text_field :email, :id => 'email', :readonly => 'readonly', :class => "registartion-input #{email_error ? 'registartion-input-error' : ''}" = f.text_field :email, :id => 'email', :readonly => 'readonly', :class => "registartion-input #{email_error ? 'registartion-input-error' : ''}"
- else
= f.text_field :email, :id => 'email', :class => 'registartion-input'
.both .both
.left=t('activerecord.attributes.user.password') .left=t('activerecord.attributes.user.password')
.right .right
@ -31,6 +34,11 @@
=f.submit t("layout.devise.shared_links.sign_up"), :class => 'button', :id => 'btnLogin' =f.submit t("layout.devise.shared_links.sign_up"), :class => 'button', :id => 'btnLogin'
.both .both
- if devise_mapping.omniauthable?
- resource_class.omniauth_providers.each do |provider|
= link_to "Sign in with #{provider.to_s.titleize}", omniauth_authorize_path(resource_name, provider)
%br
=showDeviseHintError(:login, uname_error) =showDeviseHintError(:login, uname_error)
=showDeviseHintError(:name, name_error) =showDeviseHintError(:name, name_error)
=showDeviseHintError(:email, email_error) =showDeviseHintError(:email, email_error)

View File

@ -2,6 +2,7 @@ common: &common
project_name: ABF project_name: ABF
repo_project_name: ABF repo_project_name: ABF
anonymous_access: true anonymous_access: true
preregistration: false
file_store_url: 'http://file-store.rosalinux.ru' file_store_url: 'http://file-store.rosalinux.ru'
distr_types: ['mdv', 'rhel', 'nau5'] distr_types: ['mdv', 'rhel', 'nau5']
abf_worker: abf_worker:
@ -11,6 +12,15 @@ common: &common
airbrake_api_key: 'airbrake_api_key' airbrake_api_key: 'airbrake_api_key'
devise_pepper: 'devise_pepper' devise_pepper: 'devise_pepper'
secret_token: 'secret_token' secret_token: 'secret_token'
github:
id: 'APP_ID'
secret: 'APP_SECRET'
google:
id: 'APP_ID'
secret: 'APP_SECRET'
facebook:
id: 'APP_ID'
secret: 'APP_SECRET'
wiki_formats: wiki_formats:
markdown: "Markdown" markdown: "Markdown"

View File

@ -182,14 +182,19 @@ Devise.setup do |config|
# config.navigational_formats = [:"*/*", "*/*", :html] # config.navigational_formats = [:"*/*", "*/*", :html]
# The default HTTP method used to sign out a resource. Default is :delete. # The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = :delete config.sign_out_via = :get
# ==> OmniAuth # ==> OmniAuth
# Add a new OmniAuth provider. Check the wiki for more information on setting # Add a new OmniAuth provider. Check the wiki for more information on setting
# up on your models and hooks. # up on your models and hooks.
# config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo' # config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo'
require 'openid/store/filesystem'
config.omniauth :openid, :name => 'open_id' #, :store => OpenID::Store::Filesystem.new('./tmp') # require 'openid/store/filesystem'
# config.omniauth :openid, :name => 'open_id' #, :store => OpenID::Store::Filesystem.new('./tmp')
config.omniauth :facebook, APP_CONFIG['keys']['facebook']['id'], APP_CONFIG['keys']['facebook']['secret']
config.omniauth :google_oauth2, APP_CONFIG['keys']['google']['id'], APP_CONFIG['keys']['google']['secret'], {:access_type => 'offline', :approval_prompt => ''}
config.omniauth :github, APP_CONFIG['keys']['github']['id'], APP_CONFIG['keys']['github']['secret']
# ==> Warden configuration # ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or # If you want to use other strategies, that are not supported by Devise, or

View File

@ -0,0 +1,9 @@
# require "omniauth-facebook"
# Rails.application.config.middleware.use OmniAuth::Builder do
# [:facebook, :github, :google_oauth2].each do |kind|
# provider kind, APP_CONFIG['keys']["#{kind}"]['id'], APP_CONFIG['keys']["#{kind}"]['secret']
# end
# end
OmniAuth.config.logger = Rails.logger

View File

@ -56,5 +56,5 @@ module Preregistration
end # Preregistration end # Preregistration
Rails.application.config.to_prepare do Rails.application.config.to_prepare do
::Devise::RegistrationsController.send :include, Preregistration::Devise::RegistrationsController ::Devise::RegistrationsController.send :include, Preregistration::Devise::RegistrationsController if APP_CONFIG['preregistration']
end end