izartu API

Auth
in package

Session-based authentication: login, logout, session state, route guarding and CSRF tokens. All methods are static; `Auth::start()` must run before any output.

Table of Contents

Methods

attempt()  : bool
Verify an email/password pair and, on success, bring the user's hash to PHP's default and log the user in.
canManage()  : bool
Whether the logged-in user may manage (edit/delete) a bookmark.
check()  : bool
Whether a user is currently logged in.
csrfCheck()  : bool
Validate a submitted CSRF token against the session token.
csrfToken()  : string
The per-session CSRF token, created on first use.
endSessions()  : int
End every open session of a user by deleting their files; the next request of each finds an empty session.
guard()  : void
Require an authenticated user; redirect anonymous requests to `/login`.
id()  : int|null
The logged-in user's id, or null if the request is anonymous.
logout()  : void
Log the current user out: clear and destroy the session and its cookie.
start()  : void
Start the session with hardened cookie parameters. Safe to call more than once. Must run before any output is sent.
user()  : array{id: int, username: string, role: string}|null
The logged-in user's session data, or null if the request is anonymous.
dummyHash()  : string
A well-formed bcrypt hash for an unknown user, at the cost `password_hash()` uses on this PHP, so checking it takes as long as checking a real user's hash.
login()  : void
Log a user in: move the session to a new id that starts with the user's id, `<id>-<random>`, and store the user's id, username and role.
sessionFiles()  : array{string, string}
Where PHP's files session handler keeps a user's sessions.
verify()  : bool
Whether a password matches a user's hash.

Methods

attempt()

Verify an email/password pair and, on success, bring the user's hash to PHP's default and log the user in.

public static attempt(string $email, string $password) : bool
Parameters
$email : string

Email typed at login (normalised to lower-case here).

$password : string

Plain password typed at login.

Return values
bool —

True if the credentials are valid, and still are once the user is logged in.

canManage()

Whether the logged-in user may manage (edit/delete) a bookmark.

public static canManage(Bookmark $bookmark) : bool

They must see it and either own it or hold the owner/admin role, so nobody but its owner manages a private bookmark; false for anonymous requests.

Parameters
$bookmark : Bookmark

The bookmark to manage.

Return values
bool

check()

Whether a user is currently logged in.

public static check() : bool
Return values
bool

csrfCheck()

Validate a submitted CSRF token against the session token.

public static csrfCheck(string|null $token) : bool
Parameters
$token : string|null

Token from the request (e.g. $_POST['csrf']).

Return values
bool —

True if it matches the current session token.

csrfToken()

The per-session CSRF token, created on first use.

public static csrfToken() : string
Return values
string —

A 64-character hex token.

endSessions()

End every open session of a user by deleting their files; the next request of each finds an empty session.

public static endSessions(int $user) : int

The sessions are found by their id's prefix (see login()). It works with PHP's files session handler only, run with the web server's session.save_path by a user who may read and delete those files.

Parameters
$user : int

The user's id.

Tags
throws
RuntimeException

When the session directory cannot be read or a session file cannot be deleted.

Return values
int —

How many sessions were ended.

guard()

Require an authenticated user; redirect anonymous requests to `/login`.

public static guard() : void
Tags
throws
Redirect

For an anonymous request.

id()

The logged-in user's id, or null if the request is anonymous.

public static id() : int|null
Return values
int|null

logout()

Log the current user out: clear and destroy the session and its cookie.

public static logout() : void

start()

Start the session with hardened cookie parameters. Safe to call more than once. Must run before any output is sent.

public static start() : void

user()

The logged-in user's session data, or null if the request is anonymous.

public static user() : array{id: int, username: string, role: string}|null
Return values
array{id: int, username: string, role: string}|null

dummyHash()

A well-formed bcrypt hash for an unknown user, at the cost `password_hash()` uses on this PHP, so checking it takes as long as checking a real user's hash.

protected static dummyHash() : string
Return values
string —

A bcrypt hash at PASSWORD_BCRYPT_DEFAULT_COST.

login()

Log a user in: move the session to a new id that starts with the user's id, `<id>-<random>`, and store the user's id, username and role.

protected static login(array{id: int, username: string, email: string, hash: string, role: string} $user) : void

The new id prevents session fixation, and its prefix lets endSessions() find the user's sessions. The data moves to the new id, and the old session is deleted.

Parameters
$user : array{id: int, username: string, email: string, hash: string, role: string}

The user row.

Tags
throws
RuntimeException

When PHP cannot create a session id.

sessionFiles()

Where PHP's files session handler keeps a user's sessions.

protected static sessionFiles(int $user, string $savePath) : array{string, string}

session.save_path is a directory, empty for the system's temporary one, or N;[MODE;]directory, which spreads the files over N levels of subdirectories named after the first characters of the session id.

Parameters
$user : int

The user's id.

$savePath : string

The value of session.save_path.

Return values
array{string, string} —

The session directory, and a glob() pattern for the user's session files.

verify()

Whether a password matches a user's hash.

protected static verify(array{id: int, username: string, email: string, hash: string, role: string}|null $user, string $password) : bool

An unknown user is checked against a dummy hash, so the response time does not reveal whether an account exists.

Parameters
$user : array{id: int, username: string, email: string, hash: string, role: string}|null

The user row, or null if no user has the email typed.

$password : string

Plain password typed at login.

Tags
phpstan-assert-if-true

!null $user

Return values
bool —

True only for an existing user whose hash the password matches.

On this page

Search results