diff --git a/lib/compass_admin/application.ex b/lib/compass_admin/application.ex index 2205d41..b85e079 100644 --- a/lib/compass_admin/application.ex +++ b/lib/compass_admin/application.ex @@ -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, diff --git a/lib/compass_admin_web/plugs/verify_admin_plug.ex b/lib/compass_admin_web/plugs/verify_admin_plug.ex index 16f58bc..da88d55 100644 --- a/lib/compass_admin_web/plugs/verify_admin_plug.ex +++ b/lib/compass_admin_web/plugs/verify_admin_plug.ex @@ -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 @@ -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()