Include PIN when validating plex context.

This commit is contained in:
arabcoders
2025-04-23 16:52:18 +03:00
parent e3789a9a1e
commit 0fd19debe6

View File

@@ -9,6 +9,7 @@ use App\Backends\Plex\Action\GetUser;
use App\Libs\Container;
use App\Libs\Enums\Http\Status;
use App\Libs\Exceptions\Backends\InvalidContextException;
use App\Libs\Options;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
@@ -82,6 +83,11 @@ final readonly class PlexValidateContext
{
try {
$url = $context->backendUrl->withPath('/');
if (null !== ($pin = ag($context->options, Options::PLEX_USER_PIN))) {
$url = $url->withQuery(http_build_query(['pin' => (string)$pin]));
}
$request = $this->http->request('GET', (string)$url, [
'headers' => [
'Accept' => 'application/json',
@@ -89,21 +95,25 @@ final readonly class PlexValidateContext
],
]);
if (Status::UNAUTHORIZED->value === $request->getStatusCode()) {
if (Status::UNAUTHORIZED === Status::tryFrom($request->getStatusCode())) {
throw new InvalidContextException('Backend responded with 401. Most likely means token is invalid.');
}
if (Status::NOT_FOUND->value === $request->getStatusCode()) {
if (Status::NOT_FOUND === Status::tryFrom($$request->getStatusCode())) {
throw new InvalidContextException('Backend responded with 404. Most likely means url is incorrect.');
}
return $request->getContent(true);
} catch (TransportExceptionInterface $e) {
throw new InvalidContextException(r('Failed to connect to backend. {error}', ['error' => $e->getMessage()]),
previous: $e);
throw new InvalidContextException(
r('Failed to connect to backend. {error}', ['error' => $e->getMessage()]),
previous: $e
);
} catch (ClientExceptionInterface $e) {
throw new InvalidContextException(r('Got non 200 response. {error}', ['error' => $e->getMessage()]),
previous: $e);
throw new InvalidContextException(
r('Got non 200 response. {error}', ['error' => $e->getMessage()]),
previous: $e
);
} catch (RedirectionExceptionInterface $e) {
throw new InvalidContextException(
r('Redirection recursion detected. {error}', ['error' => $e->getMessage()]),