debianize-mobilizon/lib/mobilizon_web/auth/guardian.ex

80 lines
2.0 KiB
Elixir
Raw Normal View History

2020-01-23 21:59:50 +01:00
defmodule MobilizonWeb.Auth.Guardian do
@moduledoc """
Handles the JWT tokens encoding and decoding
"""
2020-01-23 21:59:50 +01:00
use Guardian,
otp_app: :mobilizon,
permissions: %{
superuser: [:moderate, :super],
user: [:base]
}
2017-12-09 14:58:37 +01:00
alias Mobilizon.Users
alias Mobilizon.Users.User
2020-01-23 21:59:50 +01:00
require Logger
2017-12-09 14:58:37 +01:00
def subject_for_token(%User{} = user, _claims) do
2017-12-09 14:58:37 +01:00
{:ok, "User:" <> to_string(user.id)}
end
def subject_for_token(_, _) do
{:error, :unknown_resource}
end
def resource_from_claims(%{"sub" => "User:" <> uid_str}) do
Logger.debug(fn -> "Receiving claim for user #{uid_str}" end)
2017-12-09 14:58:37 +01:00
try do
case Integer.parse(uid_str) do
{uid, ""} ->
{:ok, Users.get_user_with_actors!(uid)}
2017-12-09 14:58:37 +01:00
_ ->
{:error, :invalid_id}
end
rescue
Ecto.NoResultsError -> {:error, :no_result}
end
end
def resource_from_claims(_) do
2017-12-09 14:58:37 +01:00
{:error, :reason_for_error}
end
def after_encode_and_sign(resource, claims, token, _options) do
Logger.debug(fn -> "after_encode_and_sign #{inspect(claims)}" end)
with {:ok, _} <- Guardian.DB.after_encode_and_sign(resource, claims["typ"], claims, token) do
{:ok, token}
end
end
def on_verify(claims, token, _options) do
with {:ok, _} <- Guardian.DB.on_verify(claims, token) do
{:ok, claims}
end
end
def on_revoke(claims, token, _options) do
with {:ok, _} <- Guardian.DB.on_revoke(claims, token) do
{:ok, claims}
end
end
def on_refresh({old_token, old_claims}, {new_token, new_claims}, _options) do
with {:ok, _, _} <- Guardian.DB.on_refresh({old_token, old_claims}, {new_token, new_claims}) do
{:ok, {old_token, old_claims}, {new_token, new_claims}}
end
end
def on_exchange(old_stuff, new_stuff, options), do: on_refresh(old_stuff, new_stuff, options)
# def build_claims(claims, _resource, opts) do
# claims = claims
# |> encode_permissions_into_claims!(Keyword.get(opts, :permissions))
# {:ok, claims}
# end
end