| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Copyright 2024 Google Inc. All Rights Reserved. |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
namespace Dudlewebs\WPMCS\GCP\Google\Auth\Logging; |
| 19 |
|
| 20 |
use Dudlewebs\WPMCS\GCP\Psr\Log\LogLevel; |
| 21 |
/** |
| 22 |
* A trait used to call a PSR-3 logging interface. |
| 23 |
* |
| 24 |
* @internal |
| 25 |
*/ |
| 26 |
trait LoggingTrait |
| 27 |
{ |
| 28 |
/** |
| 29 |
* @param RpcLogEvent $event |
| 30 |
*/ |
| 31 |
private function logRequest(RpcLogEvent $event) : void |
| 32 |
{ |
| 33 |
$debugEvent = ['timestamp' => $event->timestamp, 'severity' => \strtoupper(LogLevel::DEBUG), 'processId' => $event->processId ?? null, 'requestId' => $event->requestId ?? null, 'rpcName' => $event->rpcName ?? null]; |
| 34 |
$debugEvent = \array_filter($debugEvent, fn($value) => !\is_null($value)); |
| 35 |
$jsonPayload = ['request.method' => $event->method, 'request.url' => $event->url, 'request.headers' => $event->headers, 'request.payload' => $this->truncatePayload($event->payload), 'request.jwt' => $this->getJwtToken($event->headers ?? []), 'retryAttempt' => $event->retryAttempt]; |
| 36 |
// Remove null values |
| 37 |
$debugEvent['jsonPayload'] = \array_filter($jsonPayload, fn($value) => !\is_null($value)); |
| 38 |
$stringifiedEvent = \json_encode($debugEvent, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE); |
| 39 |
// There was an error stringifying the event, return to not break execution |
| 40 |
if ($stringifiedEvent === \false) { |
| 41 |
return; |
| 42 |
} |
| 43 |
$this->logger->debug($stringifiedEvent); |
| 44 |
} |
| 45 |
/** |
| 46 |
* @param RpcLogEvent $event |
| 47 |
*/ |
| 48 |
private function logResponse(RpcLogEvent $event) : void |
| 49 |
{ |
| 50 |
$debugEvent = ['timestamp' => $event->timestamp, 'severity' => \strtoupper(LogLevel::DEBUG), 'processId' => $event->processId ?? null, 'requestId' => $event->requestId ?? null, 'jsonPayload' => ['response.status' => $event->status, 'response.headers' => $event->headers, 'response.payload' => $this->truncatePayload($event->payload), 'latencyMillis' => $event->latency]]; |
| 51 |
// Remove null values |
| 52 |
$debugEvent = \array_filter($debugEvent, fn($value) => !\is_null($value)); |
| 53 |
$debugEvent['jsonPayload'] = \array_filter($debugEvent['jsonPayload'], fn($value) => !\is_null($value)); |
| 54 |
$stringifiedEvent = \json_encode($debugEvent, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE); |
| 55 |
// There was an error stringifying the event, return to not break execution |
| 56 |
if ($stringifiedEvent !== \false) { |
| 57 |
$this->logger->debug($stringifiedEvent); |
| 58 |
} |
| 59 |
} |
| 60 |
/** |
| 61 |
* @param array<mixed> $headers |
| 62 |
* @return null|array<string, string|false> |
| 63 |
*/ |
| 64 |
private function getJwtToken(array $headers) : null|array |
| 65 |
{ |
| 66 |
if (empty($headers)) { |
| 67 |
return null; |
| 68 |
} |
| 69 |
$tokenHeader = $headers['Authorization'] ?? ''; |
| 70 |
$token = \str_replace('Bearer ', '', $tokenHeader); |
| 71 |
if (\substr_count($token, '.') !== 2) { |
| 72 |
return null; |
| 73 |
} |
| 74 |
[$header, $token, $_] = \explode('.', $token); |
| 75 |
return ['header' => \base64_decode($header), 'token' => \base64_decode($token)]; |
| 76 |
} |
| 77 |
/** |
| 78 |
* @param null|string $payload |
| 79 |
* @return string |
| 80 |
*/ |
| 81 |
private function truncatePayload(null|string $payload) : null|string |
| 82 |
{ |
| 83 |
$maxLength = 500; |
| 84 |
if (\is_null($payload) || \strlen($payload) <= $maxLength) { |
| 85 |
return $payload; |
| 86 |
} |
| 87 |
return \substr($payload, 0, $maxLength) . '...'; |
| 88 |
} |
| 89 |
} |
| 90 |
|