mac_generators 0.0.1 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/generators/authentication/email/USAGE +39 -0
- data/lib/generators/authentication/email/email_generator.rb +128 -0
- data/lib/generators/authentication/{templates → email/templates}/create_identities.rb +1 -1
- data/lib/generators/authentication/email/templates/database_authentication.rb +16 -0
- data/lib/generators/authentication/{templates → email/templates}/erb/identity_new.html.erb +0 -0
- data/lib/generators/authentication/email/templates/erb/session_new.html.erb +15 -0
- data/lib/generators/authentication/{templates → email/templates}/haml/identity_new.html.haml +0 -0
- data/lib/generators/authentication/email/templates/haml/session_new.html.haml +15 -0
- data/lib/generators/authentication/{templates → email/templates}/identities_controller.rb +1 -1
- data/lib/generators/authentication/email/templates/identity.rb +6 -0
- data/lib/generators/authentication/email/templates/sessions_controller.rb +16 -0
- data/lib/generators/authentication/email/templates/warden.rb +19 -0
- data/lib/generators/authentication/omniauth/USAGE +32 -0
- data/lib/generators/authentication/omniauth/omniauth_generator.rb +121 -0
- data/lib/generators/authentication/omniauth/templates/authentication_domain.rb +1 -0
- data/lib/generators/authentication/omniauth/templates/create_identities.rb +12 -0
- data/lib/generators/authentication/omniauth/templates/identity.rb +9 -0
- data/lib/generators/authentication/omniauth/templates/oauth_authentication.rb +36 -0
- data/lib/generators/authentication/omniauth/templates/omniauth.rb +3 -0
- data/lib/generators/authentication/omniauth/templates/sessions_controller.rb +12 -0
- data/lib/generators/authentication/omniauth/templates/warden.rb +19 -0
- data/lib/mac_generators/version.rb +1 -1
- data/test/dummy/app/controllers/application_controller.rb +24 -0
- data/test/dummy/config/database.yml +3 -3
- data/test/dummy/config/environments/test.rb +0 -3
- data/test/dummy/config/locales/en.yml +14 -0
- data/test/dummy/config/routes.rb +5 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +3560 -0
- data/test/dummy/tmp/Gemfile +3 -0
- data/test/dummy/tmp/app/controllers/application_controller.rb +27 -0
- data/test/dummy/tmp/app/controllers/sessions_controller.rb +12 -0
- data/test/dummy/tmp/app/models/identity.rb +9 -0
- data/test/dummy/tmp/config/initializers/authentication_domain.rb +1 -0
- data/test/dummy/tmp/config/initializers/omniauth.rb +3 -0
- data/test/dummy/tmp/config/initializers/warden.rb +19 -0
- data/test/dummy/tmp/config/locales/en.yml +10 -0
- data/test/dummy/tmp/config/routes.rb +4 -0
- data/test/dummy/tmp/db/migrate/create_identities.rb +12 -0
- data/test/dummy/tmp/lib/strategies/oauth_authentication.rb +36 -0
- data/test/fixtures/Gemfile +0 -0
- data/test/fixtures/application_controller.rb +3 -0
- data/test/fixtures/en.yml +1 -0
- data/test/fixtures/routes.rb +2 -0
- data/test/generators/authentication_email_generator_test.rb +141 -0
- data/test/generators/authentication_omniauth_generator_test.rb +107 -0
- data/test/support/generators_test_helper.rb +51 -0
- data/test/test_helper.rb +20 -0
- metadata +115 -37
- data/README +0 -3
- data/lib/generators/authentication/USAGE +0 -8
- data/lib/generators/authentication/authentication_generator.rb +0 -89
- data/lib/generators/authentication/templates/erb/session_new.html.erb +0 -14
- data/lib/generators/authentication/templates/haml/session_new.html.haml +0 -14
- data/lib/generators/authentication/templates/identity.rb +0 -26
- data/lib/generators/authentication/templates/sessions_controller.rb +0 -21
- data/test/mac_generators_test.rb +0 -7
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b4a91211b48619c200b3a6d3ac7df516d6a61b60
|
4
|
+
data.tar.gz: 441a52f6322a8d7de17fbe8b2f71c1d47974508b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e650342c2d53508119153a64f848d0d6fa69a17224094b65fe6edf37feaca9463431a37a394bc890b938acb153769d241a192a204b668084791c3b8fc51955ef
|
7
|
+
data.tar.gz: 7f496bde084ac9cdb7f370b3c461994ace1c394a5d1d928f4801359997fe6559747f46ff622f7796b101518c3c34609ca7a89f514c2de73e858c0c7f7549a5be
|
@@ -0,0 +1,39 @@
|
|
1
|
+
Description:
|
2
|
+
Generates files for email/password authentication, based on Rails
|
3
|
+
has_secure_password functionality.
|
4
|
+
It uses warden with a single database authentication strategy.
|
5
|
+
|
6
|
+
By default without parameters all code will be generated for a model Identity
|
7
|
+
which will be used for authetication purposes.
|
8
|
+
|
9
|
+
If you want to generate authentication for another model than Identity then
|
10
|
+
pass it as a first parameter.
|
11
|
+
|
12
|
+
Also if you want signup and signin templates to be haml files pass the option
|
13
|
+
--haml, otherwise they will be erb.
|
14
|
+
|
15
|
+
Example:
|
16
|
+
rails generate authentication:email
|
17
|
+
|
18
|
+
This will create:
|
19
|
+
app/controllers/identities_controller.rb
|
20
|
+
app/controllers/sessions_controller.rb
|
21
|
+
app/views/identities/new.html.erb
|
22
|
+
app/views/sessions/new.html.erb
|
23
|
+
app/models/identity.rb
|
24
|
+
config/initializers/warden.rb
|
25
|
+
lib/strategies/database_authentication.rb
|
26
|
+
|
27
|
+
And will modify:
|
28
|
+
app/controllers/application_controller.rb
|
29
|
+
config/locales/en.yml
|
30
|
+
|
31
|
+
And will add the following routes:
|
32
|
+
route get 'sign_up' => 'identities#new', as: :sign_up
|
33
|
+
route get 'log_in' => 'sessions#new', as: :log_in
|
34
|
+
route get 'log_out' => 'sessions#destroy', as: :log_out
|
35
|
+
route resource :identity, only: [:create, :new]
|
36
|
+
route resource :sessions, only: [:create, :new]
|
37
|
+
|
38
|
+
And finally will add to Gemfile:
|
39
|
+
warden (~> 1.2.0)
|
@@ -0,0 +1,128 @@
|
|
1
|
+
module Authentication
|
2
|
+
module Generators
|
3
|
+
class EmailGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
argument :resource_name, :type => :string, :default => 'identity'
|
6
|
+
class_option :haml, type: :boolean, default: false, description: 'Generate haml templates'
|
7
|
+
|
8
|
+
def copy_controller_files
|
9
|
+
template 'identities_controller.rb', File.join('app/controllers', "#{resource_pluralize}_controller.rb")
|
10
|
+
template 'sessions_controller.rb', 'app/controllers/sessions_controller.rb'
|
11
|
+
end
|
12
|
+
|
13
|
+
def copy_view_files
|
14
|
+
if options[:haml]
|
15
|
+
template 'haml/identity_new.html.haml', "app/views/#{resource_pluralize}/new.html.haml"
|
16
|
+
template 'haml/session_new.html.haml', "app/views/sessions/new.html.haml"
|
17
|
+
else
|
18
|
+
template 'erb/identity_new.html.erb', "app/views/#{resource_pluralize}/new.html.erb"
|
19
|
+
template 'erb/session_new.html.erb', "app/views/sessions/new.html.erb"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def add_routes
|
24
|
+
route "get 'sign_up' => '#{resource_pluralize}#new', as: :sign_up"
|
25
|
+
route "get 'log_in' => 'sessions#new', as: :log_in"
|
26
|
+
route "delete 'log_out' => 'sessions#destroy', as: :log_out"
|
27
|
+
|
28
|
+
route "resource :#{resource_name}, only: [:create, :new]"
|
29
|
+
route "resource :sessions, only: [:create, :new]"
|
30
|
+
end
|
31
|
+
|
32
|
+
def generate_user
|
33
|
+
if Dir["db/migrate/*create_#{resource_pluralize}.rb"].empty?
|
34
|
+
template 'create_identities.rb', "db/migrate/#{migration_name}"
|
35
|
+
end
|
36
|
+
template 'identity.rb', "app/models/#{resource_name}.rb"
|
37
|
+
end
|
38
|
+
|
39
|
+
def add_helper_methods
|
40
|
+
insert_into_file 'app/controllers/application_controller.rb', after: /:exception/ do
|
41
|
+
<<-EOS
|
42
|
+
|
43
|
+
|
44
|
+
helper_method :current_#{resource_name}, :#{resource_name}_signed_in?, :warden_message
|
45
|
+
|
46
|
+
protected
|
47
|
+
def current_#{resource_name}
|
48
|
+
warden.user(scope: :#{resource_name})
|
49
|
+
end
|
50
|
+
|
51
|
+
def #{resource_name}_signed_in?
|
52
|
+
warden.authenticate?(scope: :#{resource_name})
|
53
|
+
end
|
54
|
+
|
55
|
+
def authenticate!
|
56
|
+
redirect_to root_path, notice: t('.not_logged') unless #{resource_name}_signed_in?
|
57
|
+
end
|
58
|
+
|
59
|
+
def warden_message
|
60
|
+
warden.message
|
61
|
+
end
|
62
|
+
|
63
|
+
def warden
|
64
|
+
request.env['warden']
|
65
|
+
end
|
66
|
+
EOS
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
def add_gems
|
72
|
+
gem 'warden', '~> 1.2.0'
|
73
|
+
gem 'bcrypt-ruby'
|
74
|
+
end
|
75
|
+
|
76
|
+
def add_translations
|
77
|
+
insert_into_file "config/locales/en.yml", after: 'en:' do
|
78
|
+
<<-EOS
|
79
|
+
|
80
|
+
sessions:
|
81
|
+
new:
|
82
|
+
log_in: 'Log in'
|
83
|
+
create:
|
84
|
+
invalid_credentials: 'Your credentials are invalid'
|
85
|
+
logged_in: 'Welcome back!'
|
86
|
+
destroy:
|
87
|
+
logged_out: 'See you later!'
|
88
|
+
#{resource_pluralize}:
|
89
|
+
new:
|
90
|
+
create: 'Create #{resource_name}'
|
91
|
+
create:
|
92
|
+
sign_up: 'Welcome to your new account!'
|
93
|
+
EOS
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def copy_warden_file
|
98
|
+
template 'warden.rb', File.join('config', 'initializers', 'warden.rb')
|
99
|
+
end
|
100
|
+
|
101
|
+
def copy_warden_strategies
|
102
|
+
template 'database_authentication.rb', File.join('lib', 'strategies', 'database_authentication.rb')
|
103
|
+
end
|
104
|
+
|
105
|
+
def instructions
|
106
|
+
message = "There are a few manual steps that you need to take care of\n\n"
|
107
|
+
message << "1. Run bundle command to install new gems.\n"
|
108
|
+
message << "2. Be sure that to have definition for root in your routes.\n"
|
109
|
+
message << "3. Run rake db:migrate to add your #{resource_pluralize} table.\n"
|
110
|
+
message << "4. Inspect warden initializer at config/initializers/warden.rb\n"
|
111
|
+
message << " and update the failure_app if need it.\n"
|
112
|
+
message << "5. Inspect generated files and learn how authentication was implemented.\n\n"
|
113
|
+
|
114
|
+
puts message
|
115
|
+
end
|
116
|
+
|
117
|
+
private
|
118
|
+
def migration_name
|
119
|
+
date = (DateTime.now.strftime "%Y %m %d %H %M %S").gsub(' ', '')
|
120
|
+
"#{date}_create_#{resource_pluralize}.rb"
|
121
|
+
end
|
122
|
+
|
123
|
+
def resource_pluralize
|
124
|
+
@resource_pluralize ||= resource_name.pluralize
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Strategies
|
2
|
+
class DatabaseAuthentication < ::Warden::Strategies::Base
|
3
|
+
def valid?
|
4
|
+
params['<%= resource_name %>'].present?
|
5
|
+
end
|
6
|
+
|
7
|
+
def authenticate!
|
8
|
+
<%= resource_name %> = <%= resource_name.classify %>.find_by_email(params['<%= resource_name %>']['email']).try(:authenticate, params['<%= resource_name %>']['password'])
|
9
|
+
|
10
|
+
return success! <%= resource_name %> if <%= resource_name %>
|
11
|
+
fail! I18n.t('sessions.create.invalid_credentials')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
Warden::Strategies.add(:database_authentication, Strategies::DatabaseAuthentication)
|
File without changes
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<%%= form_for @<%= resource_name %>, url: sessions_path do |form| %>
|
2
|
+
<%%- if warden_message %>
|
3
|
+
<div class='alert'><%%= warden_message %></div>
|
4
|
+
<%% end %>
|
5
|
+
|
6
|
+
<p>
|
7
|
+
<%%= form.label :email %><br />
|
8
|
+
<%%= form.text_field :email %>
|
9
|
+
</p>
|
10
|
+
<p>
|
11
|
+
<%%= form.label :password %><br />
|
12
|
+
<%%= form.password_field :password %>
|
13
|
+
</p>
|
14
|
+
<p class='button'><%%= form.submit t('.log_in') %></p>
|
15
|
+
<%% end %>
|
data/lib/generators/authentication/{templates → email/templates}/haml/identity_new.html.haml
RENAMED
File without changes
|
@@ -0,0 +1,15 @@
|
|
1
|
+
= form_for @<%= resource_name %>, url: sessions_path do |form|
|
2
|
+
- if warden_message
|
3
|
+
.alert
|
4
|
+
= warden_message
|
5
|
+
|
6
|
+
%p
|
7
|
+
= form.label :email
|
8
|
+
%br/
|
9
|
+
= form.text_field :email
|
10
|
+
%p
|
11
|
+
= form.label :password
|
12
|
+
%br/
|
13
|
+
= form.password_field :password
|
14
|
+
%p.button
|
15
|
+
= form.submit t('.log_in')
|
@@ -8,7 +8,7 @@ class <%= resource_pluralize.capitalize %>Controller < ApplicationController
|
|
8
8
|
@<%= resource_name %> = <%= resource_name.classify %>.new <%= resource_name %>_params
|
9
9
|
|
10
10
|
if @<%= resource_name %>.save
|
11
|
-
|
11
|
+
warden.set_user(@<%= resource_name %>, scope: :<%=resource_name %>)
|
12
12
|
redirect_to root_url, notice: t('.sign_up')
|
13
13
|
else
|
14
14
|
render :new
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class SessionsController < ApplicationController
|
2
|
+
def new
|
3
|
+
@<%= resource_name %> = <%= resource_name.classify %>.new
|
4
|
+
end
|
5
|
+
|
6
|
+
def create
|
7
|
+
warden.authenticate!(scope: :<%= resource_name %>)
|
8
|
+
redirect_to root_url, notice: t('.logged_in')
|
9
|
+
end
|
10
|
+
|
11
|
+
def destroy
|
12
|
+
warden.logout(:<%=resource_name %>)
|
13
|
+
redirect_to root_url, notice: t('.logged_out')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
load File.expand_path("../../../lib/strategies/database_authentication.rb", __FILE__)
|
2
|
+
Rails.application.config.middleware.use Warden::Manager do |manager|
|
3
|
+
manager.default_strategies :database_authentication
|
4
|
+
|
5
|
+
# TODO: Setup warden's failure app, this will be called everytime that
|
6
|
+
# and authentication failure happen.
|
7
|
+
# Failure app should be a Rack application.
|
8
|
+
# In Rails a controller can be used as a Rack app, just specify the
|
9
|
+
# controller and the action to be called. Example:
|
10
|
+
manager.failure_app = lambda { |env| SessionsController.action(:new).call(env) }
|
11
|
+
end
|
12
|
+
|
13
|
+
Warden::Manager.serialize_into_session(:<%= resource_name %>) do |<%= resource_name %>|
|
14
|
+
<%= resource_name %>.id
|
15
|
+
end
|
16
|
+
|
17
|
+
Warden::Manager.serialize_from_session(:<%= resource_name %>) do |id|
|
18
|
+
<%= resource_name.classify %>.find(id)
|
19
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
Description:
|
2
|
+
Generates files for oauth authentication using omniauth.
|
3
|
+
It uses warden with a single oauth authentication strategy.
|
4
|
+
|
5
|
+
By default without parameters all code will be generated for a model Identity
|
6
|
+
which will be used for authetication purposes.
|
7
|
+
|
8
|
+
If you want to generate authentication for another model than Identity then
|
9
|
+
pass it as a first parameter.
|
10
|
+
|
11
|
+
Example:
|
12
|
+
rails generate authentication:omniauth
|
13
|
+
|
14
|
+
This will create:
|
15
|
+
app/controllers/sessions_controller.rb
|
16
|
+
app/models/identity.rb
|
17
|
+
config/initializers/warden.rb
|
18
|
+
config/initializers/omniauth.rb
|
19
|
+
config/initializers/authentication_domain.rb
|
20
|
+
lib/strategies/oauth_authentication.rb
|
21
|
+
|
22
|
+
And will modify:
|
23
|
+
app/controllers/application_controller.rb
|
24
|
+
config/locales/en.yml
|
25
|
+
|
26
|
+
And will add the following routes:
|
27
|
+
route get 'auth/:provider/callback' => 'sessions#create', as: :log_in
|
28
|
+
route delete '/sessions/destroy' => 'sessions#destroy', as: :log_out
|
29
|
+
|
30
|
+
And finally will add to Gemfile:
|
31
|
+
warden (~> 1.2.0)
|
32
|
+
omniauth
|
@@ -0,0 +1,121 @@
|
|
1
|
+
module Authentication
|
2
|
+
module Generators
|
3
|
+
class OmniauthGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
argument :resource_name, :type => :string, :default => 'identity'
|
6
|
+
|
7
|
+
def copy_controller_files
|
8
|
+
template 'sessions_controller.rb', 'app/controllers/sessions_controller.rb'
|
9
|
+
end
|
10
|
+
|
11
|
+
def add_routes
|
12
|
+
route "get 'auth/:provider/callback' => 'sessions#create', as: :log_in"
|
13
|
+
route "delete '/sessions/destroy' => 'sessions#destroy', as: :log_out"
|
14
|
+
end
|
15
|
+
|
16
|
+
def generate_user
|
17
|
+
if Dir["db/migrate/*create_#{resource_pluralize}.rb"].empty?
|
18
|
+
template 'create_identities.rb', "db/migrate/#{migration_name}"
|
19
|
+
end
|
20
|
+
template 'identity.rb', "app/models/#{resource_name}.rb"
|
21
|
+
end
|
22
|
+
|
23
|
+
def add_helper_methods
|
24
|
+
insert_into_file 'app/controllers/application_controller.rb', after: /:exception/ do
|
25
|
+
<<-EOS
|
26
|
+
|
27
|
+
|
28
|
+
helper_method :current_#{resource_name}, :#{resource_name}_signed_in?, :warden_message
|
29
|
+
|
30
|
+
protected
|
31
|
+
def current_#{resource_name}
|
32
|
+
warden.user(scope: :#{resource_name})
|
33
|
+
end
|
34
|
+
|
35
|
+
def #{resource_name}_signed_in?
|
36
|
+
warden.authenticate?(scope: :#{resource_name})
|
37
|
+
end
|
38
|
+
|
39
|
+
def authenticate!
|
40
|
+
redirect_to root_path, notice: t('.not_logged') unless #{resource_name}_signed_in?
|
41
|
+
end
|
42
|
+
|
43
|
+
def warden_message
|
44
|
+
warden.message
|
45
|
+
end
|
46
|
+
|
47
|
+
def warden
|
48
|
+
request.env['warden']
|
49
|
+
end
|
50
|
+
EOS
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
def add_gems
|
56
|
+
gem 'warden', '~> 1.2.0'
|
57
|
+
gem 'omniauth'
|
58
|
+
end
|
59
|
+
|
60
|
+
def add_translations
|
61
|
+
insert_into_file "config/locales/en.yml", after: 'en:' do
|
62
|
+
<<-EOS
|
63
|
+
|
64
|
+
sessions:
|
65
|
+
new:
|
66
|
+
log_in: 'Log in'
|
67
|
+
create:
|
68
|
+
unauthorized_domain: 'Sorry but your domain is not authorized'
|
69
|
+
logged_in: 'Welcome back!'
|
70
|
+
destroy:
|
71
|
+
logged_out: 'See you later!'
|
72
|
+
EOS
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def copy_warden_file
|
77
|
+
template 'warden.rb', File.join('config', 'initializers', 'warden.rb')
|
78
|
+
end
|
79
|
+
|
80
|
+
def copy_configuration
|
81
|
+
template 'authentication_domain.rb', File.join('config', 'initializers', 'authentication_domain.rb')
|
82
|
+
end
|
83
|
+
|
84
|
+
def copy_omniauth_configuration
|
85
|
+
template 'omniauth.rb', File.join('config', 'initializers', 'omniauth.rb')
|
86
|
+
end
|
87
|
+
|
88
|
+
def copy_warden_strategies
|
89
|
+
template 'oauth_authentication.rb', File.join('lib', 'strategies', 'oauth_authentication.rb')
|
90
|
+
end
|
91
|
+
|
92
|
+
def instructions
|
93
|
+
message = "There are a few manual steps that you need to take care of\n\n"
|
94
|
+
message << "1. Add an omniauth provider gem like twitter, facebook, etc..\n"
|
95
|
+
message << "2. Modify config/initializers/omniauth.rb and setup your provider\n"
|
96
|
+
message << " and your provider credentials.\n"
|
97
|
+
message << "3. Run bundle command to install new gems.\n"
|
98
|
+
message << "4. If you want to restrict access to a specific email domain.\n"
|
99
|
+
message << " modify config/initializers/authentication_domain.rb and add \n"
|
100
|
+
message << " your allowed domain.\n"
|
101
|
+
message << "5. Inspect warden initializer at config/initializers/warden.rb\n"
|
102
|
+
message << " and update the failure_app.\n"
|
103
|
+
message << "6. Be sure that to have definition for root in your routes.\n"
|
104
|
+
message << "7. Run rake db:migrate to add your #{resource_pluralize} table.\n"
|
105
|
+
message << "8. Inspect generated files and learn how authentication was implemented.\n\n"
|
106
|
+
|
107
|
+
puts message
|
108
|
+
end
|
109
|
+
|
110
|
+
private
|
111
|
+
def migration_name
|
112
|
+
date = (DateTime.now.strftime "%Y %m %d %H %M %S").gsub(' ', '')
|
113
|
+
"#{date}_create_#{resource_pluralize}.rb"
|
114
|
+
end
|
115
|
+
|
116
|
+
def resource_pluralize
|
117
|
+
@resource_pluralize ||= resource_name.pluralize
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
Rails.application.config.authentication_domain = ''
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Strategies
|
2
|
+
class OauthAuthentication < ::Warden::Strategies::Base
|
3
|
+
def valid?
|
4
|
+
request.env['omniauth.auth'].present?
|
5
|
+
end
|
6
|
+
|
7
|
+
def authenticate!
|
8
|
+
auth = request.env['omniauth.auth']
|
9
|
+
|
10
|
+
if authorized_domain?(auth)
|
11
|
+
<%= resource_name %> = <%= resource_name.classify %>.find_<%= resource_name %>(auth['uid'], auth['provider']) || create_<%= resource_name %>(auth)
|
12
|
+
return success! <%= resource_name %>
|
13
|
+
end
|
14
|
+
|
15
|
+
fail! I18n.t('sessions.create.unauthorized_domain')
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def authorized_domain?(auth)
|
20
|
+
if Rails.application.config.respond_to?(:authentication_domain) && Rails.application.config.authentication_domain.present?
|
21
|
+
return auth['info']['email'].split('@').last == Rails.application.config.authentication_domain
|
22
|
+
end
|
23
|
+
|
24
|
+
true
|
25
|
+
end
|
26
|
+
|
27
|
+
def create_<%= resource_name %>(auth)
|
28
|
+
params = { uid: auth['uid'], provider: auth['provider'],
|
29
|
+
name: auth['info']['name'], email: auth['info']['email'] }
|
30
|
+
|
31
|
+
<%= resource_name.classify %>.create! params
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
Warden::Strategies.add(:oauth_authentication, Strategies::OauthAuthentication)
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class SessionsController < ApplicationController
|
2
|
+
def create
|
3
|
+
warden.authenticate!(scope: :<%= resource_name %>)
|
4
|
+
redirect_to root_url, notice: t('.logged_in')
|
5
|
+
end
|
6
|
+
|
7
|
+
def destroy
|
8
|
+
warden.logout(:<%=resource_name %>)
|
9
|
+
redirect_to root_url, notice: t('.logged_out')
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
load File.expand_path("../../../lib/strategies/oauth_authentication.rb", __FILE__)
|
2
|
+
Rails.application.config.middleware.use Warden::Manager do |manager|
|
3
|
+
manager.default_strategies :oauth_authentication
|
4
|
+
|
5
|
+
# TODO: Setup warden's failure app, this will be called everytime that
|
6
|
+
# and authentication failure happen.
|
7
|
+
# Failure app should be a Rack application.
|
8
|
+
# In Rails a controller can be used as a Rack app, just specify the
|
9
|
+
# controller and the action to be called. Example:
|
10
|
+
#manager.failure_app = lambda { |env| HomeController.action(:index).call(env) }
|
11
|
+
end
|
12
|
+
|
13
|
+
Warden::Manager.serialize_into_session(:<%= resource_name %>) do |<%= resource_name %>|
|
14
|
+
<%= resource_name %>.id
|
15
|
+
end
|
16
|
+
|
17
|
+
Warden::Manager.serialize_from_session(:<%= resource_name %>) do |id|
|
18
|
+
<%= resource_name.classify %>.find(id)
|
19
|
+
end
|
@@ -2,4 +2,28 @@ class ApplicationController < ActionController::Base
|
|
2
2
|
# Prevent CSRF attacks by raising an exception.
|
3
3
|
# For APIs, you may want to use :null_session instead.
|
4
4
|
protect_from_forgery with: :exception
|
5
|
+
|
6
|
+
helper_method :current_identity, :identity_signed_in?, :warden_message
|
7
|
+
|
8
|
+
protected
|
9
|
+
def current_identity
|
10
|
+
warden.user(scope: :identity)
|
11
|
+
end
|
12
|
+
|
13
|
+
def identity_signed_in?
|
14
|
+
warden.authenticate?(scope: :identity)
|
15
|
+
end
|
16
|
+
|
17
|
+
def authenticate!
|
18
|
+
redirect_to root_path, notice: t('.not_logged') unless identity_signed_in?
|
19
|
+
end
|
20
|
+
|
21
|
+
def warden_message
|
22
|
+
warden.message
|
23
|
+
end
|
24
|
+
|
25
|
+
def warden
|
26
|
+
request.env['warden']
|
27
|
+
end
|
28
|
+
|
5
29
|
end
|
@@ -5,7 +5,7 @@
|
|
5
5
|
# gem 'sqlite3'
|
6
6
|
development:
|
7
7
|
adapter: sqlite3
|
8
|
-
database:
|
8
|
+
database: ":memory:"
|
9
9
|
pool: 5
|
10
10
|
timeout: 5000
|
11
11
|
|
@@ -14,12 +14,12 @@ development:
|
|
14
14
|
# Do not set this db to the same as development or production.
|
15
15
|
test:
|
16
16
|
adapter: sqlite3
|
17
|
-
database:
|
17
|
+
database: ":memory:"
|
18
18
|
pool: 5
|
19
19
|
timeout: 5000
|
20
20
|
|
21
21
|
production:
|
22
22
|
adapter: sqlite3
|
23
|
-
database:
|
23
|
+
database: ":memory:"
|
24
24
|
pool: 5
|
25
25
|
timeout: 5000
|
@@ -20,4 +20,18 @@
|
|
20
20
|
# available at http://guides.rubyonrails.org/i18n.html.
|
21
21
|
|
22
22
|
en:
|
23
|
+
sessions:
|
24
|
+
new:
|
25
|
+
log_in: 'Log in'
|
26
|
+
create:
|
27
|
+
invalid_credentials: 'Your credentials are invalid'
|
28
|
+
logged_in: 'Welcome back!'
|
29
|
+
destroy:
|
30
|
+
logged_out: 'See you later!'
|
31
|
+
identities:
|
32
|
+
new:
|
33
|
+
create: 'Create identity'
|
34
|
+
create:
|
35
|
+
sign_up: 'Welcome to your new account!'
|
36
|
+
|
23
37
|
hello: "Hello world"
|
data/test/dummy/config/routes.rb
CHANGED
@@ -1,4 +1,9 @@
|
|
1
1
|
Dummy::Application.routes.draw do
|
2
|
+
resource :sessions, only: [:create, :new]
|
3
|
+
resource :identity, only: [:create, :new]
|
4
|
+
get 'log_out' => 'sessions#destroy', as: :log_out
|
5
|
+
get 'log_in' => 'sessions#new', as: :log_in
|
6
|
+
get 'sign_up' => 'identities#new', as: :sign_up
|
2
7
|
# The priority is based upon order of creation: first created -> highest priority.
|
3
8
|
# See how all your routes lay out with "rake routes".
|
4
9
|
|
File without changes
|