Skip to content

Commit

Permalink
Finish replate redis with riak.
Browse files Browse the repository at this point in the history
Signed-off-by: Edmondfrank <[email protected]>
  • Loading branch information
EdmondFrank committed Oct 21, 2024
1 parent cbd8d3a commit 1bd464f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
2 changes: 0 additions & 2 deletions lib/compass_admin/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ defmodule CompassAdmin.Application do
CompassAdminWeb.Endpoint,
# Start Riak
:poolboy.child_spec(:riak_pool, riak_config(), [riak_host, riak_port]),
# Start Redix
{Redix, {System.get_env("REDIS_URL") || redis_url, [name: :redix, backoff_max: 2_000, timeout: 2_000], }},
{Redlock,
[
pool_size: 2,
Expand Down
32 changes: 16 additions & 16 deletions lib/compass_admin_web/plugs/verify_admin_plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ defmodule CompassAdminWeb.Plugs.VerifyAdminPlug do
import Plug.Conn

alias CompassAdmin.User
alias CompassAdmin.RiakPool

@bucket "sessions"

def init(options), do: options

Expand All @@ -11,22 +14,19 @@ defmodule CompassAdminWeb.Plugs.VerifyAdminPlug do

defp verify_access!(conn, _opts) do
session = conn.cookies["session"]

case Redix.command(:redix, ["GET", "https://#{conn.host}:session:#{session}"]) do
{:ok, session_data} when session_data != nil ->
%{"warden.user.user.key" => [[id], parted_crypted_pass]} = ExMarshal.decode(session_data)
user = User.find(id)

if user && String.slice(user.encrypted_password, 0..28) == parted_crypted_pass &&
user.role_level > User.normal_role() do
conn
|> put_session(:current_user, user)
else
conn
|> auth_error()
|> halt()
end

with session_data <- Riak.find(RiakPool.conn, @bucket, "https://#{conn.host}:session:#{session}"),
true <- session_data != nil,
%{
"expiry" => expiry,
"timestamp" => timestamp,
"data" => %{ "warden.user.user.key" => [[id], parted_crypted_pass] }
} <- session_data.data |> Jason.decode!() |> Jason.decode!(),
true <- (timestamp + expiry) > Timex.to_unix(Timex.now),
user <- User.find(id),
true <- (user && String.slice(user.encrypted_password, 0..28) == parted_crypted_pass) do
conn
|> put_session(:current_user, user)
else
_ ->
conn
|> auth_error()
Expand Down

0 comments on commit 1bd464f

Please sign in to comment.