Skip to content

Commit

Permalink
feat: Hash SMTP password and remove it from sender_profile list page
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarlin-7757 authored and bhanuvrat committed Jul 10, 2024
1 parent 6e9c26b commit 80f201d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 36 deletions.
17 changes: 16 additions & 1 deletion lib/vachan_web/live/sender_profile_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,24 @@ defmodule VachanWeb.SenderProfileLive.FormComponent do

@impl true
def handle_event("save", %{"form" => sender_profile_params}, socket) do
save_sender_profile(socket, socket.assigns.action, sender_profile_params)
updated_params = hash_password_if_present(sender_profile_params)
save_sender_profile(socket, socket.assigns.action, updated_params)
end

defp hash_password_if_present(params) do
case Map.get(params, "password", "") do
"" -> params
plain_password ->
hashed_password = hash_password(plain_password)
Map.put(params, "password", hashed_password)
end
end

defp hash_password(password) do
Bcrypt.hash_pwd_salt(password)
end


defp save_sender_profile(socket, :edit, sender_profile_params) do
form = AshPhoenix.Form.validate(socket.assigns.form, sender_profile_params)

Expand Down
36 changes: 3 additions & 33 deletions lib/vachan_web/live/sender_profile_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,10 @@ defmodule VachanWeb.SenderProfileLive.Index do
IO.inspect(params, label: "Handle Params")
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end

>
defp apply_action(socket, :edit, %{"id" => id} = params) do

Check warning on line 18 in lib/vachan_web/live/sender_profile_live/index.ex

View workflow job for this annotation

GitHub Actions / test

variable "params" is unused (if the variable is not meant to be used, prefix it with an underscore)
sender_profile = SenderProfile.get_by_id!(id, ash_opts(socket))
smtp_password = Map.get(params, "password")

updated_sender_profile =
if smtp_password != "" do
hashed_password = hash_password(smtp_password)
IO.inspect(hashed_password, label: "Hashed Password - Edit")
%SenderProfile{sender_profile | password: hashed_password}
else
sender_profile
end

IO.inspect(updated_sender_profile, label: "Updated Sender Profile - Edit")
updated_sender_profile = sender_profile

socket
|> assign(:page_title, "Edit Sender Profile")
Expand All @@ -45,17 +34,7 @@ defmodule VachanWeb.SenderProfileLive.Index do
end

defp apply_action(socket, :new, params) do

Check warning on line 36 in lib/vachan_web/live/sender_profile_live/index.ex

View workflow job for this annotation

GitHub Actions / test

variable "params" is unused (if the variable is not meant to be used, prefix it with an underscore)
smtp_password = Map.get(params, "password")

hashed_password = hash_password(smtp_password)
IO.inspect(hashed_password, label: "Hashed Password - New")

sender_profile = %SenderProfile{
password: hashed_password
}

IO.inspect(sender_profile, label: "New Sender Profile")

sender_profile = %SenderProfile{}
socket
|> assign(:page_title, "New Sender Profile")
|> assign(:sender_profile, sender_profile)
Expand All @@ -81,13 +60,4 @@ defmodule VachanWeb.SenderProfileLive.Index do
String.contains?(String.capitalize(sender_profile.title), capitalized_query)
end)
end

defp hash_password(password) do
IO.inspect(password, label: "Hashing Password")
Bcrypt.hash_pwd_salt(password)
end

def verify_password(plain_password, hashed_password) do
Bcrypt.verify_pass(plain_password, hashed_password)
end
end
1 change: 0 additions & 1 deletion lib/vachan_web/live/sender_profile_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<:col :let={{_id, sender_profile}} label="Sender's Email"><%= sender_profile.email %></:col>

<:col :let={{_id, sender_profile}} label="SMTP Username"><%= sender_profile.username %></:col>
<:col :let={{_id, sender_profile}} label="SMTP Password"><%= sender_profile.password %></:col>
<:col :let={{_id, sender_profile}} label="SMTP Hostname"><%= sender_profile.smtp_host %></:col>
<:col :let={{_id, sender_profile}} label="SMTP Port"><%= sender_profile.smtp_port %></:col>
<:action :let={{_id, sender_profile}}>
Expand Down
1 change: 0 additions & 1 deletion lib/vachan_web/live/sender_profile_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<:item title="Sender's Email"><%= @sender_profile.email %></:item>

<:item title="SMTP Username"><%= @sender_profile.username %></:item>
<:item title="SMTP Password"><%= @sender_profile.password %></:item>
<:item title="SMTP Hostname"><%= @sender_profile.smtp_host %></:item>
<:item title="SMTP Port"><%= @sender_profile.smtp_port %></:item>
</.list>
Expand Down

0 comments on commit 80f201d

Please sign in to comment.