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 AccountExistsBases: Exception
The account is already created.
SourceAccounts
class Accounts(db: Database, clock: Callable[[], float] = time.time)The account, its login and its sessions, in the database.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
db | Database | ||
clock | Callable[[], float] | time.time |
check
def check(username: str, password: str) -> bool:Check a login.
Parameters
| Name | Type | Description |
|---|---|---|
username | str | |
password | str |
Raises
TooManyAttemptsAfter MAX_FAILURES failures, until WAIT_SECONDS have passed since the last one.
close_session
def close_session(token: Optional[str]) -> None:Parameters
| Name | Type | Description |
|---|---|---|
token | Optional[str] |
create
def create(username: str, password: str) -> None:Create the account.
Parameters
| Name | Type | Description |
|---|---|---|
username | str | |
password | str |
Raises
InvalidAccountAn empty user name or a too short password.AccountExistsThe account is already created.
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.
exists
def exists() -> bool:open_session
def open_session() -> str:Open a session of SESSION_DAYS days and return its token.
Sourcereset_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.
session_valid
def session_valid(token: Optional[str]) -> bool:Whether a token is the one of an open, unexpired session.
Parameters
| Name | Type | Description |
|---|---|---|
token | Optional[str] |
username
def username() -> Optional[str]:The user name of the account, or None before it is created.
SourceInvalidAccount
class InvalidAccountBases: ValueError
An empty user name, or a password shorter than MIN_PASSWORD_LENGTH.
SourceTooManyAttempts
class TooManyAttempts(retry_after: int)Bases: Exception
Too many failed logins: wait retry_after seconds.
Parameters
| Name | Type | Description |
|---|---|---|
retry_after | int |
Attributes
| Name |
|---|
retry_after |
Functions
auth_disabled
def auth_disabled() -> bool:Whether SOK_AUTH=none turns the login off (behind a reverse proxy).
SourceAttributes
| Name | Value |
|---|---|
COOKIE_NAME | 'sok_session' |
MAX_FAILURES | 5 |
MIN_PASSWORD_LENGTH | 8 |
SESSION_DAYS | 30 |
WAIT_SECONDS | 30 |
logger | logging.getLogger(__name__) |