From 3e2fb97b9ec8fdeb31bfa6eb663500826a996ca5 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Mon, 9 Nov 2026 15:52:04 -0500 Subject: [PATCH 1/2] Fix identity matching for enum-cast provider names --- src/UserProviderRepository.php | 4 +- tests/EnumUser.php | 12 ++++++ tests/ProviderName.php | 9 ++++ tests/UserProviderRepositoryTest.php | 63 ++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 tests/EnumUser.php create mode 100644 tests/ProviderName.php diff --git a/src/UserProviderRepository.php b/src/UserProviderRepository.php index 912a237..708788a 100644 --- a/src/UserProviderRepository.php +++ b/src/UserProviderRepository.php @@ -33,7 +33,7 @@ public function exists(string $driver, SocialiteUser $user): bool ->where('email', $user->getEmail()) ->get(['provider_name', 'provider_id']) ->contains(fn ($account) => ( - $account->provider_name !== $driver + $account->getRawOriginal('provider_name') !== $driver || (string) $account->provider_id !== $providerId )); } @@ -58,7 +58,7 @@ public function updateOrCreate(string $driver, SocialiteUser $user): Authenticat 'provider_id' => $providerId, ]); - if ($eloquent && ($eloquent->provider_name !== $driver || (string) $eloquent->provider_id !== $providerId)) { + if ($eloquent && ($eloquent->getRawOriginal('provider_name') !== $driver || (string) $eloquent->provider_id !== $providerId)) { throw new AuthenticationException('The provider identity does not exactly match the existing account.'); } diff --git a/tests/EnumUser.php b/tests/EnumUser.php new file mode 100644 index 0000000..6d1697a --- /dev/null +++ b/tests/EnumUser.php @@ -0,0 +1,12 @@ + ProviderName::class, + ]; +} diff --git a/tests/ProviderName.php b/tests/ProviderName.php new file mode 100644 index 0000000..b5cc465 --- /dev/null +++ b/tests/ProviderName.php @@ -0,0 +1,9 @@ +and($result->name)->toBe('Updated Name') ->and(User::count())->toBe(1); }); + +it('updates the same identity when the provider name is enum cast', function (string $email) { + Bartender::setUserModel(EnumUser::class); + + $account = EnumUser::create([ + 'provider_name' => ProviderName::Foo, + 'provider_id' => '123', + 'name' => 'Original', + 'email' => 'original@example.com', + 'password' => 'password', + 'provider_refresh_token' => 'original-refresh', + ]); + + $identity = (new SocialiteUser)->map([ + 'id' => '123', 'name' => 'Updated', 'email' => $email, + ]); + $identity->token = 'updated-token'; + + $repository = new UserProviderRepository; + + expect($repository->exists('foo', $identity))->toBeFalse(); + + $result = $repository->updateOrCreate('foo', $identity); + + expect($result->id)->toBe($account->id) + ->and($result->provider_name)->toBe(ProviderName::Foo) + ->and($account->fresh()->email)->toBe($email) + ->and($account->fresh()->provider_access_token)->toBe('updated-token') + ->and($account->fresh()->provider_refresh_token)->toBe('original-refresh') + ->and(EnumUser::count())->toBe(1); +})->with(['original@example.com', 'changed@example.com']); + +it('rejects conflicting identities when the provider name is enum cast', function (string $driver, string $id) { + Bartender::setUserModel(EnumUser::class); + + $account = EnumUser::create([ + 'provider_name' => ProviderName::Foo, + 'provider_id' => '123', + 'name' => 'Original', + 'email' => 'original@example.com', + 'password' => 'password', + 'provider_access_token' => 'original-token', + ]); + + $identity = (new SocialiteUser)->map([ + 'id' => $id, 'name' => 'Incoming', 'email' => 'original@example.com', + ]); + $identity->token = 'incoming-token'; + + $repository = new UserProviderRepository; + + expect($repository->exists($driver, $identity))->toBeTrue(); + expect(fn () => $repository->updateOrCreate($driver, $identity))->toThrow(AuthenticationException::class); + expect($account->fresh()->provider_name)->toBe(ProviderName::Foo) + ->and($account->fresh()->provider_id)->toBe('123') + ->and($account->fresh()->provider_access_token)->toBe('original-token') + ->and(EnumUser::count())->toBe(1); +})->with([ + 'different provider' => ['bar', '123'], + 'different provider ID' => ['foo', '456'], +]); From 31cadc41df08ed63c411c2f4d059a135b5d83d18 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Mon, 9 Nov 2026 15:56:45 -0500 Subject: [PATCH 2/2] Compare raw provider IDs when matching identities --- src/UserProviderRepository.php | 4 +- tests/EnumProviderIdUser.php | 12 +++++ tests/IntegerProviderIdUser.php | 12 +++++ tests/ProviderId.php | 9 ++++ tests/UserProviderRepositoryTest.php | 68 ++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 tests/EnumProviderIdUser.php create mode 100644 tests/IntegerProviderIdUser.php create mode 100644 tests/ProviderId.php diff --git a/src/UserProviderRepository.php b/src/UserProviderRepository.php index 708788a..c989dc9 100644 --- a/src/UserProviderRepository.php +++ b/src/UserProviderRepository.php @@ -34,7 +34,7 @@ public function exists(string $driver, SocialiteUser $user): bool ->get(['provider_name', 'provider_id']) ->contains(fn ($account) => ( $account->getRawOriginal('provider_name') !== $driver - || (string) $account->provider_id !== $providerId + || (string) $account->getRawOriginal('provider_id') !== $providerId )); } @@ -58,7 +58,7 @@ public function updateOrCreate(string $driver, SocialiteUser $user): Authenticat 'provider_id' => $providerId, ]); - if ($eloquent && ($eloquent->getRawOriginal('provider_name') !== $driver || (string) $eloquent->provider_id !== $providerId)) { + if ($eloquent && ($eloquent->getRawOriginal('provider_name') !== $driver || (string) $eloquent->getRawOriginal('provider_id') !== $providerId)) { throw new AuthenticationException('The provider identity does not exactly match the existing account.'); } diff --git a/tests/EnumProviderIdUser.php b/tests/EnumProviderIdUser.php new file mode 100644 index 0000000..a4c2411 --- /dev/null +++ b/tests/EnumProviderIdUser.php @@ -0,0 +1,12 @@ + ProviderId::class, + ]; +} diff --git a/tests/IntegerProviderIdUser.php b/tests/IntegerProviderIdUser.php new file mode 100644 index 0000000..5523cfe --- /dev/null +++ b/tests/IntegerProviderIdUser.php @@ -0,0 +1,12 @@ + 'integer', + ]; +} diff --git a/tests/ProviderId.php b/tests/ProviderId.php new file mode 100644 index 0000000..834f37d --- /dev/null +++ b/tests/ProviderId.php @@ -0,0 +1,9 @@ + ['bar', '123'], 'different provider ID' => ['foo', '456'], ]); + +it('updates exact identities without applying provider ID casts to comparisons', function (string $model, $castId, string $email) { + Bartender::setUserModel($model); + + $account = $model::create([ + 'provider_name' => 'foo', + 'provider_id' => '00123', + 'name' => 'Original', + 'email' => 'original@example.com', + 'password' => 'password', + 'provider_refresh_token' => 'original-refresh', + ]); + + $identity = (new SocialiteUser)->map([ + 'id' => '00123', 'name' => 'Updated', 'email' => $email, + ]); + $identity->token = 'updated-token'; + + $repository = new UserProviderRepository; + + expect($repository->exists('foo', $identity))->toBeFalse(); + + $result = $repository->updateOrCreate('foo', $identity); + + expect($result->id)->toBe($account->id) + ->and($result->provider_id)->toBe($castId) + ->and($account->fresh()->getRawOriginal('provider_id'))->toBe('00123') + ->and($account->fresh()->email)->toBe($email) + ->and($account->fresh()->provider_access_token)->toBe('updated-token') + ->and($account->fresh()->provider_refresh_token)->toBe('original-refresh') + ->and($model::count())->toBe(1); +})->with([ + 'enum cast' => [EnumProviderIdUser::class, ProviderId::Original], + 'integer cast' => [IntegerProviderIdUser::class, 123], +])->with(['original@example.com', 'changed@example.com']); + +it('rejects conflicting identities without applying provider ID casts to comparisons', function (string $model, string $id) { + Bartender::setUserModel($model); + + $account = $model::create([ + 'provider_name' => 'foo', + 'provider_id' => '00123', + 'name' => 'Original', + 'email' => 'original@example.com', + 'password' => 'password', + 'provider_access_token' => 'original-token', + ]); + + $identity = (new SocialiteUser)->map([ + 'id' => $id, 'name' => 'Incoming', 'email' => 'original@example.com', + ]); + $identity->token = 'incoming-token'; + + $repository = new UserProviderRepository; + + expect($repository->exists('foo', $identity))->toBeTrue(); + expect(fn () => $repository->updateOrCreate('foo', $identity))->toThrow(AuthenticationException::class); + expect($account->fresh()->getRawOriginal('provider_id'))->toBe('00123') + ->and($account->fresh()->name)->toBe('Original') + ->and($account->fresh()->provider_access_token)->toBe('original-token') + ->and($model::count())->toBe(1); +})->with([ + 'different enum ID' => [EnumProviderIdUser::class, '00456'], + 'integer cast conceals leading zeros' => [IntegerProviderIdUser::class, '123'], +]);