-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from cciuenf/feature/authentication
feature/authentication
- Loading branch information
Showing
46 changed files
with
1,521 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule Fuschia.Context.ApiKeys do | ||
@moduledoc """ | ||
Public Fuschia ApiKeys API | ||
""" | ||
|
||
import Ecto.Query | ||
|
||
alias Fuschia.Entities.ApiKey | ||
alias Fuschia.Repo | ||
|
||
@spec one :: %ApiKey{} | ||
def one do | ||
query() | ||
|> Repo.one() | ||
end | ||
|
||
@spec one_by_key(String.t()) :: %ApiKey{} | nil | ||
def one_by_key(key) do | ||
query() | ||
|> Repo.get_by(key: key) | ||
end | ||
|
||
@spec query :: Ecto.Query.t() | ||
def query do | ||
from a in ApiKey, | ||
where: a.active == true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule Fuschia.Context.AuthLogs do | ||
@moduledoc """ | ||
Public Fuschia Authentication Logs API | ||
""" | ||
|
||
alias Fuschia.Entities.{AuthLog, User} | ||
alias Fuschia.Repo | ||
|
||
@spec create(map) :: :ok | ||
def create(attrs) do | ||
%AuthLog{} | ||
|> AuthLog.changeset(attrs) | ||
|> Repo.insert() | ||
|
||
:ok | ||
end | ||
|
||
@spec create(String.t(), String.t(), %User{}) :: :ok | ||
def create(ip, user_agent, user) do | ||
create(%{"ip" => ip, "user_agent" => user_agent, "user_id" => user.id}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule Fuschia.Entities.ApiKey do | ||
@moduledoc """ | ||
API Key Schema | ||
""" | ||
|
||
use Fuschia.Schema | ||
|
||
schema "api_key" do | ||
field :key, Ecto.UUID | ||
field :description, :string | ||
field :active, :boolean | ||
|
||
timestamps() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
defmodule Fuschia.Entities.AuthLog do | ||
@moduledoc """ | ||
Authentication log | ||
""" | ||
|
||
use Fuschia.Schema | ||
|
||
import Ecto.Changeset | ||
|
||
alias Fuschia.Entities.User | ||
alias Fuschia.Types.TrimmedString | ||
|
||
@required_fields ~w(ip user_agent user_id)a | ||
|
||
schema "auth_log" do | ||
field :ip, TrimmedString | ||
field :user_agent, TrimmedString | ||
|
||
belongs_to :user, User | ||
|
||
timestamps(updated_at: false) | ||
end | ||
|
||
def changeset(%__MODULE__{} = struct, attrs) do | ||
struct | ||
|> cast(attrs, @required_fields) | ||
|> validate_required(@required_fields) | ||
|> foreign_key_constraint(:user_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule FuschiaWeb.Auth.ErrorHandler do | ||
@moduledoc """ | ||
Error Handler for Fuschia Auth | ||
""" | ||
|
||
import Plug.Conn, only: [send_resp: 3] | ||
|
||
@behaviour Guardian.Plug.ErrorHandler | ||
|
||
def auth_error(conn, {type, _reason}, _opts) do | ||
body = Jason.encode!(%{message: to_string(type)}) | ||
|
||
send_resp(conn, :unauthorized, body) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
defmodule FuschiaWeb.Auth.Guardian do | ||
@moduledoc """ | ||
Guardian serializer for Fuschia app | ||
""" | ||
|
||
use Guardian, otp_app: :fuschia | ||
|
||
alias Fuschia.Context.Users | ||
alias Fuschia.Entities.User | ||
|
||
def subject_for_token(user, _claims) do | ||
sub = to_string(user.id) | ||
|
||
{:ok, sub} | ||
end | ||
|
||
def resource_from_claims(claims) do | ||
user = | ||
claims | ||
|> Map.get("sub") | ||
|> Users.one() | ||
|
||
{:ok, user} | ||
end | ||
|
||
def authenticate(%{"email" => email, "password" => password}) do | ||
case Users.one_by_email(email) do | ||
nil -> {:error, :unauthorized} | ||
user -> validate_password(user, password) | ||
end | ||
end | ||
|
||
def user_claims(%{"email" => email}) do | ||
case user = Users.one_with_permissions(email) do | ||
nil -> {:error, :unauthorized} | ||
_ -> {:ok, User.for_jwt(user)} | ||
end | ||
end | ||
|
||
def create_token(user) do | ||
{:ok, token, _claims} = encode_and_sign(user) | ||
|
||
{:ok, token} | ||
end | ||
|
||
defp validate_password(_user, ""), do: {:error, :unauthorized} | ||
|
||
defp validate_password(user, password) when is_binary(password) do | ||
with %User{password_hash: password_hash} = user <- user, | ||
false <- is_nil(password_hash), | ||
true <- Bcrypt.verify_pass(password, user.password_hash) do | ||
create_token(user) | ||
else | ||
_ -> {:error, :unauthorized} | ||
end | ||
end | ||
|
||
defp validate_password(_user, _password), do: {:error, :unauthorized} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule FuschiaWeb.Auth.Pipeline do | ||
@moduledoc """ | ||
Auth Pipeline for Fuschia app | ||
""" | ||
|
||
use Guardian.Plug.Pipeline, otp_app: :fuschia | ||
|
||
plug Guardian.Plug.VerifyHeader | ||
plug Guardian.Plug.EnsureAuthenticated | ||
plug Guardian.Plug.LoadResource | ||
end |
Oops, something went wrong.