Packages
phauxth
2.5.0
2.5.1
retired
2.5.0
2.4.0
2.3.2
2.3.1
2.3.0
2.2.0
2.1.1
2.1.0
2.0.1
2.0.0
2.0.0-rc.2
2.0.0-rc.1
2.0.0-rc.0
2.0.0-beta.1
2.0.0-beta.0
2.0.0-alpha.5
2.0.0-alpha.4
retired
2.0.0-alpha.3
retired
2.0.0-alpha.2
retired
2.0.0-alpha.1
retired
2.0.0-alpha.0
retired
1.2.9
1.2.8
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.1
1.2.0
1.1.3
1.1.2
1.1.1
1.1.0
1.0.3
1.0.2
1.0.1
1.0.0
0.17.1
0.17.0
0.16.0
0.15.1
0.15.0
0.14.0
0.13.0-rc.1
0.13.0-rc.0
0.12.1-rc.1
0.12.1-rc.0
0.12.0-rc
0.11.0
0.10.3
0.10.2
0.10.0
0.9.0
0.8.2
0.8.1
0.8.0
Authentication library for Phoenix, and other Plug-based, web applications
Current section
Files
Jump to
Current section
Files
lib/phauxth/login.ex
defmodule Phauxth.Login do
@moduledoc """
Module to login users.
Before using this module, you will need to add the `crypto_module` value
to the config. The `crypto_module` must implement the Comeonin behaviour.
## Options
There are two options:
* `:user_context` - the user_context module
* this can also be set in the config
* `:log_meta` - additional custom metadata for Phauxth.Log
* this should be a keyword list
There are also options for verifying the password. See the documentation
for the `check_pass` function for details.
"""
use Phauxth.Login.Base
alias Phauxth.Config
@doc """
Checks the password, using the crypto_module's `verify_pass/2`, by comparing
the hash with the password hash found in a user struct, or map.
This is a convenience function that takes a user struct, or map, as input
and seamlessly handles the cases where no user is found.
## Options
* `:hash_key` - the password hash identifier
* this does not need to be set if the key is `:password_hash` or `:encrypted_password`
* `:hide_user` - run the `no_user_verify/1` function if no user is found
* the default is true
"""
def check_pass(user, password, opts \\ [])
def check_pass(nil, _password, opts) do
unless opts[:hide_user] == false, do: Config.crypto_module().no_user_verify(opts)
{:error, "invalid user-identifier"}
end
def check_pass(user, password, opts) when is_binary(password) do
case get_hash(user, opts[:hash_key]) do
{:ok, hash} ->
if Config.crypto_module().verify_pass(password, hash) do
{:ok, user}
else
{:error, "invalid password"}
end
_ ->
{:error, "no password hash found in the user struct"}
end
end
def check_pass(_, _, _) do
{:error, "password is not a string"}
end
defp get_hash(%{password_hash: hash}, nil), do: {:ok, hash}
defp get_hash(%{encrypted_password: hash}, nil), do: {:ok, hash}
defp get_hash(_, nil), do: nil
defp get_hash(user, hash_key) do
if hash = Map.get(user, hash_key), do: {:ok, hash}
end
end