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 +4 -4
- data/README.md +2 -2
- data/db/seeds.rb +1 -9
- data/lib/thecore_auth_commons/version.rb +1 -1
- data/lib/thecore_auth_commons.rb +42 -6
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a2e252301334fb7245c39abbf78f6f999e3025ba309ff34167ba9cde0fba26db
|
|
4
|
+
data.tar.gz: 962f5fb41a22bccc567bcc1a10094a705ae488af244bb84af3f9cb330de2caa5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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` |
|
|
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
|
|
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
|
-
|
|
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],
|
data/lib/thecore_auth_commons.rb
CHANGED
|
@@ -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
|