Skip to content

Instantly share code, notes, and snippets.

@pkoch
Created May 3, 2017 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkoch/fc3453d40683b94d5698e5fe4306f269 to your computer and use it in GitHub Desktop.
Save pkoch/fc3453d40683b94d5698e5fe4306f269 to your computer and use it in GitHub Desktop.
diff --git a/apps/artbay_api/test/controllers/auth_controller_test.exs b/apps/artbay_api/test/controllers/auth_controller_test.exs
index 9f82660..b39bd1a 100644
--- a/apps/artbay_api/test/controllers/auth_controller_test.exs
+++ b/apps/artbay_api/test/controllers/auth_controller_test.exs
@@ -29,7 +29,7 @@ defmodule ArtbayAPI.AuthControllerTest do
end
test "logins user with valid attributes", %{conn: conn} do
- {:ok, _user} = Hermitage.Services.UserRegistry.register_user(@post_attrs)
+ {:ok, %User{}} = Hermitage.Services.UserRegistry.register_user(@post_attrs)
conn = post conn, auth_path(conn, :auth, :identity), @post_attrs
assert %{"status" => "ok", "token" => _} = json_response(conn, 200)
end
diff --git a/apps/hermitage/config/test.exs b/apps/hermitage/config/test.exs
index 75e580a..3b27fd9 100644
--- a/apps/hermitage/config/test.exs
+++ b/apps/hermitage/config/test.exs
@@ -5,7 +5,4 @@ config :hermitage, Hermitage.Repo,
pool: Ecto.Adapters.SQL.Sandbox,
ownership_timeout: 60_000_000,
adapter: Ecto.Adapters.Postgres,
- username: "postgres",
- password: "postgres",
- database: "hermitage_test",
- hostname: "localhost"
+ database: "hermitage_test"
diff --git a/apps/hermitage/lib/hermitage/services/user_registry.ex b/apps/hermitage/lib/hermitage/services/user_registry.ex
index 761b36d..8c20745 100644
--- a/apps/hermitage/lib/hermitage/services/user_registry.ex
+++ b/apps/hermitage/lib/hermitage/services/user_registry.ex
@@ -23,29 +23,23 @@ defmodule Hermitage.Services.UserRegistry do
end
def register_user(params) do
- case validate_params_for_registration(params) do
- :ok ->
- case create_user_with_authorization(params) do
- {:ok, response} -> response
- {:error, reason} -> {:error, reason}
- end
- {:error, reason} -> {:error, reason}
+ with :ok <- validate_params_for_registration(params),
+ {:ok, %User{} = user} <- create_user_with_authorization(params) do
+ {:ok, user}
end
end
def login_user(params) do
- case validate_params_for_login(params) do
- {:ok, changeset} ->
- case get_authorization_from_user_changeset(changeset) do
- {:error, reason} -> {:error, reason}
- authorization ->
- if authorization.expires_at && authorization.expires_at < Guardian.Utils.timestamp do
- replace_authorization(authorization, params)
- else
- get_user_from_authorization(authorization)
- end
- end
- {:error, reason} -> {:error, reason}
+ with {:ok, %Ecto.Changeset{} = changeset} <-
+ validate_params_for_login(params),
+ {:ok, authorization} <-
+ get_authorization_from_user_changeset(changeset) do
+ if(
+ authorization.expires_at &&
+ authorization.expires_at < Guardian.Utils.timestamp,
+ do: replace_authorization(authorization, params),
+ else: get_user_from_authorization(authorization)
+ )
end
end
@@ -70,25 +64,20 @@ defmodule Hermitage.Services.UserRegistry do
end
defp create_user_with_authorization(params) do
- case Repo.transaction(fn ->
+ Repo.transaction(fn ->
user = case get_user_by(email: params["email"]) do
%User{} = user -> user
- nil ->
- case create_passwordless_user(params) do
- {:ok, user} -> user
- {:error, reason} -> Repo.rollback(reason)
- end
+ nil -> case create_passwordless_user(params) do
+ {:ok, user} -> user
+ {:error, reason} -> Repo.rollback(reason)
+ end
end
case create_authorization_for_user(user, params) do
{:error, reason} -> Repo.rollback(reason)
- authorization -> {:ok, user}
+ {:ok, %Authorization{}} -> user
end
-
- end) do
- {:ok, user} -> {:ok, user}
- {:error, reason} -> {:error, reason}
- end
+ end)
end
# AUTHORIZATIONS
@@ -100,7 +89,7 @@ defmodule Hermitage.Services.UserRegistry do
end
def create_authorization_for_user(user, params) do
- result = create_authorization(
+ create_authorization(
%{
user_id: user.id,
provider: "identity",
@@ -110,22 +99,12 @@ defmodule Hermitage.Services.UserRegistry do
expires_at: params["expires_at"]
}
)
-
- case result do
- {:ok, authorization} -> authorization
- {:error, reason} -> {:error, reason}
- end
end
def delete_authorization(%Authorization{} = authorization) do
Repo.delete(authorization)
end
- def get_authorization_by(query_params) do
- Authorization
- |> Repo.get_by(query_params)
- end
-
defp authorization_changeset(%Authorization{} = authorization, params) do
authorization
|> cast(params, [:provider, :uid, :user_id, :token, :refresh_token, :expires_at])
@@ -138,23 +117,25 @@ defmodule Hermitage.Services.UserRegistry do
defp get_authorization_from_user_changeset(changeset) do
email = Ecto.Changeset.get_field(changeset, :email)
password = Ecto.Changeset.get_field(changeset, :password)
- case get_authorization_by(uid: email, provider: "identity") do
- nil ->
- changeset = Ecto.Changeset.add_error(changeset, :email, "Username not found")
- {:error, changeset}
- authorization ->
- case password do
- pass when is_binary(pass) ->
- if Comeonin.Bcrypt.checkpw(pass, authorization.token) do
- authorization
- else
- changeset = Ecto.Changeset.add_error(changeset, :password, "Password doesn't match'")
- {:error, changeset}
- end
- end
+ try do
+ {:ok, get_authorization(email, password)}
+ catch
+ {:error, {field, message}} ->
+ {:error, Ecto.Changeset.add_error(changeset, field, message)}
end
end
+ defp get_authorization(email, password) when is_binary(password) do
+ auth = Repo.get_by(Authorization, uid: email, provider: "identity")
+ auth != nil or throw {:error, {:email, "Username not found"}}
+
+ if !Comeonin.Bcrypt.checkpw(password, auth.token) do
+ throw {:error, {:password, "Password doesn't match"}}
+ end
+
+ auth
+ end
+
defp get_user_from_authorization(authorization) do
case Repo.one(Ecto.assoc(authorization, :user)) do
nil -> {:error, :user_not_found}
@@ -163,20 +144,12 @@ defmodule Hermitage.Services.UserRegistry do
end
def replace_authorization(authorization, params) do
- case get_user_from_authorization(authorization) do
- {:ok, user} ->
- case Repo.transaction(fn ->
+ with {:ok, %User{} = user} <- get_user_from_authorization(authorization),
+ {:ok, %Authorization{}} <- Repo.transaction(fn ->
delete_authorization(authorization)
- case create_authorization_for_user(user, params) do
- {:error, reason} -> Repo.rollback(reason)
- result -> result
- end
- {:ok, user}
+ create_authorization_for_user(user, params)
end) do
- {:ok, user} -> {:ok, user}
- {:error, reason} -> {:error, reason}
- end
- {:error, reason} -> {:error, reason}
+ {:ok, user}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment