API / Minn\Auth / Sessions

Sessions final readonly class

Minn\Auth\Sessions · public/minn/src/Minn/Auth/Sessions.php · 173 lines

The session_tokens usermeta store: {sha256(token): {expiration, ip, ua, login}}. Read by a bounded scan of the serialized blob and written by serializing it ourselves, so stored data is never executed.

Constants and properties

privatereadonly Minn\Content\Users $users

Promoted from the constructor.

Public

new__construct(Minn\Content\Users $users)#
staticgenerateToken(): string#

A 43-character token; only sha256(token) is ever stored or compared.

methodisLive(int $userId, string $token): bool#

Whether a session token is still stored and unexpired for a user.

methodcreate(int $userId, int $expiration, string $ip, string $userAgent): string#

Creates a session, pruning expired ones, and returns the raw token.

staticplain(string $value): string#

A request-supplied field (the address, the user agent) as the store keeps it: the store is read back by shape, so it carries nothing that looks like the shape, and no more than 255 characters.

methoddestroyAll(int $userId): void#

Ends every session of the user (a password change).

methoddestroy(int $userId, string $token): bool#

Removes one session; true when it existed.

methoddestroyKey(int $userId, string $key): bool#

Removes the session stored under a key (the sha256 of its token); true when it existed.

methoddestroyOthers(int $userId, string $keepKey): void#

Ends every session of the user except the one stored under the given key.

methodread(int $userId): array#

Every stored session of a user, keyed by token hash. Each entry keeps its stored text under raw, so writing the store back leaves sessions this request did not make exactly as they were, including anything a plugin attached to them.

@return array<string, array{expiration: int, ip: string, ua: string, login: int, raw: string}>
staticserialize(array $sessions): string#

The map in PHP's serialized form: stored entries as they were read, new ones key by key in the order given.

Internals 5

Private and protected methods: how the class does its work. Not a contract; they change whenever the code is tidied.

privateparseEntry(string $inner): array#
privateprune(array $sessions): array#
privatewrite(int $userId, array $sessions): void#
privateentry(array $entry): string#

One new session: integers for the times, strings for the rest. @param array<string, int|string> $entry

$entryarray<string, int|string>
privatestring(string $value): string#