Karta.Accounts (Karta v1.0.0)

Organisations, users, memberships and the sessions of signed-in users.

Sessions. create_session/2 returns a random token for the session cookie and stores only its SHA-256 (Karta.Accounts.UserSession). A session lasts session_hours (default 12; config :karta, Karta.Accounts, session_hours:) from sign-in and is not extended: after that the user signs in again (with the identity provider that is usually a redirect and back). Signing out deletes the session; disabling a user deletes all of theirs.

What a user may do is a Karta.Accounts.Scope: scope/1.

Members are managed by the admin pages with the acting user's scope: add_member/4, change_role/3 and remove_member/2 check the scope (:manage_members, and :manage_owners whenever an owner is made or unmade), refuse changes to an organisation whose memberships the identity provider manages (provider_managed?/1), and never leave an organisation that has an owner without one.

Signing in through the identity provider (OpenID Connect, Karta.OIDC): oidc_sign_in/2 turns the provider's claims into a user.

Local login (off by default; KartaWeb.LocalLoginController): a password (Karta.Accounts.Password, Argon2id) and then a TOTP code (Karta.Accounts.TOTP). authenticate_password/3 gives the same answer, after the same work, for an unknown address, a wrong password, a user without a password and a disabled user. Attempts are limited per address given (10 per 15 minutes, whether or not the account exists) and per client (30 per 15 minutes; an IPv4 address or an IPv6 /64); TOTP codes per user (5 per 5 minutes). Accounts are set up from the command line: reset_local_login/2 (Karta.Release.local_user/2). Signed-in local users change their password (change_password/2) and move to a new authenticator app (replace_totp/4) after giving their current password (10 tries per 15 minutes per user).

Summary

Functions

Adds the user with this address to the organisation with role (an atom or its name). The user must exist: users are created when they first sign in. Errors: :forbidden, :managed (provider_managed?/1), :invalid_role, :no_user, :already_member.

The roles the scope may give in the organisation (owners first).

Checks an address and a password from the sign-in form: {:ok, user}, {:error, :invalid} (the same for every reason), or {:error, {:rate_limited, seconds}}.

Changes a signed-in local user's password: attrs has the current_password, the new password and its password_confirmation. {:error, changeset} for a new password that is too short, too long or not confirmed; {:error, :invalid} for a wrong current password; {:error, :no_password} for a user who signs in through the provider. Other sessions are not ended here (delete_other_sessions/2).

Changes a member's role. Errors as for add_member/4, and :last_owner (the organisation's only owner keeps the role) and :not_found.

Starts a session for user, signed in by method (:oidc or :password); returns the token for the session cookie. Sessions that have expired (anyone's) are deleted at the same time.

True when session is the one of token.

Ends all of the user's sessions except the one of token (signing out elsewhere); returns the hashes of the tokens of the sessions ended.

Ends the session of a token (signing out). Unknown tokens are ignored.

Ends all sessions of a user; returns how many there were.

Disables a user: they cannot sign in again until enabled, and all their sessions end at once.

Stores a user's first TOTP secret, once they have shown a valid code for it ({:ok, user}); a user who has one already must have it reset (reset_local_login/2) instead.

The organisation with this id or slug, or nil (for the command line).

A membership of the organisation, with its user, or nil.

The organisation with this id, when the scope can see it; otherwise nil.

A user by e-mail address (without regard to case), or nil.

Links the user with this address to the identity provider's subject (sub), so that they sign in through the provider from now on. For a user Karta already had (a local account, or one made by make_admin/1) when linking by address is off; the subject is in the log line of the refused sign-in. {:error, :not_found} when there is no such user, {:error, :subject_taken} when another user has the subject.

The organisation's memberships with their users: owners first, then by address.

The organisations the scope can see, by name: all of them for an administrator, from the same scoped query as organisations_page/2.

The user's sessions that have not ended, newest first.

Makes the user with this address an instance administrator, creating the user when there is none. For the first administrator of an instance whose identity provider has no administrators' group: the user signs in through the provider once (which creates them), then this makes them an administrator. A user created here has no provider subject yet; link it with link_oidc/2 (or let the provider link it by address, see oidc_sign_in/2).

The user for the claims of a verified ID token (and the provider's userinfo): {:ok, user} or {:error, reason}.

A page of the organisations the scope can see, by name (all of them for an administrator). Options: after, before (an organisation id) and limit, as for Karta.Pager.page/2.

True when the identity provider manages the organisation's memberships: its slug is named in the provider's group mapping (group_roles, KARTA_OIDC_GROUP_ROLES). Their members are changed in the provider.

Removes a member. Errors as for change_role/3.

Renames an organisation; its slug stays the same.

Moves a signed-in local user to a new authenticator app: their current password, and a valid code of the new secret (TOTP.new_secret/0). Errors: :invalid (a wrong password or code), :no_password, {:rate_limited, seconds}.

Sets up local login for the user with this address, creating the user when there is none: a new random password (returned, to be handed to the user once), the TOTP secret removed (the user enrols again at the next sign-in), and all sessions ended. Option admin: true makes the user an instance administrator.

What user may do: their role in each of their organisations.

How long a session lasts, in hours.

The user of a session token and when the session ends, or :error for an unknown or expired token or a disabled user.

Checks a TOTP code of a user who gave the right password: {:ok, user} (the code's step is recorded, so it cannot be used again), {:error, :invalid} or {:error, {:rate_limited, seconds}}. A user without a TOTP secret must enrol first (enrol_totp/3).

Functions

add_member(organisation, user, role)

add_member(scope, organisation, email, role)

@spec add_member(
  Karta.Accounts.Scope.t(),
  Karta.Accounts.Organisation.t(),
  String.t(),
  term()
) ::
  {:ok, Karta.Accounts.Membership.t()} | {:error, atom()}

Adds the user with this address to the organisation with role (an atom or its name). The user must exist: users are created when they first sign in. Errors: :forbidden, :managed (provider_managed?/1), :invalid_role, :no_user, :already_member.

assignable_roles(scope, organisation)

The roles the scope may give in the organisation (owners first).

authenticate_password(email, password, client_ip)

@spec authenticate_password(String.t(), String.t(), :inet.ip_address()) ::
  {:ok, Karta.Accounts.User.t()}
  | {:error, :invalid | {:rate_limited, pos_integer()}}

Checks an address and a password from the sign-in form: {:ok, user}, {:error, :invalid} (the same for every reason), or {:error, {:rate_limited, seconds}}.

change_password(user, attrs)

@spec change_password(Karta.Accounts.User.t(), map()) ::
  {:ok, Karta.Accounts.User.t()}
  | {:error,
     Ecto.Changeset.t()
     | :invalid
     | :no_password
     | {:rate_limited, pos_integer()}}

Changes a signed-in local user's password: attrs has the current_password, the new password and its password_confirmation. {:error, changeset} for a new password that is too short, too long or not confirmed; {:error, :invalid} for a wrong current password; {:error, :no_password} for a user who signs in through the provider. Other sessions are not ended here (delete_other_sessions/2).

change_role(scope, membership, role)

@spec change_role(Karta.Accounts.Scope.t(), Karta.Accounts.Membership.t(), term()) ::
  {:ok, Karta.Accounts.Membership.t()} | {:error, atom()}

Changes a member's role. Errors as for add_member/4, and :last_owner (the organisation's only owner keeps the role) and :not_found.

create_organisation(attrs)

create_session(user, method)

@spec create_session(Karta.Accounts.User.t(), :oidc | :password) :: binary()

Starts a session for user, signed in by method (:oidc or :password); returns the token for the session cookie. Sessions that have expired (anyone's) are deleted at the same time.

create_user(attrs)

current_session?(user_session, token)

@spec current_session?(Karta.Accounts.UserSession.t(), term()) :: boolean()

True when session is the one of token.

delete_other_sessions(user, token)

@spec delete_other_sessions(Karta.Accounts.User.t(), binary()) :: [binary()]

Ends all of the user's sessions except the one of token (signing out elsewhere); returns the hashes of the tokens of the sessions ended.

delete_session(token)

@spec delete_session(term()) :: :ok

Ends the session of a token (signing out). Unknown tokens are ignored.

delete_user_sessions(user)

@spec delete_user_sessions(Karta.Accounts.User.t()) :: non_neg_integer()

Ends all sessions of a user; returns how many there were.

disable_user(user)

@spec disable_user(Karta.Accounts.User.t()) :: {:ok, Karta.Accounts.User.t()}

Disables a user: they cannot sign in again until enabled, and all their sessions end at once.

enrol_totp(user, secret, code)

@spec enrol_totp(Karta.Accounts.User.t(), binary(), String.t()) ::
  {:ok, Karta.Accounts.User.t()}
  | {:error, :invalid | {:rate_limited, pos_integer()}}

Stores a user's first TOTP secret, once they have shown a valid code for it ({:ok, user}); a user who has one already must have it reset (reset_local_login/2) instead.

find_organisation(ref)

@spec find_organisation(String.t()) :: Karta.Accounts.Organisation.t() | nil

The organisation with this id or slug, or nil (for the command line).

get_membership(organisation, id)

@spec get_membership(Karta.Accounts.Organisation.t(), term()) ::
  Karta.Accounts.Membership.t() | nil

A membership of the organisation, with its user, or nil.

get_organisation(scope, id)

@spec get_organisation(Karta.Accounts.Scope.t(), term()) ::
  Karta.Accounts.Organisation.t() | nil

The organisation with this id, when the scope can see it; otherwise nil.

get_user_by_email(email)

@spec get_user_by_email(String.t()) :: Karta.Accounts.User.t() | nil

A user by e-mail address (without regard to case), or nil.

list_members(organisation)

The organisation's memberships with their users: owners first, then by address.

list_organisations(scope)

@spec list_organisations(Karta.Accounts.Scope.t()) :: [
  Karta.Accounts.Organisation.t()
]

The organisations the scope can see, by name: all of them for an administrator, from the same scoped query as organisations_page/2.

list_sessions(user)

@spec list_sessions(Karta.Accounts.User.t()) :: [Karta.Accounts.UserSession.t()]

The user's sessions that have not ended, newest first.

make_admin(email)

@spec make_admin(String.t()) ::
  {:ok, Karta.Accounts.User.t()} | {:error, Ecto.Changeset.t()}

Makes the user with this address an instance administrator, creating the user when there is none. For the first administrator of an instance whose identity provider has no administrators' group: the user signs in through the provider once (which creates them), then this makes them an administrator. A user created here has no provider subject yet; link it with link_oidc/2 (or let the provider link it by address, see oidc_sign_in/2).

oidc_sign_in(claims, opts \\ [])

@spec oidc_sign_in(map(), keyword()) ::
  {:ok, Karta.Accounts.User.t()} | {:error, atom()}

The user for the claims of a verified ID token (and the provider's userinfo): {:ok, user} or {:error, reason}.

  • sub and email are required, and email_verified must be true (:no_subject, :no_email, :email_not_verified).
  • The user is found by sub; their address and name follow the provider's (:email_conflict when another user has the new address). A user without a sub yet who has the same address is not taken over: :link_required (and the subject is logged, for link_oidc/2), because providers let users change their own address. Only with link_by_email: true is such a user linked to the sub at sign-in. A user linked to another sub is never linked (:subject_conflict). Anyone else is created, unless auto_create: false (:not_registered). A disabled user is refused (:disabled).
  • Groups (the claim named by groups_claim, default "groups"): with admin_groups set, the user is an instance administrator exactly when they are in one of them. group_roles ([{group, organisation slug, role}]) decides the memberships of the organisations it names: each such membership is set to the highest role the user's groups give, or removed when none does. Memberships of other organisations are left as they are. An organisation that does not exist is skipped (and logged).

organisations_page(scope, opts \\ [])

@spec organisations_page(Karta.Accounts.Scope.t(), keyword()) :: Karta.Pager.Page.t()

A page of the organisations the scope can see, by name (all of them for an administrator). Options: after, before (an organisation id) and limit, as for Karta.Pager.page/2.

provider_managed?(organisation)

@spec provider_managed?(Karta.Accounts.Organisation.t()) :: boolean()

True when the identity provider manages the organisation's memberships: its slug is named in the provider's group mapping (group_roles, KARTA_OIDC_GROUP_ROLES). Their members are changed in the provider.

remove_member(scope, membership)

@spec remove_member(Karta.Accounts.Scope.t(), Karta.Accounts.Membership.t()) ::
  {:ok, Karta.Accounts.Membership.t()} | {:error, atom()}

Removes a member. Errors as for change_role/3.

rename_organisation(organisation, attrs)

@spec rename_organisation(Karta.Accounts.Organisation.t(), map()) ::
  {:ok, Karta.Accounts.Organisation.t()} | {:error, Ecto.Changeset.t()}

Renames an organisation; its slug stays the same.

replace_totp(user, password, secret, code)

@spec replace_totp(Karta.Accounts.User.t(), String.t(), binary(), String.t()) ::
  {:ok, Karta.Accounts.User.t()}
  | {:error, :invalid | :no_password | {:rate_limited, pos_integer()}}

Moves a signed-in local user to a new authenticator app: their current password, and a valid code of the new secret (TOTP.new_secret/0). Errors: :invalid (a wrong password or code), :no_password, {:rate_limited, seconds}.

reset_local_login(email, opts \\ [])

@spec reset_local_login(String.t(), keyword()) ::
  {:ok, Karta.Accounts.User.t(), String.t()} | {:error, Ecto.Changeset.t()}

Sets up local login for the user with this address, creating the user when there is none: a new random password (returned, to be handed to the user once), the TOTP secret removed (the user enrols again at the next sign-in), and all sessions ended. Option admin: true makes the user an instance administrator.

scope(user)

What user may do: their role in each of their organisations.

session_hours()

@spec session_hours() :: pos_integer()

How long a session lasts, in hours.

session_user(token)

@spec session_user(term()) :: {:ok, Karta.Accounts.User.t(), DateTime.t()} | :error

The user of a session token and when the session ends, or :error for an unknown or expired token or a disabled user.

update_organisation(organisation, attrs)

verify_totp(user, code)

@spec verify_totp(Karta.Accounts.User.t(), String.t()) ::
  {:ok, Karta.Accounts.User.t()}
  | {:error, :invalid | {:rate_limited, pos_integer()}}

Checks a TOTP code of a user who gave the right password: {:ok, user} (the code's step is recorded, so it cannot be used again), {:error, :invalid} or {:error, {:rate_limited, seconds}}. A user without a TOTP secret must enrol first (enrol_totp/3).