AuthShield v0.0.4 AuthShield View Source

AuthShield is an simple implementation that was created to be used with other frameworks (as Phoenix) or applications in order to provide an simple authentication and authorization management to the services.

Installation

AuthShield is published on Hex. Add {:auth_shield, "~> 0.0.4"} to your list of dependencies in mix.exs.

Then run mix deps.get to install AuthShield and its dependencies, including Ecto, Plug and Argon2.

After the packages are installed you must configure your database and generates an migration to add the AuthShield tables to it.

On your config.exs set the configuration bellow:

# This is the default auth_shield database configuration
# but its highly recomendate that you configure it to be in
# the same database if you want to extend the identity to
# your on custom tables.
config :auth_shield, ecto_repos: [AuthShield.Repo]

config :auth_shield, AuthShield.Repo,
  database: "authshield_dev",
  username: "postgres",
  password: "postgres",
  hostname: "localhost",
  port: 5432

# You can set the session expiration and block attempts by changing this config
# All timestamps are in seconds.
config :auth_shield, AuthShield,
  session_expiration: 60 * 15,
  max_login_attempts: 10,
  login_block_time: 60 * 15,
  brute_force_login_interval: 1,
  brute_force_login_attempts: 5

In your test.exs use the configuration bellow to run it in sandbox mode:

config :auth_shield, AuthShield.Repo, pool: Ecto.Adapters.SQL.Sandbox

After you finish the configurations use mix ecto.gen.migration create_auth_shield_tables to generate the migration that will be use on database and tables criation.

Go to the generated migration and call the AuthShield up and down migration functions as the exemple bellow:

defmodule AuthShield.Repo.Migrations.CreateAuthShieldTables do
  use Ecto.Migration

  def up do
    AuthShield.Migrations.up()
  end

  def down do
    AuthShield.Migrations.down()
  end
end

Create the database database (if its not created yet) by using mix ecto.migrate and then run the migrations with mix ecto.migrate.

Link to this section Summary

Types

Session options used on authentication plug

Functions

Login the user by its password credential.

Login the user by its password credential.

Logout the authenticated user session by a given session or session_id.

Refresh the authenticated user session by a given session or session_id

Creates a new user on the system.

Link to this section Types

Link to this type

session_options()

View Source
session_options() :: [user_agent: String.t(), remote_ip: String.t()]

Session options used on authentication plug

Link to this section Functions

Link to this function

login(conn)

View Source
login(conn :: Plug.Conn.t()) ::
  {:ok, AuthShield.Authentication.Schemas.Session.t()}
  | {:error, :unauthenticated | Ecto.Changeset.t()}

Login the user by its password credential.

If the user and its credential is authenticated it will return {:ok, AuthShield.Authentication.Schemas.Session.t()}.

This session should be stored and used on authentication to keep users logged.

Exemples:

  AuthShield.login(%Plug.Conn%{
    body_params: %{
      "email" => "lucas@gmail.com",
      "password" => "Mypass@rd23"
    }
  )
Link to this function

login(params, opts \\ [])

View Source
login(params :: AuthShield.Validations.Login.t(), opts :: session_options()) ::
  {:ok, AuthShield.Authentication.Schemas.Session.t()}
  | {:error, :unauthenticated | Ecto.Changeset.t()}

Login the user by its password credential.

If the user and its credential is authenticated it will return {:ok, AuthShield.Authentication.Schemas.Session.t()}.

This session should be stored and used on authentication to keep users logged.

Exemples:

  AuthShield.login(%{"email" => "lucas@gmail.com", "password" => "Mypass@rd23"})
Link to this function

logout(session)

View Source
logout(session :: AuthShield.Authentication.Schemas.Session.t() | String.t()) ::
  {:ok, AuthShield.Authentication.Schemas.Session.t()}
  | {:error, :session_not_exist}
  | {:error, Ecto.Changeset.t()}

Logout the authenticated user session by a given session or session_id.

If the user is authenticated and has an active session it will return {:ok, AuthShield.Authentication.Schemas.Session.t()}.

This session can be ignored because use is not active anymore.

Exemples:

  AuthShield.logout("ecb4c67d-6380-4984-ae04-1563e885d59e")
Link to this function

refresh_session(session)

View Source
refresh_session(
  session :: AuthShield.Authentication.Schemas.Session.t() | String.t()
) ::
  {:ok, AuthShield.Authentication.Schemas.Session.t()}
  | {:error, :session_expired}
  | {:error, :session_not_exist}
  | {:error, Ecto.Changeset.t()}

Refresh the authenticated user session by a given session or session_id

If the user is authenticated and has an active session it will return {:ok, AuthShield.Authentication.Schemas.Session.t()}.

This session should be stored and used on authentication to keep users logged.

Exemples:

  AuthShield.refresh_session(session)
  AuthShield.refresh_session("ecb4c67d-6380-4984-ae04-1563e885d59e")
Link to this function

signup(params)

View Source
signup(params :: AuthShield.Validations.SignUp.t()) ::
  {:ok, AuthShield.Resources.Schemas.User.t()}
  | {:error, map()}
  | {:error, Ecto.Changeset.t()}

Creates a new user on the system.

Exemples:

  AuthShield.signup(%{
    first_name: "Lucas",
    last_name: "Mesquita",
    email: "lucas@gmail.com",
    password: "My_passw@rd2"
  })