-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Notifier] [Firebase] Add 'HTTP v1' api endpoint #53336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3e5bf69
[FirebaseNotifier] Add 'HTTP v1' api endpoint
cesurapp ac9002f
Removed Jwt Package Dependency
cesurapp 73525c9
Update src/Symfony/Component/Notifier/Bridge/Firebase/FirebaseJwtTran…
cesurapp ec11593
Update src/Symfony/Component/Notifier/Bridge/Firebase/FirebaseJwtTran…
cesurapp b035895
Removed Lagacy API & DSN Optimized
cesurapp 17f0ea8
Merge branch '7.2' into feature_7.1
cesurapp 6957bd4
Update src/Symfony/Component/Notifier/Bridge/Firebase/CHANGELOG.md
cesurapp a068ca4
Env Special Chars Encode
cesurapp 495e796
Merge remote-tracking branch 'origin/feature_7.1' into feature_7.1
cesurapp 0ff2696
Fixed Specials Chaharacter
cesurapp 47075c1
Fix String Replace
cesurapp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
[FirebaseNotifier] Add 'HTTP v1' api endpoint
- Loading branch information
commit 3e5bf699571e4bafc3f991b2dd59f0f769cf6ad2
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
src/Symfony/Component/Notifier/Bridge/Firebase/FirebaseJwtTransport.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\Firebase; | ||
|
||
use Ahc\Jwt\JWT; | ||
use Symfony\Component\Notifier\Exception\InvalidArgumentException; | ||
use Symfony\Component\Notifier\Exception\TransportException; | ||
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; | ||
use Symfony\Component\Notifier\Message\ChatMessage; | ||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Component\Notifier\Message\SentMessage; | ||
use Symfony\Component\Notifier\Transport\AbstractTransport; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @author Cesur APAYDIN <https://github.com/cesurapp> | ||
*/ | ||
final class FirebaseJwtTransport extends AbstractTransport | ||
{ | ||
protected const HOST = "fcm.googleapis.com/v1/projects/project_id/messages:send"; | ||
|
||
private array $credentials; | ||
|
||
public function __construct(#[\SensitiveParameter] array $credentials, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) | ||
{ | ||
$this->credentials = $credentials; | ||
$this->client = $client; | ||
|
||
$this->setHost(str_repl 8000 ace('project_id', $credentials['project_id'], $this->getDefaultHost())); | ||
|
||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return sprintf('firebase-jwt://%s', $this->getEndpoint()); | ||
} | ||
|
||
public function supports(MessageInterface $message): bool | ||
{ | ||
return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof FirebaseOptions); | ||
} | ||
|
||
protected function doSend(MessageInterface $message): SentMessage | ||
{ | ||
if (!$message instanceof ChatMessage) { | ||
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); | ||
} | ||
|
||
$endpoint = sprintf('https://%s', $this->getEndpoint()); | ||
$options = $message->getOptions()?->toArray() ?? []; | ||
$options['token'] = $message->getRecipientId(); | ||
unset($options['to']); | ||
|
||
if (!$options['token']) { | ||
throw new InvalidArgumentException(sprintf('The "%s" transport required the "to" option to be set.', __CLASS__)); | ||
} | ||
$options['notification']['body'] = $message->getSubject(); | ||
$options['data'] ??= []; | ||
|
||
// Send | ||
$response = $this->client->request('POST', $endpoint, [ | ||
'headers' => [ | ||
'Authorization' => sprintf('Bearer %s', $this->getJwtToken()), | ||
], | ||
'json' => array_filter(['message' => $options]), | ||
]); | ||
|
||
try { | ||
$statusCode = $response->getStatusCode(); | ||
} catch (TransportExceptionInterface $e) { | ||
throw new TransportException('Could not reach the remote Firebase server.', $response, 0, $e); | ||
} | ||
|
||
$contentType = $response->getHeaders(false)['content-type'][0] ?? ''; | ||
$jsonContents = str_starts_with($contentType, 'application/json') ? $response->toArray(false) : null; | ||
$errorMessage = null; | ||
|
||
if ($jsonContents && isset($jsonContents['results'][0]['error'])) { | ||
$errorMessage = $jsonContents['results'][0]['error']; | ||
} elseif (200 !== $statusCode) { | ||
$errorMessage = $response->getContent(false); | ||
} | ||
|
||
if (null !== $errorMessage) { | ||
throw new TransportException('Unable to post the Firebase message: ' . $errorMessage, $response); | ||
8000 } | ||
|
||
$success = $response->toArray(false); | ||
|
||
$sentMessage = new SentMessage($message, (string)$this); | ||
$sentMessage->setMessageId($success['results'][0]['message_id'] ?? ''); | ||
|
||
return $sentMessage; | ||
} | ||
|
||
private function getJwtToken(): string | ||
{ | ||
$time = time(); | ||
$payload = [ | ||
'iss' => $this->credentials['client_email'], | ||
'sub' => $this->credentials['client_email'], | ||
'aud' => 'https://fcm.googleapis.com/', | ||
'iat' => $time, | ||
'exp' => $time + 3600, | ||
'kid' => $this->credentials['private_key_id'] | ||
]; | ||
|
||
$jwt = new JWT(openssl_pkey_get_private($this->credentials['private_key']), 'RS256'); | ||
|
||
return $jwt->encode($payload); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/Symfony/Component/Notifier/Bridge/Firebase/Tests/FirebaseJwtTransportFactoryTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\Firebase\Tests; | ||
|
||
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory; | ||
use Symfony\Component\Notifier\Test\TransportFactoryTestCase; | ||
|
||
/** | ||
* @author Cesur APAYDIN <https://github.com/cesurapp> | ||
*/ | ||
final class FirebaseJwtTransportFactoryTest extends TransportFactoryTestCase | ||
{ | ||
public function createFactory(): FirebaseTransportFactory | ||
{ | ||
return new FirebaseTransportFactory(); | ||
} | ||
|
||
public static function createProvider(): iterable | ||
{ | ||
yield [ | ||
'firebase-jwt://fcm.googleapis.com/v1/projects/test_project/messages:send', | ||
'firebase-jwt://credentials_content:ewogICJ0eXBlIjogIiIsCiAgInByb2plY3RfaWQiOiAidGVzdF9wcm9qZWN0Igp9Cg==@default', | ||
]; | ||
} | ||
|
||
public static function supportsProvider(): iterable | ||
{ | ||
yield [true, 'firebase-jwt://credentials_path:crendentials.json@default']; | ||
yield [true, 'firebase-jwt://credentials_content:base64Content@default']; | ||
yield [false, 'somethingElse://username:password@default']; | ||
} | ||
|
||
public static function unsupportedSchemeProvider(): iterable | ||
{ | ||
yield ['somethingElse://username:password@default']; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/Symfony/Component/Notifier/Bridge/Firebase/Tests/FirebaseJwtTransportTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\Firebase\Tests; | ||
|
||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Component\HttpClient\Response\MockResponse; | ||
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseJwtTransport; | ||
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseOptions; | ||
use Symfony\Component\Notifier\Exception\TransportException; | ||
use Symfony\Component\Notifier\Message\ChatMessage; | ||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
use Symfony\Component\Notifier\Test\TransportTestCase; | ||
use Symfony\Component\Notifier\Tests\Transport\DummyMessage; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
/** | ||
* @author Cesur APAYDIN <https://github.com/cesurapp> | ||
*/ | ||
final class FirebaseJwtTransportTest extends TransportTestCase | ||
{ | ||
public static function createTransport(HttpClientInterface $client = null): FirebaseJwtTransport | ||
{ | ||
return new FirebaseJwtTransport([ | ||
'project_id' => 'test_project', | ||
'client_email' => 'firebase-adminsdk-test@test.iam.gserviceaccount.com', | ||
'private_key_id' => 'sdas7d6a8ds6ds78a', | ||
'private_key' => "-----BEGIN RSA PRIVATE KEY-----\nMIICWwIBAAKBgGN4fgq4BFQwjK7kzWUYSFE1ryGIBtUScY5TqLY2BAROBnZS+SIa\nH4VcZJStPUwjtsVxJTf57slhMM5FbAOQkWFMmRlHGWc7EZy6UMMvP8FD21X3Ty9e\nZzJ/Be30la1Uy7rechBh3RN+Y3rSKV+gDmsjdo5/4Jekj4LfluDXbwVJAgMBAAEC\ngYA5SqY2IEUGBKyS81/F8ZV9iNElHAbrZGMZWeAbisMHg7U/I40w8iDjnBKme52J\npCxaTk/kjMTXIm6M7/lFmFfTHgl5WLCimu2glMyKFM2GBYX/cKx9RnI36q3uJYml\n1G1f2H7ALurisenEqMaq8bdyApd/XNqcijogfsZ1K/irTQJBAKEQFkqNDgwUgAwr\njhG/zppl5yEJtP+Pncp/2t/s6khk0q8N92xw6xl8OV/ww+rwlJB3IKVKw903LztQ\nP1D3zpMCQQCeGlOvMx9XxiktNIkdXekGP/bFUR9/u0ABaYl9valZ2B3yZzujJJHV\n0EtyKGorT39wWhWY7BI8NTYgivCIWGozAkEAhMnOlwhUXIFKUL5YEyogHAuH0yU9\npLWzUhC3U4bwYV8+lDTfmPg/3HMemorV/Az9b13H/H73nJqyxiQTD54/IQJAZUX/\n7O4WWac5oRdR7VnGdpZqgCJixvMvILh1tfHTlRV2uVufO/Wk5Q00BsAUogGeZF2Q\nEBDH7YE4VsgpI21fOQJAJdSB7mHvStlYCQMEAYWCWjk+NRW8fzZCkQkqzOV6b9dw\nDFp6wp8aLw87hAHUz5zXTCRYi/BpvDhfP6DDT2sOaw==\n-----END RSA PRIVATE KEY-----" | ||
], $client ?? new MockHttpClient()); | ||
} | ||
|
||
public static function toStringProvider(): iterable | ||
{ | ||
yield ['firebase-jwt://fcm.googleapis.com/v1/projects/test_project/messages:send', self::createTransport()]; | ||
} | ||
|
||
public static function supportedMessagesProvider(): iterable | ||
{ | ||
yield [new ChatMessage('Hello!')]; | ||
} | ||
|
||
public static function unsupportedMessagesProvider(): iterable | ||
{ | ||
yield [new SmsMessage('0611223344', 'Hello!')]; | ||
yield [new DummyMessage()]; | ||
} | ||
|
||
/** | ||
* @dataProvider sendWithErrorThrowsExceptionProvider | ||
*/ | ||
public function testSendWithErrorThrowsTransportException(ResponseInterface $response) | ||
{ | ||
$this->expectException(TransportException::class); | ||
|
||
$client = new MockHttpClient(static fn (): ResponseInterface => $response); | ||
$options = new class('recipient-id', []) extends FirebaseOptions {}; | ||
|
||
$transport = self::createTransport($client); | ||
|
||
$transport->send(new ChatMessage('Hello!', $options)); | ||
} | ||
|
||
public static function sendWithErrorThrowsExceptionProvider(): iterable | ||
{ | ||
yield [new MockResponse( | ||
json_encode(['results' => [['error' => 'testErrorCode']]]), | ||
['response_headers' => ['content-type' => ['application/json']], 'http_code' => 200] | ||
)]; | ||
|
||
yield [new MockResponse( | ||
json_encode(['results' => [['error' => 'testErrorCode']]]), | ||
['response_headers' => ['content-type' => ['application/json']], 'http_code' => 400] | ||
)]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.