Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/UserProviderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public function exists(string $driver, SocialiteUser $user): bool
->where('email', $user->getEmail())
->get(['provider_name', 'provider_id'])
->contains(fn ($account) => (
$account->provider_name !== $driver
|| (string) $account->provider_id !== $providerId
$account->getRawOriginal('provider_name') !== $driver
|| (string) $account->getRawOriginal('provider_id') !== $providerId
));
}

Expand All @@ -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->getRawOriginal('provider_id') !== $providerId)) {
throw new AuthenticationException('The provider identity does not exactly match the existing account.');
}

Expand Down
12 changes: 12 additions & 0 deletions tests/EnumProviderIdUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace DirectoryTree\Bartender\Tests;

class EnumProviderIdUser extends User
{
protected $table = 'users';

protected $casts = [
'provider_id' => ProviderId::class,
];
}
12 changes: 12 additions & 0 deletions tests/EnumUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace DirectoryTree\Bartender\Tests;

class EnumUser extends User
{
protected $table = 'users';

protected $casts = [
'provider_name' => ProviderName::class,
];
}
12 changes: 12 additions & 0 deletions tests/IntegerProviderIdUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace DirectoryTree\Bartender\Tests;

class IntegerProviderIdUser extends User
{
protected $table = 'users';

protected $casts = [
'provider_id' => 'integer',
];
}
9 changes: 9 additions & 0 deletions tests/ProviderId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace DirectoryTree\Bartender\Tests;

enum ProviderId: string
{
case Original = '00123';
case Other = '00456';
}
9 changes: 9 additions & 0 deletions tests/ProviderName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace DirectoryTree\Bartender\Tests;

enum ProviderName: string
{
case Foo = 'foo';
case Bar = 'bar';
}
131 changes: 131 additions & 0 deletions tests/UserProviderRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php

use DirectoryTree\Bartender\Facades\Bartender;
use DirectoryTree\Bartender\Tests\EnumProviderIdUser;
use DirectoryTree\Bartender\Tests\EnumUser;
use DirectoryTree\Bartender\Tests\IntegerProviderIdUser;
use DirectoryTree\Bartender\Tests\ProviderId;
use DirectoryTree\Bartender\Tests\ProviderName;
use DirectoryTree\Bartender\Tests\User;
use DirectoryTree\Bartender\UserProviderRepository;
use Illuminate\Auth\AuthenticationException;
Expand Down Expand Up @@ -340,3 +345,129 @@ public function getName()
->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'],
]);

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'],
]);
Loading