SSO #1
|
|
@ -1,89 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
|
||||
use Firebase\JWT\JWT;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
#[AsCommand(name: 'app:listen-subscription', description: 'Listen to Mercure subscriptions')]
|
||||
class ListenSubscritptionCommand extends Command{
|
||||
|
||||
public function __construct(
|
||||
private HttpClientInterface $httpClient
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int{
|
||||
$tab = [
|
||||
'user' => 'charles',
|
||||
'remoteAddr' => 'rawsfsda'
|
||||
];
|
||||
$key = "!ChangeThisMercureHubJWTSecretKey!";
|
||||
$payload = [
|
||||
'mercure' => [
|
||||
'publish' => ['*'],
|
||||
'subscribe' => ['*'],
|
||||
'payload' => $tab
|
||||
],
|
||||
];
|
||||
|
||||
$jwt = JWT::encode($payload, $key, 'HS256');
|
||||
try{
|
||||
$response = $this->httpClient->request('GET', $_ENV['MERCURE_URL'] . '/subscriptions', [
|
||||
'headers' => [
|
||||
'Authorization' => 'Bearer ' . $jwt,
|
||||
'Content-Type' => 'application/ld+json',
|
||||
]
|
||||
]);
|
||||
$data = json_decode($response->getContent(), true);
|
||||
$subscriptions = $this->getSubscription($data);
|
||||
$encodedSubscriptions = json_encode($subscriptions, JSON_PRETTY_PRINT);
|
||||
$section = $output->section();
|
||||
$section2 = $output->section();
|
||||
$section->writeln('<info>Active Subscriptions:</info>');
|
||||
$section2->writeln($encodedSubscriptions);
|
||||
return Command::SUCCESS;
|
||||
}catch (\Throwable $e){
|
||||
$output->writeln('<error>Error fetching subscriptions: ' . $e->getMessage() . '</error>');
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
private function getSubscription(array $data): array {
|
||||
$subscriptions = [];
|
||||
foreach ($data['subscriptions'] as $sub) {
|
||||
if ($sub['active']) {
|
||||
$subscriptions[] = [
|
||||
'topic' => $sub['topic'],
|
||||
'user' => $sub['payload']['user'] ?? null,
|
||||
'remoteAddr' => $sub['payload']['remoteAddr'] ?? null,
|
||||
];
|
||||
}
|
||||
}
|
||||
return $subscriptions;
|
||||
}
|
||||
|
||||
private function getUserFromSubscription(array $subscription): ?string {
|
||||
$parts = parse_url($subscription['topic']);
|
||||
$userIdentifier = null;
|
||||
if (isset($parts['query'])) {
|
||||
parse_str($parts['query'], $query);
|
||||
if (isset($query['userId'])) {
|
||||
$userIdentifier = $query['userId'];
|
||||
}
|
||||
}
|
||||
return $userIdentifier;
|
||||
}
|
||||
|
||||
private function addSubscriptionToDb(array $subscription): void {
|
||||
// try{
|
||||
//
|
||||
// }
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue