oauth_plug.ex 3.3 KB
Newer Older
1
# Pleroma: A lightweight social networking server
kaniini's avatar
kaniini committed
2
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 4
# SPDX-License-Identifier: AGPL-3.0-only

5 6
defmodule Pleroma.Plugs.OAuthPlug do
  import Plug.Conn
Maksim's avatar
fix/273  
Maksim committed
7
  import Ecto.Query
8

Haelwenn's avatar
Haelwenn committed
9
  alias Pleroma.Repo
10
  alias Pleroma.User
11
  alias Pleroma.Web.OAuth.App
Haelwenn's avatar
Haelwenn committed
12
  alias Pleroma.Web.OAuth.Token
Maksim's avatar
fix/273  
Maksim committed
13 14 15 16

  @realm_reg Regex.compile!("Bearer\:?\s+(.*)$", "i")

  def init(options), do: options
17 18

  def call(%{assigns: %{user: %User{}}} = conn, _), do: conn
lain's avatar
lain committed
19

20 21 22 23 24 25
  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
26 27 28 29 30 31 32 33 34
      _ ->
        # 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
35 36 37
    end
  end

Thog's avatar
Thog committed
38
  def call(conn, _) do
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    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
59 60
    end
  end
Maksim's avatar
fix/273  
Maksim committed
61 62 63

  # Gets user by token
  #
64 65
  @spec fetch_user_and_token(String.t()) :: {:ok, User.t(), Token.t()} | nil
  defp fetch_user_and_token(token) do
href's avatar
href committed
66 67 68 69 70 71
    query =
      from(t in Token,
        where: t.token == ^token,
        join: user in assoc(t, :user),
        preload: [user: user]
      )
Maksim's avatar
fix/273  
Maksim committed
72

73
    # credo:disable-for-next-line Credo.Check.Readability.MaxLineLength
74
    with %Token{user: user} = token_record <- Repo.one(query) do
75
      {:ok, user, token_record}
Maksim's avatar
fix/273  
Maksim committed
76 77 78
    end
  end

79 80 81 82 83 84 85 86 87 88
  @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

Maksim's avatar
fix/273  
Maksim committed
89 90 91 92 93 94 95 96 97 98 99 100
  # 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
  #
101 102
  @spec fetch_token_str(Plug.Conn.t()) :: :no_token_found | {:ok, String.t()}
  defp fetch_token_str(%Plug.Conn{} = conn) do
Maksim's avatar
fix/273  
Maksim committed
103 104
    headers = get_req_header(conn, "authorization")

105
    with :no_token_found <- fetch_token_str(headers),
Maksim's avatar
fix/273  
Maksim committed
106 107 108
         do: fetch_token_from_session(conn)
  end

109 110
  @spec fetch_token_str(Keyword.t()) :: :no_token_found | {:ok, String.t()}
  defp fetch_token_str([]), do: :no_token_found
Maksim's avatar
fix/273  
Maksim committed
111

112
  defp fetch_token_str([token | tail]) do
Maksim's avatar
fix/273  
Maksim committed
113 114 115 116
    trimmed_token = String.trim(token)

    case Regex.run(@realm_reg, trimmed_token) do
      [_, match] -> {:ok, String.trim(match)}
117
      _ -> fetch_token_str(tail)
Maksim's avatar
fix/273  
Maksim committed
118 119
    end
  end
120
end