Packages
openmaize
2.2.0
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/database.ex
defmodule Openmaize.Database do
@moduledoc """
Functions that are used to access the database.
"""
import Ecto.Changeset
alias Openmaize.{Config, Password}
@doc """
Hash the password and add it to the user model or changeset.
Before the password is hashed, it is checked to make sure that
it is not too weak. See the documentation for the Openmaize.Password
module for more information about the options available.
This function will return a changeset. If there are any errors, they
will be added to the changeset.
Comeonin.Bcrypt is the default hashing function, but this can be changed to
Comeonin.Pbkdf2, or any other algorithm, by setting the Config.crypto_mod value.
"""
def add_password_hash(user, params) do
(params[:password] || params["password"])
|> Password.valid_password?(Config.password_strength)
|> add_hash_changeset(user)
end
@doc """
Add a confirmation token to the user model or changeset.
Add the following three entries to your user schema:
field :confirmation_token, :string
field :confirmation_sent_at, Ecto.DateTime
field :confirmed_at, Ecto.DateTime
## Examples
In the following example, the 'add_confirm_token' function is called with
a key generated by 'Openmaize.ConfirmEmail.gen_token_link':
changeset |> Openmaize.Database.add_confirm_token(key)
"""
def add_confirm_token(user, key) do
change(user, %{confirmation_token: key,
confirmation_sent_at: Ecto.DateTime.utc})
end
@doc """
Add a reset token to the user model or changeset.
Add the following two entries to your user schema:
field :reset_token, :string
field :reset_sent_at, Ecto.DateTime
As with 'add_confirm_token', the function 'Openmaize.ConfirmEmail.gen_token_link'
can be used to generate the token and link.
"""
def add_reset_token(user, key) do
change(user, %{reset_token: key, reset_sent_at: Ecto.DateTime.utc})
end
@doc """
Change the 'confirmed_at' value in the database to the current time.
"""
def user_confirmed(user, repo) do
change(user, %{confirmed_at: Ecto.DateTime.utc})
|> repo.update
end
@doc """
Add the password hash for the new password to the database.
If the update is successful, the reset_token and reset_sent_at
values will be set to nil.
"""
def password_reset(user, password, repo) do
Password.valid_password?(password, Config.password_strength)
|> reset_update_repo(user, repo)
end
@doc """
Function used to check if a token has expired.
"""
def check_time(nil, _), do: false
def check_time(sent_at, valid_secs) do
(sent_at |> Ecto.DateTime.to_erl
|> :calendar.datetime_to_gregorian_seconds) + valid_secs >
(:calendar.universal_time |> :calendar.datetime_to_gregorian_seconds)
end
defp add_hash_changeset({:ok, password}, user) do
change(user, %{Config.hash_name =>
Config.crypto_mod.hashpwsalt(password)})
end
defp add_hash_changeset({:error, message}, user) do
change(user, %{password: ""}) |> add_error(:password, message)
end
defp reset_update_repo({:ok, password}, user, repo) do
repo.transaction(fn ->
user = change(user, %{Config.hash_name =>
Config.crypto_mod.hashpwsalt(password)})
|> repo.update!
change(user, %{reset_token: nil, reset_sent_at: nil})
|> repo.update!
end)
end
defp reset_update_repo({:error, message}, _user, _repo) do
{:error, message}
end
end