Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@
# General application configuration
import Config

config :myapp18, :scopes,
user: [
default: true,
module: Myapp18.Accounts.Scope,
assign_key: :current_scope,
access_path: [:user, :id],
schema_key: :user_id,
schema_type: :id,
schema_table: :users,
test_data_fixture: Myapp18.AccountsFixtures,
test_login_helper: :register_and_log_in_user
]

config :myapp18,
ecto_repos: [Myapp18.Repo],
generators: [timestamp_type: :utc_datetime]
Expand Down
3 changes: 3 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Config

# Only in tests, remove the complexity from the password hashing algorithm
config :bcrypt_elixir, :log_rounds, 1

# Configure your database
#
# The MIX_TEST_PARTITION environment variable can be used
Expand Down
308 changes: 308 additions & 0 deletions lib/myapp18/accounts.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
defmodule Myapp18.Accounts do
@moduledoc """
The Accounts context.
"""

import Ecto.Query, warn: false
alias Myapp18.Repo

alias Myapp18.Accounts.{User, UserToken, UserNotifier}

## Database getters

@doc """
Gets a user by email.

## Examples

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

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

"""
def get_user_by_email(email) when is_binary(email) do
Repo.get_by(User, email: email)
end

@doc """
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

"""
def get_user_by_email_and_password(email, password)
when is_binary(email) and is_binary(password) do
user = Repo.get_by(User, email: email)
if User.valid_password?(user, password), do: user
end

@doc """
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)

"""
def get_user!(id), do: Repo.get!(User, id)

## User registration

@doc """
Registers a user.

## Examples

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

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

"""
def register_user(attrs) do
%User{}
|> User.email_changeset(attrs)
|> Repo.insert()
end

## Settings

@doc """
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.
"""
def sudo_mode?(user, minutes \\ -20)

def sudo_mode?(%User{authenticated_at: ts}, minutes) when is_struct(ts, DateTime) do
DateTime.after?(ts, DateTime.utc_now() |> DateTime.add(minutes, :minute))
end

def sudo_mode?(_user, _minutes), do: false

@doc """
Returns an `%Ecto.Changeset{}` for changing the user email.

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

## Examples

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

"""
def change_user_email(user, attrs \\ %{}, opts \\ []) do
User.email_changeset(user, attrs, opts)
end

@doc """
Updates the user email using the given token.

If the token matches, the user email is updated and the token is deleted.
"""
def update_user_email(user, token) do
context = "change:#{user.email}"

with {:ok, query} <- UserToken.verify_change_email_token_query(token, context),
%UserToken{sent_to: email} <- Repo.one(query),
{:ok, _} <- Repo.transaction(user_email_multi(user, email, context)) do
:ok
else
_ -> :error
end
end

defp user_email_multi(user, email, context) do
changeset = User.email_changeset(user, %{email: email})

Ecto.Multi.new()
|> Ecto.Multi.update(:user, changeset)
|> Ecto.Multi.delete_all(:tokens, UserToken.by_user_and_contexts_query(user, [context]))
end

@doc """
Returns an `%Ecto.Changeset{}` for changing the user password.

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

## Examples

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

"""
def change_user_password(user, attrs \\ %{}, opts \\ []) do
User.password_changeset(user, attrs, opts)
end

@doc """
Updates the user password.

Returns 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{}}

"""
def update_user_password(user, attrs) do
user
|> User.password_changeset(attrs)
|> update_user_and_delete_all_tokens()
|> case do
{:ok, user, expired_tokens} -> {:ok, user, expired_tokens}
{:error, :user, changeset, _} -> {:error, changeset}
end
end

## Session

@doc """
Generates a session token.
"""
def generate_user_session_token(user) do
{token, user_token} = UserToken.build_session_token(user)
Repo.insert!(user_token)
token
end

@doc """
Gets the user with the given signed token.

If the token is valid `{user, token_created}` is returned, otherwise `nil` is returned.
"""
def get_user_by_session_token(token) do
UserToken.valid_user_session_query(token)
|> Repo.one()
end

@doc """
Gets the user with the given magic link token.
"""
def get_user_by_magic_link_token(token) do
with {:ok, query} <- UserToken.verify_magic_link_token_query(token),
{user, _token} <- Repo.one(query) do
user
else
_ -> nil
end
end

@doc """
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`.
"""
def login_user_by_magic_link(token) do
{:ok, query} = UserToken.verify_magic_link_token_query(token)

case Repo.one(query) do
# Prevent session fixation attacks by disallowing magic links for unconfirmed users with password
{%User{confirmed_at: nil, hashed_password: hash}, _token} when not is_nil(hash) ->
raise """
magic link log in is not allowed for unconfirmed users with a password set!

This cannot happen with the default implementation, which indicates that you
might have adapted the code to a different use case. Please make sure to read the
"Mixing magic link and password registration" section of `mix help phx.gen.auth`.
"""

{%User{confirmed_at: nil} = user, _token} ->
user
|> User.confirm_changeset()
|> update_user_and_delete_all_tokens()

{user, token} ->
Repo.delete!(token)
{:ok, user, []}

nil ->
{:error, :not_found}
end
end

@doc ~S"""
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: ...}}

"""
def deliver_user_update_email_instructions(%User{} = user, current_email, update_email_url_fun)
when is_function(update_email_url_fun, 1) do
{encoded_token, user_token} = UserToken.build_email_token(user, "change:#{current_email}")

Repo.insert!(user_token)
UserNotifier.deliver_update_email_instructions(user, update_email_url_fun.(encoded_token))
end

@doc ~S"""
Delivers the magic link login instructions to the given user.
"""
def deliver_login_instructions(%User{} = user, magic_link_url_fun)
when is_function(magic_link_url_fun, 1) do
{encoded_token, user_token} = UserToken.build_email_token(user, "login")
Repo.insert!(user_token)
UserNotifier.deliver_login_instructions(user, magic_link_url_fun.(encoded_token))
end

@doc """
Deletes the signed token with the given context.
"""
def delete_user_session_token(token) do
Repo.delete_all(UserToken.by_token_and_context_query(token, "session"))
:ok
end

## Token helper

defp update_user_and_delete_all_tokens(changeset) do
%{data: %User{} = user} = changeset

with {:ok, %{user: user, tokens_to_expire: expired_tokens}} <-
Ecto.Multi.new()
|> Ecto.Multi.update(:user, changeset)
|> Ecto.Multi.all(:tokens_to_expire, UserToken.by_user_and_contexts_query(user, :all))
|> Ecto.Multi.delete_all(:tokens, fn %{tokens_to_expire: tokens_to_expire} ->
UserToken.delete_all_query(tokens_to_expire)
end)
|> Repo.transaction() do
{:ok, user, expired_tokens}
end
end
end
33 changes: 33 additions & 0 deletions lib/myapp18/accounts/scope.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
defmodule Myapp18.Accounts.Scope do
@moduledoc """
Defines the scope of the caller to be used throughout the app.

The `Myapp18.Accounts.UserScope` allows public interfaces to receive
information about the caller, such as if the call is initiated from an
end-user, and if so, which user. Additionally, such a scope can carry fields
such as "super user" or other privileges for use as authorization, or to
ensure specific code paths can only be access for a given scope.

It is useful for logging as well as for scoping pubsub subscriptions and
broadcasts when a caller subscribes to an interface or performs a particular
action.

Feel free to extend the fields on this struct to fit the needs of
growing application requirements.
"""

alias Myapp18.Accounts.User

defstruct user: nil

@doc """
Creates a scope for the given user.

Returns nil if no user is given.
"""
def for_user(%User{} = user) do
%__MODULE__{user: user}
end

def for_user(nil), do: nil
end
Loading