omniauth-multiple_providers 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: db49c67f0650ab6e188a421fd7767ddd5ab2227c
4
+ data.tar.gz: 130a7f8a180fe7aa7de9dedbb4a481f28fa3e473
5
+ SHA512:
6
+ metadata.gz: 1d95ba43b2704dfd43e3cb6c6734ac981eb77def0170b5c83494ab7ed116e5d6fb70317517b9558346cb23457f0e8c2f3904f524858f3bb220dcc8a3811dcb29
7
+ data.tar.gz: a22b5abcf209e8cc5bd3c992a408e186f7cc436c3bab3be097f24475ca3132762461b540f547b3e5f4bdd3c02f03454ec19520d753a7affc115ad91ee96ca97e
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ *.swp
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 omniauth-multiple_providers.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 takuyan
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.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # Omniauth::MultipleProviders
2
+
3
+ Good parts for Omniauth and Multiple Providers.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'omniauth'
10
+ gem 'omniauth-facebook' # etc you needs
11
+ gem 'omniauth-twitter' # etc you needs
12
+ # ...
13
+ gem 'omniauth-multiple_providers'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install omniauth-multiple_providers
22
+
23
+ ## Usage
24
+
25
+ $ rails generate omniauth:multiple_providers:install
26
+ $ rake db:migrate # => create provider_users table
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,36 @@
1
+ module Omniauth
2
+ class MultipleProvidersController < ApplicationController
3
+ def new
4
+ if %w[twitter github google_oauth2 facebook].include?(params[:provider])
5
+ redirect_to "/auth/#{params[:provider]}"
6
+ else
7
+ redirect_to root_path, error: "#{params[:provider]}による認証は存在しません"
8
+ end
9
+ end
10
+
11
+ def create
12
+ @user = User.find_or_create_by_oauth(env['omniauth.auth'], current_user)
13
+ if @user.new_record?
14
+ redirect_to new_user_session_path, flash: {error: "ユーザデータの保存に失敗しました:#{@user.errors.full_messages.join(', ')}"}
15
+ elsif @user.email.blank?
16
+ redirect_to prepare_path
17
+ else
18
+ sign_in(@user)
19
+ redirect_to root_path, notice: 'OK'
20
+ end
21
+ end
22
+
23
+ def failure
24
+ redirect_to root_path, flash: {error: 'OAuth認証に失敗しました'}
25
+ end
26
+
27
+ def destroy
28
+ if up = current_user.provider_users.find_by(provider: params[:id])
29
+ up.destroy
30
+ redirect_to root_path, notice: "#{up.provider}の認証を削除しました。"
31
+ else
32
+ redirect_to root_path, notice: 'Does not exist your provider ID'
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,34 @@
1
+ require 'rails/generators'
2
+
3
+ module Omniauth
4
+ module MultipleProviders
5
+ module Generators
6
+ class InstallGenerator < ::Rails::Generators::Base
7
+ source_root File.expand_path("../templates", __FILE__)
8
+
9
+ def add_provider_user
10
+ copy_file 'provider_user.rb', 'app/models/provider_user.rb'
11
+ end
12
+
13
+ def create_provider_user
14
+ # FIXME add datetime require
15
+ copy_file 'create_provider_users.rb', "db/migrate/#{DateTime.now.strftime('%Y%m%d%H%M%S')}_create_provider_users.rb"
16
+ end
17
+
18
+ def insert_to_user
19
+ #insert_into_file 'app/models/user.rb', ' include Omniauth::MultipleProviders::Omniauthable', after: 'class User < ActiveRecord::Base'
20
+ inject_into_class 'app/models/user.rb', User do
21
+ " include Omniauth::MultipleProviders::Omniauthable\n"
22
+ end
23
+ end
24
+
25
+ def add_multiple_providers_routes
26
+ route "get '/auth/:provider/callback' => 'omniauth/multiple_providers#create'"
27
+ route "get '/auth/failure' => 'omniauth/multiple_providers#failure'"
28
+ route "resources :omniauth, only: [:new, :create, :failure, :destroy], controller: 'omniauth/multiple_providers'"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,25 @@
1
+ class CreateProviderUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table :provider_users do |t|
4
+ t.integer :user_id
5
+ t.string :email
6
+ t.string :provider
7
+ t.string :locale
8
+ t.string :gender
9
+ t.string :uid
10
+ t.string :access_token
11
+ t.string :refresh_token
12
+ t.string :secret
13
+ t.integer :expires_at
14
+ t.string :nickname
15
+ t.string :name
16
+ t.string :image_path
17
+
18
+ t.timestamps
19
+ end
20
+
21
+ add_index :provider_users, :user_id
22
+ add_index :provider_users, :provider
23
+ add_index :provider_users, :uid
24
+ end
25
+ end
@@ -0,0 +1,4 @@
1
+ class ProviderUser < ActiveRecord::Base
2
+ include Omniauth::MultipleProviders::AuthHashContainable
3
+ end
4
+
@@ -0,0 +1,80 @@
1
+ require 'active_support/concern'
2
+
3
+ module Omniauth
4
+ module MultipleProviders
5
+ #
6
+ # # Usage
7
+ # class ProviderUser < ActiveRecord::Base
8
+ # include Omniauth::MultipleProviders::AuthHashContainable
9
+ # end
10
+ module AuthHashContainable
11
+ extend ActiveSupport::Concern
12
+
13
+ included do
14
+ belongs_to :user
15
+ validates_presence_of :user_id, :provider, :uid
16
+ validates_uniqueness_of :provider, scope: :user_id
17
+ validates_uniqueness_of :uid, scope: :provider
18
+ end
19
+
20
+ # TODO use method_missing
21
+ def update_by(auth)
22
+ self.provider = auth['provider']
23
+ self.uid = auth['uid']
24
+ case auth['provider']
25
+ when 'twitter'
26
+ update_by_twitter(auth)
27
+ when 'facebook'
28
+ update_by_facebook(auth)
29
+ when 'github'
30
+ update_by_github(auth)
31
+ when 'google_oauth2'
32
+ update_by_google_oauth2(auth)
33
+ end
34
+ self.save
35
+ end
36
+
37
+ # USAGE: if you want customize, you just overwrite follow methods.
38
+ def update_by_twitter(auth)
39
+ self.access_token = auth['credentials']['token']
40
+ self.secret = auth['credentials']['secret']
41
+ self.name = auth['info']['nickname']
42
+ self.nickname = auth['info']['nickname']
43
+ self.image_path = auth['info']['image']
44
+ end
45
+
46
+ def update_by_facebook(auth)
47
+ self.access_token = auth['credentials']['token']
48
+ self.expires_at = auth['credentials']['expires_at']
49
+ self.email = auth['info']['email']
50
+ self.name = auth['info']['name']
51
+ self.nickname = auth['info']['nickname']
52
+ self.image_path = auth['info']['image']
53
+ self.gender = auth['extra']['raw_info']['gender']
54
+ self.locale = auth['extra']['raw_info']['locale']
55
+ end
56
+
57
+ def update_by_github(auth)
58
+ self.email = auth['info']['email']
59
+ self.name = auth['info']['name']
60
+ self.nickname = auth['info']['nickname']
61
+ self.image_path = auth['info']['image']
62
+ self.access_token = auth['credentials']['token']
63
+ self.secret = auth['credentials']['secret']
64
+ self.refresh_token = auth['credentials']['refresh_token']
65
+ self.expires_at = auth['credentials']['expires_at']
66
+ end
67
+
68
+ def update_by_google_oauth2(auth)
69
+ self.email = auth['info']['email']
70
+ self.name = auth['info']['name']
71
+ self.access_token = auth['credentials']['token']
72
+ self.refresh_token = auth['credentials']['refresh_token']
73
+ self.expires_at = auth['credentials']['expires_at']
74
+ self.image_path = auth['info']['image']
75
+ self.gender = auth['extra']['raw_info']['gender']
76
+ self.locale = auth['extra']['raw_info']['locale']
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,95 @@
1
+ require 'active_support/concern'
2
+
3
+ module Omniauth
4
+ module MultipleProviders
5
+ module Omniauthable
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ has_many :provider_users, dependent: :destroy
10
+ end
11
+
12
+ def create_or_update_provider_user_by(auth)
13
+ if provider_user = self.provider_users.find_by(uid: auth['uid'], provider: auth['provider'])
14
+ provider_user.update_by(auth)
15
+ else
16
+ provider_user = self.provider_users.build
17
+ provider_user.update_by(auth)
18
+ end
19
+ end
20
+
21
+ def oauthed?(provider_name)
22
+ if up = self.provider_users.find_by(provider: provider_name)
23
+ up
24
+ else
25
+ nil
26
+ end
27
+ end
28
+
29
+ module ClassMethods
30
+ def find_or_create_by_oauth(auth, current_user)
31
+ @user = if current_user
32
+ current_user.create_or_update_provider_user_by(auth)
33
+ current_user
34
+ else
35
+ if user = User.find_by_oauth(auth)
36
+ user.create_or_update_provider_user_by(auth)
37
+ user
38
+ else
39
+ user = User.create_by_oauth(auth)
40
+ user.save
41
+ user.create_or_update_provider_user_by(auth)
42
+ user
43
+ end
44
+ end
45
+ end
46
+
47
+ def find_by_oauth(auth)
48
+ if up = ProviderUser.find_by(uid: auth['uid'], provider: auth['provider'])
49
+ up.user
50
+ else
51
+ nil
52
+ end
53
+ end
54
+
55
+ def create_by_oauth(auth)
56
+ logger.debug "##### Auth Hash: #{auth.to_json}"
57
+ case auth['provider']
58
+ when 'twitter'
59
+ # Twitter not give me email from api
60
+ User.create_by_twitter(auth)
61
+ when 'facebook', 'google_oauth2', 'github'
62
+ # Other api give me email
63
+ User.create_by_omniauth(auth)
64
+ else
65
+ User.new
66
+ end
67
+ end
68
+
69
+ def create_by_omniauth(auth)
70
+ u = User.new
71
+ u.email = auth['info']['email']
72
+ u.name = auth['info']['name']
73
+ password = Devise.friendly_token[0,20]
74
+ u.password = password
75
+ u.password_confirmation = password
76
+ u.skip_confirmation! # Because we get confirmed (by providers) email !
77
+ u.save
78
+ u
79
+ end
80
+
81
+ def create_by_twitter(auth)
82
+ # https://github.com/arunagw/omniauth-twitter#authentication-hash
83
+ u = User.new
84
+ # u.email = auth['info']['email'] # Twitter not give me email from api
85
+ u.name = auth['info']['name']
86
+ password = Devise.friendly_token[0,20]
87
+ u.password = password
88
+ u.password_confirmation = password
89
+ u.save
90
+ u
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,5 @@
1
+ module Omniauth
2
+ module MultipleProviders
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ require "omniauth/multiple_providers/version"
2
+ require "omniauth/multiple_providers/models/concerns/omniauthable"
3
+ require "omniauth/multiple_providers/models/concerns/auth_hash_containable"
4
+ require 'rails/engine'
5
+
6
+ module Omniauth
7
+ module MultipleProviders
8
+ class Engine < ::Rails::Engine
9
+ isolate_namespace Omniauth::MultipleProviders
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'omniauth/multiple_providers/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "omniauth-multiple_providers"
8
+ spec.version = Omniauth::MultipleProviders::VERSION
9
+ spec.authors = ["Takuya Kato"]
10
+ spec.email = ["info@takuya.com"]
11
+ spec.description = %q{Support omniauth for multiple provider}
12
+ spec.summary = %q{Support omniauth for multiple provider}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency 'omniauth'
25
+ spec.add_dependency 'activesupport'
26
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-multiple_providers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Takuya Kato
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: omniauth
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Support omniauth for multiple provider
70
+ email:
71
+ - info@takuya.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - app/controllers/omniauth/multiple_providers_controller.rb
82
+ - lib/generators/omniauth/multiple_providers/install/install_generator.rb
83
+ - lib/generators/omniauth/multiple_providers/install/templates/create_provider_users.rb
84
+ - lib/generators/omniauth/multiple_providers/install/templates/provider_user.rb
85
+ - lib/omniauth/multiple_providers.rb
86
+ - lib/omniauth/multiple_providers/models/concerns/auth_hash_containable.rb
87
+ - lib/omniauth/multiple_providers/models/concerns/omniauthable.rb
88
+ - lib/omniauth/multiple_providers/version.rb
89
+ - omniauth-multiple_providers.gemspec
90
+ homepage: ''
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.2.0
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Support omniauth for multiple provider
114
+ test_files: []