Packages
openmaize
1.0.0-beta.1
3.0.1
retired
3.0.0
2.9.0
2.8.0
2.7.0
2.6.0
2.5.1
2.5.0
2.4.0
2.3.2
2.3.1
2.3.0
2.2.0
2.1.5
2.1.4
2.1.3
2.1.2
2.1.1
2.1.0
2.0.2
2.0.1
2.0.0
1.0.1
1.0.0
1.0.0-beta.5
1.0.0-beta.4
1.0.0-beta.3
1.0.0-beta.2
1.0.0-beta.1
1.0.0-beta.0
0.19.3
0.19.2
0.19.1
0.19.0
0.18.1
0.18.0
0.17.2
0.17.1
0.17.0
0.16.2
0.16.1
0.16.0
0.15.1
0.15.0
0.14.0
0.13.0
0.12.0
0.11.1
0.11.0
0.10.2
0.10.1
0.10.0
0.8.1
0.8.0
0.7.5
0.7.4
0.7.2
0.7.1
0.7.0
0.6.7
0.6.6
0.6.3
0.6.2
0.6.1
0.6.0
0.5.0
0.4.1
0.4.0
Authentication library for Elixir using Plug
Current section
Files
Jump to
Current section
Files
lib/openmaize/confirm/base.ex
defmodule Openmaize.Confirm.Base do
@moduledoc """
Base implementation of the email confirmation module.
This is used by both the Openmaize.Confirm and Openmaize.ResetPassword
modules.
You can also use it to create your own custom module / plug.
"""
@doc false
defmacro __using__(_) do
quote do
@behaviour Plug
import unquote(__MODULE__)
@doc false
def init(opts) do
{Keyword.get(opts, :key_expires_after, 120),
Keyword.get(opts, :unique_id, :email),
Keyword.get(opts, :mail_function)}
end
@doc false
def call(%Plug.Conn{params: %{"key" => key} = user_params} = conn, opts)
when byte_size(key) == 32 do
check_user_key(conn, user_params, key, :nopassword, opts)
end
def call(conn, {_, _, _}), do: invalid_link_error(conn)
defoverridable [init: 1, call: 2]
end
end
import Plug.Conn
import Comeonin.Tools
alias Openmaize.Config
@doc """
Check the user key and, if necessary, the user password.
If this function is successful, the database will be updated, and an
`openmaize_info` message will be added to the conn. If there is an error,
an `openmaize_error` message will be added to the conn.
"""
def check_user_key(conn, user_params, key, password,
{key_expiry, uniq, mail_func}) do
case Map.get(user_params, to_string(uniq)) do
nil -> finalize(nil, conn, nil, mail_func)
user_id ->
user_id
|> Config.db_module.find_user(uniq)
|> check_key(key, key_expiry * 60, password)
|> finalize(conn, user_id, mail_func)
end
end
@doc """
Error message in the case of an invalid link.
"""
def invalid_link_error(conn) do
put_private(conn, :openmaize_error, "Invalid link")
end
defp check_key(nil, _, _, _), do: false
defp check_key(user, key, valid_secs, :nopassword) do
Config.db_module.check_time(user.confirmation_sent_at, valid_secs) and
secure_check(user.confirmation_token, key) and
Config.db_module.user_confirmed(user)
end
defp check_key(user, key, valid_secs, password) do
Config.db_module.check_time(user.reset_sent_at, valid_secs) and
secure_check(user.reset_token, key) and
Config.db_module.password_reset(user, password)
end
defp finalize({:ok, user}, conn, _, mail_func) do
mail_func && mail_func.(user.email)
put_private(conn, :openmaize_info, "Account successfully confirmed")
end
defp finalize({:error, message}, conn, _user_id, _) do
put_private(conn, :openmaize_error, message)
end
defp finalize(_, conn, user_id, _) do
put_private(conn, :openmaize_error, "Confirmation for #{user_id} failed")
end
end