trestle-auth 0.2.3 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of trestle-auth might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.browserslistrc +1 -0
- data/.gitignore +5 -0
- data/.rspec +1 -0
- data/.travis.yml +20 -3
- data/Gemfile +14 -0
- data/README.md +35 -8
- data/app/assets/bundle/trestle/auth/bundle.css +1 -0
- data/app/assets/bundle/trestle/auth/userbox.css +1 -0
- data/app/assets/stylesheets/trestle/_custom-auth.css +6 -0
- data/app/assets/stylesheets/trestle/auth.css +2 -0
- data/app/controllers/trestle/auth/sessions_controller.rb +4 -5
- data/app/helpers/trestle/auth/title_helper.rb +20 -0
- data/app/helpers/trestle/auth/user_helper.rb +2 -6
- data/app/views/layouts/trestle/auth.html.erb +11 -16
- data/app/views/trestle/auth/_userbox.html.erb +17 -6
- data/app/views/trestle/auth/sessions/_form.html.erb +32 -0
- data/app/views/trestle/auth/sessions/new.html.erb +8 -31
- data/bin/rails +20 -0
- data/config/{locale → locales}/en.yml +7 -0
- data/config/locales/es.yml +15 -0
- data/config/{locale → locales}/pl.yml +7 -0
- data/config/{locale → locales}/pt-BR.yml +7 -0
- data/config/locales/zh-CN.yml +8 -0
- data/config/routes.rb +8 -3
- data/frontend/_form.scss +79 -0
- data/frontend/_layout.scss +35 -0
- data/{app/assets/stylesheets/trestle/auth/_defaults.scss → frontend/_variables.scss} +2 -6
- data/frontend/index.scss +5 -0
- data/{app/assets/stylesheets/trestle/auth → frontend}/userbox.scss +14 -12
- data/gemfiles/rails-4.2.gemfile +18 -0
- data/gemfiles/rails-5.0.gemfile +18 -0
- data/gemfiles/rails-5.1.gemfile +18 -0
- data/gemfiles/rails-5.2.gemfile +18 -0
- data/gemfiles/rails-6.0.gemfile +18 -0
- data/lib/generators/trestle/auth/account/account_generator.rb +32 -0
- data/lib/generators/trestle/auth/account/templates/admin.rb.erb +47 -0
- data/lib/generators/trestle/auth/admin/admin_generator.rb +15 -1
- data/lib/generators/trestle/auth/admin/templates/admin.rb.erb +28 -5
- data/lib/generators/trestle/auth/install/install_generator.rb +39 -83
- data/lib/generators/trestle/auth/install/templates/basic.rb.erb +121 -0
- data/lib/generators/trestle/auth/install/templates/devise.rb.erb +87 -0
- data/lib/trestle/auth.rb +14 -8
- data/lib/trestle/auth/backends.rb +34 -0
- data/lib/trestle/auth/backends/base.rb +28 -0
- data/lib/trestle/auth/backends/basic.rb +72 -0
- data/lib/trestle/auth/backends/devise.rb +14 -0
- data/lib/trestle/auth/backends/warden.rb +53 -0
- data/lib/trestle/auth/configuration.rb +41 -6
- data/lib/trestle/auth/configuration/warden.rb +11 -0
- data/lib/trestle/auth/controller/authentication.rb +55 -0
- data/lib/trestle/auth/controller/locale.rb +18 -0
- data/lib/trestle/auth/controller/time_zone.rb +18 -0
- data/lib/trestle/auth/controller_methods.rb +3 -74
- data/lib/trestle/auth/engine.rb +1 -1
- data/lib/trestle/auth/model_methods.rb +2 -3
- data/lib/trestle/auth/version.rb +1 -1
- data/package.json +29 -0
- data/trestle-auth.gemspec +14 -10
- data/webpack.config.js +49 -0
- data/yarn.lock +4765 -0
- metadata +67 -31
- data/app/assets/javascripts/trestle/auth.js +0 -0
- data/app/assets/stylesheets/trestle/_custom-auth.scss +0 -4
- data/app/assets/stylesheets/trestle/auth.scss +0 -13
- data/app/assets/stylesheets/trestle/auth/_form.scss +0 -120
- data/app/assets/stylesheets/trestle/auth/_layout.scss +0 -20
@@ -0,0 +1,28 @@
|
|
1
|
+
module Trestle
|
2
|
+
module Auth
|
3
|
+
module Backends
|
4
|
+
class Base
|
5
|
+
attr_reader :controller, :request, :session, :cookies
|
6
|
+
|
7
|
+
def initialize(controller:, request:, session:, cookies:)
|
8
|
+
@controller, @request, @session, @cookies = controller, request, session, cookies
|
9
|
+
end
|
10
|
+
|
11
|
+
# Default params scope to use for the login form.
|
12
|
+
def scope
|
13
|
+
:user
|
14
|
+
end
|
15
|
+
|
16
|
+
# Stores the previous return location in the session to return to after logging in.
|
17
|
+
def store_location(url)
|
18
|
+
session[:trestle_return_to] = url
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns (and deletes) the previously stored return location from the session.
|
22
|
+
def previous_location
|
23
|
+
session.delete(:trestle_return_to)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Trestle
|
2
|
+
module Auth
|
3
|
+
module Backends
|
4
|
+
class Basic < Base
|
5
|
+
# Returns the current logged in user (after #authentication).
|
6
|
+
attr_reader :user
|
7
|
+
|
8
|
+
# Authenticates a user from a login form request.
|
9
|
+
def authenticate!
|
10
|
+
params = login_params
|
11
|
+
|
12
|
+
if user = Trestle.config.auth.authenticate(params)
|
13
|
+
login!(user)
|
14
|
+
remember_me! if Trestle.config.auth.remember.enabled && params[:remember_me]
|
15
|
+
user
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Authenticates a user from the session or cookie. Called on each request via a before_action.
|
20
|
+
def authenticate
|
21
|
+
@user = find_authenticated_user || find_remembered_user
|
22
|
+
end
|
23
|
+
|
24
|
+
# Checks if there is a logged in user.
|
25
|
+
def logged_in?
|
26
|
+
!!user
|
27
|
+
end
|
28
|
+
|
29
|
+
# Stores the given user in the session as logged in.
|
30
|
+
def login!(user)
|
31
|
+
session[:trestle_user] = user.id
|
32
|
+
@user = user
|
33
|
+
end
|
34
|
+
|
35
|
+
# Logs out the current user.
|
36
|
+
def logout!
|
37
|
+
if logged_in? && Trestle.config.auth.remember.enabled
|
38
|
+
Trestle.config.auth.remember.forget_me(user)
|
39
|
+
cookies.delete(:trestle_remember_token)
|
40
|
+
end
|
41
|
+
|
42
|
+
session.delete(:trestle_user)
|
43
|
+
@user = nil
|
44
|
+
end
|
45
|
+
|
46
|
+
protected
|
47
|
+
def remember_me!
|
48
|
+
Trestle.config.auth.remember.remember_me(user)
|
49
|
+
cookies.signed[:trestle_remember_token] = Trestle.config.auth.remember.cookie(user)
|
50
|
+
end
|
51
|
+
|
52
|
+
def find_authenticated_user
|
53
|
+
Trestle.config.auth.find_user(session[:trestle_user]) if session[:trestle_user]
|
54
|
+
end
|
55
|
+
|
56
|
+
def find_remembered_user
|
57
|
+
return unless Trestle.config.auth.remember.enabled
|
58
|
+
|
59
|
+
if token = cookies.signed[:trestle_remember_token]
|
60
|
+
user = Trestle.config.auth.remember.authenticate(token)
|
61
|
+
login!(user) if user
|
62
|
+
user
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def login_params
|
67
|
+
controller.params.require(:user).permit!
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Trestle
|
2
|
+
module Auth
|
3
|
+
module Backends
|
4
|
+
class Devise < Warden
|
5
|
+
# Authenticates a user from a login form request.
|
6
|
+
# Devise requires that params authentication is explicitly enabled.
|
7
|
+
def authenticate!
|
8
|
+
request.env["devise.allow_params_authentication"] = true
|
9
|
+
super
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Trestle
|
2
|
+
module Auth
|
3
|
+
module Backends
|
4
|
+
class Warden < Base
|
5
|
+
# Authenticates a user from a login form request.
|
6
|
+
def authenticate!
|
7
|
+
authenticate
|
8
|
+
end
|
9
|
+
|
10
|
+
# Authenticates the user using Warden.
|
11
|
+
def authenticate
|
12
|
+
warden.authenticate(scope: scope)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Checks if there is a logged in user.
|
16
|
+
def logged_in?
|
17
|
+
warden.authenticated?(scope)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Returns the current logged in user.
|
21
|
+
def user
|
22
|
+
warden.user(scope)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Stores the given user as logged in.
|
26
|
+
def login!(user)
|
27
|
+
warden.set_user(user, scope: scope)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Logs out the current user.
|
31
|
+
def logout!
|
32
|
+
if scope
|
33
|
+
warden.logout(scope)
|
34
|
+
warden.clear_strategies_cache!(scope: scope)
|
35
|
+
else
|
36
|
+
warden.logout
|
37
|
+
warden.clear_strategies_cache!
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Set the login params scope from configuration, which is also used as the Warden scope.
|
42
|
+
def scope
|
43
|
+
Trestle.config.auth.warden.scope
|
44
|
+
end
|
45
|
+
|
46
|
+
protected
|
47
|
+
def warden
|
48
|
+
request.env['warden']
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -1,9 +1,8 @@
|
|
1
1
|
module Trestle
|
2
2
|
module Auth
|
3
3
|
class Configuration
|
4
|
-
|
5
|
-
|
6
|
-
autoload :Rememberable
|
4
|
+
require_relative "configuration/rememberable"
|
5
|
+
require_relative "configuration/warden"
|
7
6
|
|
8
7
|
include Configurable
|
9
8
|
|
@@ -26,19 +25,55 @@ module Trestle
|
|
26
25
|
Trestle.config.auth.user_scope.find_by(id: id)
|
27
26
|
}
|
28
27
|
|
28
|
+
option :human_attribute_name, ->(field) {
|
29
|
+
model = Trestle.config.auth.user_class rescue nil
|
30
|
+
|
31
|
+
if model && model.respond_to?(:human_attribute_name)
|
32
|
+
model.human_attribute_name(field)
|
33
|
+
else
|
34
|
+
field.to_s.humanize
|
35
|
+
end
|
36
|
+
}
|
37
|
+
|
29
38
|
option :avatar, ->(user) {
|
30
|
-
gravatar(user.email)
|
39
|
+
avatar { gravatar(user.email) }
|
40
|
+
}, evaluate: false
|
41
|
+
|
42
|
+
option :format_user_name, ->(user) {
|
43
|
+
if user.respond_to?(:first_name) && user.respond_to?(:last_name)
|
44
|
+
safe_join([user.first_name, content_tag(:strong, user.last_name)], " ")
|
45
|
+
else
|
46
|
+
display(user)
|
47
|
+
end
|
31
48
|
}, evaluate: false
|
32
49
|
|
33
50
|
option :locale, ->(user) {
|
34
51
|
user.locale if user.respond_to?(:locale)
|
35
|
-
}
|
52
|
+
}, evaluate: false
|
36
53
|
|
37
54
|
option :time_zone, ->(user) {
|
38
55
|
user.time_zone if user.respond_to?(:time_zone)
|
39
|
-
}
|
56
|
+
}, evaluate: false
|
57
|
+
|
58
|
+
option :enable_login, true
|
59
|
+
option :enable_logout, true
|
60
|
+
|
61
|
+
option :login_url, -> { login_url }, evaluate: false
|
62
|
+
|
63
|
+
option :redirect_on_login, -> { Trestle.config.path }, evaluate: false
|
64
|
+
option :redirect_on_logout, -> { login_url }, evaluate: false
|
65
|
+
|
66
|
+
option :logo
|
40
67
|
|
41
68
|
option :remember, Rememberable.new
|
69
|
+
|
70
|
+
option :backend, Backends::Basic
|
71
|
+
|
72
|
+
def backend=(backend)
|
73
|
+
assign(:backend, Backends.lookup(backend))
|
74
|
+
end
|
75
|
+
|
76
|
+
option :warden, Warden.new
|
42
77
|
end
|
43
78
|
end
|
44
79
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Trestle
|
2
|
+
module Auth
|
3
|
+
module Controller
|
4
|
+
module Authentication
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
helper_method :current_user, :logged_in?, :authentication_scope
|
9
|
+
|
10
|
+
prepend_before_action :require_authenticated_user
|
11
|
+
prepend_before_action :authenticate_user
|
12
|
+
end
|
13
|
+
|
14
|
+
protected
|
15
|
+
def authentication_backend
|
16
|
+
@_authentication_backend ||= Trestle.config.auth.backend.new(controller: self, request: request, session: session, cookies: cookies)
|
17
|
+
end
|
18
|
+
|
19
|
+
def current_user
|
20
|
+
authentication_backend.user
|
21
|
+
end
|
22
|
+
|
23
|
+
def logged_in?
|
24
|
+
authentication_backend.logged_in?
|
25
|
+
end
|
26
|
+
|
27
|
+
def authenticate_user
|
28
|
+
authentication_backend.authenticate
|
29
|
+
end
|
30
|
+
|
31
|
+
def require_authenticated_user
|
32
|
+
logged_in? || login_required!
|
33
|
+
end
|
34
|
+
|
35
|
+
def login!(user)
|
36
|
+
authentication_backend.login!(user)
|
37
|
+
end
|
38
|
+
|
39
|
+
def logout!
|
40
|
+
authentication_backend.logout!
|
41
|
+
end
|
42
|
+
|
43
|
+
def login_required!
|
44
|
+
authentication_backend.store_location(request.fullpath)
|
45
|
+
redirect_to instance_exec(&Trestle.config.auth.login_url)
|
46
|
+
false
|
47
|
+
end
|
48
|
+
|
49
|
+
def authentication_scope
|
50
|
+
authentication_backend.scope
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Trestle
|
2
|
+
module Auth
|
3
|
+
module Controller
|
4
|
+
module Locale
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
around_action :set_locale, if: :logged_in? if Trestle.config.auth.locale
|
9
|
+
end
|
10
|
+
|
11
|
+
protected
|
12
|
+
def set_locale
|
13
|
+
I18n.with_locale(instance_exec(current_user, &Trestle.config.auth.locale) || I18n.default_locale) { yield }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Trestle
|
2
|
+
module Auth
|
3
|
+
module Controller
|
4
|
+
module TimeZone
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
around_action :set_time_zone, if: :logged_in? if Trestle.config.auth.time_zone
|
9
|
+
end
|
10
|
+
|
11
|
+
protected
|
12
|
+
def set_time_zone
|
13
|
+
Time.use_zone(instance_exec(current_user, &Trestle.config.auth.time_zone) || Rails.application.config.time_zone) { yield }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -3,80 +3,9 @@ module Trestle
|
|
3
3
|
module ControllerMethods
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
before_action :require_authenticated_user
|
10
|
-
|
11
|
-
around_action :set_locale, if: :logged_in?
|
12
|
-
around_action :set_time_zone, if: :logged_in?
|
13
|
-
end
|
14
|
-
|
15
|
-
protected
|
16
|
-
def current_user
|
17
|
-
@current_user ||= begin
|
18
|
-
if session[:trestle_user]
|
19
|
-
Trestle.config.auth.find_user(session[:trestle_user])
|
20
|
-
elsif Trestle.config.auth.remember.enabled && token = cookies.signed[:trestle_remember_token]
|
21
|
-
user = Trestle.config.auth.remember.authenticate(token)
|
22
|
-
login!(user) if user
|
23
|
-
user
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def login!(user)
|
29
|
-
session[:trestle_user] = user.id
|
30
|
-
@current_user = user
|
31
|
-
end
|
32
|
-
|
33
|
-
def logout!
|
34
|
-
forget_me!
|
35
|
-
session.delete(:trestle_user)
|
36
|
-
@current_user = nil
|
37
|
-
end
|
38
|
-
|
39
|
-
def logged_in?
|
40
|
-
!!current_user
|
41
|
-
end
|
42
|
-
|
43
|
-
def store_location
|
44
|
-
session[:trestle_return_to] = request.fullpath
|
45
|
-
end
|
46
|
-
|
47
|
-
def previous_location
|
48
|
-
session.delete(:trestle_return_to)
|
49
|
-
end
|
50
|
-
|
51
|
-
def require_authenticated_user
|
52
|
-
logged_in? || login_required!
|
53
|
-
end
|
54
|
-
|
55
|
-
def login_required!
|
56
|
-
store_location
|
57
|
-
redirect_to trestle.login_url
|
58
|
-
false
|
59
|
-
end
|
60
|
-
|
61
|
-
def remember_me!
|
62
|
-
return unless Trestle.config.auth.remember.enabled
|
63
|
-
Trestle.config.auth.remember.remember_me(current_user)
|
64
|
-
cookies.signed[:trestle_remember_token] = Trestle.config.auth.remember.cookie(current_user)
|
65
|
-
end
|
66
|
-
|
67
|
-
def forget_me!
|
68
|
-
return unless Trestle.config.auth.remember.enabled
|
69
|
-
Trestle.config.auth.remember.forget_me(current_user) if logged_in?
|
70
|
-
cookies.delete(:trestle_remember_token)
|
71
|
-
end
|
72
|
-
|
73
|
-
def set_locale
|
74
|
-
I18n.with_locale(Trestle.config.auth.locale(current_user) || I18n.default_locale) { yield }
|
75
|
-
end
|
76
|
-
|
77
|
-
def set_time_zone
|
78
|
-
Time.use_zone(Trestle.config.auth.time_zone(current_user) || Rails.application.config.time_zone) { yield }
|
79
|
-
end
|
6
|
+
include Trestle::Auth::Controller::Authentication
|
7
|
+
include Trestle::Auth::Controller::Locale
|
8
|
+
include Trestle::Auth::Controller::TimeZone
|
80
9
|
end
|
81
10
|
end
|
82
11
|
end
|
data/lib/trestle/auth/engine.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Trestle
|
2
2
|
module Auth
|
3
3
|
class Engine < ::Rails::Engine
|
4
|
-
config.assets.precompile << "trestle/auth.css" << "trestle/auth
|
4
|
+
config.assets.precompile << "trestle/auth.css" << "trestle/auth/userbox.css"
|
5
5
|
|
6
6
|
config.before_initialize do
|
7
7
|
Trestle::Engine.paths["app/helpers"].concat(paths["app/helpers"].existent)
|
@@ -2,9 +2,8 @@ module Trestle
|
|
2
2
|
module Auth
|
3
3
|
module ModelMethods
|
4
4
|
extend ActiveSupport::Concern
|
5
|
-
extend ActiveSupport::Autoload
|
6
5
|
|
7
|
-
|
6
|
+
require_relative "model_methods/rememberable"
|
8
7
|
|
9
8
|
included do
|
10
9
|
has_secure_password
|
@@ -13,7 +12,7 @@ module Trestle
|
|
13
12
|
module ClassMethods
|
14
13
|
def authenticate(identifier, password)
|
15
14
|
user = find_by(Trestle.config.auth.authenticate_with => identifier) || NullUser.new
|
16
|
-
user.authenticate(password)
|
15
|
+
user if user.authenticate(password)
|
17
16
|
end
|
18
17
|
end
|
19
18
|
end
|