2022-11-23 09:09:41 +13:00
|
|
|
defmodule Example.User do
|
2022-10-25 11:07:07 +13:00
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource,
|
|
|
|
data_layer: AshPostgres.DataLayer,
|
2022-10-31 16:43:00 +13:00
|
|
|
extensions: [
|
|
|
|
AshAuthentication,
|
|
|
|
AshGraphql.Resource,
|
|
|
|
AshJsonApi.Resource
|
|
|
|
]
|
2022-10-25 11:07:07 +13:00
|
|
|
|
2022-11-02 18:18:20 +13:00
|
|
|
require Logger
|
|
|
|
|
2022-10-25 11:07:07 +13:00
|
|
|
@type t :: %__MODULE__{
|
|
|
|
id: Ecto.UUID.t(),
|
|
|
|
username: String.t(),
|
|
|
|
hashed_password: String.t(),
|
|
|
|
created_at: DateTime.t(),
|
|
|
|
updated_at: DateTime.t()
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes do
|
2022-11-23 09:09:41 +13:00
|
|
|
uuid_primary_key(:id, writable?: true)
|
2022-10-25 11:07:07 +13:00
|
|
|
|
|
|
|
attribute(:username, :ci_string, allow_nil?: false)
|
2022-11-10 14:47:06 +13:00
|
|
|
attribute(:hashed_password, :string, allow_nil?: true, sensitive?: true, private?: true)
|
2022-10-25 11:07:07 +13:00
|
|
|
|
|
|
|
create_timestamp(:created_at)
|
|
|
|
update_timestamp(:updated_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2022-10-31 16:43:00 +13:00
|
|
|
read :read do
|
|
|
|
primary? true
|
|
|
|
end
|
|
|
|
|
2022-10-25 11:07:07 +13:00
|
|
|
destroy :destroy do
|
|
|
|
primary? true
|
|
|
|
end
|
2022-10-31 16:43:00 +13:00
|
|
|
|
|
|
|
read :current_user do
|
|
|
|
get? true
|
|
|
|
manual Example.CurrentUserRead
|
|
|
|
end
|
2022-11-04 21:05:47 +13:00
|
|
|
|
|
|
|
update :update do
|
|
|
|
primary? true
|
|
|
|
end
|
2022-11-10 14:47:06 +13:00
|
|
|
|
|
|
|
create :register_with_oauth2 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
|
2022-11-23 09:09:41 +13:00
|
|
|
change AshAuthentication.Strategy.OAuth2.IdentityChange
|
2022-11-10 14:47:06 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
read :sign_in_with_oauth2 do
|
|
|
|
argument :user_info, :map, allow_nil?: false
|
|
|
|
argument :oauth_tokens, :map, allow_nil?: false
|
2022-11-23 09:09:41 +13:00
|
|
|
prepare AshAuthentication.Strategy.OAuth2.SignInPreparation
|
2022-11-10 14:47:06 +13:00
|
|
|
|
|
|
|
filter expr(username == get_path(^arg(:user_info), [:nickname]))
|
|
|
|
end
|
2022-10-25 11:07:07 +13:00
|
|
|
end
|
|
|
|
|
2022-10-31 16:43:00 +13:00
|
|
|
graphql do
|
|
|
|
type :user
|
|
|
|
|
|
|
|
queries do
|
|
|
|
get(:get_user, :read)
|
|
|
|
list(:list_users, :read)
|
|
|
|
read_one(:current_user, :current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
mutations do
|
2022-11-23 09:09:41 +13:00
|
|
|
create :register, :register_with_password
|
2022-10-31 16:43:00 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
json_api do
|
|
|
|
type "user"
|
|
|
|
|
|
|
|
routes do
|
|
|
|
base("/users")
|
|
|
|
get(:read)
|
|
|
|
get(:current_user, route: "/me")
|
|
|
|
index(:read)
|
2022-11-23 09:09:41 +13:00
|
|
|
post(:register_with_password)
|
2022-10-31 16:43:00 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-25 11:07:07 +13:00
|
|
|
postgres do
|
2022-11-23 09:09:41 +13:00
|
|
|
table("user")
|
2022-10-25 11:07:07 +13:00
|
|
|
repo(Example.Repo)
|
|
|
|
end
|
|
|
|
|
|
|
|
authentication do
|
|
|
|
api(Example)
|
|
|
|
|
2022-11-23 09:09:41 +13:00
|
|
|
tokens do
|
|
|
|
enabled?(true)
|
|
|
|
revocation_resource(Example.TokenRevocation)
|
|
|
|
end
|
2022-10-25 11:07:07 +13:00
|
|
|
|
2022-11-23 09:09:41 +13:00
|
|
|
strategies do
|
|
|
|
confirmation do
|
|
|
|
monitor_fields([:username])
|
|
|
|
inhibit_updates?(true)
|
|
|
|
|
|
|
|
sender(fn user, token ->
|
|
|
|
Logger.debug("Confirmation request for user #{user.username}, token #{inspect(token)}")
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
password :password do
|
|
|
|
resettable do
|
|
|
|
sender(fn user, token ->
|
|
|
|
Logger.debug(
|
|
|
|
"Password reset request for user #{user.username}, token #{inspect(token)}"
|
|
|
|
)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
oauth2 :oauth2 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)
|
|
|
|
authorization_params(scope: "openid profile email")
|
|
|
|
auth_method(:client_secret_post)
|
|
|
|
identity_resource(Example.UserIdentity)
|
|
|
|
end
|
|
|
|
end
|
2022-11-02 18:18:20 +13:00
|
|
|
end
|
|
|
|
|
2022-10-25 11:07:07 +13:00
|
|
|
identities do
|
2022-11-04 21:05:47 +13:00
|
|
|
identity(:username, [:username], eager_check_with: Example)
|
2022-10-25 11:07:07 +13:00
|
|
|
end
|
|
|
|
|
2022-11-23 09:09:41 +13:00
|
|
|
def get_config(path, _resource) do
|
|
|
|
value =
|
|
|
|
:ash_authentication
|
|
|
|
|> Application.get_all_env()
|
|
|
|
|> get_in(path)
|
|
|
|
|
|
|
|
{:ok, value}
|
2022-10-25 11:07:07 +13:00
|
|
|
end
|
|
|
|
end
|