Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do a compile-time check to make sure that the configured token_resource is an Ash.Resource #749

Merged
merged 3 commits into from
Jul 24, 2024

Conversation

simpers
Copy link
Contributor

@simpers simpers commented Jul 19, 2024

Issue

During a refactor of the auth resources, the tokens.token_resource field was not updated properly on my user resource, and so I ended up getting a very vague :error result when trying to create tokens, without any other context. It was caused by the module not existing, but the current checks only check that the value configured is an atom.

Errors

Initial error when trying to log in:

[error] | file=gen_server.erl line=2474 mfa=:gen_server.error_info/8 [22:44:23.824]: GenServer #PID<0.1271.0> terminating
** (MatchError) no match of right hand side value: :error
    (ash_authentication 4.0.1) lib/ash_authentication/strategies/password/sign_in_preparation.ex:117: AshAuthentication.Strategy.Password.SignInPreparation.generate_token/3
    (ash_authentication 4.0.1) lib/ash_authentication/strategies/password/sign_in_preparation.ex:44: anonymous fn/3 in AshAuthentication.Strategy.Password.SignInPreparation.prepare/3
    (ash 3.2.4) lib/ash/actions/read/read.ex:2097: anonymous fn/2 in Ash.Actions.Read.run_after_action/2
    (elixir 1.17.2) lib/enum.ex:4858: Enumerable.List.reduce/3
    (elixir 1.17.2) lib/enum.ex:2585: Enum.reduce_while/3
    ...

After adding some logging, I got this error:

[error] | file=lib/ash_authentication/jwt.ex line=119 mfa=AshAuthentication.Jwt.token_for_user/3 [23:26:09.713]: Failed to generate token for user: "Module `Lpe.Resources.Token` is not an Ash resource"

Improvement

Improved compile-time validation of the token_resource option of the tokens DSL by checking that the passed value is an Ash.Resource.

Further improvements?

Could this be improved to test it being an AshAuthentication.TokenResource directly?

@zachdaniel zachdaniel requested a review from jimsynz July 23, 2024 10:54
@@ -109,11 +109,23 @@ defmodule AshAuthentication.Verifier do
end
end

@spec is_token_resource(any()) :: boolean() | {boolean(), any()}
defp is_token_resource(module) when is_atom(module) do
with true <- function_exported?(module, :spark_is, 0),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest doing Spark.Dsl.is?(Ash.Resource) :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's exactly what I was looking for! Haha thanks

@zachdaniel zachdaniel merged commit 866d806 into team-alembic:main Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants