Skip to content

Commit

Permalink
use client_id to sign JWT #55
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Apr 28, 2020
1 parent 3cdd44c commit 144a3cf
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 36 deletions.
1 change: 1 addition & 0 deletions .env_sample
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export ADMIN_EMAIL=youremail@gmail.com
export AUTH_API_KEY=2PzB7PPnpuLsbWmWtXpGyI+kfSQSQ1zUW2Atz/+8PdZuSEJzHgzGnJWV35nTKRwx
export EMAIL_APP_URL=https://dwylmail.herokuapp.com
export GITHUB_CLIENT_ID=CreateGitHubApp
export GITHUB_CLIENT_SECRET=SuperSecret
Expand Down
5 changes: 4 additions & 1 deletion lib/auth/email.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ defmodule Auth.Email do
}
"""
def sendemail(params) do
# IO.inspect(params, label: "sendemail/1 params:")
url = System.get_env("EMAIL_APP_URL") <> "/api/send"
jwt = AuthPlug.Token.generate_jwt!(params)
# until further notice use the SECRET_KEY_BASE for sending email:
secret = System.get_env("SECRET_KEY_BASE")
jwt = AuthPlug.Token.generate_jwt!(params, secret)
headers = [Authorization: "#{jwt}"]
options = [ssl: [{:versions, [:"tlsv1.2"]}],
timeout: 50_000, recv_timeout: 50_000] # github.com/dwyl/auth/issues/48
Expand Down
10 changes: 5 additions & 5 deletions lib/auth/person.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ defmodule Auth.Person do
end

def create_person(person) do
IO.inspect(person, label: "create_person:51")
# IO.inspect(person, label: "create_person:51")
person =
%Person{}
|> changeset(person)
|> put_email_status_verified()
|> IO.inspect(label: "after put_email_status_verified")
# |> IO.inspect(label: "after put_email_status_verified")

case get_person_by_email(person.changes.email) do
nil ->
Expand Down Expand Up @@ -137,13 +137,13 @@ defmodule Auth.Person do
givenName: profile.given_name,
auth_provider: "google"
})
|> IO.inspect(label: "merged")
# |> IO.inspect(label: "merged")
end

def create_google_person(profile) do
transform_google_profile_data_to_person(profile)
|> create_person()
|> IO.inspect(label: "create_person:")
# |> IO.inspect(label: "create_person:")
end

# @doc """
Expand All @@ -162,7 +162,7 @@ defmodule Auth.Person do

defp put_email_hash(changeset) do
put_change(changeset, :email_hash, changeset.changes.email)
|> IO.inspect(label: "changeset with :email_hash")
# |> IO.inspect(label: "changeset with :email_hash")
end

def put_email_status_verified(changeset) do
Expand Down
16 changes: 9 additions & 7 deletions lib/auth_web/controllers/apikey_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ defmodule AuthWeb.ApikeyController do
key |> String.split("/") |> List.first() |> decode_decrypt()
end

def create(conn, %{"apikey" => apikey_params}) do
# IO.inspect(apikey_params, label: "apikey_params")
person_id = conn.assigns.decoded.id

params = Map.merge(apikey_params, %{
def make_apikey(apikey_params, person_id) do
Map.merge(apikey_params, %{
"client_secret" => encrypt_encode(person_id),
"client_id" => encrypt_encode(person_id),
"person_id" => person_id
})
})
end

{:ok, apikey} = Apikey.create_apikey(params)
def create(conn, %{"apikey" => apikey_params}) do
{:ok, apikey} =
apikey_params
|> make_apikey(conn.assigns.decoded.id)
|> Apikey.create_apikey()

conn
|> put_flash(:info, "Apikey created successfully.")
Expand Down
15 changes: 8 additions & 7 deletions lib/auth_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ defmodule AuthWeb.AuthController do
if the state is defined, redirect to it.
"""
def handler(conn, person, state) do
# IO.inspect(person, label: "handler/3 > person")
IO.inspect(person, label: "handler/3 > person")
# Send welcome email:
Auth.Email.sendemail(%{
email: person.email,
name: person.givenName,
template: "welcome"
})
|> IO.inspect(label: "email")
# Auth.Email.sendemail(%{
# email: person.email,
# name: person.givenName,
# template: "welcome"
# })
# |> IO.inspect(label: "email")

IO.inspect(state, label: "state handler/3:53")

Expand All @@ -61,6 +61,7 @@ defmodule AuthWeb.AuthController do
false -> # display welcome page
conn
|> put_view(AuthWeb.PageView)
# |> AuthPlug.create_jwt_session(person)
|> render(:welcome, person: person)
end
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule Auth.Mixfile do
# https://github.com/dwyl/elixir-auth-google
{:elixir_auth_google, "~> 1.2.0"},
# https://github.com/dwyl/auth_plug
{:auth_plug, "~> 0.12.0"},
{:auth_plug, "~> 0.14.0"},

# Field Validation and Encryption: github.com/dwyl/fields
{:fields, "~> 2.4.0"},
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%{
"argon2_elixir": {:hex, :argon2_elixir, "2.3.0", "e251bdafd69308e8c1263e111600e6d68bd44f23d2cccbe43fcb1a417a76bc8e", [:make, :mix], [{:comeonin, "~> 5.3", [hex: :comeonin, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "28ccb63bff213aecec1f7f3dde9648418b031f822499973281d8f494b9d5a3b3"},
"auth_plug": {:hex, :auth_plug, "0.12.0", "8cbd8eebfd4760a01137c0be5c4585cf43d4a7f4bdba418fce0c968b152f5eb2", [:mix], [{:joken, "~> 2.2.0", [hex: :joken, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "91a9a607359149045aa8c42e6d57a56763ea1ca86d2a5f14ca7d33524bf1e6e7"},
"auth_plug": {:hex, :auth_plug, "0.14.0", "2067a1d9c688ed491c25dbb20ce91d48568b1fe98571a41a59665743148da27a", [:mix], [{:joken, "~> 2.2.0", [hex: :joken, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "50dd20e44621a5a0f6d3a6b5bd21ff526b8ea49a35bc1d88098c25ef0c951d37"},
"certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "805abd97539caf89ec6d4732c91e62ba9da0cda51ac462380bbd28ee697a8c42"},
"comeonin": {:hex, :comeonin, "5.3.1", "7fe612b739c78c9c1a75186ef2d322ce4d25032d119823269d0aa1e2f1e20025", [:mix], [], "hexpm", "d6222483060c17f0977fad1b7401ef0c5863c985a64352755f366aee3799c245"},
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm", "4a0850c9be22a43af9920a71ab17c051f5f7d45c209e40269a1938832510e4d9"},
Expand Down
4 changes: 2 additions & 2 deletions test/auth/apikey_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Auth.ApikeyTest do

keys = Auth.Apikey.list_apikeys_for_person(person.id)
# IO.inspect(keys, label: "keys")
assert keys == []
assert length(keys) == 1

# Insert Two API keys:
params = %{
Expand All @@ -27,7 +27,7 @@ defmodule Auth.ApikeyTest do
}) |> Auth.Apikey.create_apikey()

keys = Auth.Apikey.list_apikeys_for_person(person.id)
assert length(keys) == 2
assert length(keys) == 3
end


Expand Down
4 changes: 2 additions & 2 deletions test/auth_web/controllers/auth_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ defmodule AuthWeb.AuthControllerTest do
auth_provider: "google"
}
person = Auth.Person.create_person(data) # |> IO.inspect(label: "person")
conn = AuthPlug.create_jwt_session(conn, Map.merge(data, %{id: person.id}))
conn = AuthPlug.create_jwt_session(conn, Map.merge(data, person))
conn = get(conn, "/auth/google/callback",
%{code: "234", state: nil})

assert html_response(conn, 200) =~ "google account"
# assert html_response(conn, 200) =~ "google account"
# assert html_response(conn, 302) =~ "redirected"
end
end
22 changes: 12 additions & 10 deletions test/auth_web/controllers/page_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ defmodule AuthWeb.PageControllerTest do
end

test "google_handler/2 show welcome (state=nil) > handler/3", %{conn: conn} do
data = %{
email: "nelson@gmail.com",
givenName: "McTestin",
picture: "https://youtu.be/naoknj1ebqI",
auth_provider: "google"
}
person = Auth.Person.create_person(data) # |> IO.inspect(label: "person")
conn = AuthPlug.create_jwt_session(conn, Map.merge(data, %{id: person.id}))
conn = get(conn, "/auth/google/callback",
%{code: "234", state: nil})
# IO.inspect(System.get_env("AUTH_API_KEY"), label: "AUTH_API_KEY")
IO.inspect(AuthPlug.Token.client_id(), label: "AuthPlug.Token.client_id()")
# data = %{
# email: "nelson@gmail.com",
# givenName: "McTestin",
# picture: "https://youtu.be/naoknj1ebqI",
# auth_provider: "google"
# }
# person = Auth.Person.create_person(data) |> IO.inspect(label: "person")
# conn = AuthPlug.create_jwt_session(conn, Map.merge(data, %{id: person.id}))
# |> IO.inspect(label: "conn")
conn = get(conn, "/auth/google/callback", %{code: "234", state: nil})

assert html_response(conn, 200) =~ "google account"
# assert html_response(conn, 302) =~ "redirected"
Expand Down

0 comments on commit 144a3cf

Please sign in to comment.