2018-12-23 21:04:54 +01:00
|
|
|
# Pleroma: A lightweight social networking server
|
2018-12-31 16:41:47 +01:00
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 21:04:54 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-09-06 19:06:25 +02:00
|
|
|
defmodule Pleroma.Plugs.OAuthPlug do
|
|
|
|
import Plug.Conn
|
2018-12-05 15:29:49 +01:00
|
|
|
import Ecto.Query
|
2017-09-06 19:06:25 +02:00
|
|
|
|
2019-02-09 16:16:26 +01:00
|
|
|
alias Pleroma.Repo
|
2019-03-05 03:52:23 +01:00
|
|
|
alias Pleroma.User
|
2019-05-13 20:35:45 +02:00
|
|
|
alias Pleroma.Web.OAuth.App
|
2019-02-09 16:16:26 +01:00
|
|
|
alias Pleroma.Web.OAuth.Token
|
2018-12-05 15:29:49 +01:00
|
|
|
|
|
|
|
@realm_reg Regex.compile!("Bearer\:?\s+(.*)$", "i")
|
|
|
|
|
|
|
|
def init(options), do: options
|
2017-09-06 19:06:25 +02:00
|
|
|
|
|
|
|
def call(%{assigns: %{user: %User{}}} = conn, _), do: conn
|
2018-03-30 15:01:53 +02:00
|
|
|
|
2019-05-02 14:04:00 +02:00
|
|
|
def call(%{params: %{"access_token" => access_token}} = conn, _) do
|
|
|
|
with {:ok, user, token_record} <- fetch_user_and_token(access_token) do
|
|
|
|
conn
|
|
|
|
|> assign(:token, token_record)
|
|
|
|
|> assign(:user, user)
|
|
|
|
else
|
2019-05-13 20:35:45 +02:00
|
|
|
_ ->
|
|
|
|
# token found, but maybe only with app
|
|
|
|
with {:ok, app, token_record} <- fetch_app_and_token(access_token) do
|
|
|
|
conn
|
|
|
|
|> assign(:token, token_record)
|
|
|
|
|> assign(:app, app)
|
|
|
|
else
|
|
|
|
_ -> conn
|
|
|
|
end
|
2019-05-02 14:04:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-19 02:22:07 +01:00
|
|
|
def call(conn, _) do
|
2019-05-13 20:35:45 +02:00
|
|
|
case fetch_token_str(conn) do
|
|
|
|
{:ok, token} ->
|
|
|
|
with {:ok, user, token_record} <- fetch_user_and_token(token) do
|
|
|
|
conn
|
|
|
|
|> assign(:token, token_record)
|
|
|
|
|> assign(:user, user)
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
# token found, but maybe only with app
|
|
|
|
with {:ok, app, token_record} <- fetch_app_and_token(token) do
|
|
|
|
conn
|
|
|
|
|> assign(:token, token_record)
|
|
|
|
|> assign(:app, app)
|
|
|
|
else
|
|
|
|
_ -> conn
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
conn
|
2017-09-06 19:06:25 +02:00
|
|
|
end
|
|
|
|
end
|
2018-12-05 15:29:49 +01:00
|
|
|
|
|
|
|
# Gets user by token
|
|
|
|
#
|
2018-12-07 09:55:28 +01:00
|
|
|
@spec fetch_user_and_token(String.t()) :: {:ok, User.t(), Token.t()} | nil
|
|
|
|
defp fetch_user_and_token(token) do
|
2019-01-26 15:55:53 +01:00
|
|
|
query =
|
|
|
|
from(t in Token,
|
|
|
|
where: t.token == ^token,
|
|
|
|
join: user in assoc(t, :user),
|
|
|
|
preload: [user: user]
|
|
|
|
)
|
2018-12-05 15:29:49 +01:00
|
|
|
|
2019-03-05 05:37:33 +01:00
|
|
|
# credo:disable-for-next-line Credo.Check.Readability.MaxLineLength
|
2019-10-16 20:59:21 +02:00
|
|
|
with %Token{user: %{deactivated: false} = user} = token_record <- Repo.one(query) do
|
2018-12-07 09:55:28 +01:00
|
|
|
{:ok, user, token_record}
|
2018-12-05 15:29:49 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-13 20:35:45 +02: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
|
|
|
|
|
2018-12-05 15:29:49 +01:00
|
|
|
# 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
|
|
|
|
#
|
2018-12-07 09:55:28 +01:00
|
|
|
@spec fetch_token_str(Plug.Conn.t()) :: :no_token_found | {:ok, String.t()}
|
|
|
|
defp fetch_token_str(%Plug.Conn{} = conn) do
|
2018-12-05 15:29:49 +01:00
|
|
|
headers = get_req_header(conn, "authorization")
|
|
|
|
|
2018-12-07 09:55:28 +01:00
|
|
|
with :no_token_found <- fetch_token_str(headers),
|
2018-12-05 15:29:49 +01:00
|
|
|
do: fetch_token_from_session(conn)
|
|
|
|
end
|
|
|
|
|
2018-12-07 09:55:28 +01:00
|
|
|
@spec fetch_token_str(Keyword.t()) :: :no_token_found | {:ok, String.t()}
|
|
|
|
defp fetch_token_str([]), do: :no_token_found
|
2018-12-05 15:29:49 +01:00
|
|
|
|
2018-12-07 09:55:28 +01:00
|
|
|
defp fetch_token_str([token | tail]) do
|
2018-12-05 15:29:49 +01:00
|
|
|
trimmed_token = String.trim(token)
|
|
|
|
|
|
|
|
case Regex.run(@realm_reg, trimmed_token) do
|
|
|
|
[_, match] -> {:ok, String.trim(match)}
|
2018-12-07 09:55:28 +01:00
|
|
|
_ -> fetch_token_str(tail)
|
2018-12-05 15:29:49 +01:00
|
|
|
end
|
|
|
|
end
|
2017-09-06 19:06:25 +02:00
|
|
|
end
|