From cf9ad01dd5362d55940266854dd12f961858e711 Mon Sep 17 00:00:00 2001 From: James Harton <59449+jimsynz@users.noreply.github.com> Date: Fri, 16 Dec 2022 13:06:51 +1300 Subject: [PATCH] feat(Auth0): Add a pre-configured Auth0 strategy. (#99) --- .formatter.exs | 3 +++ config/dev.exs | 6 ++++++ lib/ash_authentication/dsl.ex | 23 +++++++++++++++++++++-- test/support/example/user.ex | 21 +++++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/.formatter.exs b/.formatter.exs index 24088f4..864fcc4 100644 --- a/.formatter.exs +++ b/.formatter.exs @@ -2,6 +2,8 @@ spark_locals_without_parens = [ access_token_attribute_name: 1, access_token_expires_at_attribute_name: 1, api: 1, + auth0: 1, + auth0: 2, auth_method: 1, authorization_params: 1, authorize_path: 1, @@ -54,6 +56,7 @@ spark_locals_without_parens = [ site: 1, store_all_tokens?: 1, store_changes_action_name: 1, + store_token_action_name: 1, strategy_attribute_name: 1, subject_name: 1, token_lifetime: 1, diff --git a/config/dev.exs b/config/dev.exs index 53e11ef..a7be239 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -37,6 +37,12 @@ config :ash_authentication, authorize_path: "/authorize", token_path: "/oauth/token", user_path: "/userinfo" + ], + auth0: [ + client_id: System.get_env("OAUTH2_CLIENT_ID"), + redirect_uri: "http://localhost:4000/auth", + client_secret: System.get_env("OAUTH2_CLIENT_SECRET"), + site: System.get_env("OAUTH2_SITE") ] ], tokens: [ diff --git a/lib/ash_authentication/dsl.ex b/lib/ash_authentication/dsl.ex index 44f6798..5b1f443 100644 --- a/lib/ash_authentication/dsl.ex +++ b/lib/ash_authentication/dsl.ex @@ -22,6 +22,8 @@ defmodule AshAuthentication.Dsl do OptionsHelpers } + @type strategy :: :confirmation | :oauth2 | :password | :auth0 + @shared_strategy_options [ name: [ type: :atom, @@ -176,7 +178,8 @@ defmodule AshAuthentication.Dsl do describe: "Configure authentication strategies on this resource", entities: [ strategy(:password), - strategy(:oauth2) + strategy(:oauth2), + strategy(:auth0) ] }, %Section{ @@ -193,7 +196,7 @@ defmodule AshAuthentication.Dsl do # The result spec should be changed to `Entity.t` when Spark 0.2.18 goes out. @doc false - @spec strategy(:confirmation | :oauth2 | :password) :: map + @spec strategy(strategy) :: map def strategy(:password) do %Entity{ name: :password, @@ -685,4 +688,20 @@ defmodule AshAuthentication.Dsl do ) } end + + def strategy(:auth0) do + :oauth2 + |> strategy() + |> Map.merge(%{ + name: :auth0, + describe: "Auth0 authentication", + auto_set_fields: [ + authorization_params: [scope: "openid profile email"], + auth_method: :client_secret_post, + authorize_path: "/authorize", + token_path: "/oauth/token", + user_path: "/userinfo" + ] + }) + end end diff --git a/test/support/example/user.ex b/test/support/example/user.ex index 6ea1eb9..23cf2e3 100644 --- a/test/support/example/user.ex +++ b/test/support/example/user.ex @@ -46,6 +46,17 @@ defmodule Example.User do primary? true end + create :register_with_auth0 do + argument :user_info, :map, allow_nil?: false + argument :oauth_tokens, :map, allow_nil?: false + upsert? true + upsert_identity :username + + change AshAuthentication.GenerateTokenChange + change Example.GenericOAuth2Change + change AshAuthentication.Strategy.OAuth2.IdentityChange + end + create :register_with_oauth2 do argument :user_info, :map, allow_nil?: false argument :oauth_tokens, :map, allow_nil?: false @@ -141,6 +152,16 @@ defmodule Example.User do auth_method :client_secret_post identity_resource Example.UserIdentity end + + auth0 :auth0 do + client_id &get_config/2 + redirect_uri &get_config/2 + client_secret &get_config/2 + site &get_config/2 + authorize_path &get_config/2 + token_path &get_config/2 + user_path &get_config/2 + end end end