Files
denkfabrik-li 9ddd39c41d Refuse to provision over a deleted account's address instead of crashing
The unique index on `email` spans soft-deleted rows -- AvailableEmailRule
is built on exactly that, so a deleted account keeps its address until
erasure removes the row. The registration form learns this from
validation. The machine paths have no form: a directory or an identity
provider hands over an address and provision() inserts it.

Measured on main, a client deleted last week signing in through a
provider that may auto-provision:

  GET /auth/google/callback → 500   (QueryException, unique constraint)

Same shape through LDAP at POST /login. Nothing is created, nothing is
signed in, and what the person meets is a server error.

Both provisioners now ask ClientProvisioning::addressIsFree() first and
refuse. The social flow already has a refusal for an identity it cannot
provision -- "There is no account here for that address." -- which is also
all a stranger should learn: whether an address was once an account here
is not the provider's to publish. The LDAP flow falls through to the
ordinary failed sign-in.

Deliberately not resurrecting the deleted account. Restoring one because
a directory says the address exists is a decision for a person, not a
side effect of somebody logging in.

Two tests, one per path: the sign-in is refused, nothing is created, and
the trashed row is still trashed. Both go red without the fix.
2026-08-28 06:40:50 +02:00

87 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Identity\Ldap;
use App\Models\User;
use App\Modules\Audit\Action;
use App\Modules\Clients\ClientProvisioning;
use App\Modules\Identity\AuthSource;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
/**
* A directory identity signing in for the first time, with no local
* account yet.
*
* Always a client, never staff — there is no role parameter on this path,
* so no configuration mistake can make a directory mint an account with
* authority over the installation. v1's equivalent had a "default role"
* setting that offered staff roles and bypassed the permission checks
* while it did so.
*
* Approval is the directory's own setting, `auto_approve`, rather than the
* `ClientsAutoApprove` a self-registration asks. Those look like the same
* question and are not: one is about strangers arriving at a public form,
* the other about people your directory has already authenticated, and an
* installation can reasonably answer them differently — make the web wait,
* let staff in. `ClientsAutoGroup` is still shared, since which group a new
* client joins does not turn on how they arrived.
*
* When approval is required, the login that created the account is refused
* by the ordinary inactive-account branch with the ordinary "not approved
* yet" message, and the account waits in the account-requests queue.
*/
class LdapProvisioner
{
public function __construct(
private readonly LdapAuthenticator $ldap,
private readonly ClientProvisioning $clients,
) {}
public function provision(string $email, string $password): ?User
{
$settings = LdapSettings::current();
if (! $this->ldap->enabled() || ! $settings->auto_provision) {
return null;
}
// Same refusals as an ordinary LDAP login — an empty or
// whitespace-only password must never reach a bind.
$identity = $this->ldap->attempt($email, $password);
if ($identity === null) {
return null;
}
// Same reason as SocialProvisioner: a deleted account keeps its
// address until erasure removes the row, so provisioning over one
// raises a QueryException at the moment of login. Refused here, the
// sign-in fails the ordinary way instead, and a directory identity
// does not silently reclaim an account somebody deleted.
if (! $this->clients->addressIsFree($identity->email)) {
Log::warning('A directory identity was not provisioned: the address belongs to a deleted account.', [
'email' => $identity->email,
]);
return null;
}
return $this->clients->provision(
name: $identity->name,
email: $identity->email,
// A password they will never use and never learn: this account
// authenticates against the directory. Generated rather than
// left null so nothing downstream has to special-case an empty
// hash, and never the directory password.
password: Str::password(64),
action: Action::LdapClientProvisioned,
source: AuthSource::Ldap,
ldapDn: $identity->dn,
autoApprove: $settings->auto_approve,
);
}
}