Skip to content
This repository has been archived by the owner on Nov 15, 2024. It is now read-only.

Commit

Permalink
reset password new rule
Browse files Browse the repository at this point in the history
  • Loading branch information
augnustin committed Oct 6, 2022
1 parent a1968d6 commit f9a8b7a
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 7 deletions.
4 changes: 4 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ config :vae, Vae.Scheduler,
rncp_update_task: [
schedule: "0 3 1,15 * *", # 3AM on the first and 15th of month
task: &Vae.Authorities.Rncp.Update.update_all/0
],
security_reset_password_task: [
schedule: "0 4 1 * *", # 4AM on the first of month
task: &Vae.User.reset_old_users_password/0
]
], else: []

Expand Down
Binary file added db/pgadmin4/pgadmin4.db
Binary file not shown.
Empty file added db/pgadmin4/storage/.gitkeep
Empty file.
5 changes: 5 additions & 0 deletions lib/vae/helpers/string.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ defmodule Vae.String do
:crypto.hash(:md5, string |> Base.encode16())
end

def generate_hash(nb_chars) do
:crypto.strong_rand_bytes(nb_chars) |> Base.url_encode64() |> binary_part(0, nb_chars)
end


def to_id(param) when is_binary(param) do
param
|> to_id_string()
Expand Down
2 changes: 1 addition & 1 deletion lib/vae_web/controllers/session_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule VaeWeb.SessionController do
changeset = Pow.Plug.change_user(conn, conn.params["user"])

conn
|> put_flash(:danger, "L'authentification a échoué. Merci de réessayer ou de cliquer sur \"[Mot de passe oublié ?](/reset-password/new)\" si vous ne parvenez pas à le retrouver.\n\nNB: les règles de sécurité ont évoluées sur Avril et votre mot de passe a certainement été réinitialisé automatiquement.")
|> put_flash(:danger, "L'authentification a échoué. Merci de réessayer ou de cliquer sur \"[Mot de passe oublié ?](/reset-password/new)\" si vous ne parvenez pas à le retrouver.\n\nNB : les règles de sécurité ont évolué sur Avril, il se peut que votre mot de passe ait été réinitialisé automatiquement et c'est pourquoi vous devez en créer un nouveau. Merci de votre compréhension")
|> render("new.html", changeset: changeset)
end
end
Expand Down
26 changes: 26 additions & 0 deletions lib/vae_web/models/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ defmodule Vae.User do
use Pow.Extension.Ecto.Schema,
extensions: [PowResetPassword]

import Ecto.Query

import Pow.Ecto.Schema.Changeset,
only: [password_changeset: 3]

Expand All @@ -35,6 +37,8 @@ defmodule Vae.User do
field(:is_admin, :boolean)
field(:is_delegate, :boolean)
field(:pe_id, :string)
field(:reset_password_sent_at, :utc_datetime)
field(:password_hash, :string)

has_many(:applications, UserApplication, on_replace: :delete, on_delete: :delete_all)

Expand Down Expand Up @@ -118,6 +122,15 @@ defmodule Vae.User do
end)
end

def reset_random_password(user) do
password = "#{Vae.String.generate_hash(8)}@#{Vae.String.generate_hash(8)}1"
password_changeset(user, %{
password: password,
password_confirmation: password,
}, @pow_config)
|> Repo.update()
end

def extract_identity_data(changeset) do
duplicated_fields = ~w(email first_name last_name)a

Expand Down Expand Up @@ -224,4 +237,17 @@ defmodule Vae.User do

def transferable_applications(_), do: []

def reset_old_users_password() do
from(u in User, where: fragment("?::date", u.inserted_at) < ^~D[2022-09-29])
|> Repo.update_all(set: [reset_password_sent_at: DateTime.utc_now()])

six_months_ago = Date.utc_today() |> Timex.shift(months: -6)
from(u in User, where:
fragment("?::date", u.inserted_at) < ^six_months_ago and
not is_nil(u.reset_password_sent_at) and not is_nil(u.password_hash)
)
|> Repo.all()
|> Enum.each(&User.reset_random_password(&1))
end

end
8 changes: 2 additions & 6 deletions lib/vae_web/models/user_application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ defmodule Vae.UserApplication do
end

def init_booklet_hash(changeset) do
change(changeset, booklet_hash: changeset.data.booklet_hash || generate_hash(64))
change(changeset, booklet_hash: changeset.data.booklet_hash || Vae.String.generate_hash(64))
end

def delete_with_resumes(%UserApplication{} = ua) do
Expand Down Expand Up @@ -113,7 +113,7 @@ defmodule Vae.UserApplication do

def generate_delegate_access_hash_changeset(application) do
change(application, %{
delegate_access_hash: application.delegate_access_hash || generate_hash(64),
delegate_access_hash: application.delegate_access_hash || Vae.String.generate_hash(64),
delegate_access_refreshed_at: DateTime.utc_now() |> DateTime.truncate(:second)
})
end
Expand Down Expand Up @@ -257,10 +257,6 @@ defmodule Vae.UserApplication do
|> Vae.URI.to_absolute_string(endpoint)
end

defp generate_hash(length) do
:crypto.strong_rand_bytes(length) |> Base.url_encode64() |> binary_part(0, length)
end

def merge_applications_with_unicity_constraint(list1, list2) do
Enum.reduce(list1, list2, fn %UserApplication{user_id: user_id, certification_id: certification_id} = a1, result ->
case Vae.Enum.reject_and_find(result, &(&1.user_id == user_id && &1.certification_id == certification_id)) do
Expand Down

0 comments on commit f9a8b7a

Please sign in to comment.