thecore_auth_commons 3.6.1 → 3.6.2

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: a2e252301334fb7245c39abbf78f6f999e3025ba309ff34167ba9cde0fba26db
4
+ data.tar.gz: 962f5fb41a22bccc567bcc1a10094a705ae488af244bb84af3f9cb330de2caa5
5
5
  SHA512:
6
- metadata.gz: 59d97568329cf50d32c444eaef8059199bc6f4d192976ab5b04b9209414b405be8808d16cf7eb87617976effb084e76a374a336d84dba87bbbf0fdd269b23559
7
- data.tar.gz: 22984f97a81b0a638cfa259ede0e525390d00889793976436744da448549c3d0cd4fd14ba8e7119ca67f1c36b3517b8c98c557a719c54e34aa0e99b580d22a6d
6
+ metadata.gz: 3e10fd5b4b36f478eeb6612c9336400e3cf9a49b0ce8938a74b8bb77e9108c464b96478e6d45c1a38fa4acf947675e6246878dd668e86d94a86e621856d0edf1
7
+ data.tar.gz: d50a7f9e96935300394691c04da037ded09759e5268c840ae2817172ecb550873b133ac0de3d517ff88711df08ae207c870f2c5f4857ff608240b061e4b6870b
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
 
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.2".freeze
3
3
  end
@@ -44,6 +44,42 @@ module ThecoreAuthCommons
44
44
  u
45
45
  end
46
46
 
47
+ INITIAL_ADMIN_PASSWORD_FILE = "admin_initial_password".freeze
48
+
49
+ # Crea il primo admin, solo se nel DB non ce n'è nessuno. La password è ADMIN_PASSWORD,
50
+ # oppure una casuale scritta in <storage_dir>/admin_initial_password (0600) e mai nel log.
51
+ # Un file già presente non viene mai sovrascritto: in quel caso l'admin non viene creato.
52
+ # Se il salvataggio fallisce il file viene tolto, così il seed successivo può riprovare.
53
+ def self.seed_first_admin(storage_dir: Rails.root.join("storage"))
54
+ return if User.where(admin: true).exists?
55
+
56
+ email = "admin@#{ENV["BASE_DOMAIN"].presence || "example.com"}"
57
+ password = ENV["ADMIN_PASSWORD"].presence
58
+ unless password
59
+ generated_password_file = File.join(storage_dir.to_s, INITIAL_ADMIN_PASSWORD_FILE)
60
+ password = generate_secure_password
61
+ begin
62
+ FileUtils.mkdir_p(storage_dir.to_s)
63
+ # EXCL: mai sovrascrivere, anche con backend e worker che lanciano il seed insieme
64
+ File.open(generated_password_file, File::WRONLY | File::CREAT | File::EXCL, 0o600) { |f| f.puts password }
65
+ rescue Errno::EEXIST
66
+ puts "Admin #{email} NON creato: #{generated_password_file} esiste già. Cancellalo e rilancia il seed."
67
+ return
68
+ end
69
+ end
70
+
71
+ begin
72
+ u = User.find_or_initialize_by(email: email)
73
+ u.password = u.password_confirmation = password
74
+ u.admin = true
75
+ u.save!(validate: false)
76
+ rescue StandardError
77
+ File.delete(generated_password_file) if generated_password_file
78
+ raise
79
+ end
80
+ puts(generated_password_file ? "Admin #{email} creato: password in #{generated_password_file}" : "Admin #{email} creato con la password di ADMIN_PASSWORD")
81
+ end
82
+
47
83
  def self.import_ldap_users_task
48
84
  puts "== Avvio sincronizzazione utenti da LDAP =="
49
85
 
@@ -127,17 +163,17 @@ module ThecoreAuthCommons
127
163
 
128
164
  # Obbliga almeno un carattere da ogni gruppo
129
165
  password = [
130
- lowercase.sample,
131
- uppercase.sample,
132
- numbers.sample,
133
- symbols.sample,
166
+ lowercase.sample(random: SecureRandom),
167
+ uppercase.sample(random: SecureRandom),
168
+ numbers.sample(random: SecureRandom),
169
+ symbols.sample(random: SecureRandom),
134
170
  ]
135
171
 
136
172
  # Caratteri restanti scelti a caso tra tutti
137
173
  all_characters = lowercase + uppercase + numbers + symbols
138
- (length - 4).times { password << all_characters.sample }
174
+ (length - 4).times { password << all_characters.sample(random: SecureRandom) }
139
175
 
140
176
  # Mischia per evitare ordine prevedibile
141
- password.shuffle.join
177
+ password.shuffle(random: SecureRandom).join
142
178
  end
143
179
  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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriele Tassoni