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
@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.
@spec assignable_roles(Karta.Accounts.Scope.t(), Karta.Accounts.Organisation.t()) :: [ Karta.Accounts.Membership.role() ]
The roles the scope may give in the organisation (owners first).
@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}}.
@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).
@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.
@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.
@spec current_session?(Karta.Accounts.UserSession.t(), term()) :: boolean()
True when session is the one of 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.
@spec delete_session(term()) :: :ok
Ends the session of a token (signing out). Unknown tokens are ignored.
@spec delete_user_sessions(Karta.Accounts.User.t()) :: non_neg_integer()
Ends all sessions of a user; returns how many there were.
@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.
@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.
@spec find_organisation(String.t()) :: Karta.Accounts.Organisation.t() | nil
The organisation with this id or slug, or nil (for the command line).
@spec get_membership(Karta.Accounts.Organisation.t(), term()) :: Karta.Accounts.Membership.t() | nil
A membership of the organisation, with its user, or nil.
@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.
@spec get_user_by_email(String.t()) :: Karta.Accounts.User.t() | nil
A user by e-mail address (without regard to case), or nil.
@spec link_oidc(String.t(), String.t()) :: {:ok, Karta.Accounts.User.t()} | {:error, :not_found | :subject_taken}
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.
@spec list_members(Karta.Accounts.Organisation.t()) :: [Karta.Accounts.Membership.t()]
The organisation's memberships with their users: owners first, then by address.
@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.
@spec list_sessions(Karta.Accounts.User.t()) :: [Karta.Accounts.UserSession.t()]
The user's sessions that have not ended, newest first.
@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).
@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}.
subandemailare required, andemail_verifiedmust betrue(:no_subject,:no_email,:email_not_verified).- The user is found by
sub; their address and name follow the provider's (:email_conflictwhen another user has the new address). A user without asubyet who has the same address is not taken over::link_required(and the subject is logged, forlink_oidc/2), because providers let users change their own address. Only withlink_by_email: trueis such a user linked to thesubat sign-in. A user linked to anothersubis never linked (:subject_conflict). Anyone else is created, unlessauto_create: false(:not_registered). A disabled user is refused (:disabled). - Groups (the claim named by
groups_claim, default"groups"): withadmin_groupsset, 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).
@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.
@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.
@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.
@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.
@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}.
@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.
@spec scope(Karta.Accounts.User.t()) :: Karta.Accounts.Scope.t()
What user may do: their role in each of their organisations.
@spec session_hours() :: pos_integer()
How long a session lasts, in hours.
@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.
@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).