mirror of
https://github.com/team-alembic/ash_authentication.git
synced 2024-09-21 13:53:25 +12:00
feat(Auth0): Add a pre-configured Auth0 strategy. (#99)
This commit is contained in:
parent
9802d0b3f7
commit
cf9ad01dd5
4 changed files with 51 additions and 2 deletions
|
@ -2,6 +2,8 @@ spark_locals_without_parens = [
|
||||||
access_token_attribute_name: 1,
|
access_token_attribute_name: 1,
|
||||||
access_token_expires_at_attribute_name: 1,
|
access_token_expires_at_attribute_name: 1,
|
||||||
api: 1,
|
api: 1,
|
||||||
|
auth0: 1,
|
||||||
|
auth0: 2,
|
||||||
auth_method: 1,
|
auth_method: 1,
|
||||||
authorization_params: 1,
|
authorization_params: 1,
|
||||||
authorize_path: 1,
|
authorize_path: 1,
|
||||||
|
@ -54,6 +56,7 @@ spark_locals_without_parens = [
|
||||||
site: 1,
|
site: 1,
|
||||||
store_all_tokens?: 1,
|
store_all_tokens?: 1,
|
||||||
store_changes_action_name: 1,
|
store_changes_action_name: 1,
|
||||||
|
store_token_action_name: 1,
|
||||||
strategy_attribute_name: 1,
|
strategy_attribute_name: 1,
|
||||||
subject_name: 1,
|
subject_name: 1,
|
||||||
token_lifetime: 1,
|
token_lifetime: 1,
|
||||||
|
|
|
@ -37,6 +37,12 @@ config :ash_authentication,
|
||||||
authorize_path: "/authorize",
|
authorize_path: "/authorize",
|
||||||
token_path: "/oauth/token",
|
token_path: "/oauth/token",
|
||||||
user_path: "/userinfo"
|
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: [
|
tokens: [
|
||||||
|
|
|
@ -22,6 +22,8 @@ defmodule AshAuthentication.Dsl do
|
||||||
OptionsHelpers
|
OptionsHelpers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@type strategy :: :confirmation | :oauth2 | :password | :auth0
|
||||||
|
|
||||||
@shared_strategy_options [
|
@shared_strategy_options [
|
||||||
name: [
|
name: [
|
||||||
type: :atom,
|
type: :atom,
|
||||||
|
@ -176,7 +178,8 @@ defmodule AshAuthentication.Dsl do
|
||||||
describe: "Configure authentication strategies on this resource",
|
describe: "Configure authentication strategies on this resource",
|
||||||
entities: [
|
entities: [
|
||||||
strategy(:password),
|
strategy(:password),
|
||||||
strategy(:oauth2)
|
strategy(:oauth2),
|
||||||
|
strategy(:auth0)
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
%Section{
|
%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.
|
# The result spec should be changed to `Entity.t` when Spark 0.2.18 goes out.
|
||||||
@doc false
|
@doc false
|
||||||
@spec strategy(:confirmation | :oauth2 | :password) :: map
|
@spec strategy(strategy) :: map
|
||||||
def strategy(:password) do
|
def strategy(:password) do
|
||||||
%Entity{
|
%Entity{
|
||||||
name: :password,
|
name: :password,
|
||||||
|
@ -685,4 +688,20 @@ defmodule AshAuthentication.Dsl do
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -46,6 +46,17 @@ defmodule Example.User do
|
||||||
primary? true
|
primary? true
|
||||||
end
|
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
|
create :register_with_oauth2 do
|
||||||
argument :user_info, :map, allow_nil?: false
|
argument :user_info, :map, allow_nil?: false
|
||||||
argument :oauth_tokens, :map, allow_nil?: false
|
argument :oauth_tokens, :map, allow_nil?: false
|
||||||
|
@ -141,6 +152,16 @@ defmodule Example.User do
|
||||||
auth_method :client_secret_post
|
auth_method :client_secret_post
|
||||||
identity_resource Example.UserIdentity
|
identity_resource Example.UserIdentity
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue