SaltDns.Accounts (Salt v1.0.0)

The Accounts context.

Summary

Functions

Adds a user to an organisation with a role (no check of who asks: the system, fixtures).

Adds a user to an organisation with a role, as the scope's user: an owner or admin of the organisation, and only an owner may add someone as owner. {:error, :forbidden} otherwise.

Whether the scope's user may create a new organisation. On an installation that is not invitation-only, everyone may. With invited, only a user who is an owner of an organisation already: someone invited as admin, editor or viewer, or a user with no membership, may not. Without this, anyone the provider lets through (and with invited that means anyone an owner or admin invited, at any role) could start their own organisation.

Returns an organisation changeset for forms.

Returns an %Ecto.Changeset{} for changing the user email.

Returns an %Ecto.Changeset{} for changing the user password.

Creates an organisation and makes the scope's user its owner, in one transaction. {:error, :forbidden} when can_create_organisation?/1 says no.

Ends every session of the user: deletes all their session tokens (password and OpenID Connect) and disconnects their open pages. Returns how many tokens were removed.

Ends the sessions of one provider session: deletes the user's "oidc_session" tokens whose oidc_sid is sid, and those with no oidc_sid (made before Salt kept it; they cannot be told apart, and nothing may survive that should not), and disconnects the open pages of exactly the deleted tokens. Password sessions and the user's OpenID Connect sessions with another sid stay. Returns how many tokens were removed.

Deletes the session token (password or OpenID Connect).

Delivers the magic link login instructions to the given user.

Delivers the update email instructions to the given user.

A map of user id to email, for the ids given (for showing who did something).

Generates a session token. With method: :oidc the token belongs to a sign-in through the OpenID Connect provider and lives for SALT_DNS_OIDC_SESSION_HOURS instead of 14 days.

The membership of user in organisation, or nil.

Gets an organisation the scope's user belongs to. Raises Ecto.NoResultsError otherwise.

Gets a single user.

Gets a user by email.

Gets a user by email and password.

Gets the user with the given magic link token.

The user the identity provider knows as sub, or nil.

Gets the user with the given signed token.

Whether the installation only lets invited people in (SALT_DNS_OIDC_AUTO_CREATE=invited).

The members of an organisation with their users, owners first.

The organisations the scope's user belongs to, by name.

Logs the user in by magic link.

Whether this installation can send e-mail (config :salt_dns, :mail_delivery, true unless a release says otherwise). A production release has no mailer configured, so it sets this false in config/runtime.exs; everything that would need a mail (the e-mailed sign-in link, the address-change link, the confirmation of a new account) is then hidden or closed with a plain explanation, instead of promising a message that is dropped.

Whether the session token came from a sign-in through the OpenID Connect provider.

Finds or creates the user for the claims of a verified OpenID Connect sign-in (SaltDns.OIDC.callback/3).

True unless config :salt_dns, :password_login, false. When false, e-mail and password login, magic-link login and self-registration are all off and only the OpenID Connect provider signs users in (oidc_sign_in/1).

Registers a user.

Whether anyone may create an account on the registration page: password login is on, mail can be sent (the new account is confirmed with an e-mailed link, mail_delivery?/0) and config :salt_dns, :registration, true (SALT_DNS_REGISTRATION=true). Off by default: an open registration hands every visitor an organisation, zones, credentials and the use of the resolvers and outbound transfers. Users are then created by an operator (SaltDns.Release.create_user_from_env/0), by OpenID Connect sign-in, or by add_member/3 inviting an existing user. Code that creates a user on purpose (the release task, fixtures) calls register_user/1 directly.

Removes a member. The last owner cannot be removed: {:error, :last_owner}.

Checks whether the user is in sudo mode.

Keeps the language of the console this person chose.

Changes a member's role. The last owner of an organisation cannot be demoted: the result is {:error, :last_owner}.

Changes a member's role as the scope's user (see add_member/4: only an owner may make someone an owner), with the rules of update_member_role/2.

Updates an organisation's name and TTL bounds.

Updates the user email using the given token.

Updates the user password.

Functions

add_member(organisation, user, role)

Adds a user to an organisation with a role (no check of who asks: the system, fixtures).

add_member(scope, organisation, user, role)

Adds a user to an organisation with a role, as the scope's user: an owner or admin of the organisation, and only an owner may add someone as owner. {:error, :forbidden} otherwise.

can_create_organisation?(scope)

@spec can_create_organisation?(SaltDns.Accounts.Scope.t()) :: boolean()

Whether the scope's user may create a new organisation. On an installation that is not invitation-only, everyone may. With invited, only a user who is an owner of an organisation already: someone invited as admin, editor or viewer, or a user with no membership, may not. Without this, anyone the provider lets through (and with invited that means anyone an owner or admin invited, at any role) could start their own organisation.

change_organisation(organisation, attrs \\ %{})

@spec change_organisation(SaltDns.Accounts.Organisation.t(), map()) ::
  Ecto.Changeset.t()

Returns an organisation changeset for forms.

change_user_email(user, attrs \\ %{}, opts \\ [])

Returns an %Ecto.Changeset{} for changing the user email.

See SaltDns.Accounts.User.email_changeset/3 for a list of supported options.

Examples

iex> change_user_email(user)
%Ecto.Changeset{data: %User{}}

change_user_password(user, attrs \\ %{}, opts \\ [])

Returns an %Ecto.Changeset{} for changing the user password.

See SaltDns.Accounts.User.password_changeset/3 for a list of supported options.

Examples

iex> change_user_password(user)
%Ecto.Changeset{data: %User{}}

create_organisation(scope, attrs)

@spec create_organisation(SaltDns.Accounts.Scope.t(), map()) ::
  {:ok, SaltDns.Accounts.Organisation.t()}
  | {:error, Ecto.Changeset.t() | :forbidden}

Creates an organisation and makes the scope's user its owner, in one transaction. {:error, :forbidden} when can_create_organisation?/1 says no.

delete_all_user_session_tokens(user)

@spec delete_all_user_session_tokens(SaltDns.Accounts.User.t()) :: non_neg_integer()

Ends every session of the user: deletes all their session tokens (password and OpenID Connect) and disconnects their open pages. Returns how many tokens were removed.

Pages are disconnected through the application's PubSub, so this reaches them when it runs inside the running application. A release task runs in a separate Erlang VM, where there is no PubSub: it still deletes the tokens, and a page that is already open ends when it is next loaded or reconnects.

delete_oidc_session_tokens_by_sid(user, sid)

@spec delete_oidc_session_tokens_by_sid(SaltDns.Accounts.User.t(), String.t()) ::
  non_neg_integer()

Ends the sessions of one provider session: deletes the user's "oidc_session" tokens whose oidc_sid is sid, and those with no oidc_sid (made before Salt kept it; they cannot be told apart, and nothing may survive that should not), and disconnects the open pages of exactly the deleted tokens. Password sessions and the user's OpenID Connect sessions with another sid stay. Returns how many tokens were removed.

delete_user_session_token(token)

Deletes the session token (password or OpenID Connect).

deliver_login_instructions(user, magic_link_url_fun)

Delivers the magic link login instructions to the given user.

deliver_user_update_email_instructions(user, current_email, update_email_url_fun)

Delivers the update email instructions to the given user.

Examples

iex> deliver_user_update_email_instructions(user, current_email, &url(~p"/users/settings/confirm-email/#{&1}"))
{:ok, %{to: ..., body: ...}}

emails_by_ids(ids)

@spec emails_by_ids([integer()]) :: %{required(integer()) => String.t()}

A map of user id to email, for the ids given (for showing who did something).

generate_user_session_token(user, opts \\ [])

Generates a session token. With method: :oidc the token belongs to a sign-in through the OpenID Connect provider and lives for SALT_DNS_OIDC_SESSION_HOURS instead of 14 days.

get_membership(organisation, user)

The membership of user in organisation, or nil.

get_organisation!(scope, id)

Gets an organisation the scope's user belongs to. Raises Ecto.NoResultsError otherwise.

get_user!(id)

Gets a single user.

Raises Ecto.NoResultsError if the User does not exist.

Examples

iex> get_user!(123)
%User{}

iex> get_user!(456)
** (Ecto.NoResultsError)

get_user_by_email(email)

Gets a user by email.

Examples

iex> get_user_by_email("foo@example.com")
%User{}

iex> get_user_by_email("unknown@example.com")
nil

get_user_by_email_and_password(email, password)

Gets a user by email and password.

Examples

iex> get_user_by_email_and_password("foo@example.com", "correct_password")
%User{}

iex> get_user_by_email_and_password("foo@example.com", "invalid_password")
nil

get_user_by_oidc_sub(sub)

@spec get_user_by_oidc_sub(String.t()) :: SaltDns.Accounts.User.t() | nil

The user the identity provider knows as sub, or nil.

get_user_by_session_token(token)

Gets the user with the given signed token.

If the token is valid {user, token_inserted_at} is returned, otherwise nil is returned.

invitation_only?()

@spec invitation_only?() :: boolean()

Whether the installation only lets invited people in (SALT_DNS_OIDC_AUTO_CREATE=invited).

list_memberships(organisation)

The members of an organisation with their users, owners first.

list_organisations(scope)

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

The organisations the scope's user belongs to, by name.

login_user_by_magic_link(token)

Logs the user in by magic link.

There are three cases to consider:

  1. The user has already confirmed their email. They are logged in and the magic link is expired.

  2. The user has not confirmed their email and no password is set. In this case, the user gets confirmed, logged in, and all tokens - including session ones - are expired. In theory, no other tokens exist but we delete all of them for best security practices.

  3. The user has not confirmed their email but a password is set. This cannot happen in the default implementation but may be the source of security pitfalls. See the "Mixing magic link and password registration" section of mix help phx.gen.auth.

mail_delivery?()

@spec mail_delivery?() :: boolean()

Whether this installation can send e-mail (config :salt_dns, :mail_delivery, true unless a release says otherwise). A production release has no mailer configured, so it sets this false in config/runtime.exs; everything that would need a mail (the e-mailed sign-in link, the address-change link, the confirmation of a new account) is then hidden or closed with a plain explanation, instead of promising a message that is dropped.

oidc_session_token?(token)

@spec oidc_session_token?(binary()) :: boolean()

Whether the session token came from a sign-in through the OpenID Connect provider.

oidc_sign_in(claims, opts \\ [])

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

Finds or creates the user for the claims of a verified OpenID Connect sign-in (SaltDns.OIDC.callback/3).

Users are recognised by the provider's sub claim and by nothing else: a user with the same email but another (or no) sub is never taken over, so a provider account cannot capture an existing account by claiming its email address. A new user is created from the email claim, which must be present and email_verified: true. Errors: :invalid_claims, :no_email, :email_not_verified, :email_taken, :invalid_email, :not_registered (when auto_create is false), :no_invitation (when it is :invited and no pending invitation exists for the verified address).

auto_create is true (any person the provider lets through gets a user), false (only existing users sign in) or :invited (a new user is created only for an address with a pending invitation, SaltDns.Accounts.Invitations; the user, the memberships and the acceptance happen in one transaction).

password_login?()

@spec password_login?() :: boolean()

True unless config :salt_dns, :password_login, false. When false, e-mail and password login, magic-link login and self-registration are all off and only the OpenID Connect provider signs users in (oidc_sign_in/1).

register_user(attrs)

Registers a user.

Examples

iex> register_user(%{field: value})
{:ok, %User{}}

iex> register_user(%{field: bad_value})
{:error, %Ecto.Changeset{}}

registration_open?()

@spec registration_open?() :: boolean()

Whether anyone may create an account on the registration page: password login is on, mail can be sent (the new account is confirmed with an e-mailed link, mail_delivery?/0) and config :salt_dns, :registration, true (SALT_DNS_REGISTRATION=true). Off by default: an open registration hands every visitor an organisation, zones, credentials and the use of the resolvers and outbound transfers. Users are then created by an operator (SaltDns.Release.create_user_from_env/0), by OpenID Connect sign-in, or by add_member/3 inviting an existing user. Code that creates a user on purpose (the release task, fixtures) calls register_user/1 directly.

remove_member(membership)

@spec remove_member(SaltDns.Accounts.Membership.t()) ::
  {:ok, SaltDns.Accounts.Membership.t()} | {:error, :last_owner}

Removes a member. The last owner cannot be removed: {:error, :last_owner}.

A person left with no membership in any organisation has no access left, so all their sessions end (delete_all_user_session_tokens/1). A person who still belongs elsewhere stays signed in, but their open pages are disconnected so that they load again with the rights they have now.

sudo_mode?(user, minutes \\ -20)

Checks whether the user is in sudo mode.

The user is in sudo mode when the last authentication was done no further than 20 minutes ago. The limit can be given as second argument in minutes.

update_locale(user, locale)

@spec update_locale(SaltDns.Accounts.User.t(), String.t()) ::
  {:ok, SaltDns.Accounts.User.t()} | :error

Keeps the language of the console this person chose.

update_member_role(membership, role)

@spec update_member_role(SaltDns.Accounts.Membership.t(), atom()) ::
  {:ok, SaltDns.Accounts.Membership.t()}
  | {:error, Ecto.Changeset.t() | :last_owner}

Changes a member's role. The last owner of an organisation cannot be demoted: the result is {:error, :last_owner}.

update_member_role(scope, membership, role)

@spec update_member_role(
  SaltDns.Accounts.Scope.t(),
  SaltDns.Accounts.Membership.t(),
  atom()
) ::
  {:ok, SaltDns.Accounts.Membership.t()}
  | {:error, Ecto.Changeset.t() | :last_owner | :forbidden}

Changes a member's role as the scope's user (see add_member/4: only an owner may make someone an owner), with the rules of update_member_role/2.

update_organisation(organisation, attrs)

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

Updates an organisation's name and TTL bounds.

update_user_email(user, token)

Updates the user email using the given token.

If the token matches, the user email is updated and the token is deleted.

update_user_password(user, attrs)

Updates the user password.

Returns a tuple with the updated user, as well as a list of expired tokens.

Examples

iex> update_user_password(user, %{password: ...})
{:ok, {%User{}, [...]}}

iex> update_user_password(user, %{password: "too short"})
{:error, %Ecto.Changeset{}}