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