Skip to content

Instantly share code, notes, and snippets.

@mprymek
Last active November 25, 2015 21:29
Show Gist options
  • Save mprymek/0fe6bb614c59e9b788fd to your computer and use it in GitHub Desktop.
Save mprymek/0fe6bb614c59e9b788fd to your computer and use it in GitHub Desktop.

Revisions

  1. mprymek renamed this gist Aug 5, 2014. 1 changed file with 10 additions and 10 deletions.
    20 changes: 10 additions & 10 deletions elixir → auth_plug.ex
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    Simple Phoenix authentication plug
    # Simple Phoenix authentication plug
    #
    # - based on Plug's session store
    # - redirects unauthenticated requests to login page "/login/<request url>"
    # - /static/... requests are not authenticated
    # - authentication is valid as long as session is valid (you can change this behaviour easily)

    - based on Plug's session store
    - redirects unauthenticated requests to login page "/login/<request url>"
    - /static/... requests are not authenticated
    - authentication is valid as long as session is valid (you can change this behaviour easily)

    Because we need session to be fetched BEFORE this plug, we must put this to router.ex:
    # Because we need session to be fetched BEFORE this plug, we must put this to router.ex:

    #----------------------------
    # copied from https://github.com/phoenixframework/phoenix/blob/master/lib/phoenix/router.ex
    @@ -19,7 +19,7 @@ Because we need session to be fetched BEFORE this plug, we must put this to rout
    # here put your other plugs...
    #----------------------------

    Auth plug code:
    # Auth plug code:

    #----------------------------
    defmodule WebApp.Plugs.Auth do
    @@ -57,7 +57,7 @@ defmodule WebApp.Plugs.Auth do
    end
    #----------------------------

    Login page in router.ex:
    # Login page in router.ex:

    #----------------------------
    get "/login/*src", WebApp.LoginController, :login
    @@ -66,7 +66,7 @@ Login page in router.ex:
    #----------------------------


    Login page controller:
    # Login page controller:

    #----------------------------
    defmodule WebApp.LoginController do
  2. mprymek renamed this gist Aug 5, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. mprymek revised this gist Aug 5, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -72,12 +72,12 @@ Login page controller:
    defmodule WebApp.LoginController do
    use Phoenix.Controller

    def login(conn, _) do
    def login(conn, _, messages\\[]) do
    if WebApp.Plugs.Auth.authenticated?(conn) do
    # already logged in
    redirect conn, "/#{conn.params["src"]}"
    else
    render conn, "login"
    render conn, "login", [messages: messages]
    end
    end

  4. mprymek created this gist Aug 5, 2014.
    105 changes: 105 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,105 @@
    Simple Phoenix authentication plug

    - based on Plug's session store
    - redirects unauthenticated requests to login page "/login/<request url>"
    - /static/... requests are not authenticated
    - authentication is valid as long as session is valid (you can change this behaviour easily)

    Because we need session to be fetched BEFORE this plug, we must put this to router.ex:

    #----------------------------
    # copied from https://github.com/phoenixframework/phoenix/blob/master/lib/phoenix/router.ex
    key = Phoenix.Config.router!(__MODULE__, [:session_key])
    secret = Phoenix.Config.router!(__MODULE__, [:session_secret])
    plug Plug.Session, store: :cookie, key: key, secret: secret
    plug Phoenix.Plugs.SessionFetcher

    # auth plug now
    plug WebApp.Plugs.Auth, []
    # here put your other plugs...
    #----------------------------

    Auth plug code:

    #----------------------------
    defmodule WebApp.Plugs.Auth do
    require Logger

    def init(opts), do: opts

    def call(conn, _opts) do
    cond do
    skip?(conn.path_info) ->
    conn
    authenticated?(conn) ->
    conn
    true ->
    Logger.debug "AUTH for #{inspect conn.path_info}"
    url = "/#{Enum.join(conn.path_info,"/")}"
    Phoenix.Controller.Connection.redirect(conn,"/login#{url}")
    Phoenix.Controller.Connection.halt!(conn)
    end
    end

    defp skip?(["static"|_]), do: true
    defp skip?(["login"|_]), do: true
    #
    # you can insert any unauthenticated pages here...
    #
    defp skip?(_), do: false

    def authenticated?(conn) do
    # BEWARE!
    # User is authenticated as long as session is valid!
    # You probably want to check if user still exists and is authorized here.
    Plug.Conn.get_session(conn, :user) != nil
    end
    end
    #----------------------------

    Login page in router.ex:

    #----------------------------
    get "/login/*src", WebApp.LoginController, :login
    post "/login/*src", WebApp.LoginController, :login_post
    get "/logout", WebApp.LoginController, :logout
    #----------------------------


    Login page controller:

    #----------------------------
    defmodule WebApp.LoginController do
    use Phoenix.Controller

    def login(conn, _) do
    if WebApp.Plugs.Auth.authenticated?(conn) do
    # already logged in
    redirect conn, "/#{conn.params["src"]}"
    else
    render conn, "login"
    end
    end

    def login_post(conn, _) do
    passwd = conn.params["password"]
    user = conn.params["username"]
    case WebApp.Auth.auth(user,passwd) do
    nil ->
    login(conn,[{:error,"Login failed."}])
    group ->
    conn = conn
    |> Plug.Conn.put_session(:user, String.to_atom(user))
    |> Plug.Conn.put_session(:group, group)
    redirect conn, "/#{conn.params["src"]}"
    end
    end

    def logout(conn, _) do
    conn = conn
    |> Plug.Conn.delete_session(:user)
    |> Plug.Conn.delete_session(:group)
    redirect conn, "/login"
    end
    end
    #----------------------------