Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Auth;
/** * Session tokens returned by com.atproto.server session endpoints. * * @api */final readonly class AtprotoSession {
/** * @param array<string, mixed> $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<string, mixed> $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<string, mixed> $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<string, mixed> $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<string, mixed> $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<string, mixed> */ 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<string, mixed> */ 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<string, mixed> $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<string, mixed> $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; }
}