Introduce application tokens
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -6,6 +6,8 @@ defmodule Mobilizon.Web.Auth.Context do
|
||||
|
||||
import Plug.Conn
|
||||
|
||||
alias Mobilizon.Applications.Application, as: AuthApplication
|
||||
alias Mobilizon.Applications.ApplicationToken
|
||||
alias Mobilizon.Users.User
|
||||
|
||||
@spec init(Plug.opts()) :: Plug.opts()
|
||||
@@ -28,18 +30,13 @@ defmodule Mobilizon.Web.Auth.Context do
|
||||
|
||||
{conn, context} =
|
||||
case Guardian.Plug.current_resource(conn) do
|
||||
%User{id: user_id, email: user_email} = user ->
|
||||
if Application.get_env(:sentry, :dsn) != nil do
|
||||
Sentry.Context.set_user_context(%{
|
||||
id: user_id,
|
||||
email: user_email,
|
||||
ip_address: context.ip
|
||||
})
|
||||
end
|
||||
%User{} = user ->
|
||||
set_user_context({conn, context}, user)
|
||||
|
||||
context = Map.put(context, :current_user, user)
|
||||
conn = assign(conn, :user_locale, user.locale)
|
||||
{conn, context}
|
||||
%ApplicationToken{user: %User{} = user} = app_token ->
|
||||
conn
|
||||
|> set_app_token_context(context, app_token)
|
||||
|> set_user_context(user)
|
||||
|
||||
nil ->
|
||||
{conn, context}
|
||||
@@ -49,4 +46,35 @@ defmodule Mobilizon.Web.Auth.Context do
|
||||
|
||||
put_private(conn, :absinthe, %{context: context})
|
||||
end
|
||||
|
||||
defp set_user_context({conn, context}, %User{id: user_id, email: user_email} = user) do
|
||||
if Application.get_env(:sentry, :dsn) != nil do
|
||||
Sentry.Context.set_user_context(%{
|
||||
id: user_id,
|
||||
email: user_email,
|
||||
ip_address: context.ip
|
||||
})
|
||||
end
|
||||
|
||||
context = Map.put(context, :current_user, user)
|
||||
conn = assign(conn, :user_locale, user.locale)
|
||||
{conn, context}
|
||||
end
|
||||
|
||||
defp set_app_token_context(
|
||||
conn,
|
||||
context,
|
||||
%ApplicationToken{application: %AuthApplication{client_id: client_id} = app} = app_token
|
||||
) do
|
||||
if Application.get_env(:sentry, :dsn) != nil do
|
||||
Sentry.Context.set_user_context(%{
|
||||
app_token_client_id: client_id
|
||||
})
|
||||
end
|
||||
|
||||
context =
|
||||
context |> Map.put(:current_auth_app_token, app_token) |> Map.put(:current_auth_app, app)
|
||||
|
||||
{conn, context}
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user