Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Auth\OAuth;
/** * OAuth token response data. * * @api */final readonly class TokenSet {
/** * @param array<string, mixed> $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<string, mixed> $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<string, mixed> $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<string, mixed> $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<string, mixed> */ 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<string, mixed> */ 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<string, mixed> $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<string, mixed> $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]; }
}