mac_generators 0.0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- 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
@@ -0,0 +1,27 @@
|
|
1
|
+
class ApplicationController < ActionController::Base
|
2
|
+
protect_from_forgery with: :exception
|
3
|
+
|
4
|
+
helper_method :current_identity, :identity_signed_in?, :warden_message
|
5
|
+
|
6
|
+
protected
|
7
|
+
def current_identity
|
8
|
+
warden.user(scope: :identity)
|
9
|
+
end
|
10
|
+
|
11
|
+
def identity_signed_in?
|
12
|
+
warden.authenticate?(scope: :identity)
|
13
|
+
end
|
14
|
+
|
15
|
+
def authenticate!
|
16
|
+
redirect_to root_path, notice: t('.not_logged') unless identity_signed_in?
|
17
|
+
end
|
18
|
+
|
19
|
+
def warden_message
|
20
|
+
warden.message
|
21
|
+
end
|
22
|
+
|
23
|
+
def warden
|
24
|
+
request.env['warden']
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class SessionsController < ApplicationController
|
2
|
+
def create
|
3
|
+
warden.authenticate!(scope: :identity)
|
4
|
+
redirect_to root_url, notice: t('.logged_in')
|
5
|
+
end
|
6
|
+
|
7
|
+
def destroy
|
8
|
+
warden.logout(:identity)
|
9
|
+
redirect_to root_url, notice: t('.logged_out')
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
Rails.application.config.authentication_domain = ''
|
@@ -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(:identity) do |identity|
|
14
|
+
identity.id
|
15
|
+
end
|
16
|
+
|
17
|
+
Warden::Manager.serialize_from_session(:identity) do |id|
|
18
|
+
Identity.find(id)
|
19
|
+
end
|
@@ -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
|
+
identity = Identity.find_identity(auth['uid'], auth['provider']) || create_identity(auth)
|
12
|
+
return success! identity
|
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_identity(auth)
|
28
|
+
params = { uid: auth['uid'], provider: auth['provider'],
|
29
|
+
name: auth['info']['name'], email: auth['info']['email'] }
|
30
|
+
|
31
|
+
Identity.create! params
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
Warden::Strategies.add(:oauth_authentication, Strategies::OauthAuthentication)
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
en:
|
@@ -0,0 +1,141 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'generators/authentication/email/email_generator'
|
3
|
+
|
4
|
+
describe Authentication::Generators::EmailGenerator do
|
5
|
+
include GeneratorsTestHelper
|
6
|
+
|
7
|
+
before do
|
8
|
+
copy_routes
|
9
|
+
copy_locales
|
10
|
+
copy_gemfile
|
11
|
+
copy_application_controller
|
12
|
+
make_migrations_dir
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'copies controllers templates for user signup and session' do
|
16
|
+
run_generator
|
17
|
+
|
18
|
+
assert_file 'app/controllers/identities_controller.rb' do |m|
|
19
|
+
assert_match 'IdentitiesController', m
|
20
|
+
assert_match 'def new', m
|
21
|
+
assert_match 'def create', m
|
22
|
+
end
|
23
|
+
|
24
|
+
assert_file 'app/controllers/sessions_controller.rb' do |m|
|
25
|
+
assert_match 'SessionsController', m
|
26
|
+
assert_match 'def new', m
|
27
|
+
assert_match 'def create', m
|
28
|
+
assert_match 'def destroy', m
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'copies user_controller template for user class' do
|
33
|
+
run_generator ['user']
|
34
|
+
|
35
|
+
assert_file 'app/controllers/users_controller.rb' do |m|
|
36
|
+
assert_match 'UsersController', m
|
37
|
+
assert_match 'def new', m
|
38
|
+
assert_match 'def create', m
|
39
|
+
assert_match '@user = User.new', m
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'copies erb templates' do
|
44
|
+
run_generator
|
45
|
+
|
46
|
+
assert_file 'app/views/identities/new.html.erb' do |m|
|
47
|
+
assert_match 'form_for @identity, url: identity_path', m
|
48
|
+
end
|
49
|
+
|
50
|
+
assert_file 'app/views/sessions/new.html.erb' do |m|
|
51
|
+
assert_match 'form_for @identity, url: sessions_path', m
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'copies haml templates' do
|
56
|
+
run_generator ['--haml']
|
57
|
+
|
58
|
+
assert_file 'app/views/identities/new.html.haml' do |m|
|
59
|
+
assert_match 'form_for @identity, url: identity_path', m
|
60
|
+
end
|
61
|
+
|
62
|
+
assert_file 'app/views/sessions/new.html.haml' do |m|
|
63
|
+
assert_match 'form_for @identity, url: sessions_path', m
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'add routes' do
|
68
|
+
run_generator
|
69
|
+
|
70
|
+
assert_file 'config/routes.rb' do |m|
|
71
|
+
assert_match "get 'sign_up' => 'identities#new'", m
|
72
|
+
assert_match "get 'log_in' => 'sessions#new'", m
|
73
|
+
assert_match "delete 'log_out' => 'sessions#destroy'", m
|
74
|
+
|
75
|
+
assert_match "resource :identity, only: [:create, :new]", m
|
76
|
+
assert_match "resource :sessions, only: [:create, :new]", m
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'generate migration' do
|
81
|
+
Authentication::Generators::EmailGenerator.class_eval do
|
82
|
+
def migration_name
|
83
|
+
"create_#{resource_pluralize}.rb"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
run_generator
|
88
|
+
|
89
|
+
assert_file 'db/migrate/create_identities.rb' do |m|
|
90
|
+
assert_match 'create_table :identities', m
|
91
|
+
assert_match 't.string :email', m
|
92
|
+
assert_match 't.string :password_hash', m
|
93
|
+
assert_match 't.string :password_digest', m
|
94
|
+
end
|
95
|
+
|
96
|
+
assert_file 'app/models/identity.rb' do |m|
|
97
|
+
assert_match 'class Identity < ActiveRecord::Base', m
|
98
|
+
assert_match 'has_secure_password validations: true', m
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'add helper methods to application controller' do
|
103
|
+
run_generator
|
104
|
+
|
105
|
+
assert_file 'app/controllers/application_controller.rb' do |m|
|
106
|
+
assert_match 'helper_method :current_identity, :identity_signed_in?', m
|
107
|
+
assert_match 'def current_identity', m
|
108
|
+
assert_match 'def identity_signed_in?', m
|
109
|
+
assert_match 'def authenticate!', m
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'add warden gem to Gemfile' do
|
114
|
+
run_generator
|
115
|
+
|
116
|
+
assert_file 'Gemfile' do |m|
|
117
|
+
assert_match "gem \"warden\", \"~> 1.2.0\"", m
|
118
|
+
assert_match "gem \"bcrypt-ruby\"", m
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'copy warden configuration' do
|
123
|
+
run_generator
|
124
|
+
|
125
|
+
assert_file 'config/initializers/warden.rb' do |m|
|
126
|
+
assert_match 'manager.default_strategies :database_authentication', m
|
127
|
+
assert_match 'Warden::Manager.serialize_into_session(:identity)', m
|
128
|
+
assert_match 'Warden::Manager.serialize_from_session(:identity)', m
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'copy warden strategies' do
|
133
|
+
run_generator
|
134
|
+
|
135
|
+
assert_file 'lib/strategies/database_authentication.rb' do |m|
|
136
|
+
assert_match 'def valid?', m
|
137
|
+
assert_match 'authenticate!', m
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'generators/authentication/omniauth/omniauth_generator'
|
3
|
+
|
4
|
+
describe Authentication::Generators::OmniauthGenerator do
|
5
|
+
include GeneratorsTestHelper
|
6
|
+
|
7
|
+
before do
|
8
|
+
copy_routes
|
9
|
+
copy_locales
|
10
|
+
copy_gemfile
|
11
|
+
copy_application_controller
|
12
|
+
make_migrations_dir
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'copies controllers templates for session' do
|
16
|
+
run_generator
|
17
|
+
|
18
|
+
assert_file 'app/controllers/sessions_controller.rb' do |m|
|
19
|
+
assert_match 'SessionsController', m
|
20
|
+
assert_match 'def create', m
|
21
|
+
assert_match 'def destroy', m
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'add routes' do
|
26
|
+
run_generator
|
27
|
+
|
28
|
+
assert_file 'config/routes.rb' do |m|
|
29
|
+
assert_match "get 'auth/:provider/callback' => 'sessions#create'", m
|
30
|
+
assert_match "delete '/sessions/destroy' => 'sessions#destroy'", m
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'generate migration' do
|
35
|
+
Authentication::Generators::OmniauthGenerator.class_eval do
|
36
|
+
def migration_name
|
37
|
+
"create_#{resource_pluralize}.rb"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
run_generator
|
42
|
+
|
43
|
+
assert_file 'db/migrate/create_identities.rb' do |m|
|
44
|
+
assert_match 'create_table :identities', m
|
45
|
+
assert_match 't.string :provider', m
|
46
|
+
assert_match 't.string :uid', m
|
47
|
+
assert_match 't.string :name', m
|
48
|
+
assert_match 't.string :email', m
|
49
|
+
end
|
50
|
+
|
51
|
+
assert_file 'app/models/identity.rb' do |m|
|
52
|
+
assert_match 'class Identity < ActiveRecord::Base', m
|
53
|
+
assert_match 'def find_identity(uid, provider)', m
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'add helper methods to application controller' do
|
58
|
+
run_generator
|
59
|
+
|
60
|
+
assert_file 'app/controllers/application_controller.rb' do |m|
|
61
|
+
assert_match 'helper_method :current_identity, :identity_signed_in?', m
|
62
|
+
assert_match 'def current_identity', m
|
63
|
+
assert_match 'def identity_signed_in?', m
|
64
|
+
assert_match 'def authenticate!', m
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'add warden gem to Gemfile' do
|
69
|
+
run_generator
|
70
|
+
|
71
|
+
assert_file 'Gemfile' do |m|
|
72
|
+
assert_match "gem \"warden\", \"~> 1.2.0\"", m
|
73
|
+
assert_match "gem \"omniauth\"", m
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'copy warden configuration' do
|
78
|
+
run_generator
|
79
|
+
|
80
|
+
assert_file 'config/initializers/warden.rb' do |m|
|
81
|
+
assert_match 'manager.default_strategies :oauth_authentication', m
|
82
|
+
assert_match 'Warden::Manager.serialize_into_session(:identity)', m
|
83
|
+
assert_match 'Warden::Manager.serialize_from_session(:identity)', m
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'copy domain authentication configuration' do
|
88
|
+
run_generator
|
89
|
+
|
90
|
+
assert_file 'config/initializers/authentication_domain.rb'
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'copy omniauth configuration' do
|
94
|
+
run_generator
|
95
|
+
|
96
|
+
assert_file 'config/initializers/omniauth.rb'
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'copy warden strategies' do
|
100
|
+
run_generator
|
101
|
+
|
102
|
+
assert_file 'lib/strategies/oauth_authentication.rb' do |m|
|
103
|
+
assert_match 'def valid?', m
|
104
|
+
assert_match 'authenticate!', m
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module GeneratorsTestHelper
|
2
|
+
def self.included(base)
|
3
|
+
base.class_eval do
|
4
|
+
destination File.join(Rails.root, "tmp")
|
5
|
+
setup :prepare_destination
|
6
|
+
|
7
|
+
begin
|
8
|
+
base.tests Rails::Generators.const_get(base.name.sub(/Test$/, ''))
|
9
|
+
rescue
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def copy_routes
|
15
|
+
routes = File.expand_path("../../fixtures/routes.rb", __FILE__)
|
16
|
+
destination = File.join(destination_root, "config")
|
17
|
+
|
18
|
+
copy_file routes, destination
|
19
|
+
end
|
20
|
+
|
21
|
+
def copy_gemfile
|
22
|
+
gemfile = File.expand_path("../../fixtures/Gemfile", __FILE__)
|
23
|
+
|
24
|
+
copy_file gemfile, destination_root
|
25
|
+
end
|
26
|
+
|
27
|
+
def copy_application_controller
|
28
|
+
controller = File.expand_path("../../fixtures/application_controller.rb", __FILE__)
|
29
|
+
destination = File.join(destination_root, 'app', 'controllers')
|
30
|
+
|
31
|
+
copy_file controller, destination
|
32
|
+
end
|
33
|
+
|
34
|
+
def copy_locales
|
35
|
+
controller = File.expand_path("../../fixtures/en.yml", __FILE__)
|
36
|
+
destination = File.join(destination_root, 'config', 'locales')
|
37
|
+
|
38
|
+
copy_file controller, destination
|
39
|
+
end
|
40
|
+
|
41
|
+
def copy_file(file, destination)
|
42
|
+
FileUtils.mkdir_p(destination)
|
43
|
+
FileUtils.cp file, destination
|
44
|
+
end
|
45
|
+
|
46
|
+
def make_migrations_dir
|
47
|
+
destination = File.join(destination_root, 'db', 'migrate')
|
48
|
+
|
49
|
+
FileUtils.mkdir_p(destination)
|
50
|
+
end
|
51
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -3,6 +3,9 @@ ENV["RAILS_ENV"] = "test"
|
|
3
3
|
|
4
4
|
require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
5
5
|
require "rails/test_help"
|
6
|
+
require "minitest/rails"
|
7
|
+
require "minitest/focus"
|
8
|
+
require "minitest/colorize"
|
6
9
|
|
7
10
|
Rails.backtrace_cleaner.remove_silencers!
|
8
11
|
|
@@ -13,3 +16,20 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
|
13
16
|
if ActiveSupport::TestCase.method_defined?(:fixture_path=)
|
14
17
|
ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
|
15
18
|
end
|
19
|
+
|
20
|
+
require 'rails/generators/test_case'
|
21
|
+
class Rails::Generators::TestCase
|
22
|
+
register_spec_type(self) do |desc|
|
23
|
+
Class === desc && desc < Rails::Generators::Base
|
24
|
+
end
|
25
|
+
|
26
|
+
register_spec_type(/Generator( ?Test)?\z/i, self)
|
27
|
+
|
28
|
+
def self.determine_default_generator(name)
|
29
|
+
generator = determine_constant_from_test_name(name) do |constant|
|
30
|
+
Class === constant && constant < Rails::Generators::Base
|
31
|
+
end
|
32
|
+
raise NameError.new("Unable to resolve generator for #{name}") if generator.nil?
|
33
|
+
generator
|
34
|
+
end
|
35
|
+
end
|