domp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ .DS_Store
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in devise-omniauth-multiple-providers.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,84 @@
1
+ # DOMP: Devise & Omniauth Multiple Providers
2
+
3
+ Generator for using Devise & Omniauth with Multiple Providers. I wrote this because I got tired of repeating the same code in every app that required multiple providers.
4
+
5
+ Basically, to have multiple providers working in a brand new application, all you need to do is run the following:
6
+ ```
7
+ gem "devise"
8
+ ```
9
+ ```
10
+ rails generate devise:install
11
+ rails generate devise User
12
+ rails generate domp User facebook twitter
13
+ ```
14
+
15
+ Note: this is not an engine but rather a generator which will generate models, controllers and setup configs for you. You are free to change everything. You are the king.
16
+
17
+ ## Installation
18
+
19
+ $ gem install domp
20
+
21
+ ## Usage
22
+
23
+ First, you need to have `devise` already installed and your model generated:
24
+
25
+ ```
26
+ rails generate devise:install
27
+ rails generate devise User
28
+ ```
29
+
30
+ Run the `domp` generator:
31
+
32
+ ```
33
+ rails generate domp User facebook twitter
34
+ ```
35
+
36
+ During the generation you will be promted for the provider's application id and secret key.
37
+
38
+ ## What is generated for you:
39
+
40
+ ### AuthenticationProvider
41
+ Model & migration. Will be populated with the providers you specify.
42
+
43
+ ### UserAuthentication
44
+ Model & migration.
45
+
46
+ ### User model additions
47
+ Here's what the `User` model will look like:
48
+
49
+ ```ruby
50
+ class User < ActiveRecord::Base
51
+ has_many :authentications, class_name: 'UserAuthentication'
52
+ devise :omniauthable, :database_authenticatable, :registerable,
53
+ :recoverable, :rememberable, :trackable, :validatable
54
+ ```
55
+
56
+ ### User::OmniauthCallbacksController
57
+ Here goes all the logic of creating multiple authentications. You are free to change everything, it's just a boilerplate that will make sense it most of the apps.
58
+
59
+ ### Route additions
60
+ ```ruby
61
+ devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
62
+ ```
63
+ ### Devise config additions
64
+
65
+ The following will be added to `config/initializers/devise.rb`:
66
+
67
+ ```ruby
68
+ config.omniauth :facebook, 'x', 'x'
69
+ config.omniauth :twitter, 'x', 'x'
70
+ ```
71
+
72
+ ### Gemfile additions
73
+ ```
74
+ gem "omniauth"
75
+ gem "omniauth-facebook"
76
+ gem "omniauth-twitter"
77
+ ```
78
+ ## Contributing
79
+
80
+ 1. Fork it
81
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
82
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
83
+ 4. Push to the branch (`git push origin my-new-feature`)
84
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'domp/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "domp"
8
+ gem.version = Domp::VERSION
9
+ gem.authors = ["Alexander Zaytsev"]
10
+ gem.email = ["alexander@say26.com"]
11
+ gem.description = %q{Devise Omniauth Multiple Providers}
12
+ gem.summary = %q{Generator to bootstrap usage of multiple providers with Devise and Omniauth}
13
+ gem.homepage = "https://github.com/AlexanderZaytsev/domp"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,5 @@
1
+ require "domp/version"
2
+
3
+ module Domp
4
+
5
+ end
@@ -0,0 +1,3 @@
1
+ module Domp
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,12 @@
1
+ Description:
2
+ Generator to bootstrap usage of multiple providers with Devise and Omniauth
3
+
4
+ Example:
5
+ rails generate domp User facebook twitter
6
+
7
+ This will create:
8
+ create app/models/authentication_provider.rb
9
+ create app/models/user_authentication.rb
10
+ create db/migrate/20130118200452_create_authentication_providers.rb
11
+ create db/migrate/20130118200453_create_user_authentications.rb
12
+ create app/controllers/users/omniauth_callbacks_controller.rb
@@ -0,0 +1,75 @@
1
+ require 'bundler'
2
+ class DompGenerator < Rails::Generators::NamedBase
3
+ source_root File.expand_path('../templates', __FILE__)
4
+ argument :providers, type: :array, banner: "provider"
5
+
6
+ def check_if_devise_is_installed
7
+ unless File.exists?("config/initializers/devise.rb")
8
+ puts "Devise not found. Install it first."
9
+ exit
10
+ end
11
+ end
12
+
13
+ def check_if_devise_has_generated_model
14
+ unless File.exists?("app/models/#{file_name}.rb")
15
+ puts "Could not find `app/models/#{file_name}.rb`. You need to generate it with Devise first."
16
+ exit
17
+ end
18
+ end
19
+
20
+ def update_devise_config
21
+ omniauth_config = []
22
+
23
+ providers.each do |provider|
24
+ id = ask("#{provider.capitalize} application ID:")
25
+ secret = ask("#{provider.capitalize} application secret:")
26
+ omniauth_config << "\n config.omniauth :#{provider}, '#{id}', '#{secret}'\n"
27
+ end
28
+
29
+ inject_into_file 'config/initializers/devise.rb', after: "# config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo'" do
30
+ omniauth_config.join('')
31
+ end
32
+ end
33
+
34
+ def update_model_class
35
+ inject_into_class "app/models/#{file_name}.rb", class_name do
36
+ " has_many :authentications, class_name: '#{class_name}Authentication'\n"
37
+ end
38
+
39
+ inject_into_file "app/models/#{file_name}.rb", after: " devise" do
40
+ " :omniauthable, "
41
+ end
42
+ end
43
+
44
+ def update_gemfile
45
+ gem 'omniauth'
46
+ providers.each do |provider|
47
+ gem "omniauth-#{provider}"
48
+ end
49
+ end
50
+
51
+ def update_routes
52
+ inject_into_file 'config/routes.rb', after: "devise_for :#{table_name}" do
53
+ ", controllers: { omniauth_callbacks: '#{table_name}/omniauth_callbacks' }"
54
+ end
55
+ end
56
+
57
+ def copy_templates
58
+ template 'authentication_provider.rb', 'app/models/authentication_provider.rb'
59
+ template "model_authentication.rb", "app/models/#{file_name}_authentication.rb"
60
+ template "authentication_providers_migration.rb", "db/migrate/#{Time.now.strftime('%Y%m%d%H%M%S')}_create_authentication_providers.rb"
61
+ template "model_authentications_migration.rb", "db/migrate/#{(Time.now + 1).strftime('%Y%m%d%H%M%S')}_create_#{class_name.downcase}_authentications.rb"
62
+ template "omniauth_callbacks_controller.rb", "app/controllers/#{table_name}/omniauth_callbacks_controller.rb"
63
+ end
64
+
65
+ def bundle
66
+ Bundler.with_clean_env do
67
+ run 'bundle install'
68
+ end
69
+ end
70
+
71
+ def migrate
72
+ rake 'db:migrate'
73
+ end
74
+
75
+ end
@@ -0,0 +1,8 @@
1
+ <% module_namespacing do -%>
2
+ class AuthenticationProvider < ActiveRecord::Base
3
+ has_many :<%= table_name %>
4
+ <% if Rails::VERSION::MAJOR < 4 -%>
5
+ attr_accessible :name
6
+ <% end -%>
7
+ end
8
+ <% end -%>
@@ -0,0 +1,16 @@
1
+ <% module_namespacing do -%>
2
+ class CreateAuthenticationProviders < ActiveRecord::Migration
3
+ def change
4
+ create_table "authentication_providers", :force => true do |t|
5
+ t.string "name"
6
+ t.datetime "created_at", :null => false
7
+ t.datetime "updated_at", :null => false
8
+ end
9
+ add_index "authentication_providers", ["name"], :name => "index_name_on_authentication_providers"
10
+ <% providers.each do |provider| -%>
11
+ AuthenticationProvider.create(name: '<%= provider %>')
12
+ <% end -%>
13
+ end
14
+ end
15
+ <% end -%>
16
+
@@ -0,0 +1,23 @@
1
+ <% module_namespacing do -%>
2
+ class <%= class_name %>Authentication < ActiveRecord::Base
3
+ belongs_to :<%= class_name.downcase %>
4
+ belongs_to :authentication_provider
5
+
6
+ serialize :params
7
+
8
+ <% if Rails::VERSION::MAJOR < 4 -%>
9
+ attr_accessible :user_id, :authentication_provider_id, :uid, :token, :token_expires_at, :params
10
+
11
+ <% end -%>
12
+ def self.create_from_omniauth(params, <%= class_name.downcase %>, provider)
13
+ create(
14
+ <%= class_name.downcase %>: <%= class_name.downcase %>,
15
+ authentication_provider: provider,
16
+ uid: params['uid'],
17
+ token: params['credentials']['token'],
18
+ token_expires_at: params['credentials']['expires_at'],
19
+ params: params,
20
+ )
21
+ end
22
+ end
23
+ <% end -%>
@@ -0,0 +1,19 @@
1
+ <% module_namespacing do -%>
2
+ class Create<%= class_name %>Authentications < ActiveRecord::Migration
3
+ def change
4
+ create_table "<%= class_name.downcase %>_authentications", :force => true do |t|
5
+ t.integer "<%= class_name.downcase %>_id"
6
+ t.integer "authentication_provider_id"
7
+ t.string "uid"
8
+ t.string "token"
9
+ t.datetime "token_expires_at"
10
+ t.text "params"
11
+ t.datetime "created_at", :null => false
12
+ t.datetime "updated_at", :null => false
13
+ end
14
+ add_index "<%= class_name.downcase %>_authentications", ["authentication_provider_id"], :name => "index_<%= class_name.downcase %>_authentications_on_authentication_provider_id"
15
+ add_index "<%= class_name.downcase %>_authentications", ["<%= class_name.downcase %>_id"], :name => "index_<%= class_name.downcase %>_authentications_on_user_id"
16
+ end
17
+ end
18
+ <% end -%>
19
+
@@ -0,0 +1,46 @@
1
+ <% module_namespacing do -%>
2
+ class <%= class_name %>::OmniauthCallbacksController < Devise::OmniauthCallbacksController
3
+
4
+ <% providers.each do |provider| -%>
5
+ def <%= provider %>
6
+ create
7
+ end
8
+
9
+ <% end -%>
10
+ private
11
+
12
+ def create
13
+ auth_params = request.env["omniauth.auth"]
14
+ provider = AuthenticationProvider.where(name: auth_params.provider).first
15
+
16
+ authentication = provider.<%= class_name.downcase %>_authentications.where(uid: auth_params.uid).first
17
+ if authentication
18
+ sign_in_with_existing_authentication(authentication)
19
+ elsif <%= class_name.downcase %>_signed_in?
20
+ create_authentication_and_sign_in(auth_params, current_<%= class_name.downcase %>, provider)
21
+ else
22
+ create_<%= class_name.downcase %>_and_authentication_and_sign_in(auth_params, provider)
23
+ end
24
+ end
25
+
26
+ def sign_in_with_existing_authentication(authentication)
27
+ sign_in_and_redirect(:<%= class_name.downcase %>, authentication.<%= class_name.downcase %>)
28
+ end
29
+
30
+ def create_authentication_and_sign_in(auth_params, <%= class_name.downcase %>, provider)
31
+ <%= class_name %>Authentication.create_from_omniauth(auth_params, <%= class_name.downcase %>, provider)
32
+
33
+ sign_in_and_redirect(:<%= class_name.downcase %>, <%= class_name.downcase %>)
34
+ end
35
+
36
+ def create_<%= class_name.downcase %>_and_authentication_and_sign_in(auth_params, provider)
37
+ <%= class_name.downcase %> = <%= class_name %>.create_from_omniauth(auth_params)
38
+ if <%= class_name.downcase %>.valid?
39
+ create_authentication_and_sign_in(auth_params, <%= class_name.downcase %>, provider)
40
+ else
41
+ flash[:error] = <%= class_name.downcase %>.errors.full_messages.first
42
+ redirect_to new_<%= class_name.downcase %>_registration_url
43
+ end
44
+ end
45
+ end
46
+ <% end -%>
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: domp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexander Zaytsev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-18 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Devise Omniauth Multiple Providers
15
+ email:
16
+ - alexander@say26.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - domp.gemspec
27
+ - lib/domp.rb
28
+ - lib/domp/version.rb
29
+ - lib/generators/domp/USAGE
30
+ - lib/generators/domp/domp_generator.rb
31
+ - lib/generators/domp/templates/authentication_provider.rb
32
+ - lib/generators/domp/templates/authentication_providers_migration.rb
33
+ - lib/generators/domp/templates/model_authentication.rb
34
+ - lib/generators/domp/templates/model_authentications_migration.rb
35
+ - lib/generators/domp/templates/omniauth_callbacks_controller.rb
36
+ homepage: https://github.com/AlexanderZaytsev/domp
37
+ licenses: []
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 1.8.24
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Generator to bootstrap usage of multiple providers with Devise and Omniauth
60
+ test_files: []