thecore_auth_commons 3.6.1 → 3.6.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cbdfa5e8e3ee139099bf681626e66c17459f3c0df764de2d1795fcb5bba8a71e
4
- data.tar.gz: 73c4937049f42505c872d0360bbb38889d038f1c135332081fe3237e2ea7c82b
3
+ metadata.gz: 3e5604a65938fe43fe952c42c11bdc339ff3217d7d0ca63b659ae5c871c9be5f
4
+ data.tar.gz: a7a71c049092955e8e2d47c0117ed62e693d7505ea21cb74ad0d9a3837ab4d13
5
5
  SHA512:
6
- metadata.gz: 59d97568329cf50d32c444eaef8059199bc6f4d192976ab5b04b9209414b405be8808d16cf7eb87617976effb084e76a374a336d84dba87bbbf0fdd269b23559
7
- data.tar.gz: 22984f97a81b0a638cfa259ede0e525390d00889793976436744da448549c3d0cd4fd14ba8e7119ca67f1c36b3517b8c98c557a719c54e34aa0e99b580d22a6d
6
+ metadata.gz: f13691ce6ca514d637d5031ac1e1555a5a23823e9b669c4c4333d237d8a8c4e0761177932471784597e305cc6a4d46907336921cc096cbf325fb1f93434caa70
7
+ data.tar.gz: dec750f7817baa78f77da553e0dc0cc787b4765937ca2ebf6822de8cd23d1f657af4e19450843c2c5d10852028ac644af8f5db6a9bda43d00659f000c411d6f9
data/README.md CHANGED
@@ -23,7 +23,7 @@ Provides authentication and role-based authorization for Rails applications in t
23
23
  | `GOOGLE_CLIENT_ID` | — | Google OAuth2 client ID |
24
24
  | `GOOGLE_CLIENT_SECRET` | — | Google OAuth2 client secret |
25
25
  | `BASE_DOMAIN` | `example.com` | Used to build the default admin email at seed time |
26
- | `ADMIN_PASSWORD` | `Change#1` | Default admin password at seed time |
26
+ | `ADMIN_PASSWORD` | — (random) | First admin's password at seed time; when unset the seed generates a random one and writes it to `storage/admin_initial_password` (see Seeding) |
27
27
 
28
28
  OmniAuth providers are registered only when their respective env vars are all present.
29
29
 
@@ -73,7 +73,7 @@ actions: [:manage, :create, :read, :update, :destroy]
73
73
  targets: ["all"] + ApplicationRecord.subclasses.map { |m| m.to_s.underscore }
74
74
  ```
75
75
 
76
- It also creates the first admin user (`admin@BASE_DOMAIN` / `ADMIN_PASSWORD`) if no admin exists yet.
76
+ It also creates the first admin user, `admin@BASE_DOMAIN`, only if the database has no admin yet (existing admins are never touched). Its password is `ADMIN_PASSWORD` when set. Otherwise the seed generates a random one and writes it to `<Rails.root>/storage/admin_initial_password` with mode `0600`; the log gets one line saying where it is, never the password. In a `thecore_devcontainer` deployment that's `/root/persistence/${COMPOSE_PROJECT_NAME}/storage/admin_initial_password` on the host, or `docker compose exec backend cat storage/admin_initial_password`. After the first login change the password and delete the file. An existing file is never overwritten: if it's there, the admin is not created and the log says to delete the file and seed again. Until 3.6.1 the fallback was the fixed `Change#1`.
77
77
 
78
78
  To add a permission via console:
79
79
 
@@ -112,7 +112,7 @@ Deleting a `LdapServer` record keeps all users whose `auth_source` is `"ldap #{i
112
112
 
113
113
  ### OAuth2 (Entra ID / Google)
114
114
 
115
- Handled by `Users::OmniauthCallbacksController`. On callback, `ThecoreAuthCommons.check_user` finds or creates the user (with a random secure password) and sets `auth_source` to `'google'` or `'microsoft'`. New OAuth users are created as admin.
115
+ Handled by `Users::OmniauthCallbacksController`. On callback, `ThecoreAuthCommons.check_user` finds the user by the provider-verified email or creates them (random secure password, `auth_source` `'google'` or `'microsoft'`, no admin, no roles: an administrator assigns those). An existing user's password, admin flag and `auth_source` are never changed by an OAuth login.
116
116
 
117
117
  ## Password policy
118
118
 
data/db/seeds.rb CHANGED
@@ -3,15 +3,7 @@ puts "Loading ThecoreAuthCommons seeds"
3
3
  # Needing this since in the rake task is not aeger loaded
4
4
  Zeitwerk::Loader.eager_load_all
5
5
 
6
- email = "admin@#{ENV["BASE_DOMAIN"].presence || "example.com"}"
7
- psswd = ENV["ADMIN_PASSWORD"].presence || "Change#1"
8
-
9
- unless User.where(admin: true).exists?
10
- u = User.find_or_initialize_by(email: email)
11
- u.password = u.password_confirmation = psswd
12
- u.admin = true
13
- u.save(validate: false)
14
- end
6
+ ThecoreAuthCommons.seed_first_admin
15
7
 
16
8
  @values = {
17
9
  predicates: %i[can cannot],
@@ -1,3 +1,3 @@
1
1
  module ThecoreAuthCommons
2
- VERSION = "3.6.1".freeze
2
+ VERSION = "3.6.3".freeze
3
3
  end
@@ -30,18 +30,58 @@ module ThecoreAuthCommons
30
30
  ENV["GOOGLE_CLIENT_ID"].present? && ENV["GOOGLE_CLIENT_SECRET"].present?
31
31
  end
32
32
 
33
- # Controlla se l'utente esiste, altrimenti lo crea con una password casuale
34
- # e lo restituisce. Se l'utente esiste già, lo restituisce senza modificarlo.
35
-
33
+ # Restituisce l'utente con l'email verificata dal provider OAuth, creandolo se non esiste:
34
+ # password casuale, auth_source = provider, nessun admin e nessun ruolo (si assegnano a mano).
35
+ # Di un utente esistente aggiorna solo nome e cognome: password, admin e auth_source restano,
36
+ # così chi ha anche una password locale può continuare a usarla. nil se l'utente non è salvabile.
36
37
  def self.check_user(email, name, surname, provider)
37
- u = User.find_or_initialize_by(email: email)
38
- u.name = name
39
- u.surname = surname
40
- u.password = u.password_confirmation = generate_secure_password
41
- u.auth_source = provider # 'google' or 'microsoft'
42
- u.admin = true
38
+ return if email.blank?
39
+
40
+ u = User.find_or_initialize_by(email: email.to_s.strip.downcase)
41
+ u.name = name if u.respond_to?(:name=)
42
+ u.surname = surname if u.respond_to?(:surname=)
43
+ if u.new_record?
44
+ u.password = u.password_confirmation = generate_secure_password
45
+ u.auth_source = provider # 'google' or 'microsoft'
46
+ end
43
47
  u.save if u.changed?
44
- u
48
+ u if u.persisted?
49
+ end
50
+
51
+ INITIAL_ADMIN_PASSWORD_FILE = "admin_initial_password".freeze
52
+
53
+ # Crea il primo admin, solo se nel DB non ce n'è nessuno. La password è ADMIN_PASSWORD,
54
+ # oppure una casuale scritta in <storage_dir>/admin_initial_password (0600) e mai nel log.
55
+ # Un file già presente non viene mai sovrascritto: in quel caso l'admin non viene creato.
56
+ # Se il salvataggio fallisce il file viene tolto, così il seed successivo può riprovare.
57
+ def self.seed_first_admin(storage_dir: Rails.root.join("storage"))
58
+ return if User.where(admin: true).exists?
59
+
60
+ email = "admin@#{ENV["BASE_DOMAIN"].presence || "example.com"}"
61
+ password = ENV["ADMIN_PASSWORD"].presence
62
+ unless password
63
+ generated_password_file = File.join(storage_dir.to_s, INITIAL_ADMIN_PASSWORD_FILE)
64
+ password = generate_secure_password
65
+ begin
66
+ FileUtils.mkdir_p(storage_dir.to_s)
67
+ # EXCL: mai sovrascrivere, anche con backend e worker che lanciano il seed insieme
68
+ File.open(generated_password_file, File::WRONLY | File::CREAT | File::EXCL, 0o600) { |f| f.puts password }
69
+ rescue Errno::EEXIST
70
+ puts "Admin #{email} NON creato: #{generated_password_file} esiste già. Cancellalo e rilancia il seed."
71
+ return
72
+ end
73
+ end
74
+
75
+ begin
76
+ u = User.find_or_initialize_by(email: email)
77
+ u.password = u.password_confirmation = password
78
+ u.admin = true
79
+ u.save!(validate: false)
80
+ rescue StandardError
81
+ File.delete(generated_password_file) if generated_password_file
82
+ raise
83
+ end
84
+ puts(generated_password_file ? "Admin #{email} creato: password in #{generated_password_file}" : "Admin #{email} creato con la password di ADMIN_PASSWORD")
45
85
  end
46
86
 
47
87
  def self.import_ldap_users_task
@@ -127,17 +167,17 @@ module ThecoreAuthCommons
127
167
 
128
168
  # Obbliga almeno un carattere da ogni gruppo
129
169
  password = [
130
- lowercase.sample,
131
- uppercase.sample,
132
- numbers.sample,
133
- symbols.sample,
170
+ lowercase.sample(random: SecureRandom),
171
+ uppercase.sample(random: SecureRandom),
172
+ numbers.sample(random: SecureRandom),
173
+ symbols.sample(random: SecureRandom),
134
174
  ]
135
175
 
136
176
  # Caratteri restanti scelti a caso tra tutti
137
177
  all_characters = lowercase + uppercase + numbers + symbols
138
- (length - 4).times { password << all_characters.sample }
178
+ (length - 4).times { password << all_characters.sample(random: SecureRandom) }
139
179
 
140
180
  # Mischia per evitare ordine prevedibile
141
- password.shuffle.join
181
+ password.shuffle(random: SecureRandom).join
142
182
  end
143
183
  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.6.1
4
+ version: 3.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriele Tassoni