demo_laravel_project/app/Actions/Auth/AuthenticateOidcUser.php
= b8e5995c73
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (8.3) (push) Has been cancelled
tests / ci (8.4) (push) Has been cancelled
tests / ci (8.5) (push) Has been cancelled
Add OIDC Socialite driver with configuration and provider tests
2026-06-17 07:17:05 +00:00

38 lines
958 B
PHP

<?php
namespace App\Actions\Auth;
use App\DTOs\OidcUserData;
use App\Models\User;
use Illuminate\Auth\AuthenticationException;
class AuthenticateOidcUser
{
/**
* Authenticate or link OIDC authenticated user.
*
* @throws AuthenticationException
*/
public function execute(OidcUserData $data): User
{
// Search by subject claim
$user = User::query()->where('oidc_sub', $data->sub)->first();
if (! $user) {
// Fallback: search by email to map existing accounts
$user = User::query()->where('email', $data->email)->first();
if ($user) {
// Link account
$user->oidc_sub = $data->sub;
$user->save();
} else {
// Deny registration
throw new AuthenticationException('Account not found. Please contact an administrator.');
}
}
return $user;
}
}