simple_auth 2.0.4 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.travis.yml +6 -9
  4. data/CHANGELOG.md +4 -0
  5. data/Gemfile +1 -1
  6. data/MIGRATE.md +40 -0
  7. data/README.md +34 -137
  8. data/Rakefile +8 -18
  9. data/bin/console +5 -0
  10. data/gemfiles/{rails_4_1.gemfile → rails_4_2.gemfile} +1 -1
  11. data/gemfiles/{rails_4_0.gemfile → rails_5_0.gemfile} +1 -1
  12. data/lib/simple_auth.rb +26 -11
  13. data/lib/simple_auth/action_controller.rb +53 -81
  14. data/lib/simple_auth/action_controller/require_login_action.rb +47 -0
  15. data/lib/simple_auth/config.rb +13 -36
  16. data/lib/simple_auth/generator.rb +2 -2
  17. data/lib/simple_auth/railtie.rb +0 -11
  18. data/lib/simple_auth/session.rb +19 -143
  19. data/lib/simple_auth/templates/install/initializer.rb +23 -0
  20. data/lib/simple_auth/version.rb +1 -6
  21. data/simple_auth.gemspec +6 -3
  22. data/test/controllers/admin/dashboard_controller_test.rb +31 -0
  23. data/test/controllers/dashboard_controller_test.rb +56 -0
  24. data/test/controllers/pages_controller_test.rb +16 -0
  25. data/test/generators/install_test.rb +13 -0
  26. data/test/support/dummy/app/controllers/admin/dashboard_controller.rb +35 -0
  27. data/{spec/support → test/support/dummy}/app/controllers/application_controller.rb +0 -0
  28. data/test/support/dummy/app/controllers/dashboard_controller.rb +23 -0
  29. data/test/support/dummy/app/controllers/pages_controller.rb +7 -0
  30. data/{spec/support → test/support/dummy}/app/models/user.rb +1 -1
  31. data/test/support/dummy/config/application.rb +17 -0
  32. data/test/support/dummy/config/initializers/simple_auth.rb +23 -0
  33. data/test/support/dummy/config/routes.rb +23 -0
  34. data/test/support/schema.rb +6 -0
  35. data/test/test_helper.rb +15 -0
  36. metadata +75 -65
  37. data/.rspec +0 -1
  38. data/gemfiles/rails_3_1.gemfile +0 -5
  39. data/gemfiles/rails_3_2.gemfile +0 -5
  40. data/lib/simple_auth/active_record.rb +0 -95
  41. data/lib/simple_auth/compat.rb +0 -2
  42. data/lib/simple_auth/compat/active_record.rb +0 -28
  43. data/lib/simple_auth/compat/config.rb +0 -17
  44. data/lib/simple_auth/exceptions.rb +0 -4
  45. data/lib/simple_auth/helper.rb +0 -12
  46. data/lib/simple_auth/rspec.rb +0 -29
  47. data/locales/en.yml +0 -5
  48. data/locales/pt-BR.yml +0 -5
  49. data/spec/controllers/redirect_logged_user_spec.rb +0 -87
  50. data/spec/controllers/require_logged_user_spec.rb +0 -146
  51. data/spec/schema.rb +0 -9
  52. data/spec/simple_auth/active_record_spec.rb +0 -146
  53. data/spec/simple_auth/compat_spec.rb +0 -45
  54. data/spec/simple_auth/config_spec.rb +0 -21
  55. data/spec/simple_auth/helper_spec.rb +0 -24
  56. data/spec/simple_auth/initializer_spec.rb +0 -9
  57. data/spec/simple_auth/session_spec.rb +0 -212
  58. data/spec/spec_helper.rb +0 -23
  59. data/spec/support/app/models/customer.rb +0 -3
  60. data/spec/support/app/models/person.rb +0 -4
  61. data/spec/support/app/views/dashboard/index.erb +0 -0
  62. data/spec/support/app/views/session/new.erb +0 -0
  63. data/spec/support/config/boot.rb +0 -16
  64. data/spec/support/config/database.yml +0 -3
  65. data/spec/support/config/routes.rb +0 -4
  66. data/templates/initializer.rb +0 -22
@@ -0,0 +1,16 @@
1
+ require "test_helper"
2
+
3
+ class PagesControllerTest < ActionController::TestCase
4
+ setup do
5
+ @routes = Rails.application.routes
6
+ @controller.reset_session
7
+
8
+ User.delete_all
9
+ User.create!(password: "test", email: "john@example.com")
10
+ end
11
+
12
+ test "sets flash message while redirecting unlogged user" do
13
+ get :index
14
+ assert_equal "You must be logged in to access this page.", flash[:alert]
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ require "test_helper"
2
+
3
+ class InstallTest < Rails::Generators::TestCase
4
+ tests SimpleAuth::InstallGenerator
5
+ destination File.expand_path("../../tmp", File.dirname(__FILE__))
6
+ setup :prepare_destination
7
+ setup :run_generator
8
+
9
+ test "copies initializer" do
10
+ path = "#{__dir__}/../../tmp/config/initializers/simple_auth.rb"
11
+ assert_file File.expand_path(path)
12
+ end
13
+ end
@@ -0,0 +1,35 @@
1
+ require_relative "../application_controller"
2
+
3
+ module Admin
4
+ class DashboardController < ::ApplicationController
5
+ before_action :require_logged_admin, only: "index"
6
+
7
+ def index
8
+ head :ok
9
+ end
10
+
11
+ def log_in_as_user
12
+ user = User.create!(password: "test")
13
+ user_session.record = user
14
+ head :ok
15
+ end
16
+
17
+ def log_in_as_admin
18
+ user = User.create!(password: "test")
19
+ admin_session.record = user
20
+ head :ok
21
+ end
22
+
23
+ def log_in_with_admin_flag
24
+ user = User.create!(admin: true, password: "test")
25
+ user_session.record = user
26
+ head :ok
27
+ end
28
+
29
+ private
30
+
31
+ def authorized?
32
+ current_admin.present? || current_user.admin?
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,23 @@
1
+ class DashboardController < ApplicationController
2
+ before_action :require_logged_user, except: %w[log_in not_logged]
3
+ before_action :redirect_logged_user, only: "not_logged"
4
+
5
+ def index
6
+ head :ok
7
+ end
8
+
9
+ def log_in
10
+ user_session.record = User.last!
11
+ head :ok
12
+ end
13
+
14
+ def not_logged
15
+ head :ok
16
+ end
17
+
18
+ private
19
+
20
+ def authorized_user?
21
+ current_user.try(:email).to_s.match(/@example.com\z/)
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ class PagesController < ApplicationController
2
+ before_action :require_logged_user
3
+
4
+ def index
5
+ head :ok
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  class User < ActiveRecord::Base
2
- authentication
2
+ has_secure_password
3
3
  end
@@ -0,0 +1,17 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+
3
+ require "rails"
4
+ require "action_controller/railtie"
5
+
6
+ Bundler.require :default, :test
7
+
8
+ module Dummy
9
+ class Application < Rails::Application
10
+ config.eager_load = false
11
+ config.active_support.test_order = :random
12
+ config.secret_key_base = SecureRandom.hex(100)
13
+ config.i18n.load_path += Dir["#{__dir__}/config/locales/**/*.yml"]
14
+ end
15
+ end
16
+
17
+ Dummy::Application.initialize!
@@ -0,0 +1,23 @@
1
+ SimpleAuth.setup do |config|
2
+ # Define with scopes will be installed.
3
+ # This can be useful if you want to have separated sessions
4
+ # (e.g. regular user and admin user).
5
+ #
6
+ # To enable both user and admin sessions, you can define this options
7
+ # like this:
8
+ #
9
+ # config.scopes = %i[user admin]
10
+ #
11
+ config.scopes = %i[user admin]
12
+
13
+ # Set the login url.
14
+ # This is where users will be redirected to when they're unlogged.
15
+ config.login_url = proc { login_path }
16
+
17
+ # Logged users will be redirect to this url
18
+ # when `before_action :redirect_logged_user` filter is used.
19
+ config.logged_url = proc { dashboard_path }
20
+
21
+ # Install SimpleAuth helpers to the controllers.
22
+ config.install_helpers!
23
+ end
@@ -0,0 +1,23 @@
1
+ Rails.application.routes.draw do
2
+ get "/dashboard", to: "dashboard#index"
3
+ get "/admin/dashboard", to: "admin/dashboard#index"
4
+ get "/login", to: "sessions#new"
5
+
6
+ controller :dashboard do
7
+ get :log_in
8
+ get :not_logged
9
+ end
10
+
11
+ controller :pages do
12
+ get :index
13
+ end
14
+
15
+ namespace :admin do
16
+ controller :dashboard do
17
+ get :index
18
+ get :log_in_as_admin
19
+ get :log_in_as_user
20
+ get :log_in_with_admin_flag
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,6 @@
1
+ ActiveRecord::Schema.define(version: 0) do
2
+ create_table :users do |t|
3
+ t.string :email, :password_digest, :username
4
+ t.boolean :admin, default: false, null: false
5
+ end
6
+ end
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
+
3
+ require "bundler/setup"
4
+ require "rack/test"
5
+ require "minitest/utils"
6
+ require "minitest/autorun"
7
+
8
+ require "./test/support/dummy/config/application"
9
+ require "./test/support/dummy/config/routes"
10
+
11
+ require "active_record"
12
+ ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
13
+ require "./test/support/schema"
14
+
15
+ Dir["./test/support/**/*.rb"].each {|file| require file }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.4
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-05 00:00:00.000000000 Z
11
+ date: 2016-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 3.1.0
19
+ version: 4.2.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 3.1.0
26
+ version: 4.2.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: sqlite3-ruby
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -39,19 +39,47 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rspec-rails
42
+ name: activerecord
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 3.0.0.beta2
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 3.0.0.beta2
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-utils
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: bcrypt
57
85
  requirement: !ruby/object:Gem::Requirement
@@ -83,58 +111,45 @@ dependencies:
83
111
  description: A simple authentication system for Rails apps
84
112
  email:
85
113
  - fnando.vieira@gmail.com
86
- executables: []
114
+ executables:
115
+ - console
87
116
  extensions: []
88
117
  extra_rdoc_files: []
89
118
  files:
90
119
  - ".gitignore"
91
- - ".rspec"
92
120
  - ".travis.yml"
93
121
  - CHANGELOG.md
94
122
  - Gemfile
123
+ - MIGRATE.md
95
124
  - README.md
96
125
  - Rakefile
97
- - gemfiles/rails_3_1.gemfile
98
- - gemfiles/rails_3_2.gemfile
99
- - gemfiles/rails_4_0.gemfile
100
- - gemfiles/rails_4_1.gemfile
126
+ - bin/console
127
+ - gemfiles/rails_4_2.gemfile
128
+ - gemfiles/rails_5_0.gemfile
101
129
  - lib/simple_auth.rb
102
130
  - lib/simple_auth/action_controller.rb
103
- - lib/simple_auth/active_record.rb
104
- - lib/simple_auth/compat.rb
105
- - lib/simple_auth/compat/active_record.rb
106
- - lib/simple_auth/compat/config.rb
131
+ - lib/simple_auth/action_controller/require_login_action.rb
107
132
  - lib/simple_auth/config.rb
108
- - lib/simple_auth/exceptions.rb
109
133
  - lib/simple_auth/generator.rb
110
- - lib/simple_auth/helper.rb
111
134
  - lib/simple_auth/railtie.rb
112
- - lib/simple_auth/rspec.rb
113
135
  - lib/simple_auth/session.rb
136
+ - lib/simple_auth/templates/install/initializer.rb
114
137
  - lib/simple_auth/version.rb
115
- - locales/en.yml
116
- - locales/pt-BR.yml
117
138
  - simple_auth.gemspec
118
- - spec/controllers/redirect_logged_user_spec.rb
119
- - spec/controllers/require_logged_user_spec.rb
120
- - spec/schema.rb
121
- - spec/simple_auth/active_record_spec.rb
122
- - spec/simple_auth/compat_spec.rb
123
- - spec/simple_auth/config_spec.rb
124
- - spec/simple_auth/helper_spec.rb
125
- - spec/simple_auth/initializer_spec.rb
126
- - spec/simple_auth/session_spec.rb
127
- - spec/spec_helper.rb
128
- - spec/support/app/controllers/application_controller.rb
129
- - spec/support/app/models/customer.rb
130
- - spec/support/app/models/person.rb
131
- - spec/support/app/models/user.rb
132
- - spec/support/app/views/dashboard/index.erb
133
- - spec/support/app/views/session/new.erb
134
- - spec/support/config/boot.rb
135
- - spec/support/config/database.yml
136
- - spec/support/config/routes.rb
137
- - templates/initializer.rb
139
+ - test/controllers/admin/dashboard_controller_test.rb
140
+ - test/controllers/dashboard_controller_test.rb
141
+ - test/controllers/pages_controller_test.rb
142
+ - test/generators/install_test.rb
143
+ - test/support/dummy/app/controllers/admin/dashboard_controller.rb
144
+ - test/support/dummy/app/controllers/application_controller.rb
145
+ - test/support/dummy/app/controllers/dashboard_controller.rb
146
+ - test/support/dummy/app/controllers/pages_controller.rb
147
+ - test/support/dummy/app/models/user.rb
148
+ - test/support/dummy/config/application.rb
149
+ - test/support/dummy/config/initializers/simple_auth.rb
150
+ - test/support/dummy/config/routes.rb
151
+ - test/support/schema.rb
152
+ - test/test_helper.rb
138
153
  homepage: http://rubygems.org/gems/simple_auth
139
154
  licenses: []
140
155
  metadata: {}
@@ -146,7 +161,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
146
161
  requirements:
147
162
  - - ">="
148
163
  - !ruby/object:Gem::Version
149
- version: '0'
164
+ version: 2.2.0
150
165
  required_rubygems_version: !ruby/object:Gem::Requirement
151
166
  requirements:
152
167
  - - ">="
@@ -154,27 +169,22 @@ required_rubygems_version: !ruby/object:Gem::Requirement
154
169
  version: '0'
155
170
  requirements: []
156
171
  rubyforge_project:
157
- rubygems_version: 2.2.2
172
+ rubygems_version: 2.5.1
158
173
  signing_key:
159
174
  specification_version: 4
160
175
  summary: A simple authentication system for Rails apps
161
176
  test_files:
162
- - spec/controllers/redirect_logged_user_spec.rb
163
- - spec/controllers/require_logged_user_spec.rb
164
- - spec/schema.rb
165
- - spec/simple_auth/active_record_spec.rb
166
- - spec/simple_auth/compat_spec.rb
167
- - spec/simple_auth/config_spec.rb
168
- - spec/simple_auth/helper_spec.rb
169
- - spec/simple_auth/initializer_spec.rb
170
- - spec/simple_auth/session_spec.rb
171
- - spec/spec_helper.rb
172
- - spec/support/app/controllers/application_controller.rb
173
- - spec/support/app/models/customer.rb
174
- - spec/support/app/models/person.rb
175
- - spec/support/app/models/user.rb
176
- - spec/support/app/views/dashboard/index.erb
177
- - spec/support/app/views/session/new.erb
178
- - spec/support/config/boot.rb
179
- - spec/support/config/database.yml
180
- - spec/support/config/routes.rb
177
+ - test/controllers/admin/dashboard_controller_test.rb
178
+ - test/controllers/dashboard_controller_test.rb
179
+ - test/controllers/pages_controller_test.rb
180
+ - test/generators/install_test.rb
181
+ - test/support/dummy/app/controllers/admin/dashboard_controller.rb
182
+ - test/support/dummy/app/controllers/application_controller.rb
183
+ - test/support/dummy/app/controllers/dashboard_controller.rb
184
+ - test/support/dummy/app/controllers/pages_controller.rb
185
+ - test/support/dummy/app/models/user.rb
186
+ - test/support/dummy/config/application.rb
187
+ - test/support/dummy/config/initializers/simple_auth.rb
188
+ - test/support/dummy/config/routes.rb
189
+ - test/support/schema.rb
190
+ - test/test_helper.rb
data/.rspec DELETED
@@ -1 +0,0 @@
1
- --color
@@ -1,5 +0,0 @@
1
- source "https://rubygems.org"
2
- gemspec path: ".."
3
-
4
- gem "rails", "~> 3.1.0"
5
- gem "bcrypt-ruby", "~> 3.0.0"
@@ -1,5 +0,0 @@
1
- source "https://rubygems.org"
2
- gemspec path: ".."
3
-
4
- gem "rails", "~> 3.2.0"
5
- gem "bcrypt-ruby", "~> 3.0.0"
@@ -1,95 +0,0 @@
1
- module SimpleAuth
2
- module ActiveRecord
3
- def self.included(base)
4
- base.class_eval { extend Macro }
5
- end
6
-
7
- module Macro
8
- # Set virtual attributes, callbacks and validations.
9
- # Is called automatically after setting up configuration with
10
- # `SimpleAuth.setup {|config| config.model = :user}`.
11
- #
12
- # class User < ActiveRecord::Base
13
- # authentication
14
- # end
15
- #
16
- # Can set configuration when a block is provided.
17
- #
18
- # class User < ActiveRecord::Base
19
- # authentication do |config|
20
- # config.credentials = ["email"]
21
- # end
22
- # end
23
- #
24
- def authentication(options = {}, &block)
25
- SimpleAuth.setup(&block) if block_given?
26
- SimpleAuth::Config.model ||= name.underscore.to_sym
27
-
28
- # Possibly multiple calls in a given model.
29
- # So, just return.
30
- return if respond_to?(:authenticate)
31
-
32
- macro = method(:has_secure_password)
33
-
34
- if macro.arity.zero?
35
- has_secure_password
36
- else
37
- has_secure_password(options)
38
- end
39
-
40
- extend ClassMethods
41
- include InstanceMethods
42
-
43
- if options.fetch(:validations, true)
44
- validates_length_of :password, minimum: 4,
45
- if: -> { password.present? }
46
- end
47
- end
48
- end
49
-
50
- module InstanceMethods
51
- end
52
-
53
- module ClassMethods
54
- # Find user by its credential.
55
- #
56
- # User.find_by_credential "john@doe.com" # using e-mail
57
- # User.find_by_credential "john" # using username
58
- #
59
- def find_by_credential(credential)
60
- # Collect each attribute that should be used as credential.
61
- query = SimpleAuth::Config.credentials.each_with_object([]) do |attr_name, buffer|
62
- buffer << "#{attr_name} = :credential"
63
- end.join(" or ")
64
-
65
- # Set the scope.
66
- scope = SimpleAuth::Config.model_class.where(query, credential: credential.to_s)
67
-
68
- # Find the record using the conditions we built
69
- scope.first
70
- end
71
-
72
- # Find user by its credential. If no user is found, raise
73
- # SimpleAuth::RecordNotFound exception.
74
- #
75
- # User.find_by_credential! "john@doe.com"
76
- #
77
- def find_by_credential!(credential)
78
- record = find_by_credential(credential)
79
- raise SimpleAuth::RecordNotFound, "couldn't find #{SimpleAuth::Config.model} using #{credential.inspect} as credential" unless record
80
- record
81
- end
82
-
83
- # Receive a credential and a password and try to authenticate the specified user.
84
- # If the credential is valid, then an user is returned; otherwise nil is returned.
85
- #
86
- # User.authenticate "johndoe", "test"
87
- # User.authenticate "john@doe.com", "test"
88
- #
89
- def authenticate(credential, password)
90
- record = find_by_credential(credential.to_s)
91
- record.try(:authenticate, password)
92
- end
93
- end
94
- end
95
- end