Skip to content

Commit

Permalink
Added Authenticate.Remember module so that Remember module can be mor…
Browse files Browse the repository at this point in the history
…e easily customized
  • Loading branch information
riverrun committed Jul 7, 2020
1 parent bffa4c3 commit 6518c3f
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 257 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Phauxth Changelog

## Version 2.4.0

* Enhancements
* added Authenticate.Remember module so that the Remember module can be more easily customized
* updated dependencies

## Version 2.3.2

* Bug fixes
Expand Down
12 changes: 0 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ and well-documented.
For a general overview of some of the goals of Phauxth and its basic usage,
see [this post](https://riverrun.github.io/projects/phauxth/2017/09/25/phauxth.html).

## Upgrading from version 2.0 to 2.1

In version 2.1 and above, you need to remove all the references to Comeonin.
Make the following changes in your app (if necessary, replacing Argon2
with the hashing module you are using):

* in the user context module, change `Comeonin.Argon2.add_hash` to `Argon2.add_hash`
* if you are using a custom login module, remove `alias Comeonin.Argon2`
* in the Phauxth config, replace `Comeonin.Argon2` with `Argon2`

For more details, see the [upgrade guide](https://github.com/riverrun/phauxth/blob/master/UPGRADE_v2.1.md).

## Getting started

[This guide](https://github.com/riverrun/phauxth/wiki/Getting-started)
Expand Down
167 changes: 0 additions & 167 deletions UPGRADE_v2.1.md

This file was deleted.

75 changes: 75 additions & 0 deletions lib/phauxth/authenticate/remember.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
defmodule Phauxth.Authenticate.Remember do
@moduledoc """
Base module for remember me functionality.
This is `use`-d by Phauxth.Remember, and it can also be used
to produce a custom remember me module.
"""

defmacro __using__(_) do
quote do
use Phauxth.Authenticate.Base

alias Phauxth.Config

@max_age 7 * 24 * 60 * 60

@impl Plug
def init(opts) do
create_session_func =
opts[:create_session_func] ||
raise """
Phauxth.Remember - you need to set a `create_session_func` in the opts
"""

unless is_function(create_session_func, 1) do
raise """
Phauxth.Remember - the `create_session_func` should be a function
that takes one argument
"""
end

opts =
opts
|> Keyword.put_new(:max_age, 14_400)
|> Keyword.put_new(:token_module, Config.token_module())
|> super()

{create_session_func, opts}
end

@impl Plug
def call(%Plug.Conn{assigns: %{current_user: %{}}} = conn, _), do: conn

def call(%Plug.Conn{req_cookies: %{"remember_me" => _}} = conn, {create_session_func, opts}) do
conn |> super(opts) |> add_session(create_session_func)
end

def call(conn, _), do: conn

@impl Phauxth.Authenticate.Base
def authenticate(%Plug.Conn{req_cookies: %{"remember_me" => token}}, user_context, opts) do
token_module = opts[:token_module]

with {:ok, user_id} <- token_module.verify(token, opts),
do: get_user({:ok, %{"user_id" => user_id}}, user_context)
end

@impl Phauxth.Authenticate.Base
def set_user(nil, conn), do: super(nil, delete_rem_cookie(conn))
def set_user(user, conn), do: super(user, conn)

defp add_session(%Plug.Conn{assigns: %{current_user: %{}}} = conn, create_session_func) do
{:ok, %{id: session_id}} = create_session_func.(conn)

conn
|> put_session(:phauxth_session_id, session_id)
|> configure_session(renew: true)
end

defp add_session(conn, _), do: conn

defoverridable Phauxth.Authenticate.Base
end
end
end
64 changes: 4 additions & 60 deletions lib/phauxth/remember.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ defmodule Phauxth.Remember do
This module also contains functions to add / delete the `remember_me`
cookie.
If you want to customize this Plug in any way, see the documentation for
Phauxth.Authenticate.Remember.
## Configuration / setup
Add the `user_context` module (the module you are using to handle
Expand Down Expand Up @@ -51,66 +54,7 @@ defmodule Phauxth.Remember do
Make sure you add the Phauxth.Remember Plug after Phauxth.Authenticate.
"""

use Phauxth.Authenticate.Base

alias Phauxth.Config

@max_age 7 * 24 * 60 * 60

@impl Plug
def init(opts) do
create_session_func =
opts[:create_session_func] ||
raise """
Phauxth.Remember - you need to set a `create_session_func` in the opts
"""

unless is_function(create_session_func, 1) do
raise """
Phauxth.Remember - the `create_session_func` should be a function
that takes one argument
"""
end

opts =
opts
|> Keyword.put_new(:max_age, 14_400)
|> Keyword.put_new(:token_module, Config.token_module())
|> super()

{create_session_func, opts}
end

@impl Plug
def call(%Plug.Conn{assigns: %{current_user: %{}}} = conn, _), do: conn

def call(%Plug.Conn{req_cookies: %{"remember_me" => _}} = conn, {create_session_func, opts}) do
conn |> super(opts) |> add_session(create_session_func)
end

def call(conn, _), do: conn

@impl Phauxth.Authenticate.Base
def authenticate(%Plug.Conn{req_cookies: %{"remember_me" => token}}, user_context, opts) do
token_module = opts[:token_module]

with {:ok, user_id} <- token_module.verify(token, opts),
do: get_user({:ok, %{"user_id" => user_id}}, user_context)
end

@impl Phauxth.Authenticate.Base
def set_user(nil, conn), do: super(nil, delete_rem_cookie(conn))
def set_user(user, conn), do: super(user, conn)

defp add_session(%Plug.Conn{assigns: %{current_user: %{}}} = conn, create_session_func) do
{:ok, %{id: session_id}} = create_session_func.(conn)

conn
|> put_session(:phauxth_session_id, session_id)
|> configure_session(renew: true)
end

defp add_session(conn, _), do: conn
use Phauxth.Authenticate.Remember

@doc """
Adds a remember me cookie to the conn.
Expand Down
10 changes: 5 additions & 5 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Phauxth.Mixfile do
use Mix.Project

@version "2.3.2"
@version "2.4.0"

@description """
Authentication library for Phoenix, and other Plug-based, web applications
Expand Down Expand Up @@ -29,10 +29,10 @@ defmodule Phauxth.Mixfile do

defp deps do
[
{:plug, "~> 1.7"},
{:argon2_elixir, "~> 2.0", optional: true},
{:ex_doc, "~> 0.20", only: :dev, runtime: false},
{:dialyxir, "~> 1.0.0-rc.3", only: :dev, runtime: false}
{:plug, "~> 1.10"},
{:argon2_elixir, "~> 2.3", optional: true},
{:ex_doc, "~> 0.22", only: :dev, runtime: false},
{:dialyxir, "~> 1.0.0", only: :dev, runtime: false}
]
end

Expand Down
Loading

0 comments on commit 6518c3f

Please sign in to comment.