Skip to content

Commit

Permalink
chore: remove AshAuthentication, upgrade all the deps
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Apr 2, 2024
1 parent 235a8f1 commit 6a943f2
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 397 deletions.
4 changes: 0 additions & 4 deletions lib/ash_hq/accounts/resources/user/policies.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ defmodule AshHq.Accounts.User.Policies do
use Spark.Dsl.Fragment, of: Ash.Resource, authorizers: [Ash.Policy.Authorizer]

policies do
bypass AshAuthentication.Checks.AshAuthenticationInteraction do
authorize_if(always())
end

policy action(:read) do
authorize_if(expr(id == ^actor(:id)))
end
Expand Down

This file was deleted.

This file was deleted.

279 changes: 123 additions & 156 deletions lib/ash_hq/accounts/resources/user/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ defmodule AshHq.Accounts.User do

use Ash.Resource,
data_layer: AshPostgres.DataLayer,
extensions: [AshAuthentication],
fragments: [AshHq.Accounts.User.Policies]

require Ash.Query
Expand All @@ -13,161 +12,129 @@ defmodule AshHq.Accounts.User do
actions do
defaults [:read]

create :register_with_github do
argument :user_info, :map do
allow_nil? false
end

argument :oauth_tokens, :map do
allow_nil? false
end

change fn changeset, _ ->
user_info = Ash.Changeset.get_argument(changeset, :user_info)

changeset =
if user_info["email_verified"] do
Ash.Changeset.force_change_attribute(
changeset,
:confirmed_at,
Ash.Changeset.get_attribute(changeset, :confirmed_at) || DateTime.utc_now()
)
else
changeset
end

changeset
|> Ash.Changeset.change_attribute(:email, Map.get(user_info, "email"))
|> Ash.Changeset.change_attribute(:github_info, user_info)
end

change AshAuthentication.GenerateTokenChange
upsert? true
upsert_identity :unique_email
end

update :change_password do
accept []

argument :current_password, :string do
sensitive? true
allow_nil? false
end

argument :password, :string do
sensitive? true
allow_nil? false
end

argument :password_confirmation, :string do
sensitive? true
allow_nil? false
end

change set_context(%{strategy_name: :password})

validate confirm(:password, :password_confirmation)

validate {AshAuthentication.Strategy.Password.PasswordValidation,
strategy_name: :password, password_argument: :current_password} do
only_when_valid? true
before_action? true
end

change AshAuthentication.Strategy.Password.HashPasswordChange
end

update :update_email do
accept [:email]

argument :current_password, :string do
sensitive? true
allow_nil? false
end

change set_context(%{strategy_name: :password})

validate {AshAuthentication.Strategy.Password.PasswordValidation,
password_argument: :current_password} do
only_when_valid? true
before_action? true
end
end

update :resend_confirmation_instructions do
accept []

change fn changeset, _context ->
Ash.Changeset.before_action(changeset, fn changeset ->
case AshHq.Accounts.UserToken.email_token_for_user(changeset.data.id,
authorize?: false
) do
{:ok, %{extra_data: %{"email" => changing_to}}} ->
temp_changeset = %{
changeset
| attributes: Map.put(changeset.attributes, :email, changing_to)
}

strategy = AshAuthentication.Info.strategy!(changeset.resource, :confirm)

{:ok, token} =
AshAuthentication.AddOn.Confirmation.confirmation_token(
strategy,
temp_changeset,
changeset.data
)

AshHq.Accounts.User.Senders.SendConfirmationEmail.send(changeset.data, token, [])

changeset

_ ->
Ash.Changeset.add_error(changeset, "Could not determine what email to use")
end
end)
end
end

update :update_merch_settings do
argument :address, :string
argument :name, :string

accept [:shirt_size]
change set_attribute(:encrypted_address, arg(:address))
change set_attribute(:encrypted_name, arg(:name))
end
end

authentication do
api AshHq.Accounts

strategies do
password :password do
identity_field :email
hashed_password_field :hashed_password
sign_in_tokens_enabled? true

resettable do
sender AshHq.Accounts.User.Senders.SendPasswordResetEmail
end
end
end

tokens do
enabled? true
token_resource AshHq.Accounts.UserToken
signing_secret AshHq.Accounts.Secrets
store_all_tokens? true
require_token_presence_for_authentication? true
end

add_ons do
confirmation :confirm do
monitor_fields [:email]

sender AshHq.Accounts.User.Senders.SendConfirmationEmail
end
end
# create :register_with_github do
# argument :user_info, :map do
# allow_nil? false
# end

# argument :oauth_tokens, :map do
# allow_nil? false
# end

# change fn changeset, _ ->
# user_info = Ash.Changeset.get_argument(changeset, :user_info)

# changeset =
# if user_info["email_verified"] do
# Ash.Changeset.force_change_attribute(
# changeset,
# :confirmed_at,
# Ash.Changeset.get_attribute(changeset, :confirmed_at) || DateTime.utc_now()
# )
# else
# changeset
# end

# changeset
# |> Ash.Changeset.change_attribute(:email, Map.get(user_info, "email"))
# |> Ash.Changeset.change_attribute(:github_info, user_info)
# end

# change AshAuthentication.GenerateTokenChange
# upsert? true
# upsert_identity :unique_email
# end

# update :change_password do
# accept []

# argument :current_password, :string do
# sensitive? true
# allow_nil? false
# end

# argument :password, :string do
# sensitive? true
# allow_nil? false
# end

# argument :password_confirmation, :string do
# sensitive? true
# allow_nil? false
# end

# change set_context(%{strategy_name: :password})

# validate confirm(:password, :password_confirmation)

# validate {AshAuthentication.Strategy.Password.PasswordValidation,
# strategy_name: :password, password_argument: :current_password} do
# only_when_valid? true
# before_action? true
# end

# change AshAuthentication.Strategy.Password.HashPasswordChange
# end

# update :update_email do
# accept [:email]

# argument :current_password, :string do
# sensitive? true
# allow_nil? false
# end

# change set_context(%{strategy_name: :password})

# validate {AshAuthentication.Strategy.Password.PasswordValidation,
# password_argument: :current_password} do
# only_when_valid? true
# before_action? true
# end
# end

# update :resend_confirmation_instructions do
# accept []

# change fn changeset, _context ->
# Ash.Changeset.before_action(changeset, fn changeset ->
# case AshHq.Accounts.UserToken.email_token_for_user(changeset.data.id,
# authorize?: false
# ) do
# {:ok, %{extra_data: %{"email" => changing_to}}} ->
# temp_changeset = %{
# changeset
# | attributes: Map.put(changeset.attributes, :email, changing_to)
# }

# strategy = AshAuthentication.Info.strategy!(changeset.resource, :confirm)

# {:ok, token} =
# AshAuthentication.AddOn.Confirmation.confirmation_token(
# strategy,
# temp_changeset,
# changeset.data
# )

# AshHq.Accounts.User.Senders.SendConfirmationEmail.send(changeset.data, token, [])

# changeset

# _ ->
# Ash.Changeset.add_error(changeset, "Could not determine what email to use")
# end
# end)
# end
# end

# update :update_merch_settings do
# argument :address, :string
# argument :name, :string

# accept [:shirt_size]
# change set_attribute(:encrypted_address, arg(:address))
# change set_attribute(:encrypted_name, arg(:name))
# end
end

attributes do
Expand Down
7 changes: 1 addition & 6 deletions lib/ash_hq/accounts/resources/user_token/user_token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ defmodule AshHq.Accounts.UserToken do

use Ash.Resource,
data_layer: AshPostgres.DataLayer,
authorizers: [Ash.Policy.Authorizer],
extensions: [AshAuthentication.TokenResource]
authorizers: [Ash.Policy.Authorizer]

actions do
defaults [:read, :destroy]
Expand All @@ -31,10 +30,6 @@ defmodule AshHq.Accounts.UserToken do
end

policies do
bypass AshAuthentication.Checks.AshAuthenticationInteraction do
authorize_if always()
end

policy always() do
description """
There are currently no usages of user tokens resource that should be publicly
Expand Down
17 changes: 0 additions & 17 deletions lib/ash_hq/accounts/secrets.ex

This file was deleted.

9 changes: 0 additions & 9 deletions lib/ash_hq_web/auth_overrides.ex

This file was deleted.

Loading

0 comments on commit 6a943f2

Please sign in to comment.