thecore_auth_commons 3.5.6 → 3.5.8
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 +4 -4
- data/app/controllers/users/sessions_controller.rb +6 -5
- data/app/services/ldap/authenticator.rb +44 -30
- data/config/locales/en.thecore_auth_commons.yml +5 -0
- data/config/locales/it.thecore_auth_commons.yml +5 -0
- data/db/migrate/20251216110301_add_ldap_match_fields_to_ldap_server.rb +10 -0
- data/db/migrate/20251216111217_add_code_to_ldap_server.rb +6 -0
- data/lib/thecore_auth_commons/version.rb +1 -1
- data/lib/thecore_auth_commons.rb +31 -29
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b1eb9060882d36e90d8c5b1b77c6df18be6969382eba49fd59df9a30f5196433
|
|
4
|
+
data.tar.gz: 3f5bb4bd1029d7d3646ef0aede9129ff18eb893ba35915386eebbd498e7094cb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: dc229651cf4cdda2944b339d4329d472efa031409ae2968e61e43ad9d744eaa804945cb10e759b6d4b04ae9bedcade5836d85f4b351db33b4b6db83c9c1e1cd8
|
|
7
|
+
data.tar.gz: 98b4790b2c327802296b0dc624f770134cf5d829e644f7211bbc036914df990d16834fff8997ffa945c364e4c2e5e43837b88215dcbfff896ee2e25aa23db941
|
|
@@ -4,19 +4,20 @@ class Users::SessionsController < Devise::SessionsController
|
|
|
4
4
|
self.resource = warden.authenticate(auth_options)
|
|
5
5
|
|
|
6
6
|
if resource
|
|
7
|
+
Rails.logger.info("Authentication: Found local user, signing in")
|
|
7
8
|
sign_in_and_redirect(resource)
|
|
8
9
|
else
|
|
10
|
+
Rails.logger.info("Authentication: Not found a local user, trying LDAP")
|
|
9
11
|
user = Ldap::Authenticator.new(
|
|
10
12
|
email: params[:user][:email],
|
|
11
|
-
password: params[:user][:password]
|
|
13
|
+
password: params[:user][:password],
|
|
12
14
|
).authenticate
|
|
13
15
|
|
|
14
16
|
if user
|
|
15
|
-
|
|
16
|
-
sign_in(:user, user)
|
|
17
|
-
redirect_to after_sign_in_path_for(user)
|
|
17
|
+
sign_in_and_redirect(user)
|
|
18
18
|
else
|
|
19
|
-
|
|
19
|
+
set_flash_message!(:alert, :invalid)
|
|
20
|
+
|
|
20
21
|
self.resource = resource_class.new(sign_in_params)
|
|
21
22
|
clean_up_passwords(resource)
|
|
22
23
|
respond_with_navigational(resource) { render :new, status: :unauthorized }
|
|
@@ -6,42 +6,56 @@ module Ldap
|
|
|
6
6
|
@email = email
|
|
7
7
|
end
|
|
8
8
|
|
|
9
|
-
def
|
|
10
|
-
|
|
9
|
+
def auth_on_single_server(server)
|
|
10
|
+
Rails.logger.debug("LDAP: Trying to authenticate #{email} on server #{server.inspect}")
|
|
11
|
+
ldap = Net::LDAP.new(
|
|
12
|
+
host: server.host,
|
|
13
|
+
port: server.port,
|
|
14
|
+
encryption: server.use_ssl ? :simple_tls : nil,
|
|
15
|
+
auth: {
|
|
16
|
+
method: :simple,
|
|
17
|
+
username: server.admin_user,
|
|
18
|
+
password: server.admin_password,
|
|
19
|
+
},
|
|
20
|
+
)
|
|
11
21
|
|
|
12
|
-
|
|
13
|
-
|
|
22
|
+
Rails.logger.debug("LDAP: Binding to server #{server.inspect} ")
|
|
23
|
+
filter = Net::LDAP::Filter.eq(server.auth_field, email) # server.auth_field
|
|
24
|
+
treebase = server.base_dn
|
|
25
|
+
|
|
26
|
+
Rails.logger.debug("LDAP: Searching for user #{email} in base #{treebase} with filter #{filter.to_s}")
|
|
27
|
+
ldap.search(base: treebase, filter: filter) do |entry|
|
|
28
|
+
user_dn = entry.dn
|
|
29
|
+
|
|
30
|
+
# Prova autenticazione utente
|
|
31
|
+
user_ldap = Net::LDAP.new(
|
|
14
32
|
host: server.host,
|
|
15
33
|
port: server.port,
|
|
16
34
|
encryption: server.use_ssl ? :simple_tls : nil,
|
|
17
35
|
auth: {
|
|
18
36
|
method: :simple,
|
|
19
|
-
username:
|
|
20
|
-
password:
|
|
21
|
-
}
|
|
37
|
+
username: user_dn,
|
|
38
|
+
password: password,
|
|
39
|
+
},
|
|
22
40
|
)
|
|
23
41
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
)
|
|
41
|
-
|
|
42
|
-
if user_ldap.bind
|
|
43
|
-
return find_or_create_user(entry, server.id)
|
|
44
|
-
end
|
|
42
|
+
Rails.logger.debug("LDAP: Trying to bind as user #{user_dn} on server #{server.inspect}")
|
|
43
|
+
return entry if user_ldap.bind
|
|
44
|
+
end
|
|
45
|
+
Rails.logger.debug("LDAP: Authentication failed for #{email} on server #{server.inspect}")
|
|
46
|
+
nil
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def authenticate
|
|
50
|
+
return nil if @password.blank?
|
|
51
|
+
|
|
52
|
+
LdapServer.all.each do |server|
|
|
53
|
+
entry = auth_on_single_server(server)
|
|
54
|
+
if entry
|
|
55
|
+
Rails.logger.info("Authentication: LDAP authentication succeeded for #{email} on server #{server.name}")
|
|
56
|
+
return find_or_create_user(entry, server)
|
|
57
|
+
else
|
|
58
|
+
Rails.logger.info("Authentication: LDAP authentication failed for #{email} on server #{server.name}")
|
|
45
59
|
end
|
|
46
60
|
end
|
|
47
61
|
|
|
@@ -52,8 +66,8 @@ module Ldap
|
|
|
52
66
|
|
|
53
67
|
attr_reader :email, :password
|
|
54
68
|
|
|
55
|
-
def find_or_create_user(entry,
|
|
56
|
-
ThecoreAuthCommons.align_user email, entry,
|
|
69
|
+
def find_or_create_user(entry, server)
|
|
70
|
+
ThecoreAuthCommons.align_user email, entry, server
|
|
57
71
|
end
|
|
58
72
|
end
|
|
59
73
|
end
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
it:
|
|
2
|
+
devise:
|
|
3
|
+
sessions:
|
|
4
|
+
invalid: "Credenziali %{authentication_keys} o password non valide."
|
|
5
|
+
user:
|
|
6
|
+
invalid: "Credenziali %{authentication_keys} o password non valide."
|
|
2
7
|
error:
|
|
3
8
|
messages:
|
|
4
9
|
password_requires_letters_and_numbers: "deve contenere almeno una lettera e un numero"
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
class AddLdapMatchFieldsToLdapServer < ActiveRecord::Migration[7.2]
|
|
2
|
+
def change
|
|
3
|
+
add_column :ldap_servers, :name, :string
|
|
4
|
+
add_index :ldap_servers, :name
|
|
5
|
+
add_column :ldap_servers, :surname, :string
|
|
6
|
+
add_index :ldap_servers, :surname
|
|
7
|
+
add_column :ldap_servers, :phone, :string
|
|
8
|
+
add_index :ldap_servers, :phone
|
|
9
|
+
end
|
|
10
|
+
end
|
data/lib/thecore_auth_commons.rb
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
require
|
|
2
|
-
require
|
|
3
|
-
require
|
|
4
|
-
require
|
|
1
|
+
require "devise"
|
|
2
|
+
require "cancancan"
|
|
3
|
+
require "kaminari"
|
|
4
|
+
require "activerecord-nulldb-adapter"
|
|
5
5
|
require "thecore_settings"
|
|
6
6
|
require "net/ldap"
|
|
7
|
-
require
|
|
8
|
-
require
|
|
9
|
-
require
|
|
7
|
+
require "omniauth"
|
|
8
|
+
require "omniauth-google-oauth2"
|
|
9
|
+
require "omniauth-entra-id"
|
|
10
10
|
|
|
11
11
|
require "thecore_auth_commons/engine"
|
|
12
12
|
|
|
13
13
|
require "thecore/seed"
|
|
14
14
|
|
|
15
15
|
module ThecoreAuthCommons
|
|
16
|
-
def self.oauth_vars?
|
|
17
|
-
|
|
16
|
+
def self.oauth_vars?
|
|
17
|
+
entra_id_vars? || google_oauth2_vars?
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
def self.entra_id_vars?
|
|
21
|
-
ENV[
|
|
21
|
+
ENV["ENTRA_CLIENT_ID"].present? && ENV["ENTRA_CLIENT_SECRET"].present? && ENV["ENTRA_TENANT_ID"].present?
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
def self.google_oauth2_vars?
|
|
25
|
-
ENV[
|
|
25
|
+
ENV["GOOGLE_CLIENT_ID"].present? && ENV["GOOGLE_CLIENT_SECRET"].present?
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
# Controlla se l'utente esiste, altrimenti lo crea con una password casuale
|
|
29
29
|
# e lo restituisce. Se l'utente esiste già, lo restituisce senza modificarlo.
|
|
30
30
|
|
|
31
|
-
def self.check_user
|
|
31
|
+
def self.check_user(email, name, surname, provider)
|
|
32
32
|
u = User.find_or_initialize_by(email: email)
|
|
33
33
|
u.name = name
|
|
34
34
|
u.surname = surname
|
|
@@ -54,8 +54,8 @@ module ThecoreAuthCommons
|
|
|
54
54
|
auth: {
|
|
55
55
|
method: :simple,
|
|
56
56
|
username: server.admin_user,
|
|
57
|
-
password: server.admin_password
|
|
58
|
-
}
|
|
57
|
+
password: server.admin_password,
|
|
58
|
+
},
|
|
59
59
|
)
|
|
60
60
|
|
|
61
61
|
unless ldap.bind
|
|
@@ -73,31 +73,34 @@ module ThecoreAuthCommons
|
|
|
73
73
|
puts "Importando utente: #{email}"
|
|
74
74
|
|
|
75
75
|
# Password must contain at least one uppercase letter, one lowercase letter, one number and one special character
|
|
76
|
-
ThecoreAuthCommons.align_user email, entry, server
|
|
76
|
+
ThecoreAuthCommons.align_user email, entry, server
|
|
77
77
|
imported_count += 1
|
|
78
78
|
end
|
|
79
79
|
end
|
|
80
80
|
|
|
81
81
|
puts "== Completato. Utenti importati: #{imported_count} =="
|
|
82
82
|
end
|
|
83
|
-
|
|
84
|
-
def self.align_user
|
|
83
|
+
|
|
84
|
+
def self.align_user(email, entry, server)
|
|
85
85
|
user = User.find_or_initialize_by(email: email)
|
|
86
|
-
user.auth_source = "ldap #{
|
|
86
|
+
user.auth_source = "ldap #{server.id}"
|
|
87
87
|
|
|
88
88
|
# Password don't need to be changed, just created, otherwise it will invalidate the current user session if it's logged in
|
|
89
89
|
user.password = user.password_confirmation = ThecoreAuthCommons.generate_secure_password if user.new_record?
|
|
90
90
|
|
|
91
91
|
# Eventuale mapping LDAP -> campi User
|
|
92
|
-
user.name = entry[
|
|
92
|
+
user.name = entry[server.name]&.first if user.respond_to?(:name) && server.name.present?
|
|
93
|
+
user.surname = entry[server.surname]&.first if user.respond_to?(:surname) && server.surname.present?
|
|
94
|
+
user.phone = entry[server.phone]&.first if user.respond_to?(:phone) && server.phone.present?
|
|
95
|
+
user.code = entry[server.code]&.first if user.respond_to?(:code) && server.code.present?
|
|
93
96
|
|
|
94
|
-
# Recupera
|
|
97
|
+
# Recupera dalla entry i gruppi di cui fa parte l'utente e crea i relativi record in Role assegnandoli all'utente corrente
|
|
95
98
|
is_admin = false
|
|
96
99
|
entry[:memberOf].each do |group|
|
|
97
100
|
group_name = group.split(",").first.split("=").last
|
|
98
101
|
# Se il gruppo è un admin, assegna il ruolo admin
|
|
99
|
-
is_admin = true if [
|
|
100
|
-
|
|
102
|
+
is_admin = true if ["Administrators", "Domain Admins", "Schema Admins", "Enterprise Admins", "admins", "administrators"].include?(group_name)
|
|
103
|
+
|
|
101
104
|
role = Role.find_or_create_by(name: group_name)
|
|
102
105
|
user.roles << role unless user.roles.include?(role)
|
|
103
106
|
end
|
|
@@ -109,20 +112,20 @@ module ThecoreAuthCommons
|
|
|
109
112
|
end
|
|
110
113
|
|
|
111
114
|
def self.generate_secure_password(length = 20)
|
|
112
|
-
raise ArgumentError,
|
|
115
|
+
raise ArgumentError, "Length must be at least 4" if length < 4
|
|
113
116
|
|
|
114
117
|
# Caratteri da cui attingere
|
|
115
|
-
lowercase = (
|
|
116
|
-
uppercase = (
|
|
117
|
-
numbers
|
|
118
|
-
symbols
|
|
118
|
+
lowercase = ("a".."z").to_a
|
|
119
|
+
uppercase = ("A".."Z").to_a
|
|
120
|
+
numbers = ("0".."9").to_a
|
|
121
|
+
symbols = ["!", "@", "#", "$", "%", "&", "*", "?", "-", "_", "+", "="]
|
|
119
122
|
|
|
120
123
|
# Obbliga almeno un carattere da ogni gruppo
|
|
121
124
|
password = [
|
|
122
125
|
lowercase.sample,
|
|
123
126
|
uppercase.sample,
|
|
124
127
|
numbers.sample,
|
|
125
|
-
symbols.sample
|
|
128
|
+
symbols.sample,
|
|
126
129
|
]
|
|
127
130
|
|
|
128
131
|
# Caratteri restanti scelti a caso tra tutti
|
|
@@ -132,5 +135,4 @@ module ThecoreAuthCommons
|
|
|
132
135
|
# Mischia per evitare ordine prevedibile
|
|
133
136
|
password.shuffle.join
|
|
134
137
|
end
|
|
135
|
-
|
|
136
138
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: thecore_auth_commons
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.5.
|
|
4
|
+
version: 3.5.8
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gabriele Tassoni
|
|
@@ -270,6 +270,8 @@ files:
|
|
|
270
270
|
- db/migrate/20160209153816_create_permissions_chain.rb
|
|
271
271
|
- db/migrate/20250516074016_create_ldap_servers.rb
|
|
272
272
|
- db/migrate/20250516075204_add_auth_source_to_user.rb
|
|
273
|
+
- db/migrate/20251216110301_add_ldap_match_fields_to_ldap_server.rb
|
|
274
|
+
- db/migrate/20251216111217_add_code_to_ldap_server.rb
|
|
273
275
|
- db/seeds.rb
|
|
274
276
|
- lib/tasks/ldap.rake
|
|
275
277
|
- lib/tasks/thecore_auth_commons_tasks.rake
|