feat(Auth0): Add a pre-configured Auth0 strategy. (#99)

This commit is contained in:
James Harton 2022-12-16 13:06:51 +13:00 committed by GitHub
parent 9802d0b3f7
commit cf9ad01dd5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 2 deletions

View file

@ -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,

View file

@ -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: [

View file

@ -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

View file

@ -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