2023-02-15 19:31:23 +01:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Application do
|
|
|
|
@moduledoc """
|
|
|
|
Handles the Application-related GraphQL calls.
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Applications, as: ApplicationManager
|
2023-02-21 14:50:09 +01:00
|
|
|
alias Mobilizon.Applications.{Application, ApplicationDeviceActivation, ApplicationToken}
|
2023-02-15 19:31:23 +01:00
|
|
|
alias Mobilizon.Service.Auth.Applications
|
|
|
|
alias Mobilizon.Users.User
|
|
|
|
import Mobilizon.Web.Gettext, only: [dgettext: 2]
|
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
@doc """
|
2023-03-17 18:10:59 +01:00
|
|
|
Authorize an application
|
2023-02-15 19:31:23 +01:00
|
|
|
"""
|
|
|
|
@spec authorize(any(), map(), Absinthe.Resolution.t()) :: {:ok, map()} | {:error, String.t()}
|
|
|
|
def authorize(
|
|
|
|
_parent,
|
|
|
|
%{client_id: client_id, redirect_uri: redirect_uri, scope: scope, state: state},
|
|
|
|
%{context: %{current_user: %User{id: user_id}}}
|
|
|
|
) do
|
|
|
|
case Applications.autorize(client_id, redirect_uri, scope, user_id) do
|
2023-03-17 18:10:59 +01:00
|
|
|
{:ok,
|
|
|
|
%ApplicationToken{
|
|
|
|
application: %Application{client_id: client_id},
|
|
|
|
scope: scope,
|
|
|
|
authorization_code: code
|
|
|
|
}} ->
|
|
|
|
{:ok, %{code: code, state: state, client_id: client_id, scope: scope}}
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = err} ->
|
|
|
|
{:error, err}
|
2023-02-15 19:31:23 +01:00
|
|
|
|
|
|
|
{:error, :application_not_found} ->
|
|
|
|
{:error,
|
|
|
|
dgettext(
|
|
|
|
"errors",
|
|
|
|
"No application with this client_id was found"
|
|
|
|
)}
|
|
|
|
|
|
|
|
{:error, :redirect_uri_not_in_allowed} ->
|
|
|
|
{:error,
|
|
|
|
dgettext(
|
|
|
|
"errors",
|
|
|
|
"The given redirect_uri is not in the list of allowed redirect URIs"
|
|
|
|
)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize(_parent, _args, _context) do
|
2023-03-17 18:10:59 +01:00
|
|
|
{:error, :unauthenticated}
|
2023-02-15 19:31:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec get_application(any(), map(), Absinthe.Resolution.t()) ::
|
2023-03-17 18:10:59 +01:00
|
|
|
{:ok, Application.t()} | {:error, :application_not_found | :unauthenticated}
|
2023-02-15 19:31:23 +01:00
|
|
|
def get_application(_parent, %{client_id: client_id}, %{context: %{current_user: %User{}}}) do
|
|
|
|
case ApplicationManager.get_application_by_client_id(client_id) do
|
|
|
|
%Application{} = application ->
|
|
|
|
{:ok, application}
|
|
|
|
|
|
|
|
nil ->
|
2023-03-17 18:10:59 +01:00
|
|
|
{:error, :application_not_found}
|
2023-02-15 19:31:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_application(_parent, _args, _resolution) do
|
|
|
|
{:error, :unauthenticated}
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_user_applications(_parent, _args, %{context: %{current_user: %User{id: user_id}}}) do
|
|
|
|
{:ok, ApplicationManager.list_application_tokens_for_user_id(user_id)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_user_applications(_parent, _args, _resolution) do
|
|
|
|
{:error, :unauthenticated}
|
|
|
|
end
|
|
|
|
|
|
|
|
def revoke_application_token(_parent, %{app_token_id: app_token_id}, %{
|
|
|
|
context: %{current_user: %User{id: user_id}}
|
|
|
|
}) do
|
|
|
|
case ApplicationManager.get_application_token(app_token_id) do
|
|
|
|
%ApplicationToken{user_id: ^user_id} = app_token ->
|
|
|
|
case Applications.revoke_application_token(app_token) do
|
|
|
|
{:ok, %{delete_app_token: app_token, delete_guardian_tokens: _delete_guardian_tokens}} ->
|
|
|
|
{:ok, %{id: app_token.id}}
|
|
|
|
|
|
|
|
{:error, _, _, _} ->
|
|
|
|
{:error, dgettext("errors", "Error while revoking token")}
|
|
|
|
end
|
|
|
|
|
|
|
|
_ ->
|
2023-03-17 18:10:59 +01:00
|
|
|
{:error, :application_token_not_found}
|
2023-02-15 19:31:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def revoke_application_token(_parent, _args, _resolution) do
|
|
|
|
{:error, :unauthenticated}
|
|
|
|
end
|
2023-02-21 14:50:09 +01:00
|
|
|
|
|
|
|
def activate_device(_parent, %{user_code: user_code}, %{
|
|
|
|
context: %{current_user: %User{} = user}
|
|
|
|
}) do
|
2023-03-17 18:10:59 +01:00
|
|
|
case Applications.activate_device(user_code, user) do
|
|
|
|
{:ok, %ApplicationDeviceActivation{} = app_device_activation} ->
|
|
|
|
{:ok, app_device_activation |> Map.from_struct() |> Map.take([:application, :id, :scope])}
|
|
|
|
|
|
|
|
{:error, :expired} ->
|
|
|
|
{:error, dgettext("errors", "The given user code has expired")}
|
|
|
|
|
|
|
|
{:error, :not_found} ->
|
|
|
|
{:error, dgettext("errors", "The given user code is invalid")}
|
2023-02-21 14:50:09 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-17 18:10:59 +01:00
|
|
|
def activate_device(_parent, _args, _resolution) do
|
|
|
|
{:error, :unauthenticated}
|
|
|
|
end
|
|
|
|
|
2023-02-21 14:50:09 +01:00
|
|
|
@spec authorize_device_application(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, map()} | {:error, String.t()}
|
|
|
|
def authorize_device_application(
|
|
|
|
_parent,
|
|
|
|
%{client_id: client_id, user_code: user_code},
|
2023-03-17 18:10:59 +01:00
|
|
|
%{context: %{current_user: %User{}}}
|
2023-02-21 14:50:09 +01:00
|
|
|
) do
|
2023-03-17 18:10:59 +01:00
|
|
|
case Applications.autorize_device_application(client_id, user_code) do
|
|
|
|
{:ok, %ApplicationDeviceActivation{application: app}} ->
|
2023-02-21 14:50:09 +01:00
|
|
|
{:ok, app}
|
|
|
|
|
2023-03-17 18:10:59 +01:00
|
|
|
{:error, :not_confirmed} ->
|
2023-02-21 14:50:09 +01:00
|
|
|
{:error,
|
|
|
|
dgettext(
|
|
|
|
"errors",
|
2023-03-17 18:10:59 +01:00
|
|
|
"The device user code was not provided before approving the application"
|
2023-02-21 14:50:09 +01:00
|
|
|
)}
|
2023-03-17 18:10:59 +01:00
|
|
|
|
|
|
|
{:error, :not_found} ->
|
|
|
|
{:error,
|
|
|
|
dgettext(
|
|
|
|
"errors",
|
|
|
|
"The given user code is invalid"
|
|
|
|
)}
|
|
|
|
|
|
|
|
{:error, :expired} ->
|
|
|
|
{:error, dgettext("errors", "The given user code has expired")}
|
2023-02-21 14:50:09 +01:00
|
|
|
end
|
|
|
|
end
|
2023-03-17 18:10:59 +01:00
|
|
|
|
|
|
|
def authorize_device_application(_parent, _args, _resolution) do
|
|
|
|
{:error, :unauthenticated}
|
|
|
|
end
|
2023-02-15 19:31:23 +01:00
|
|
|
end
|