thecore_auth_commons 3.6.4 → 3.7.0

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: 12a1ac91347e483964693a18bb413a7029d8c7ef01883d870c58d9e14d09f881
4
- data.tar.gz: 38f53bb70f70e1cd161ee5b747eccc13d429a76dc39f0da30fb933c03574e86d
3
+ metadata.gz: 50add36032057f0024e7af81e7055804e02e4eb61f90d176c699edc74992fddc
4
+ data.tar.gz: cfc23a269d4db8c23ea42dcf75497d8d29e3787d3cf400253e7469d393ee11ee
5
5
  SHA512:
6
- metadata.gz: e1f222792185d7a252a1f9f914708562c4fd6d218247c701f847295a6c3ae935c8646b1690f66a8c684f1185521a65ed4a3eb4f870d49bf480e3cb670ec5451f
7
- data.tar.gz: 84af957ff1d399bac491a9a38f6c94036f5450084f85baa5494d019f84ac148f3771d6878a2482f1bcaaafdd3648388cec06336891d7601f00dc0ae1bf447bc0
6
+ metadata.gz: 0ebba7a86ee0066b17941a455ee6ea4c9f492b6d40b30af9a815a2964e5ede52cfc844c61803f4ebcf4e434b1b2cbe78c93f0ae51eaad57ec7b9d9ec6ae11a26
7
+ data.tar.gz: ae818e334a2b1e04e8420d68f2eecc2c281ec88507442dcd46ce1e59ba5092a32c6c9d2e6058c4d69db0407d7cfb1325d4ee4a9cf49e81746fccba2c1d95168a
data/README.md CHANGED
@@ -94,6 +94,10 @@ user.roles << role
94
94
 
95
95
  `Users::SessionsController#create` tries Devise first; if that fails it tries `Ldap::Authenticator`. On successful LDAP auth the user is created/updated via `ThecoreAuthCommons.align_user` and signed in.
96
96
 
97
+ ### Locked users
98
+
99
+ A user with `locked = true` can't sign in through any channel (password, LDAP, OAuth) and an open session ends at its next request; the message is the same as for a wrong password. `User#active_for_authentication?` is the check, also used by the API, M2M tokens and ActionCable in the gems above.
100
+
97
101
  ### LDAP servers (`LdapServer` model)
98
102
 
99
103
  Multiple servers are supported, ordered by `priority` (ascending). Each server configures:
@@ -102,6 +106,8 @@ Multiple servers are supported, ordered by `priority` (ascending). Each server c
102
106
  - `auth_field` — LDAP attribute used as login (e.g. `mail`, `sAMAccountName`)
103
107
  - `name`, `surname`, `phone`, `code` — LDAP attributes mapped to User fields
104
108
 
109
+ `server.test_connection` binds as `admin_user` and returns `success?` and, on failure, a `message` with the reason (no admin credentials, refused bind, unreachable host); it never raises for a connection problem. RailsAdmin's **Test LDAP Server** action shows it.
110
+
105
111
  On LDAP login or import, `memberOf` groups are read; matching group names become `Role` records assigned to the user. Groups named `Administrators`, `Domain Admins`, `Schema Admins`, `Enterprise Admins`, `admins`, or `administrators` also grant `user.admin = true`.
106
112
 
107
113
  Deleting a `LdapServer` record keeps all users whose `auth_source` is `"ldap #{id}"` — same record, same roles — and turns them into local users (`auth_source = "local"`). To log in again they need a local password, set by hand.
@@ -1,17 +1,14 @@
1
1
  # app/controllers/users/sessions_controller.rb
2
2
  class Users::SessionsController < Devise::SessionsController
3
3
  def create
4
- self.resource = warden.authenticate(auth_options)
4
+ self.resource = local_user
5
5
 
6
6
  if resource
7
7
  Rails.logger.info("Authentication: Found local user, signing in")
8
8
  sign_in_and_redirect(resource)
9
9
  else
10
10
  Rails.logger.info("Authentication: Not found a local user, trying LDAP")
11
- user = Ldap::Authenticator.new(
12
- email: params[:user][:email],
13
- password: params[:user][:password],
14
- ).authenticate
11
+ user = ldap_user
15
12
 
16
13
  if user
17
14
  sign_in_and_redirect(user)
@@ -27,6 +24,23 @@ class Users::SessionsController < Devise::SessionsController
27
24
 
28
25
  private
29
26
 
27
+ # A user Devise refuses (User#active_for_authentication? false: a locked one) must get the same
28
+ # answer as a wrong password, never the failure app's redirect, which would tell that the
29
+ # password was right. Devise refuses by throw :warden from its after_set_user hook, after
30
+ # logging the user out: caught here, it becomes "no local user".
31
+ def local_user
32
+ user = catch(:warden) { warden.authenticate(auth_options) }
33
+ user if user.is_a?(resource_class)
34
+ end
35
+
36
+ def ldap_user
37
+ user = Ldap::Authenticator.new(
38
+ email: params[:user][:email],
39
+ password: params[:user][:password],
40
+ ).authenticate
41
+ user if user&.active_for_authentication?
42
+ end
43
+
30
44
  def sign_in_and_redirect(resource)
31
45
  set_flash_message!(:notice, :signed_in)
32
46
  sign_in(resource_name, resource)
@@ -21,9 +21,11 @@ class LdapServer < ApplicationRecord
21
21
 
22
22
  # Binds as the admin user and reports whether that worked, and why not. Never raises for a
23
23
  # server it cannot reach or bind to: the RailsAdmin test_ldap_server action shows the result.
24
+ # net-ldap raises Net::LDAP::Error for most connection failures, but the bare Errno (a
25
+ # SystemCallError, e.g. ECONNREFUSED) for a single host it cannot connect to.
24
26
  def test_connection
25
27
  if admin_user.blank? || admin_password.blank?
26
- return ConnectionResult.new(success: false, message: "No admin user and password are configured for this server")
28
+ return ConnectionResult.new(success: false, message: I18n.t("ldap_server.test_connection.no_admin_credentials"))
27
29
  end
28
30
 
29
31
  ldap = Net::LDAP.new(
@@ -44,7 +46,7 @@ class LdapServer < ApplicationRecord
44
46
  Rails.logger.info "Connection to LDAP server #{host} failed: #{reason}"
45
47
  ConnectionResult.new(success: false, message: reason)
46
48
  end
47
- rescue Net::LDAP::Error => e
49
+ rescue Net::LDAP::Error, SystemCallError => e
48
50
  Rails.logger.info "Connection to LDAP server #{host} failed: #{e.message}"
49
51
  ConnectionResult.new(success: false, message: e.message)
50
52
  end
data/app/models/user.rb CHANGED
@@ -38,6 +38,19 @@ class User < ApplicationRecord
38
38
  record.errors.add(attr, I18n.t("validation.errors.cannot_lock_last_admin")) if record.locked_changed? && record.locked_was == false && User.where(locked: false).count == 1
39
39
  end
40
40
 
41
+ # Devise asks this at every login (local, LDAP, OAuth) and at every request of an open session:
42
+ # a locked user is signed out and refused. Every other way to authenticate must ask it too
43
+ # (Odoo #6051).
44
+ def active_for_authentication?
45
+ super && !locked?
46
+ end
47
+
48
+ # The refusal reads like a wrong password, so it doesn't reveal that the account exists. Not
49
+ # Devise's :locked key: hosts and devise-i18n translate it as "Your account is locked."
50
+ def inactive_message
51
+ locked? ? :user_locked : super
52
+ end
53
+
41
54
  def display_name
42
55
  email
43
56
  end
@@ -1,5 +1,11 @@
1
1
  en:
2
+ ldap_server:
3
+ test_connection:
4
+ no_admin_credentials: "No admin user and password are configured for this server"
2
5
  devise:
6
+ failure:
7
+ # A locked user is refused with the same words as a wrong password (User#inactive_message)
8
+ user_locked: "Invalid credentials or password."
3
9
  sessions:
4
10
  invalid: "Invalid credentials or password."
5
11
  user:
@@ -1,5 +1,11 @@
1
1
  it:
2
+ ldap_server:
3
+ test_connection:
4
+ no_admin_credentials: "Utente e password di amministrazione non configurati per questo server"
2
5
  devise:
6
+ failure:
7
+ # A locked user is refused with the same words as a wrong password (User#inactive_message)
8
+ user_locked: "Credenziali %{authentication_keys} o password non valide."
3
9
  sessions:
4
10
  invalid: "Credenziali %{authentication_keys} o password non valide."
5
11
  user:
@@ -1,3 +1,3 @@
1
1
  module ThecoreAuthCommons
2
- VERSION = "3.6.4".freeze
2
+ VERSION = "3.7.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thecore_auth_commons
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.6.4
4
+ version: 3.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriele Tassoni
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-24 00:00:00.000000000 Z
11
+ date: 2026-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-ldap