$raw */ public function __construct( public string $did, public string $handle, #[\SensitiveParameter] public string $accessJwt, #[\SensitiveParameter] public string $refreshJwt, public ?string $pdsUrl = NULL, public ?string $email = NULL, public ?bool $emailConfirmed = NULL, public ?bool $active = NULL, public ?string $status = NULL, #[\SensitiveParameter] public array $raw = [], ) {} /** * Creates a session from a create-session response. * * @param array $response */ public static function fromCreateSessionResponse( #[\SensitiveParameter] array $response, ?string $pdsUrl = NULL, ): self { return self::fromResponse($response, $pdsUrl); } /** * Creates a session from a refresh-session response. * * @param array $response */ public static function fromRefreshSessionResponse( #[\SensitiveParameter] array $response, ?string $pdsUrl = NULL, ): self { return self::fromResponse($response, $pdsUrl); } /** * Restores a session from its persisted representation. * * @param array $data */ public static function fromArray(#[\SensitiveParameter] array $data): self { $did = self::requiredString($data, 'did', 'data'); $handle = self::requiredString($data, 'handle', 'data'); $accessJwt = self::requiredString($data, 'accessJwt', 'data'); $refreshJwt = self::requiredString($data, 'refreshJwt', 'data'); $raw = \is_array($data['raw'] ?? NULL) ? $data['raw'] : []; unset($raw['accessJwt'], $raw['refreshJwt']); /** @var array $raw */ return new self( did: $did, handle: $handle, accessJwt: $accessJwt, refreshJwt: $refreshJwt, pdsUrl: \is_string($data['pdsUrl'] ?? NULL) ? $data['pdsUrl'] : NULL, email: \is_string($data['email'] ?? NULL) ? $data['email'] : NULL, emailConfirmed: \is_bool($data['emailConfirmed'] ?? NULL) ? $data['emailConfirmed'] : NULL, active: \is_bool($data['active'] ?? NULL) ? $data['active'] : NULL, status: \is_string($data['status'] ?? NULL) ? $data['status'] : NULL, raw: $raw, ); } /** * Returns the session for encrypted persistence. * * @return array */ public function toArray(): array { return [ 'did' => $this->did, 'handle' => $this->handle, 'accessJwt' => $this->accessJwt, 'refreshJwt' => $this->refreshJwt, 'pdsUrl' => $this->pdsUrl, 'email' => $this->email, 'emailConfirmed' => $this->emailConfirmed, 'active' => $this->active, 'status' => $this->status, 'raw' => $this->raw, ]; } /** * @internal * * @return array */ public function __debugInfo(): array { return [ 'did' => $this->did, 'handle' => $this->handle, 'accessJwt' => '[redacted]', 'refreshJwt' => '[redacted]', 'pdsUrl' => $this->pdsUrl, 'email' => $this->email, 'emailConfirmed' => $this->emailConfirmed, 'active' => $this->active, 'status' => $this->status, 'raw' => $this->raw === [] ? [] : '[redacted]', ]; } /** * @param array $response */ private static function fromResponse(#[\SensitiveParameter] array $response, ?string $pdsUrl): self { $did = self::requiredString($response, 'did', 'response'); $handle = self::requiredString($response, 'handle', 'response'); $accessJwt = self::requiredString($response, 'accessJwt', 'response'); $refreshJwt = self::requiredString($response, 'refreshJwt', 'response'); $raw = $response; unset($raw['accessJwt'], $raw['refreshJwt']); return new self( did: $did, handle: $handle, accessJwt: $accessJwt, refreshJwt: $refreshJwt, pdsUrl: $pdsUrl, email: \is_string($response['email'] ?? NULL) ? $response['email'] : NULL, emailConfirmed: \is_bool($response['emailConfirmed'] ?? NULL) ? $response['emailConfirmed'] : NULL, active: \is_bool($response['active'] ?? NULL) ? $response['active'] : NULL, status: \is_string($response['status'] ?? NULL) ? $response['status'] : NULL, raw: $raw, ); } /** * @param array $values */ private static function requiredString(array $values, string $key, string $source): string { $value = $values[$key] ?? NULL; if (!\is_string($value) || $value === '') { throw new \InvalidArgumentException(\sprintf('AT Protocol session %s is missing "%s".', $source, $key)); } return $value; } }