Skip to content

Commit

Permalink
Merge pull request #17 from dwyl/send-email-issue#41
Browse files Browse the repository at this point in the history
Send Email
  • Loading branch information
SimonLab authored Mar 29, 2020
2 parents 33af8ec + 7848e56 commit f027488
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .env_sample
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export GITHUB_CLIENT_ID=d6fca75c63daa014c187
export GITHUB_CLIENT_SECRET=8eeb143935d1a505692aaef856db9b4da8245f3c
export GOOGLE_CLIENT_ID=YourAppsClientId.apps.googleusercontent.com
export GOOGLE_CLIENT_SECRET=SuperSecret
export SECRET_KEY_BASE=2PzB7PPnpuLsbWmWtXpGyI+kfSQSQ1zUW2Atz/+8PdZuSEJzHgzGnJWV35nTKRwx
export SECRET_KEY_BASE=getfrom-dashboard.heroku.com/apps/dwylmail/settings
export EMAIL_APP_URL=https://dwylmail.herokuapp.com
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
# AuthMvp
# Auth MVP

[![Build Status](https://img.shields.io/travis/dwyl/auth-mvp/master.svg?style=flat-square)](https://travis-ci.org/dwyl/auth-mvp)
[![codecov.io](https://img.shields.io/codecov/c/github/dwyl/auth-mvp/master.svg?style=flat-square)](http://codecov.io/github/dwyl/auth-mvp?branch=master)

DWYL Authentication service

## API

- `/auth/urls`: returns list of oauth applications' url.

## Sending Emails From Auth App Requires

The only requirement that you need to fulfil
in order to enable sending emails
is having the `EMAIL_APP_URL` set in your `.env`
and the `SECRET_KEY_BASE` environment variable needs to be the _same_
as the `dwylmail` Heroku app.

Visit: https://dashboard.heroku.com/apps/dwylmail/settings
and copy the `SECRET_KEY_BASE` from Config Vars.
5 changes: 4 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ config :auth_mvp,
# Configures the endpoint
config :auth_mvp, AuthMvpWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: "R+SYk92dfv9sFdtZV4dpz1zdSs65LCOE+B449/qc25LR9iBkCU1DzXAI/VIZSS74",
secret_key_base: System.get_env("SECRET_KEY_BASE"),
render_errors: [view: AuthMvpWeb.ErrorView, accepts: ~w(html json)],
pubsub: [name: AuthMvp.PubSub, adapter: Phoenix.PubSub.PG2]

Expand All @@ -28,3 +28,6 @@ config :phoenix, :json_library, Jason
# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env()}.exs"

# https://hexdocs.pm/joken/introduction.html#usage
config :joken, default_signer: System.get_env("SECRET_KEY_BASE")
1 change: 0 additions & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,3 @@ config :phoenix, :stacktrace_depth, 20

# Initialize plugs at runtime for faster development compilation
config :phoenix, :plug_init_mode, :runtime
config :joken, default_signer: "secret"
1 change: 0 additions & 1 deletion config/prod.secret.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ config :auth_mvp, AuthMvpWeb.Endpoint,
http: [:inet6, port: String.to_integer(System.get_env("PORT") || "4000")],
secret_key_base: secret_key_base

config :joken, default_signer: System.get_env("SECRET_KEY_BASE")
# ## Using releases (Elixir v1.9+)
#
# If you are doing OTP releases, you need to instruct Phoenix
Expand Down
1 change: 0 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ config :auth_mvp, AuthMvpWeb.Endpoint,
config :logger, level: :warn
config :auth_mvp, :elixir_auth_google, AuthMvp.Mock.ElixirAuthGoogle
config :auth_mvp, :elixir_auth_github, AuthMvp.Mock.ElixirAuthGithub
config :joken, default_signer: "secret"
7 changes: 5 additions & 2 deletions coveralls.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"minimum_coverage": 100
},
"skip_files": [
"test/"
"test/",
"lib/auth_mvp_web.ex",
"lib/auth_mvp/application.ex",
"lib/auth_mvp_web/views/error_helpers.ex"
]
}
}
34 changes: 34 additions & 0 deletions lib/auth_mvp/email.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule AuthMvp.Email do

@doc """
`sendemail/1` sends an email using AWS SES.
see: https://github.com/dwyl/email#sending-email
params is a map that *must* contain the keys: email, name and template.
## Examples
iex> sendemail(%{"email" => "[email protected]", "name" => "Al", "template" => "hi"})
%{
"aud" => "Joken",
"email" => "[email protected]",
"exp" => 1616864371,
"iat" => 1585327371,
"id" => 33,
"iss" => "Joken",
"jti" => "2o03dm2ktf6f1j74es0001e3",
"name" => "Al",
"nbf" => 1585327371,
"status" => "Pending",
"template" => "hi"
}
"""

def sendemail(params) do
url = System.get_env("EMAIL_APP_URL") <> "/api/send"
jwt = AuthMvp.Token.generate_and_sign!(params)
headers = ["Authorization": "#{jwt}"]
options = [ssl: [{:versions, [:'tlsv1.2']}], recv_timeout: 10000]
{:ok, response} = HTTPoison.post(url, "_nobody", headers, options)
Jason.decode!(response.body)
end
end
1 change: 1 addition & 0 deletions lib/auth_mvp_web/controllers/github_auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule AuthMvpWeb.GithubAuthController do
{:ok, session} = case AuthMvp.People.get_person_by_email(profile.email) do
nil ->
{:ok, person} = AuthMvp.People.create_person(%{email: profile.email})
AuthMvp.Email.sendemail(%{email: profile.email, template: "welcome"})
AuthMvp.People.create_session(person)

person ->
Expand Down
1 change: 1 addition & 0 deletions lib/auth_mvp_web/controllers/google_auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule AuthMvpWeb.GoogleAuthController do
nil ->
# Create the person
{:ok, person} = AuthMvp.People.create_person(%{email: profile.email})
AuthMvp.Email.sendemail(%{email: profile.email, template: "welcome"})
AuthMvp.People.create_session(person)

person ->
Expand Down
17 changes: 17 additions & 0 deletions test/auth_mvp/email_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule AuthMvp.EmailTest do
use ExUnit.Case

describe "AuthMvp.Email" do
test "sendemail/1 an email" do
params = %{
"email" => "[email protected]",
"name" => "Super Successful",
"template" => "welcome"
}
# IO.inspect(params, label: "params")
res = AuthMvp.Email.sendemail(params)
assert Map.get(params, "email") == Map.get(res, "email")
assert Map.get(res, "id") > 0
end
end
end
4 changes: 4 additions & 0 deletions test/auth_mvp_web/controllers/github_auth_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ defmodule AuthMvpWeb.GithubAuthControllerTest do
test "GET /auth/github/callback", %{conn: conn} do
conn = get(conn, Routes.github_auth_path(conn, :index, %{code: "123", state: "http://localhost/"}))
assert html_response(conn, 302)

# same again to exercise to the branch where person already exists:
conn = get(conn, Routes.github_auth_path(conn, :index, %{code: "123", state: "http://localhost/"}))
assert html_response(conn, 302)
end
end
3 changes: 3 additions & 0 deletions test/auth_mvp_web/controllers/google_auth_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ defmodule AuthMvpWeb.GoogleAuthControllerTest do

test "GET /", %{conn: conn} do
conn = get(conn, Routes.google_auth_path(conn, :index, %{code: "234", state: "http://localhost/"}))
assert html_response(conn, 302)

# same again to exercise to the branch where person already exists:
conn = get(conn, Routes.google_auth_path(conn, :index, %{code: "234", state: "http://localhost/"}))
assert html_response(conn, 302)
end
end

0 comments on commit f027488

Please sign in to comment.