From 9ee32aa47ec77f6e3069543834cbc65803d8d059 Mon Sep 17 00:00:00 2001 From: Tom Haines Date: Wed, 26 Aug 2020 14:25:20 +0100 Subject: [PATCH 1/4] #21: Generate JWT locally with Mix task --- lib/mix/tasks/smart_home/gen_token.ex | 12 ++++++++++++ lib/smart_home_auth.ex | 14 ++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 lib/mix/tasks/smart_home/gen_token.ex diff --git a/lib/mix/tasks/smart_home/gen_token.ex b/lib/mix/tasks/smart_home/gen_token.ex new file mode 100644 index 0000000..b5fc239 --- /dev/null +++ b/lib/mix/tasks/smart_home/gen_token.ex @@ -0,0 +1,12 @@ +defmodule Mix.Tasks.SmartHome.GenToken do + use Mix.Task + + @shortdoc "Generate a JWT token for use with local API development" + def run(_) do + IO.puts("Generating JWT Token...") + jwt = SmartHomeAuth.gen_auth_token() + + IO.puts("\n\n") + IO.puts(jwt) + end +end diff --git a/lib/smart_home_auth.ex b/lib/smart_home_auth.ex index b8da1dc..ff3f6bf 100644 --- a/lib/smart_home_auth.ex +++ b/lib/smart_home_auth.ex @@ -7,4 +7,18 @@ defmodule SmartHomeAuth do if it comes from the database, an external API or others. """ + + @doc """ + Generate a local-use authentication token for API development. + """ + @spec gen_auth_token :: binary + def gen_auth_token do + AuthPlug.Token.generate_jwt!( + %{ + "id" => 1, + "email" => "test@example.com", + "auth_provider" => "local-dev", + "given_name" => "Dev Superuser" + }) + end end From 84c5263e8c6b6523d5c30a13e1b0252e62d0b86f Mon Sep 17 00:00:00 2001 From: Tom Haines Date: Wed, 26 Aug 2020 15:01:04 +0100 Subject: [PATCH 2/4] #21: Add testing for new func & task --- test/mix/tasks/smart_home_test.exs | 17 +++++++++++++++++ test/smart_home_auth/helper_test.exs | 11 +++++++++++ 2 files changed, 28 insertions(+) create mode 100644 test/mix/tasks/smart_home_test.exs create mode 100644 test/smart_home_auth/helper_test.exs diff --git a/test/mix/tasks/smart_home_test.exs b/test/mix/tasks/smart_home_test.exs new file mode 100644 index 0000000..b4e42c4 --- /dev/null +++ b/test/mix/tasks/smart_home_test.exs @@ -0,0 +1,17 @@ +defmodule Mix.Tasks.SmartHomeTest do + use ExUnit.Case + import ExUnit.CaptureIO + + require Logger + + @jwt_pattern ~r/[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+\/=]*$/m + + test "Mix task returns a valid JWT" do + out = capture_io(fn -> Mix.Tasks.SmartHome.GenToken.run([]) end) + + jwt = Regex.run(@jwt_pattern, out) |> List.first() + + decoded = AuthPlug.Token.verify_jwt!(jwt) + assert %{"id" => 1} = decoded + end +end diff --git a/test/smart_home_auth/helper_test.exs b/test/smart_home_auth/helper_test.exs new file mode 100644 index 0000000..283b3bd --- /dev/null +++ b/test/smart_home_auth/helper_test.exs @@ -0,0 +1,11 @@ +defmodule SmartHomeAuth.HelperTest do + use ExUnit.Case, async: true + + test "gen_auth_token generates a correct token" do + token = SmartHomeAuth.gen_auth_token() + + decoded = AuthPlug.Token.verify_jwt!(token) + + assert %{"id" => 1} = decoded + end +end From 806aad86dfaccfe95dfaf73a7489a66530da68dc Mon Sep 17 00:00:00 2001 From: Tom Haines Date: Wed, 26 Aug 2020 15:03:17 +0100 Subject: [PATCH 3/4] #21: Remove unused require --- test/mix/tasks/smart_home_test.exs | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/mix/tasks/smart_home_test.exs b/test/mix/tasks/smart_home_test.exs index b4e42c4..b2133e1 100644 --- a/test/mix/tasks/smart_home_test.exs +++ b/test/mix/tasks/smart_home_test.exs @@ -2,8 +2,6 @@ defmodule Mix.Tasks.SmartHomeTest do use ExUnit.Case import ExUnit.CaptureIO - require Logger - @jwt_pattern ~r/[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+\/=]*$/m test "Mix task returns a valid JWT" do From b9388f35b29abad864b499d63c0815ab3aa6d407 Mon Sep 17 00:00:00 2001 From: Nelson <194400+nelsonic@users.noreply.github.com> Date: Wed, 26 Aug 2020 15:34:06 +0100 Subject: [PATCH 4/4] fix key "given_name" to "givenName" (sorry, I know) --- lib/smart_home_auth.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/smart_home_auth.ex b/lib/smart_home_auth.ex index ff3f6bf..0221b4f 100644 --- a/lib/smart_home_auth.ex +++ b/lib/smart_home_auth.ex @@ -18,7 +18,7 @@ defmodule SmartHomeAuth do "id" => 1, "email" => "test@example.com", "auth_provider" => "local-dev", - "given_name" => "Dev Superuser" + "givenName" => "Dev Superuser" }) end end