Skip to content

sok.server.auth

The account of S.O.K Server and its sessions.

One account, created on the first visit or from SOK_USERNAME and SOK_PASSWORD. The password is hashed with Argon2. A login opens a session of 30 days, kept in a cookie; the database only holds the SHA-256 of its token. After 5 failed logins, the next attempts wait 30 seconds.

AccountExists

class AccountExists

Bases: Exception

The account is already created.

Source

Accounts

class Accounts(db: Database, clock: Callable[[], float] = time.time)

The account, its login and its sessions, in the database.

Parameters

NameTypeDefaultDescription
dbDatabase
clockCallable[[], float]time.time
Source

check

def check(username: str, password: str) -> bool:

Check a login.

Parameters

NameTypeDescription
usernamestr
passwordstr

Raises

  • TooManyAttemptsAfter MAX_FAILURES failures, until WAIT_SECONDS have passed since the last one.
Source

close_session

def close_session(token: Optional[str]) -> None:

Parameters

NameTypeDescription
tokenOptional[str]
Source

create

def create(username: str, password: str) -> None:

Create the account.

Parameters

NameTypeDescription
usernamestr
passwordstr

Raises

Source

create_from_env

def create_from_env() -> bool:

Create the account from SOK_USERNAME and SOK_PASSWORD, if none exists.

Returns

  • boolTrue when the account was created.
Source

exists

def exists() -> bool:
Source

open_session

def open_session() -> str:

Open a session of SESSION_DAYS days and return its token.

Source

reset_from_env

def reset_from_env() -> bool:

Give the account the password of SOK_PASSWORD, with SOK_RESET_PASSWORD=1: the way back from a forgotten password.

SOK_USERNAME, when set, renames the account too. The open sessions are closed. Nothing else changes: the settings, the history and the analyses stay.

Returns

  • boolTrue when the password was changed.
Source

session_valid

def session_valid(token: Optional[str]) -> bool:

Whether a token is the one of an open, unexpired session.

Parameters

NameTypeDescription
tokenOptional[str]
Source

username

def username() -> Optional[str]:

The user name of the account, or None before it is created.

Source

InvalidAccount

class InvalidAccount

Bases: ValueError

An empty user name, or a password shorter than MIN_PASSWORD_LENGTH.

Source

TooManyAttempts

class TooManyAttempts(retry_after: int)

Bases: Exception

Too many failed logins: wait retry_after seconds.

Parameters

NameTypeDescription
retry_afterint
Source

Attributes

Name
retry_after

Functions

auth_disabled

def auth_disabled() -> bool:

Whether SOK_AUTH=none turns the login off (behind a reverse proxy).

Source

Attributes

NameValue
MAX_FAILURES5
MIN_PASSWORD_LENGTH8
SESSION_DAYS30
WAIT_SECONDS30
loggerlogging.getLogger(__name__)
Built with Kiln