Skip to content
Closed
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
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
bootstrap="tests/bootstrap.php"
colors="true"
>
<php>
<ini name="date.timezone" value="UTC"/>
</php>

<testsuites>
<testsuite name="unit">
<directory>tests/Unit</directory>
Expand Down
18 changes: 17 additions & 1 deletion src/Firebase/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
/**
* @internal
*/
final class Auth implements Contract\Auth
final class Auth implements Contract\Auth, Contract\Transitional\FederatedUserFetcher
{
private readonly Parser $jwtParser;

Expand Down Expand Up @@ -210,6 +210,22 @@
return UserRecord::fromResponseData($data['users'][0]);
}

public function getUserByProviderUid(Stringable|string $providerId, Stringable|string $providerUid): UserRecord
{
$providerId = (string) $providerId;
$providerUid = (string) $providerUid;

$response = $this->client->getUserByProviderUid($providerId, $providerUid);

$data = Json::decode((string) $response->getBody(), true);

if (empty($data['users'][0])) {
throw new UserNotFound("No user with federated account ID '{$providerId}:{$providerUid}' found.");
}

return UserRecord::fromResponseData($data['users'][0]);

Check warning on line 226 in src/Firebase/Auth.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Auth.php#L226

Added line #L226 was not covered by tests
}

public function createAnonymousUser(): UserRecord
{
return $this->createUser(CreateUser::new());
Expand Down
10 changes: 10 additions & 0 deletions src/Firebase/Auth/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ public function getUserByPhoneNumber(string $phoneNumber): ResponseInterface
return $this->requestApi($url, ['phoneNumber' => [$phoneNumber]]);
}

/**
* @throws AuthException
*/
public function getUserByProviderUid(string $providerId, string $uid): ResponseInterface
{
$url = $this->awareAuthResourceUrlBuilder->getUrl('/accounts:lookup');

return $this->requestApi($url, ['federatedUserId' => [['providerId' => $providerId, 'rawId' => $uid]]]);
}

/**
* @throws AuthException
*/
Expand Down
24 changes: 24 additions & 0 deletions src/Firebase/Contract/Transitional/FederatedUserFetcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Kreait\Firebase\Contract\Transitional;

use Kreait\Firebase\Auth\UserRecord;
use Kreait\Firebase\Exception;
use Stringable;

/**
* @TODO: This interface is intended to be integrated into the Auth interface on the next major release.
*/
interface FederatedUserFetcher
{
/**
* @param Stringable|non-empty-string $providerId
* @param Stringable|non-empty-string $providerUid
*
* @throws Exception\AuthException
* @throws Exception\FirebaseException
*/
public function getUserByProviderUid(Stringable|string $providerId, Stringable|string $providerUid): UserRecord;
}
16 changes: 16 additions & 0 deletions tests/Integration/AuthTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Kreait\Firebase\Auth\SignIn\FailedToSignIn;
use Kreait\Firebase\Auth\UserRecord;
use Kreait\Firebase\Contract\Auth;
use Kreait\Firebase\Contract\Transitional\FederatedUserFetcher;
use Kreait\Firebase\Exception\Auth\InvalidOobCode;
use Kreait\Firebase\Exception\Auth\RevokedIdToken;
use Kreait\Firebase\Exception\Auth\RevokedSessionCookie;
Expand Down Expand Up @@ -520,6 +521,21 @@ public function getUserByNonExistingPhoneNumber(): void
$this->auth->getUserByPhoneNumber($phoneNumber);
}

#[Test]
public function getUserByNonExistingProviderUid(): void
{
if ($this->auth instanceof FederatedUserFetcher) {
$provider = 'test.com';
$uid = 'u' . random_int(1000000, 9999999);

$this->expectException(UserNotFound::class);
/** @phpstan-ignore method.notFound */
$this->auth->getUserByProviderUid($provider, $uid);
} else {
$this->fail("{\$this->auth} does not implement FederatedUserFetcher as expected - cannot test");
}
}

#[Test]
public function createUser(): void
{
Expand Down