Skip to content

Commit d3150b2

Browse files
committed
Added readme, added deny_all_test, add behaviour for hooks
1 parent 0861132 commit d3150b2

File tree

11 files changed

+78
-59
lines changed

11 files changed

+78
-59
lines changed

README.md

+33-24
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,49 @@
1-
[Aeacus - The holder of keys](https://en.wikipedia.org/wiki/Aeacus)
1+
[Echo - ECHO Echo echo...](https://en.wikipedia.org/wiki/Echo_(mythology))
22
======
33

4-
A simple, secure, and highly configurable Elixir identity [username | email | id | etc.]/password authentication module to use with Ecto.
4+
A simple & highly extendable, meta-notification system; Echo checks notification preferences & dispatch notifications to different adapters (ex. email, logger, analytics, sms, etc.).
55

6-
[![Build Status](https://travis-ci.org/zmoshansky/aeacus.svg)](https://travis-ci.org/zmoshansky/aeacus) [![Hex.pm](http://img.shields.io/hexpm/v/aeacus.svg)](https://hex.pm/packages/aeacus) [![Hex.pm](http://img.shields.io/hexpm/dt/aeacus.svg)](https://hex.pm/packages/aeacus) [![Github Issues](http://githubbadges.herokuapp.com/zmoshansky/aeacus/issues.svg)](https://github.com/zmoshansky/aeacus/issues) [![Pending Pull-Requests](http://githubbadges.herokuapp.com/zmoshansky/aeacus/pulls.svg)](https://github.com/zmoshansky/aeacus/pulls)
6+
[![Build Status](https://travis-ci.org/zmoshansky/echo.svg)](https://travis-ci.org/zmoshansky/echo) [![Hex.pm](http://img.shields.io/hexpm/v/echo.svg)](https://hex.pm/packages/echo) [![Hex.pm](http://img.shields.io/hexpm/dt/echo.svg)](https://hex.pm/packages/echo) [![Github Issues](http://githubbadges.herokuapp.com/zmoshansky/echo/issues.svg)](https://github.com/zmoshansky/echo/issues) [![Pending Pull-Requests](http://githubbadges.herokuapp.com/zmoshansky/echo/pulls.svg)](https://github.com/zmoshansky/echo/pulls)
77

88
#### Description ####
9-
Aeacus only performs authentication, making it well suited for integration with session storage, or a token system; like [Guardian](https://github.com/hassox/guardian). Alternatively, this could be used directly over a secure (HTTPS) connection with [HTTP Basic Auth](https://en.wikipedia.org/wiki/Basic_access_authentication); But, it is highly discouraged as there is a greater security risk, due to repeatedly sending your authentication information to the server, only relying on TLS for security.
10-
11-
#### Requirements ####
12-
Aeacus requires that you have an Ecto model that has UNIQUE(identity_field) and password_field. These fields can be configured to easily match your schema, whether it be `username`, `email`, or `pass`, `password`, `hash`, `hashed_password`, etc. Of course, the passwords must be stored using the same crypto system as Aeacus; The password should be salted and hashed, plaintext is heavily discouraged. See the tests for examples.
9+
Echo is designed to be highly adaptable to your notification needs through different adapters and per adapter hooks. A notification is dispatched with `Echo.notify/2` which then calls on each registered adapter, requesting that it delivers the notification. Each adapter is passed an `event_type`, of your designation, and `data` that it may use to deliver the notification.
1310

1411
#### Config ####
15-
You must set the `:repo` and `:model` for Aeacus. The other options have sane defaults.
12+
Echo is easible configured, a possible sample configuration is given below. Custom Preferences & Adapters are easy to build, look at `lib/echo/adapters/email` & `test/support/test_preferences.ex` for examples. Hooks allow you to specify the module which implements `@behaviour Echo.Hooks`, which generates the adapter specific data needed to deliver a notification (see `test/support/email_hook` for an example); this might include: selecting the correct template, parsing data, or even nothing at all. Doing nothing allows unknown event types to be skipped by adapters.
1613

1714
```
18-
config :aeacus, Aeacus,
19-
repo: MyApp.Repo,
20-
model: MyApp.User,
21-
# Optional, The following are the default options
22-
crypto: Comeonin.Pbkdf2,
23-
identity_field: :email,
24-
password_field: :hashed_password,
25-
error_message: "Invalid identity or password."
15+
config :echo, Echo,
16+
preferences: Echo.Preferences.AllowAll,
17+
adapters: [Echo.Adapters.Logger, Echo.Adapters.Email, YourApp.CustomAdapter],
18+
hooks: %{
19+
email: YourApp.EmailHook
20+
}
2621
```
2722

28-
#### Example Session Controller ####
29-
`Aeacus.Authenticator.authenticate` expects a `Map` with keys `:identity`, and `:password`. Alternatively, `Aeacus.Authenticator.authenticate_resource` can be used if a resource is already loaded.
23+
#### Architecture ####
24+
Echo is somewhat like a pub-sub system; events are published, and adapters that are capable of handling them dispatch the notification accordingly. The intended flow is:
25+
26+
- `preferences` module provides an ACL to check whether an event should be delivered on a particular adapter according to a user's preferences (ex. deny newsletters, but allow billing emails).
27+
- `hooks` provide a way to prepare arguments to an adapter based on the `event_type` & `data`. A hook can prevent it's adapter from delivering the notification if no data is returned. This means notifications are only sent for adapters that are configured to handle them, otherwise, they are harmlessly disregarded by that adapter.
3028

29+
ex.) In the code below, an email is sent for `:user_register`, but not `:log_analytics`; `Adapter.Email` will harmlessly skip it as an `:unknown_event`.
30+
```
31+
Echo.notify(:log_analytics, data)
32+
...
33+
Echo.notify(:user_register, data)
3134
```
32-
defmodule MyApp.SessionController do
33-
def create(conn, params) do
34-
case Aeacus.Authenticator.authenticate %{identity: params[:email], password: params[:pass]} do
35-
{:ok, user} -> CreateTokenOrCookie
36-
{:error, message} -> DisplayAuthenticationScreenAgain
35+
```
36+
defmodule App.EmailHook
37+
@behaviour Echo.Hooks
38+
39+
def message(event_type, data) do
40+
...
41+
template = case event_type do
42+
:user_register -> "emails/register.html.eex"
43+
:user_forgot_password -> "emails/forgot_password.html.eex"
44+
_ -> nil
3745
end
46+
...
3847
end
3948
end
40-
```
49+
```

config/config.exs

+6-7
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ config :logger, :console,
1313
format: "$date $time [$level] $metadata$message\n",
1414
metadata: [:user_id]
1515

16-
# It is also possible to import configuration files, relative to this
17-
# directory. For example, you can emulate configuration per environment
18-
# by uncommenting the line below and defining dev.exs, test.exs and such.
19-
# Configuration from the imported file will override the ones defined
20-
# here (which is why it is important to import them last).
21-
#
22-
import_config "#{Mix.env}.exs"
16+
config :echo, Echo,
17+
preferences: Echo.Preferences.Test,
18+
adapters: [Echo.Adapters.Logger, Echo.Adapters.Email],
19+
hooks: %{
20+
email: Echo.Test.EmailHook
21+
}

config/dev.exs

-1
This file was deleted.

config/test.exs

-8
This file was deleted.

lib/echo.ex

+5-13
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ defmodule Echo do
1111
@doc """
1212
Merges the application config with the default configuration options specified by Echo.
1313
"""
14-
@spec config() :: List.t
14+
@spec config() :: Map.t
1515
def config do
16-
default_config Application.get_env(:echo, Echo)
16+
Map.merge @default_config, Enum.into(Application.get_env(:echo, Echo), %{})
1717
end
1818

1919
@doc """
20-
Notify calls dispatch/2 on each adapter, which checks preferences then sends notification.
20+
Notify calls dispatch/2 on each registered adapter, which checks preferences then sends notification.
21+
2122
A List of tuples is returned of the form {:ok, __MODULE__} or {:error, __MODULE__, term | String.t}
2223
indicating the status of each call.
2324
"""
@@ -27,13 +28,4 @@ defmodule Echo do
2728
Enum.map Echo.config[:adapters], fn(adapter) -> adapter.dispatch(event_type, data) end
2829
end
2930
end
30-
31-
32-
@spec default_config(Map.t) :: Map.t
33-
defp default_config(configuration) do
34-
case Enum.empty?(configuration) do
35-
true -> @default_config
36-
false -> Map.merge @default_config, Enum.into(configuration, %{})
37-
end
38-
end
39-
end
31+
end

lib/echo/adapters/behaviour.ex

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
defmodule Echo.Adapters.Behaviour do
22
use Behaviour
33

4-
defcallback notify(term | String.t | nil, Map.t | nil) :: {:ok, term, any} | {:error, term, term | String.t}
4+
defcallback notify(term | String.t | nil, any) :: {:ok, term, any} | {:error, term, term | String.t}
55

66
@doc false
77
defmacro __using__(_opts) do
88
quote do
9+
@behaviour Echo.Adapters.Behaviour
10+
911
@spec dispatch(term | String.t | nil, Map.t | nil) :: Tuple.t
1012
def dispatch(event_type, data) do
1113
if Echo.config[:preferences].notification_allowed?(__MODULE__, event_type, data) do

lib/echo/adapters/email.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ defmodule Echo.Adapters.Email do
66
See Adapter Project page for up-to-date details.
77
88
ex.)
9+
```
910
{:echo, "~> x.x.x"}
1011
{:mailman, "~> x.x.x"},
1112
{:eiconv, github: "zotonic/eiconv"}
13+
```
1214
"""
1315
use Echo.Adapters.Behaviour
1416

15-
1617
def notify(event_type, data) do
1718
config = Echo.config.hooks[:email].config(event_type, data)
1819
message = Echo.config.hooks[:email].message(event_type, data)

lib/echo/hooks.ex

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
defmodule Echo.Hooks do
2+
@moduledoc """
3+
message/2 must be implemented, returning the data for an adapter to deliver a notification.
4+
"""
5+
use Behaviour
6+
7+
defcallback message(term | String.t | nil, any) :: any
8+
end

mix.exs

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule Echo.Mixfile do
44
def project do
55
[
66
app: :echo,
7-
version: "0.0.1",
7+
version: "0.1.0",
88
elixir: "~> 1.0",
99
elixirc_paths: elixirc_paths(Mix.env),
1010
description: description,
@@ -36,9 +36,10 @@ defmodule Echo.Mixfile do
3636

3737
defp deps do
3838
[
39-
{:ex_doc, ">=0.1.0", only: [:test]},
39+
{:ex_doc, ">=0.1.0", only: [:dev, :test]},
40+
{:earmark, ">= 0.0.0", only: [:dev, :test]},
4041
{:mailman, "~> 0.2.0", only: [:test]},
41-
{:eiconv, github: "zotonic/eiconv", only: [:test]},
42+
{:eiconv, github: "zotonic/eiconv", only: [:test]}
4243
]
4344
end
4445

@@ -53,7 +54,9 @@ defmodule Echo.Mixfile do
5354

5455
defp description do
5556
"""
56-
A basic notification dispatch system, composed from many adapters. Useful for email, sms, analytics, logs, etc.
57+
A simple & highly extendable, meta-notification system; Echo checks
58+
notification preferences & dispatch notifications to different adapters
59+
(ex. email, logger, analytics, sms, etc.)
5760
"""
5861
end
5962

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule Echo.Preferences.DenyAllTest do
2+
use ExUnit.Case
3+
4+
test "notification_allowed?/2 always returns true" do
5+
refute Echo.Preferences.DenyAll.notification_allowed? :foo, %{}
6+
refute Echo.Preferences.DenyAll.notification_allowed? nil, nil
7+
end
8+
9+
test "notification_allowed?/3 always returns true" do
10+
refute Echo.Preferences.DenyAll.notification_allowed? :foo, :bar, %{}
11+
refute Echo.Preferences.DenyAll.notification_allowed? :nil, nil, nil
12+
end
13+
end

test/support/email_hook.ex

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule Echo.Test.EmailHook do
2+
@behaviour Echo.Hooks
23

34
# TODO - Remove pending deletion in Mailman
45
def config(_event_type, _data) do

0 commit comments

Comments
 (0)