Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send Email #17

Merged
merged 15 commits into from
Mar 29, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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=2PzB7PPnpuLsbWmWtXpGyI+kfSQSQ1zUW2Atz/+8PdZuSEJzHgzGnJWV35nTKRwx
export EMAIL_APP_URL=dwylmail.herokuapp.com
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@ 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")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to match the SECRET_KEY_BASE on dashboard.heroku.com/apps/dwylmail/settings for email to work.

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",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we aren't directly using these files so they aren't covered by tests hence adding them here.

"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 = "https://dwylmail.herokuapp.com/api/send"
jwt = AuthMvp.Token.generate_and_sign!(params)
headers = ["Authorization": "#{jwt}"]
options = [] # [ssl: [{:versions, [:'tlsv1.2']}], recv_timeout: 5000]
{: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