Implementation of a basic auth logic via API #2

Open
optclblast wants to merge 11 commits from base-auth into master
5 changed files with 158 additions and 71 deletions
Showing only changes of commit 1c38624712 - Show all commits

View File

@ -0,0 +1,8 @@
defmodule DrainCloudCore.UsersRepo do
alias DrainCloudCore.Repo, as: Repo
import Ecto.Query
def password_by_login(login) do
Repo.all(from u in :users, where: u.login = login, select: u.password, limit: 1)
end
end

View File

@ -1,17 +1,18 @@
defmodule DrainCloudCoreWeb.AuthController do
use DrainCloudCoreWeb, :controller
alias DrainCloudCoreWeb.AuthController.RegisterRequest, as: Request
alias DrainCloudCoreWeb.AuthController.RegisterRequest, as: RegisterRequest
alias DrainCloudCoreWeb.AuthController.LoginRequest, as: LoginRequest
alias :logger, as: Log
alias DrainCloudCore.Repo, as: Repo
def register(conn = %Plug.Conn{}, _params) do
req = Request.from_request(conn)
def register(conn, _params) do
req = RegisterRequest.from_request(conn)
Log.debug("new register request: #{req}")
model = Request.to_model(req)
model = RegisterRequest.to_model(req)
try do
Repo.insert(model)
@ -21,75 +22,11 @@ defmodule DrainCloudCoreWeb.AuthController do
json(conn, req)
end
end
defmodule DrainCloudCoreWeb.AuthController.RegisterRequest do
@derive [Poison.Encoder, Jason.Encoder]
def login(conn, _params) do
req = LoginRequest.from_request(conn)
defstruct login: "", password: ""
Log.debug("new login request: #{req}")
alias __MODULE__, as: Request
alias DrainCloudCore.Auth.Users, as: User
alias DrainCloudCoreWeb.Errors.InvalidArgumentException, as: InvalidArgumentException
def from_request(conn = %Plug.Conn{}) do
:logger.debug("[from_request] incoming request: #{Kernel.inspect(conn.params)}")
validate_and_build(conn.params)
end
def to_model(req = %Request{}) do
%User{
login: req.login,
password: req.password
}
end
defp validate_and_build(req) do
validate(req)
|> build
end
defp validate(req) do
validate_login(req)
|> validate_password
end
defp validate_login(req) do
if String.length(Map.get(req, "login")) >= 3 do
req
else
raise InvalidArgumentException, %{
message: "login must be 3 symbols at minimum",
args: ["login"]
}
end
end
defp validate_password(req) do
if String.length(Map.get(req, "password")) >= 8 do
req
else
raise InvalidArgumentException, %{
message: "password must be 8 symbols at minimum",
args: ["password"]
}
end
end
defp build(req) do
%Request{
login: Map.get(req, "login"),
password: Map.get(req, "password")
}
end
defimpl String.Chars, for: DrainCloudCoreWeb.AuthController.RegisterRequest do
def to_string(req) do
case Jason.encode(req) do
{:ok, str} -> str
{:error, msg} -> raise "failed to encode register request to string: #{msg}"
end
end
end
end

View File

@ -0,0 +1,71 @@
defmodule DraincloudCoreWeb.AuthController.LoginRequest do
@derive [Poison.Encoder, Jason.Encoder]
defstruct login: "", password: ""
alias __MODULE__, as: Request
alias DrainCloudCore.Auth.Users, as: User
alias DrainCloudCoreWeb.Errors.InvalidArgumentException, as: InvalidArgumentException
def from_request(conn = %Plug.Conn{}) do
# TODO remove Kernel.inspect calls
:logger.debug("[from_request] incoming request: #{Kernel.inspect(conn.params)}")
validate_and_build(conn.params)
end
def to_model(req = %Request{}) do
%User{
login: req.login,
password: req.password
}
end
defp validate_and_build(req) do
validate(req)
|> build
end
defp validate(req) do
validate_login(req)
|> validate_password
end
defp validate_login(req) do
if String.length(Map.get(req, "login")) >= 3 do
req
else
raise InvalidArgumentException, %{
message: "login must be 3 symbols at minimum",
args: ["login"]
}
end
end
defp validate_password(req) do
if String.length(Map.get(req, "password")) >= 8 do
req
else
raise InvalidArgumentException, %{
message: "password must be 8 symbols at minimum",
args: ["password"]
}
end
end
defp build(req) do
%Request{
login: Map.get(req, "login"),
password: Map.get(req, "password")
}
end
defimpl String.Chars, for: DrainCloudCoreWeb.AuthController.LoginRequest do
def to_string(req) do
case Jason.encode(req) do
{:ok, str} -> str
{:error, msg} -> raise "failed to encode login request to string: #{msg}"
end
end
end
end

View File

@ -0,0 +1,71 @@
defmodule DrainCloudCoreWeb.AuthController.RegisterRequest do
@derive [Poison.Encoder, Jason.Encoder]
defstruct login: "", password: ""
alias __MODULE__, as: Request
alias DrainCloudCore.Auth.Users, as: User
alias DrainCloudCoreWeb.Errors.InvalidArgumentException, as: InvalidArgumentException
def from_request(conn = %Plug.Conn{}) do
# TODO remove Kernel.inspect calls
:logger.debug("[from_request] incoming request: #{Kernel.inspect(conn.params)}")
validate_and_build(conn.params)
end
def to_model(req = %Request{}) do
%User{
login: req.login,
password: req.password
}
end
defp validate_and_build(req) do
validate(req)
|> build
end
defp validate(req) do
validate_login(req)
|> validate_password
end
defp validate_login(req) do
if String.length(Map.get(req, "login")) >= 3 do
req
else
raise InvalidArgumentException, %{
message: "login must be 3 symbols at minimum",
args: ["login"]
}
end
end
defp validate_password(req) do
if String.length(Map.get(req, "password")) >= 8 do
req
else
raise InvalidArgumentException, %{
message: "password must be 8 symbols at minimum",
args: ["password"]
}
end
end
defp build(req) do
%Request{
login: Map.get(req, "login"),
password: Map.get(req, "password")
}
end
defimpl String.Chars, for: DrainCloudCoreWeb.AuthController.RegisterRequest do
def to_string(req) do
case Jason.encode(req) do
{:ok, str} -> str
{:error, msg} -> raise "failed to encode register request to string: #{msg}"
end
end
end
end