2018-12-23 13:04:54 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2022-02-25 23:11:42 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 13:04:54 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-06-24 00:59:21 -06:00
|
|
|
defmodule Pleroma.Web.Plugs.OAuthPlug do
|
2020-11-21 09:47:25 -07:00
|
|
|
@moduledoc "Performs OAuth authentication by token from params / headers / cookies."
|
|
|
|
|
2017-09-06 11:06:25 -06:00
|
|
|
import Plug.Conn
|
2018-12-05 07:29:49 -07:00
|
|
|
import Ecto.Query
|
2017-09-06 11:06:25 -06:00
|
|
|
|
2020-11-25 11:47:23 -07:00
|
|
|
alias Pleroma.Helpers.AuthHelper
|
2019-02-09 08:16:26 -07:00
|
|
|
alias Pleroma.Repo
|
2019-03-04 19:52:23 -07:00
|
|
|
alias Pleroma.User
|
2019-05-13 12:35:45 -06:00
|
|
|
alias Pleroma.Web.OAuth.App
|
2019-02-09 08:16:26 -07:00
|
|
|
alias Pleroma.Web.OAuth.Token
|
2018-12-05 07:29:49 -07:00
|
|
|
|
|
|
|
@realm_reg Regex.compile!("Bearer\:?\s+(.*)$", "i")
|
|
|
|
|
|
|
|
def init(options), do: options
|
2017-09-06 11:06:25 -06:00
|
|
|
|
|
|
|
def call(%{assigns: %{user: %User{}}} = conn, _), do: conn
|
2018-03-30 07:01:53 -06:00
|
|
|
|
2017-11-18 18:22:07 -07:00
|
|
|
def call(conn, _) do
|
2020-11-21 09:47:25 -07:00
|
|
|
with {:ok, token_str} <- fetch_token_str(conn) do
|
|
|
|
with {:ok, user, user_token} <- fetch_user_and_token(token_str),
|
|
|
|
false <- Token.is_expired?(user_token) do
|
2019-05-13 12:35:45 -06:00
|
|
|
conn
|
2020-11-21 09:47:25 -07:00
|
|
|
|> assign(:token, user_token)
|
|
|
|
|> assign(:user, user)
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
with {:ok, app, app_token} <- fetch_app_and_token(token_str),
|
|
|
|
false <- Token.is_expired?(app_token) do
|
|
|
|
conn
|
|
|
|
|> assign(:token, app_token)
|
|
|
|
|> assign(:app, app)
|
|
|
|
else
|
|
|
|
_ -> conn
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
_ -> conn
|
2017-09-06 11:06:25 -06:00
|
|
|
end
|
|
|
|
end
|
2018-12-05 07:29:49 -07:00
|
|
|
|
|
|
|
# Gets user by token
|
|
|
|
#
|
2018-12-07 01:55:28 -07:00
|
|
|
@spec fetch_user_and_token(String.t()) :: {:ok, User.t(), Token.t()} | nil
|
|
|
|
defp fetch_user_and_token(token) do
|
2022-08-23 09:15:06 -06:00
|
|
|
token_query =
|
2019-01-26 07:55:53 -07:00
|
|
|
from(t in Token,
|
2022-08-23 09:15:06 -06:00
|
|
|
where: t.token == ^token
|
2019-01-26 07:55:53 -07:00
|
|
|
)
|
2018-12-05 07:29:49 -07:00
|
|
|
|
2022-08-23 09:15:06 -06:00
|
|
|
with %Token{user_id: user_id} = token_record <- Repo.one(token_query),
|
|
|
|
false <- is_nil(user_id),
|
|
|
|
%User{} = user <- User.get_cached_by_id(user_id) do
|
2018-12-07 01:55:28 -07:00
|
|
|
{:ok, user, token_record}
|
2022-08-23 09:15:06 -06:00
|
|
|
else
|
|
|
|
_ -> nil
|
2018-12-05 07:29:49 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-13 12:35:45 -06:00
|
|
|
@spec fetch_app_and_token(String.t()) :: {:ok, App.t(), Token.t()} | nil
|
|
|
|
defp fetch_app_and_token(token) do
|
|
|
|
query =
|
|
|
|
from(t in Token, where: t.token == ^token, join: app in assoc(t, :app), preload: [app: app])
|
|
|
|
|
|
|
|
with %Token{app: app} = token_record <- Repo.one(query) do
|
|
|
|
{:ok, app, token_record}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-21 09:47:25 -07:00
|
|
|
# Gets token string from conn (in params / headers / session)
|
2018-12-05 07:29:49 -07:00
|
|
|
#
|
2020-11-21 09:47:25 -07:00
|
|
|
@spec fetch_token_str(Plug.Conn.t() | list(String.t())) :: :no_token_found | {:ok, String.t()}
|
|
|
|
defp fetch_token_str(%Plug.Conn{params: %{"access_token" => access_token}} = _conn) do
|
|
|
|
{:ok, access_token}
|
2018-12-05 07:29:49 -07:00
|
|
|
end
|
|
|
|
|
2018-12-07 01:55:28 -07:00
|
|
|
defp fetch_token_str(%Plug.Conn{} = conn) do
|
2018-12-05 07:29:49 -07:00
|
|
|
headers = get_req_header(conn, "authorization")
|
|
|
|
|
2020-11-21 09:47:25 -07:00
|
|
|
with {:ok, token} <- fetch_token_str(headers) do
|
|
|
|
{:ok, token}
|
|
|
|
else
|
|
|
|
_ -> fetch_token_from_session(conn)
|
|
|
|
end
|
2018-12-05 07:29:49 -07:00
|
|
|
end
|
|
|
|
|
2018-12-07 01:55:28 -07:00
|
|
|
defp fetch_token_str([token | tail]) do
|
2018-12-05 07:29:49 -07:00
|
|
|
trimmed_token = String.trim(token)
|
|
|
|
|
|
|
|
case Regex.run(@realm_reg, trimmed_token) do
|
|
|
|
[_, match] -> {:ok, String.trim(match)}
|
2018-12-07 01:55:28 -07:00
|
|
|
_ -> fetch_token_str(tail)
|
2018-12-05 07:29:49 -07:00
|
|
|
end
|
|
|
|
end
|
2020-11-21 09:47:25 -07:00
|
|
|
|
|
|
|
defp fetch_token_str([]), do: :no_token_found
|
|
|
|
|
|
|
|
@spec fetch_token_from_session(Plug.Conn.t()) :: :no_token_found | {:ok, String.t()}
|
|
|
|
defp fetch_token_from_session(conn) do
|
2020-11-25 11:47:23 -07:00
|
|
|
case AuthHelper.get_session_token(conn) do
|
2020-11-21 09:47:25 -07:00
|
|
|
nil -> :no_token_found
|
|
|
|
token -> {:ok, token}
|
|
|
|
end
|
|
|
|
end
|
2017-09-06 11:06:25 -06:00
|
|
|
end
|