$raw */ public function __construct( #[\SensitiveParameter] public string $accessToken, #[\SensitiveParameter] public ?string $refreshToken = NULL, public string $tokenType = 'DPoP', public ?string $scope = NULL, public ?int $expiresAt = NULL, #[\SensitiveParameter] public array $raw = [], ) {} /** * Creates a token set from an OAuth token response. * * @param array $response */ public static function fromResponse(#[\SensitiveParameter] array $response, ?int $now = NULL): self { if (!\is_string($response['access_token'] ?? NULL) || $response['access_token'] === '') { throw new \InvalidArgumentException('OAuth token response is missing "access_token".'); } if (!\is_string($response['token_type'] ?? NULL) || \strcasecmp($response['token_type'], 'DPoP') !== 0) { throw new \InvalidArgumentException('OAuth token response must declare the DPoP token type.'); } $scopes = \is_string($response['scope'] ?? NULL) ? \preg_split('/ +/', \trim($response['scope'])) : FALSE; if (!\is_array($scopes) || !\in_array('atproto', $scopes, TRUE)) { throw new \InvalidArgumentException('OAuth token response must grant the atproto scope.'); } $expiresAt = NULL; if (\is_int($response['expires_in'] ?? NULL)) { $expiresAt = ($now ?? \time()) + $response['expires_in']; } $raw = $response; unset($raw['access_token'], $raw['refresh_token']); return new self( accessToken: $response['access_token'], refreshToken: \is_string($response['refresh_token'] ?? NULL) ? $response['refresh_token'] : NULL, tokenType: $response['token_type'], scope: $response['scope'], expiresAt: $expiresAt, raw: $raw, ); } /** * Restores a token set from its persisted representation. * * @param array $data */ public static function fromArray(#[\SensitiveParameter] array $data): self { if (!\is_string($data['accessToken'] ?? NULL) || $data['accessToken'] === '') { throw new \InvalidArgumentException('OAuth token data is missing "accessToken".'); } $refreshToken = self::optionalString($data, 'refreshToken'); $tokenType = \array_key_exists('tokenType', $data) ? self::optionalString($data, 'tokenType') : 'DPoP'; if ($tokenType === NULL) { throw new \InvalidArgumentException('OAuth token data "tokenType" must not be null.'); } if (\strcasecmp($tokenType, 'DPoP') !== 0) { throw new \InvalidArgumentException('OAuth token data must declare the DPoP token type.'); } $scope = self::optionalString($data, 'scope'); $scopes = $scope !== NULL ? \preg_split('/ +/', \trim($scope)) : FALSE; if ($scope !== NULL && (!\is_array($scopes) || !\in_array('atproto', $scopes, TRUE))) { throw new \InvalidArgumentException('OAuth token data scope must include "atproto".'); } $expiresAt = self::optionalInteger($data, 'expiresAt'); if (\array_key_exists('raw', $data) && !\is_array($data['raw'])) { throw new \InvalidArgumentException('OAuth token data "raw" must be an object.'); } $raw = \is_array($data['raw'] ?? NULL) ? $data['raw'] : []; if ($raw !== [] && \array_is_list($raw)) { throw new \InvalidArgumentException('OAuth token data "raw" must be an object.'); } unset($raw['access_token'], $raw['refresh_token']); /** @var array $raw */ return new self( accessToken: $data['accessToken'], refreshToken: $refreshToken, tokenType: $tokenType, scope: $scope, expiresAt: $expiresAt, raw: $raw, ); } /** * Returns the token set for encrypted persistence. * * @return array */ public function toArray(): array { return [ 'accessToken' => $this->accessToken, 'refreshToken' => $this->refreshToken, 'tokenType' => $this->tokenType, 'scope' => $this->scope, 'expiresAt' => $this->expiresAt, 'raw' => $this->raw, ]; } /** * @internal * * @return array */ public function __debugInfo(): array { return [ 'accessToken' => '[redacted]', 'refreshToken' => $this->refreshToken === NULL ? NULL : '[redacted]', 'tokenType' => $this->tokenType, 'scope' => $this->scope, 'expiresAt' => $this->expiresAt, 'raw' => $this->raw, ]; } /** @param array $values */ private static function optionalString(array $values, string $key): ?string { if (!\array_key_exists($key, $values) || $values[$key] === NULL) { return NULL; } if (!\is_string($values[$key]) || $values[$key] === '') { throw new \InvalidArgumentException(\sprintf('OAuth token data "%s" must be a non-empty string or null.', $key)); } return $values[$key]; } /** @param array $values */ private static function optionalInteger(array $values, string $key): ?int { if (!\array_key_exists($key, $values) || $values[$key] === NULL) { return NULL; } if (!\is_int($values[$key])) { throw new \InvalidArgumentException(\sprintf('OAuth token data "%s" must be an integer or null.', $key)); } return $values[$key]; } }