-
Notifications
You must be signed in to change notification settings - Fork 0
User authentication guardian #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dzhurey
wants to merge
9
commits into
master
Choose a base branch
from
user-authentication-guardian
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f3f5126
add bodyguard for authorization
72c67af
user authentication use guardian
6dafdb7
success test session controller
c6df2bd
remove bodyguard
acac907
Merge branch 'master' of github.com:awcodify/phoaw into user-authenti…
380d4c6
unit test unathenticated and fixing credo violation
8036804
remove ignored params
b0ec040
add method get_user_by_username and add space on method
137fcb2
remove unused alias
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 Phoaw.Auth.Auth do | ||
| @moduledoc """ | ||
| This module define Guardian authenticate user | ||
| """ | ||
|
|
||
| alias Comeonin.Bcrypt | ||
| alias Phoaw.Contents | ||
|
|
||
| def authenticate_user(username, plain_text_password) do | ||
| user = Contents.get_user_by_username!(username) | ||
| user |> check_password(plain_text_password) | ||
| end | ||
|
|
||
| defp check_password(nil, _), do: {:error, "Incorrect username or password"} | ||
|
|
||
| defp check_password(user, plain_text_password) do | ||
| case Bcrypt.checkpw(plain_text_password, user.password_digest) do | ||
| true -> {:ok, user} | ||
| false -> {:error, "Incorrect username or password"} | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,13 @@ | ||
| defmodule Phoaw.Auth.ErrorHandler do | ||
| @moduledoc """ | ||
| This module handle response for error authentication using Guardian | ||
| """ | ||
|
|
||
| import Plug.Conn | ||
| def auth_error(conn, {type, _reason}, _opts) do | ||
| body = to_string(type) | ||
| conn | ||
| |> put_resp_content_type("application/html") | ||
| |> send_resp(401, body) | ||
| end | ||
| end |
This file contains hidden or 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,26 @@ | ||
| defmodule Phoaw.Auth.Guardian do | ||
| @moduledoc false | ||
|
|
||
| use Guardian, otp_app: :phoaw | ||
|
|
||
| alias Phoaw.Contents | ||
|
|
||
| def subject_for_token(user, _claims) do | ||
| sub = to_string(user.id) | ||
| {:ok, sub} | ||
| end | ||
|
|
||
| def subject_for_token do | ||
| {:error, :reason_for_error} | ||
| end | ||
|
|
||
| def resource_from_claims(claims) do | ||
| id = claims["sub"] | ||
| user = Contents.get_user!(id) | ||
| {:ok, user} | ||
| end | ||
|
|
||
| def resource_from_claims do | ||
| {:error, :reason_for_error} | ||
| end | ||
| end |
This file contains hidden or 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,16 @@ | ||
| defmodule Phoaw.Auth.Pipeline do | ||
| @moduledoc """ | ||
| This module define pipeline used for Guardian authentication | ||
| """ | ||
|
|
||
| use Guardian.Plug.Pipeline, | ||
| otp_app: :phoaw, | ||
| error_handler: Phoaw.Auth.ErrorHandler, | ||
| module: Phoaw.Auth.Guardian | ||
| # If there is a session token, validate it | ||
| plug Guardian.Plug.VerifySession, claims: %{"typ" => "access"} | ||
| # If there is an authorization header, validate it | ||
| plug Guardian.Plug.VerifyHeader, claims: %{"typ" => "access"} | ||
| # Load the user if either of the verifications worked | ||
| plug Guardian.Plug.LoadResource, allow_blank: true | ||
| end |
This file contains hidden or 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 hidden or 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,37 @@ | ||
| defmodule PhoawWeb.SessionController do | ||
| use PhoawWeb, :controller | ||
|
|
||
| alias Phoaw.Contents | ||
| alias Phoaw.Contents.User | ||
| alias Phoaw.Auth.Auth | ||
| alias Phoaw.Auth.Guardian | ||
|
|
||
| def new(conn, _params) do | ||
| changeset = Contents.change_user(%User{}) | ||
| render(conn, "new.html", changeset: changeset) | ||
| end | ||
|
|
||
| def create(conn, %{"user" => session_params}) do | ||
| authenticated = Auth.authenticate_user(session_params["username"], session_params["password"]) | ||
| authenticated |> login_reply(conn) | ||
| end | ||
|
|
||
| defp login_reply({:error, error}, conn) do | ||
| conn | ||
| |> put_flash(:error, error) | ||
| |> redirect(to: Routes.session_path(conn, :new)) | ||
| end | ||
|
|
||
| defp login_reply({:ok, user}, conn) do | ||
| conn | ||
| |> put_flash(:success, "Welcome back!") | ||
| |> Guardian.Plug.sign_in(user) | ||
| |> redirect(to: Routes.user_path(conn, :index)) | ||
| end | ||
|
|
||
| def logout(conn, _) do | ||
| conn | ||
| |> Guardian.Plug.sign_out() | ||
| |> redirect(to: "/") | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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,19 @@ | ||
| <%= form_for @changeset, @action, fn f -> %> | ||
| <%= if @changeset.action do %> | ||
| <div class="alert alert-danger"> | ||
| <p>Oops, something went wrong! Please check the errors below.</p> | ||
| </div> | ||
| <% end %> | ||
|
|
||
| <%= label f, :username %> | ||
| <%= text_input f, :username %> | ||
| <%= error_tag f, :username %> | ||
|
|
||
| <%= label f, :password %> | ||
| <%= password_input f, :password %> | ||
| <%= error_tag f, :password %> | ||
|
|
||
| <div> | ||
| <%= submit "Save" %> | ||
| </div> | ||
| <% end %> |
This file contains hidden or 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,3 @@ | ||
| <h1>Login</h1> | ||
|
|
||
| <%= render "form.html", Map.put(assigns, :action, Routes.session_path(@conn, :create)) %> |
This file contains hidden or 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,3 @@ | ||
| defmodule PhoawWeb.SessionView do | ||
| use PhoawWeb, :view | ||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,55 @@ | ||
| defmodule PhoawWeb.SessionControllerTest do | ||
| use PhoawWeb.ConnCase | ||
|
|
||
| alias Phoaw.Contents | ||
|
|
||
| @create_attrs %{ | ||
| email: "test@example.com", | ||
| password: "test1234", | ||
| password_confirmation: "test1234", | ||
| username: "example" | ||
| } | ||
| @login_attrs %{ | ||
| username: "example", | ||
| password: "test1234" | ||
| } | ||
| @invalid_attrs %{username: "", password: ""} | ||
|
|
||
| setup do | ||
| {:ok, user} = Contents.create_user(@create_attrs) | ||
| {:ok, conn: build_conn(), user: user} | ||
| end | ||
|
|
||
| describe "new session" do | ||
| test "render form login", %{conn: conn} do | ||
| conn = get(conn, Routes.session_path(conn, :new)) | ||
| assert html_response(conn, 200) =~ "Login" | ||
| end | ||
| end | ||
|
|
||
| describe "create session login" do | ||
| test "redirects to user list when login successful", %{conn: conn} do | ||
| conn = post(conn, Routes.session_path(conn, :create), user: @login_attrs) | ||
| assert redirected_to(conn) == Routes.user_path(conn, :index) | ||
|
|
||
| conn = get(conn, Routes.user_path(conn, :index)) | ||
| assert html_response(conn, 200) =~ "Listing Users" | ||
| end | ||
|
|
||
| test "renders errors when login failed", %{conn: conn} do | ||
| conn = post(conn, Routes.session_path(conn, :create), user: @invalid_attrs) | ||
| assert redirected_to(conn) == Routes.session_path(conn, :new) | ||
| end | ||
| end | ||
|
|
||
| describe "Log out user" do | ||
| test "redirects to home page when logout successful", %{conn: conn} do | ||
| conn = post(conn, Routes.session_path(conn, :create), user: @login_attrs) | ||
| assert redirected_to(conn) == Routes.user_path(conn, :index) | ||
|
|
||
| conn = post(conn, Routes.session_path(conn, :logout)) | ||
| assert conn.status == 302 | ||
| assert redirected_to(conn) == "/" | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why skip this files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't use this file yet, so we didn't have test for this file