2020-12-15 14:24:14 +01:00
|
|
|
defmodule RecycledCloud.Accounts.User do
|
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
2020-12-21 15:40:19 +01:00
|
|
|
require Logger
|
|
|
|
alias RecycledCloud.{LDAP,Accounts}
|
2020-12-22 13:20:54 +01:00
|
|
|
alias RecycledCloud.Accounts.User
|
2020-12-21 15:40:19 +01:00
|
|
|
alias RecycledCloud.Repo
|
2020-12-15 14:24:14 +01:00
|
|
|
|
2021-01-05 17:20:35 +01:00
|
|
|
@min_password_lenght 6
|
|
|
|
@max_password_lenght 80
|
|
|
|
|
2020-12-15 14:24:14 +01:00
|
|
|
@derive {Inspect, except: [:password]}
|
|
|
|
schema "users" do
|
2020-12-21 15:40:19 +01:00
|
|
|
field :username, :string
|
2020-12-15 14:24:14 +01:00
|
|
|
field :password, :string, virtual: true
|
2020-12-22 14:22:34 +01:00
|
|
|
field :dn, :string, virtual: true
|
2020-12-21 15:40:19 +01:00
|
|
|
field :email, :string, virtual: true
|
2020-12-15 14:24:14 +01:00
|
|
|
field :confirmed_at, :naive_datetime
|
2021-01-20 17:28:48 +01:00
|
|
|
field :partner_id, :integer
|
2021-02-03 10:12:23 +01:00
|
|
|
field :captcha, :integer, virtual: :true
|
2020-12-15 14:24:14 +01:00
|
|
|
|
2020-12-23 12:51:05 +01:00
|
|
|
has_many :keys, Accounts.Key
|
|
|
|
|
2020-12-15 14:24:14 +01:00
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2020-12-22 14:22:34 +01:00
|
|
|
# Note: the returned LDAP values usually are lists of charlist (e.g. `%{'uid'
|
|
|
|
# => ['myusername']}`).
|
|
|
|
defp get_ldap_attributes(%User{username: uid}), do: get_ldap_attributes(uid)
|
|
|
|
defp get_ldap_attributes(uid) do
|
2021-01-05 17:20:35 +01:00
|
|
|
query = fn ldap_conn -> LDAP.search(ldap_conn, :uid, uid) end
|
2020-12-22 14:22:34 +01:00
|
|
|
case query |> LDAP.execute do
|
|
|
|
{:ok, []} -> {:error, "could not find matching object"}
|
2021-01-05 17:20:35 +01:00
|
|
|
{:ok, [entry]} -> {:ok, entry}
|
2021-01-07 08:15:09 +01:00
|
|
|
{:ok, [_entry|_]} -> {:error, "found more than one object with uid #{uid}"}
|
2020-12-22 14:22:34 +01:00
|
|
|
{:error, err} -> {:error, inspect(err)}
|
|
|
|
end
|
2020-12-21 15:40:19 +01:00
|
|
|
end
|
|
|
|
|
2020-12-22 14:22:34 +01:00
|
|
|
def maybe_populate_ldap_attributes(user) do
|
|
|
|
# TODO: could be useful to cache this data somehow, perhaps in an ETS
|
|
|
|
# table? We query the LDAP server every time we try to fetch an user's
|
|
|
|
# data at the moment, which is inefficient.
|
|
|
|
|
|
|
|
case get_ldap_attributes(user) do
|
2021-01-05 17:20:35 +01:00
|
|
|
{:ok, %{:mail => [raw_email], :dn => raw_dn}} ->
|
2020-12-22 14:22:34 +01:00
|
|
|
email = List.to_string(raw_email)
|
|
|
|
dn = List.to_string(raw_dn)
|
|
|
|
user |> Map.put(:email, email) |> Map.put(:dn, dn)
|
|
|
|
{:ok, _} ->
|
|
|
|
Logger.warn("Malformed LDAP user object")
|
2020-12-22 13:20:54 +01:00
|
|
|
user
|
2020-12-22 14:22:34 +01:00
|
|
|
{:error, err} ->
|
|
|
|
Logger.warn("Error querying LDAP backend: #{err}")
|
2020-12-22 13:20:54 +01:00
|
|
|
user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-08 14:41:37 +01:00
|
|
|
defp get_last_ldap_uid() do
|
|
|
|
query = fn ldap_conn ->
|
|
|
|
LDAP.search(ldap_conn, :objectClass, 'posixAccount')
|
|
|
|
end
|
|
|
|
|
|
|
|
case query |> LDAP.execute do
|
|
|
|
{:ok, entries} ->
|
|
|
|
uids = Enum.map(entries, fn entry ->
|
|
|
|
entry.uidNumber |> Enum.at(0) |> List.to_integer
|
|
|
|
end)
|
|
|
|
|
|
|
|
{:ok, uids |> Enum.max()}
|
|
|
|
{:error, err} -> {:error, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-05 17:20:35 +01:00
|
|
|
defp create_ldap_user(%User{} = user) do
|
2021-01-08 14:41:37 +01:00
|
|
|
# TODO: read dn template from conf, gid_number
|
2021-01-05 17:20:35 +01:00
|
|
|
conf = Application.get_env(:recycledcloud, :ldap)
|
|
|
|
basetree = conf |> Keyword.get(:base_dn)
|
2021-01-08 14:41:37 +01:00
|
|
|
default_group_gid = conf |> Keyword.get(:default_group_gid, 1000)
|
|
|
|
|
2021-01-05 17:20:35 +01:00
|
|
|
dn = ("uid=" <> user.username <> ",ou=Users," <> basetree) |> String.to_charlist
|
|
|
|
|
2021-01-08 14:41:37 +01:00
|
|
|
{:ok, last_uid_number} = get_last_ldap_uid()
|
|
|
|
uid_number = last_uid_number + 1
|
|
|
|
gid_number = default_group_gid
|
2021-01-05 17:20:35 +01:00
|
|
|
|
|
|
|
ldif = [
|
|
|
|
{'uid', [user.username]},
|
|
|
|
{'cn', [user.username]},
|
|
|
|
{'givenName', [user.username]},
|
|
|
|
{'sn', [user.username]},
|
|
|
|
{'mail', [user.email]},
|
|
|
|
{'objectClass', ["inetOrgPerson", "posixAccount", "shadowAccount"]},
|
|
|
|
{'homeDirectory', ["/home/#{user.username}"]},
|
|
|
|
{'loginShell', ["/bin/bash"]},
|
|
|
|
{'userPassword', ["/bin/bash"]},
|
|
|
|
{'uidNumber', [Integer.to_string(uid_number)]},
|
|
|
|
{'gidNumber', [Integer.to_string(gid_number)]}
|
|
|
|
]
|
|
|
|
|
|
|
|
query = fn ldap_conn ->
|
|
|
|
case :eldap.add(ldap_conn, dn, ldif) do
|
|
|
|
:ok ->
|
|
|
|
:eldap.modify_password(ldap_conn, dn, to_charlist(user.password))
|
|
|
|
err -> err
|
|
|
|
end
|
|
|
|
end
|
2021-01-07 08:15:09 +01:00
|
|
|
query |> LDAP.execute
|
2021-01-05 17:20:35 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def register(%User{} = user) do
|
|
|
|
case create_ldap_user(user) do
|
|
|
|
:ok -> {:ok, get_by_username(user.username)}
|
|
|
|
err -> err
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-21 15:40:19 +01:00
|
|
|
def get_by_username(username) when is_binary(username) do
|
|
|
|
local_user = Repo.get_by(User, username: username)
|
|
|
|
if local_user do
|
2020-12-22 14:22:34 +01:00
|
|
|
local_user |> User.maybe_populate_ldap_attributes()
|
2020-12-21 15:40:19 +01:00
|
|
|
else
|
2020-12-22 14:22:34 +01:00
|
|
|
case get_ldap_attributes(username) do
|
2021-01-05 17:20:35 +01:00
|
|
|
{:ok, %{:uid => [raw_uid], :mail => [raw_email], :dn => raw_dn}} ->
|
2020-12-22 14:22:34 +01:00
|
|
|
uid = List.to_string(raw_uid)
|
|
|
|
email = List.to_string(raw_email)
|
|
|
|
dn = List.to_string(raw_dn)
|
2021-01-05 17:20:35 +01:00
|
|
|
case Accounts.insert_user(%{username: uid, email: email, dn: dn}) do
|
2020-12-22 14:22:34 +01:00
|
|
|
{:ok, user} ->
|
|
|
|
user
|
|
|
|
{:error, err} ->
|
|
|
|
Logger.warn("Something went wrong importing user from LDAP: #{inspect(err)}")
|
|
|
|
nil
|
2020-12-21 15:40:19 +01:00
|
|
|
end
|
|
|
|
{:error, err} ->
|
2020-12-22 14:22:34 +01:00
|
|
|
Logger.warn("Error querying LDAP backend: #{err}")
|
2020-12-21 15:40:19 +01:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-22 14:22:34 +01:00
|
|
|
def get!(id) do
|
|
|
|
Repo.get!(User, id) |> User.maybe_populate_ldap_attributes()
|
|
|
|
end
|
|
|
|
|
2020-12-15 14:24:14 +01:00
|
|
|
@doc """
|
|
|
|
A user changeset for registration.
|
|
|
|
|
|
|
|
It is important to validate the length of both email and password.
|
|
|
|
Otherwise databases may truncate the email without warnings, which
|
|
|
|
could lead to unpredictable or insecure behaviour. Long passwords may
|
|
|
|
also be very expensive to hash for certain algorithms.
|
|
|
|
"""
|
2020-12-22 14:22:34 +01:00
|
|
|
def registration_changeset(user, attrs) do
|
2021-02-03 10:12:23 +01:00
|
|
|
expected_captcha_result = attrs |> Map.get("expected_captcha")
|
|
|
|
|
2021-01-05 17:20:35 +01:00
|
|
|
user
|
2021-02-03 10:12:23 +01:00
|
|
|
|> cast(attrs, [:username, :password, :email, :dn, :captcha])
|
|
|
|
|> RecycledCloud.Captcha.validate(expected_captcha_result)
|
|
|
|
|> validate_username()
|
|
|
|
|> validate_email()
|
2021-01-05 17:20:35 +01:00
|
|
|
|> validate_password()
|
|
|
|
end
|
|
|
|
|
|
|
|
def insertion_changeset(user, attrs) do
|
2020-12-15 14:24:14 +01:00
|
|
|
user
|
2020-12-22 14:22:34 +01:00
|
|
|
|> cast(attrs, [:username, :password, :email, :dn])
|
2020-12-15 14:24:14 +01:00
|
|
|
|> validate_email()
|
2021-02-03 10:12:23 +01:00
|
|
|
|> validate_username()
|
2020-12-15 14:24:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
defp validate_email(changeset) do
|
|
|
|
changeset
|
|
|
|
|> validate_required([:email])
|
|
|
|
|> validate_format(:email, ~r/^[^\s]+@[^\s]+$/, message: "must have the @ sign and no spaces")
|
|
|
|
|> validate_length(:email, max: 160)
|
|
|
|
end
|
|
|
|
|
2020-12-21 15:40:19 +01:00
|
|
|
defp validate_password(changeset) do
|
2020-12-15 14:24:14 +01:00
|
|
|
changeset
|
|
|
|
|> validate_required([:password])
|
2021-01-05 17:20:35 +01:00
|
|
|
|> validate_length( :password, min: @min_password_lenght, max: @max_password_lenght)
|
2020-12-21 15:40:19 +01:00
|
|
|
#|> validate_format(:password, ~r/[a-z]/, message: "at least one lower case character")
|
|
|
|
#|> validate_format(:password, ~r/[A-Z]/, message: "at least one upper case character")
|
|
|
|
#|> validate_format(:password, ~r/[!?@#$%^&*_0-9]/, message: "at least one digit or punctuation character")
|
2020-12-15 14:24:14 +01:00
|
|
|
end
|
|
|
|
|
2021-02-03 10:12:23 +01:00
|
|
|
defp validate_username(changeset) do
|
|
|
|
changeset
|
|
|
|
|> validate_required([:username])
|
|
|
|
end
|
|
|
|
|
2020-12-15 14:24:14 +01:00
|
|
|
@doc """
|
|
|
|
A user changeset for changing the email.
|
|
|
|
|
|
|
|
It requires the email to change otherwise an error is added.
|
|
|
|
"""
|
|
|
|
def email_changeset(user, attrs) do
|
|
|
|
user
|
|
|
|
|> cast(attrs, [:email])
|
|
|
|
|> validate_email()
|
|
|
|
|> case do
|
|
|
|
%{changes: %{email: _}} = changeset -> changeset
|
|
|
|
%{} = changeset -> add_error(changeset, :email, "did not change")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
A user changeset for changing the password.
|
|
|
|
"""
|
2020-12-21 15:40:19 +01:00
|
|
|
def password_changeset(user, attrs) do
|
2020-12-15 14:24:14 +01:00
|
|
|
user
|
|
|
|
|> cast(attrs, [:password])
|
|
|
|
|> validate_confirmation(:password, message: "does not match password")
|
2020-12-21 15:40:19 +01:00
|
|
|
|> validate_password()
|
2020-12-15 14:24:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Confirms the account by setting `confirmed_at`.
|
|
|
|
"""
|
|
|
|
def confirm_changeset(user) do
|
|
|
|
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
|
|
|
|
change(user, confirmed_at: now)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Verifies the password.
|
|
|
|
"""
|
2021-01-05 17:20:35 +01:00
|
|
|
def valid_password?(%User{} = user, password)
|
2020-12-21 15:40:19 +01:00
|
|
|
when byte_size(password) > 0 do
|
|
|
|
query = fn ldap_conn ->
|
2021-01-05 17:20:35 +01:00
|
|
|
:eldap.simple_bind(ldap_conn, user.dn, password)
|
2020-12-21 15:40:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
case query |> LDAP.execute_single do
|
|
|
|
:ok -> true
|
|
|
|
{:error, _} -> false
|
|
|
|
end
|
2020-12-15 14:24:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid_password?(_, _) do
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Validates the current password otherwise adds an error to the changeset.
|
|
|
|
"""
|
|
|
|
def validate_current_password(changeset, password) do
|
|
|
|
if valid_password?(changeset.data, password) do
|
|
|
|
changeset
|
|
|
|
else
|
|
|
|
add_error(changeset, :current_password, "is not valid")
|
|
|
|
end
|
|
|
|
end
|
2020-12-21 15:40:19 +01:00
|
|
|
|
2021-01-08 14:41:37 +01:00
|
|
|
def set_password(%User{dn: nil} = _user, _), do: {:error, "User DN must be set"}
|
|
|
|
def set_password(_, nil), do: {:error, "Password cannot be nil"}
|
|
|
|
def set_password(%User{dn: dn} = _user, new_password) when
|
|
|
|
byte_size(new_password) > 0do
|
2020-12-21 15:40:19 +01:00
|
|
|
query = fn ldap_conn ->
|
|
|
|
:eldap.modify_password(
|
|
|
|
ldap_conn,
|
2021-01-08 14:41:37 +01:00
|
|
|
to_charlist(dn),
|
2021-01-05 17:20:35 +01:00
|
|
|
to_charlist(new_password)
|
2020-12-21 15:40:19 +01:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-01-05 17:20:35 +01:00
|
|
|
query |> LDAP.execute
|
2020-12-21 15:40:19 +01:00
|
|
|
end
|
2020-12-22 15:10:38 +01:00
|
|
|
|
|
|
|
def set_email(user, email) do
|
|
|
|
ldif = :eldap.mod_replace('mail', [email |> String.to_charlist])
|
|
|
|
|
|
|
|
query = fn ldap_conn ->
|
|
|
|
:eldap.modify(ldap_conn, String.to_charlist(user.dn), [ldif])
|
|
|
|
end
|
|
|
|
|
2021-01-05 17:20:35 +01:00
|
|
|
query |> LDAP.execute
|
2020-12-22 15:10:38 +01:00
|
|
|
end
|
2020-12-15 14:24:14 +01:00
|
|
|
end
|