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

79 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Identity\Social;
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 provider 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 misconfiguration can let an identity provider mint an account
* with authority over the installation. v1's equivalent had a
* `social_login_default_role` setting that offered staff roles.
*
* Approval is the provider's own `auto_approve`, not the
* `ClientsAutoApprove` a self-registration asks, for the reason LDAP
* already established: one is about strangers arriving at a public form,
* the other about people a provider you configured has authenticated.
* `ClientsAutoGroup` stays shared, since which group a new client joins
* does not turn on how they arrived.
*/
class SocialProvisioner
{
public function __construct(
private readonly ClientProvisioning $clients,
) {}
/**
* @param bool $autoApprove Decided by the caller, not read from the
* settings row: an address the provider
* never verified goes to the approval
* queue whatever the setting says.
*/
public function provision(SocialSettings $settings, SocialIdentity $identity, bool $autoApprove): ?User
{
if (! $settings->auto_provision || $identity->email === null) {
return null;
}
// A deleted account still holds its address, and the insert below
// would hit the unique index — a 500 in the middle of a sign-in.
// Refusing here gives the caller the same "there is no account here
// for that address" it gives every other unprovisionable identity,
// which is also all a stranger should learn: whether an address was
// once an account here is not the provider's to publish.
if (! $this->clients->addressIsFree($identity->email)) {
Log::warning('A provider identity was not provisioned: the address belongs to a deleted account.', [
'provider' => $settings->provider->value,
'email' => $identity->email,
]);
return null;
}
return $this->clients->provision(
name: $identity->name ?? $identity->email,
email: $identity->email,
// A password they will never use and never learn: this account
// signs in through the provider. Generated rather than left
// null so nothing downstream has to special-case an empty
// hash, and it is why promoting one to staff requires setting
// a real password — see AccountConversion::requiresNewPassword().
password: Str::password(64),
action: Action::SocialClientProvisioned,
source: AuthSource::Social,
autoApprove: $autoApprove,
context: ['provider' => $settings->provider->label()],
);
}
}