devise-omniauth 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +58 -0
  3. data/Rakefile +40 -0
  4. data/app/assets/javascripts/devise-omniauth/application.js +15 -0
  5. data/app/assets/stylesheets/devise-omniauth/application.css +13 -0
  6. data/app/controllers/devise_omniauth/application_controller.rb +4 -0
  7. data/app/controllers/devise_omniauth/confirmations_controller.rb +4 -0
  8. data/app/controllers/devise_omniauth/passwords_controller.rb +4 -0
  9. data/app/controllers/devise_omniauth/registrations_controller.rb +4 -0
  10. data/app/controllers/devise_omniauth/sessions_controller.rb +4 -0
  11. data/app/controllers/devise_omniauth/unlocks_controller.rb +4 -0
  12. data/app/controllers/devise_omniauth/users/omniauth_callbacks_controller.rb +17 -0
  13. data/app/helpers/devise-omniauth/application_helper.rb +4 -0
  14. data/app/models/devise_omniauth/authentication.rb +34 -0
  15. data/app/models/user.rb +19 -0
  16. data/app/views/layouts/devise-omniauth/application.html.erb +14 -0
  17. data/config/initializers/devise.rb +5 -0
  18. data/config/routes.rb +7 -0
  19. data/db/migrate/20120626001219_devise_create_users.rb +49 -0
  20. data/db/migrate/20120701141457_create_devise_omniauth_authentications.rb +13 -0
  21. data/lib/devise-omniauth.rb +5 -0
  22. data/lib/devise-omniauth/engine.rb +5 -0
  23. data/lib/devise-omniauth/version.rb +3 -0
  24. data/lib/generators/devise_omniauth/USAGE +13 -0
  25. data/lib/generators/devise_omniauth/install_generator.rb +35 -0
  26. data/lib/generators/devise_omniauth/templates/README +10 -0
  27. data/lib/generators/devise_omniauth/templates/config/initializers/devise.rb +235 -0
  28. data/lib/generators/devise_omniauth/templates/config/locales/en.yml +58 -0
  29. data/lib/generators/devise_omniauth/templates/views/devise/confirmations/new.html.erb +12 -0
  30. data/lib/generators/devise_omniauth/templates/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  31. data/lib/generators/devise_omniauth/templates/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  32. data/lib/generators/devise_omniauth/templates/views/devise/mailer/unlock_instructions.html.erb +7 -0
  33. data/lib/generators/devise_omniauth/templates/views/devise/passwords/edit.html.erb +16 -0
  34. data/lib/generators/devise_omniauth/templates/views/devise/passwords/new.html.erb +12 -0
  35. data/lib/generators/devise_omniauth/templates/views/devise/registrations/edit.html.erb +25 -0
  36. data/lib/generators/devise_omniauth/templates/views/devise/registrations/new.html.erb +18 -0
  37. data/lib/generators/devise_omniauth/templates/views/devise/sessions/new.html.erb +17 -0
  38. data/lib/generators/devise_omniauth/templates/views/devise/shared/_links.erb +25 -0
  39. data/lib/generators/devise_omniauth/templates/views/devise/unlocks/new.html.erb +12 -0
  40. data/lib/tasks/devise-omniauth_tasks.rake +4 -0
  41. data/test/devise-omniauth_test.rb +7 -0
  42. data/test/dummy/README.rdoc +261 -0
  43. data/test/dummy/Rakefile +7 -0
  44. data/test/dummy/app/assets/javascripts/application.js +15 -0
  45. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  46. data/test/dummy/app/controllers/application_controller.rb +3 -0
  47. data/test/dummy/app/helpers/application_helper.rb +2 -0
  48. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  49. data/test/dummy/config.ru +4 -0
  50. data/test/dummy/config/application.rb +59 -0
  51. data/test/dummy/config/boot.rb +10 -0
  52. data/test/dummy/config/database.yml +55 -0
  53. data/test/dummy/config/environment.rb +5 -0
  54. data/test/dummy/config/environments/development.rb +37 -0
  55. data/test/dummy/config/environments/production.rb +67 -0
  56. data/test/dummy/config/environments/test.rb +37 -0
  57. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  58. data/test/dummy/config/initializers/inflections.rb +15 -0
  59. data/test/dummy/config/initializers/mime_types.rb +5 -0
  60. data/test/dummy/config/initializers/secret_token.rb +7 -0
  61. data/test/dummy/config/initializers/session_store.rb +8 -0
  62. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  63. data/test/dummy/config/locales/en.yml +5 -0
  64. data/test/dummy/config/routes.rb +4 -0
  65. data/test/dummy/public/404.html +26 -0
  66. data/test/dummy/public/422.html +26 -0
  67. data/test/dummy/public/500.html +25 -0
  68. data/test/dummy/public/favicon.ico +0 -0
  69. data/test/dummy/script/rails +6 -0
  70. data/test/fixtures/devise_omniauth/authentications.yml +11 -0
  71. data/test/integration/navigation_test.rb +10 -0
  72. data/test/test_helper.rb +15 -0
  73. data/test/unit/devise_omniauth/authentication_test.rb +9 -0
  74. metadata +203 -0
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,58 @@
1
+ devise-omniauth
2
+ ===============
3
+
4
+ # This gem is very much in development and I do not at all recommend using it in a project yet. Feel free to contribute though :)
5
+
6
+ Reusable Omniauth integration for Devise supporting multiple providers. Gives you a User model
7
+
8
+ Run the installer
9
+
10
+ rails generate devise_omniauth:install
11
+
12
+ Follow the instructions output from the installer
13
+
14
+ Install the database migrations
15
+
16
+ rake devise_omniauth:install:migrations
17
+
18
+ Run migrations
19
+
20
+ rake db:migrate
21
+
22
+
23
+
24
+
25
+
26
+ # Below are old notes, ignore
27
+ Mount the engine's routes in your routes.rb. Note: if you change the :as option you will need to customize the paths in the devise views (see below)
28
+
29
+ mount DeviseOmniauth::Engine => "/", :as => "devise_omniauth"
30
+
31
+ Now you can access the routes url helpers from this engine in your application using the prefix you cleared in the :as above, for instance:
32
+
33
+ devise_omniauth.user_omniauth_authorize_path(:facebook)
34
+
35
+ Install the devise initializer
36
+
37
+ rails generate initializer devise
38
+
39
+ Open the newly created config/initializers/devise.rb and customize the following lines with your oauth settings:
40
+
41
+ config.omniauth :facebook, "[SET APP_ID HERE]", "[SET APP_SECRET HERE]"
42
+
43
+ To customize the devise views (optional):
44
+
45
+ rails generate devise:views
46
+
47
+
48
+
49
+
50
+
51
+ For each provider you want to use, add them to your config/initializers/devise.rb, for instance:
52
+
53
+ require "omniauth-facebook"
54
+ config.omniauth :facebook, "APP_ID", "APP_SECRET"
55
+
56
+ Make sure your model is omniauthable (app/models/user.rb
57
+
58
+ devise :omniauthable
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'DeviseOmniauth'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+
28
+ Bundler::GemHelper.install_tasks
29
+
30
+ require 'rake/testtask'
31
+
32
+ Rake::TestTask.new(:test) do |t|
33
+ t.libs << 'lib'
34
+ t.libs << 'test'
35
+ t.pattern = 'test/**/*_test.rb'
36
+ t.verbose = false
37
+ end
38
+
39
+
40
+ task :default => :test
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,4 @@
1
+ module DeviseOmniauth
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module DeviseOmniauth
2
+ class ConfirmationsController < Devise::ConfirmationsController
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module DeviseOmniauth
2
+ class PasswordsController < Devise::PasswordsController
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module DeviseOmniauth
2
+ class RegistrationsController < Devise::RegistrationsController
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module DeviseOmniauth
2
+ class SessionsController < Devise::SessionsController
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module DeviseOmniauth
2
+ class UnlocksController < Devise::UnlocksController
3
+ end
4
+ end
@@ -0,0 +1,17 @@
1
+ module DeviseOmniauth
2
+ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
3
+ def facebook
4
+
5
+ auth = DeviseOmniauth::Authentication.find_or_create_for_facebook_oauth(request.env["omniauth.auth"], current_user)
6
+ @user = auth.user
7
+
8
+ if @user.persisted?
9
+ flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
10
+ sign_in_and_redirect @user, :event => :authentication
11
+ else
12
+ session["devise.facebook_data"] = request.env["omniauth.auth"]
13
+ redirect_to new_user_registration_url
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ module DeviseOmniauth
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,34 @@
1
+ module DeviseOmniauth
2
+ class Authentication < ActiveRecord::Base
3
+ belongs_to :user
4
+ attr_accessible :provider, :provider_id, :user
5
+
6
+ def self.find_or_create_for_facebook_oauth(auth_info, signed_in_resource=nil)
7
+ # Try to find an existing authentication
8
+ auth = self.where(provider: auth_info.provider, provider_id: auth_info.uid).first
9
+ # No existing authentication was found
10
+ unless auth
11
+ # Try to find a user with the email address contained in auth_info
12
+ # TODO should we have the user confirm they want to link these?
13
+ user = User.find_by_email(auth_info.info.email)
14
+ # If there isn't a user, create one with the auth_info
15
+ # TODO should the user confirm this and/or have the opportunity to link to a different email?
16
+ unless user
17
+ user = User.create(
18
+ name: auth_info.extra.raw_info.name,
19
+ email: auth_info.info.email,
20
+ password: Devise.friendly_token[0, 20]
21
+ )
22
+ end
23
+ # Create the Authentication for the user we found or created
24
+ auth = self.create(
25
+ user: user,
26
+ provider: auth_info.provider,
27
+ provider_id: auth_info.uid
28
+ )
29
+ end
30
+ # Return the Authentication
31
+ auth
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,19 @@
1
+ class User < ActiveRecord::Base
2
+ # Include default devise modules. Others available are:
3
+ # TODO only have omniauthable and required here and give instructions for adding the rest in your app
4
+ devise :database_authenticatable, :registerable, :token_authenticatable, :lockable, :timeoutable,
5
+ :recoverable, :rememberable, :trackable, :validatable, :confirmable, :omniauthable
6
+
7
+ # Setup accessible (or protected) attributes for your model
8
+ attr_accessible :email, :password, :password_confirmation, :remember_me, :name
9
+
10
+ has_many :omniauth_authentications, :class_name => DeviseOmniauth::Authentication
11
+
12
+ def self.new_with_session(params, session)
13
+ super.tap do |user|
14
+ if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
15
+ user.email = data["email"] if user.email.blank?
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>DeviseOmniauth</title>
5
+ <%= stylesheet_link_tag "devise-omniauth/application", :media => "all" %>
6
+ <%= javascript_include_tag "devise-omniauth/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,5 @@
1
+ # This is an empty shell file just so that you can run the generators in your application without error.
2
+ # For this gem to work you must run "rails generate devise_omniauth:install" in your application. See the README for more info
3
+ Devise.setup do |config|
4
+ require 'devise/orm/active_record'
5
+ end
@@ -0,0 +1,7 @@
1
+ DeviseOmniauth::Engine.routes.draw do
2
+
3
+ devise_for :users, {
4
+ controllers: {:omniauth_callbacks => "devise_omniauth/users/omniauth_callbacks"}
5
+ }
6
+
7
+ end
@@ -0,0 +1,49 @@
1
+ class DeviseCreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table(:users) do |t|
4
+
5
+ ## Store the user's name
6
+ t.string :name
7
+
8
+ ## Database authenticatable
9
+ t.string :email, :null => false, :default => ""
10
+ t.string :encrypted_password, :null => false, :default => ""
11
+
12
+ ## Recoverable
13
+ t.string :reset_password_token
14
+ t.datetime :reset_password_sent_at
15
+
16
+ ## Rememberable
17
+ t.datetime :remember_created_at
18
+
19
+ ## Trackable
20
+ t.integer :sign_in_count, :default => 0
21
+ t.datetime :current_sign_in_at
22
+ t.datetime :last_sign_in_at
23
+ t.string :current_sign_in_ip
24
+ t.string :last_sign_in_ip
25
+
26
+ ## Confirmable
27
+ t.string :confirmation_token
28
+ t.datetime :confirmed_at
29
+ t.datetime :confirmation_sent_at
30
+ t.string :unconfirmed_email # Only if using reconfirmable
31
+
32
+ ## Lockable
33
+ t.integer :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts
34
+ t.string :unlock_token # Only if unlock strategy is :email or :both
35
+ t.datetime :locked_at
36
+
37
+ ## Token authenticatable
38
+ t.string :authentication_token
39
+
40
+ t.timestamps
41
+ end
42
+
43
+ add_index :users, :email, :unique => true
44
+ add_index :users, :reset_password_token, :unique => true
45
+ add_index :users, :confirmation_token, :unique => true
46
+ add_index :users, :unlock_token, :unique => true
47
+ add_index :users, :authentication_token, :unique => true
48
+ end
49
+ end
@@ -0,0 +1,13 @@
1
+ class CreateDeviseOmniauthAuthentications < ActiveRecord::Migration
2
+ def change
3
+ create_table :devise_omniauth_authentications do |t|
4
+ t.integer :user_id
5
+ t.string :provider
6
+ t.string :provider_id
7
+ t.timestamps
8
+ end
9
+
10
+ add_index :devise_omniauth_authentications, :user_id, :name => 'devise_omniauth_authentications_on_user_id'
11
+ add_index :devise_omniauth_authentications, [:provider, :provider_id], :name => 'devise_omniauth_authentications_on_provider_and_provider_id'
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ require 'devise'
2
+ require "devise-omniauth/engine"
3
+
4
+ module DeviseOmniauth
5
+ end
@@ -0,0 +1,5 @@
1
+ module DeviseOmniauth
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace DeviseOmniauth
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module DeviseOmniauth
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,13 @@
1
+ Description:
2
+ Install devise-omniauth by copying a devise initializer, the devise views, devise locale translations
3
+
4
+ Example:
5
+ rails generate devise_omniauth:install
6
+
7
+ This will create:
8
+ app/views/devise/*
9
+ config/initializers/devise.rb
10
+ config/locales/devise.en.yml
11
+
12
+ Add will add this to your routes.rb:
13
+ mount DeviseOmniauth::Engine => '/', :as => 'devise_omniauth'
@@ -0,0 +1,35 @@
1
+ module DeviseOmniauth
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../templates", __FILE__)
5
+
6
+ desc "Creates a Devise initializer and copy locale files to your application."
7
+ class_option :orm
8
+
9
+ # TODO is there a way for these to come from devise even with the changes?
10
+ def copy_views
11
+ directory 'views/devise/', 'app/views/devise'
12
+ end
13
+
14
+ # TODO the source should come from devise and not duplicated in this gem
15
+ def copy_initializer
16
+ template "config/initializers/devise.rb", "config/initializers/devise.rb"
17
+ end
18
+
19
+ # TODO the source should come from devise and not duplicated in this gem
20
+ def copy_locale
21
+ copy_file "config/locales/en.yml", "config/locales/devise.en.yml"
22
+ end
23
+
24
+ # TODO should be able to customize the :as option (also will have to change in views)
25
+ def add_devise_routes
26
+ devise_route = "mount DeviseOmniauth::Engine => '/', :as => 'devise_omniauth'"
27
+ route devise_route
28
+ end
29
+
30
+ def show_readme
31
+ readme "README" if behavior == :invoke
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,10 @@
1
+ ===============================================================================
2
+
3
+ Open the newly created config/initializers/devise.rb and customize the following lines with your oauth settings:
4
+
5
+ config.omniauth :facebook, "[SET APP_ID HERE]", "[SET APP_SECRET HERE]"
6
+
7
+ You must also also define root :to in your routes.rb for devise to work properly. You can set it to anything you want.
8
+ See the devise wiki for more information
9
+
10
+ ===============================================================================