2017-09-06 18:06:25 +01:00
|
|
|
defmodule Pleroma.Plugs.OAuthPlug do
|
|
|
|
import Plug.Conn
|
2018-12-05 14:29:49 +00:00
|
|
|
import Ecto.Query
|
2017-09-06 18:06:25 +01:00
|
|
|
|
2018-12-05 14:29:49 +00:00
|
|
|
alias Pleroma.{
|
|
|
|
User,
|
|
|
|
Repo,
|
|
|
|
Web.OAuth.Token
|
|
|
|
}
|
|
|
|
|
|
|
|
@realm_reg Regex.compile!("Bearer\:?\s+(.*)$", "i")
|
|
|
|
|
|
|
|
def init(options), do: options
|
2017-09-06 18:06:25 +01:00
|
|
|
|
|
|
|
def call(%{assigns: %{user: %User{}}} = conn, _), do: conn
|
2018-03-30 14:01:53 +01:00
|
|
|
|
2017-11-19 01:22:07 +00:00
|
|
|
def call(conn, _) do
|
2018-12-05 14:29:49 +00:00
|
|
|
with {:ok, token} <- fetch_token(conn),
|
|
|
|
{:ok, user} <- fetch_user(token) do
|
|
|
|
assign(conn, :user, user)
|
2017-09-06 18:06:25 +01:00
|
|
|
else
|
|
|
|
_ -> conn
|
|
|
|
end
|
|
|
|
end
|
2018-12-05 14:29:49 +00:00
|
|
|
|
|
|
|
# Gets user by token
|
|
|
|
#
|
|
|
|
@spec fetch_user(String.t()) :: {:ok, User.t()} | nil
|
|
|
|
defp fetch_user(token) do
|
|
|
|
query = from(q in Token, where: q.token == ^token, preload: [:user])
|
|
|
|
|
|
|
|
with %Token{user: %{info: %{deactivated: false} = _} = user} <- Repo.one(query) do
|
|
|
|
{:ok, user}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Gets token from session by :oauth_token key
|
|
|
|
#
|
|
|
|
@spec fetch_token_from_session(Plug.Conn.t()) :: :no_token_found | {:ok, String.t()}
|
|
|
|
defp fetch_token_from_session(conn) do
|
|
|
|
case get_session(conn, :oauth_token) do
|
|
|
|
nil -> :no_token_found
|
|
|
|
token -> {:ok, token}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Gets token from headers
|
|
|
|
#
|
|
|
|
@spec fetch_token(Plug.Conn.t()) :: :no_token_found | {:ok, String.t()}
|
|
|
|
defp fetch_token(%Plug.Conn{} = conn) do
|
|
|
|
headers = get_req_header(conn, "authorization")
|
|
|
|
|
|
|
|
with :no_token_found <- fetch_token(headers),
|
|
|
|
do: fetch_token_from_session(conn)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec fetch_token(Keyword.t()) :: :no_token_found | {:ok, String.t()}
|
|
|
|
defp fetch_token([]), do: :no_token_found
|
|
|
|
|
|
|
|
defp fetch_token([token | tail]) do
|
|
|
|
trimmed_token = String.trim(token)
|
|
|
|
|
|
|
|
case Regex.run(@realm_reg, trimmed_token) do
|
|
|
|
[_, match] -> {:ok, String.trim(match)}
|
|
|
|
_ -> fetch_token(tail)
|
|
|
|
end
|
|
|
|
end
|
2017-09-06 18:06:25 +01:00
|
|
|
end
|