oauth_plug.ex 2.08 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
Haelwenn's avatar
Haelwenn committed
11
  alias Pleroma.Web.OAuth.Token
Maksim's avatar
fix/273    
Maksim committed
12
13
14
15

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

  def init(options), do: options
16
17

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

Thog's avatar
Thog committed
19
  def call(conn, _) do
20
21
    with {:ok, token_str} <- fetch_token_str(conn),
         {:ok, user, token_record} <- fetch_user_and_token(token_str) do
22
      conn
23
      |> assign(:token, token_record)
24
25
26
27
28
      |> assign(:user, user)
    else
      _ -> conn
    end
  end
Maksim's avatar
fix/273    
Maksim committed
29
30
31

  # Gets user by token
  #
32
33
  @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
34
35
36
37
38
39
    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
40

41
42
    with %Token{user: %{info: %{deactivated: false} = _} = user} = token_record <- Repo.one(query) do
      {:ok, user, token_record}
Maksim's avatar
fix/273    
Maksim committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    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
  #
58
59
  @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
60
61
    headers = get_req_header(conn, "authorization")

62
    with :no_token_found <- fetch_token_str(headers),
Maksim's avatar
fix/273    
Maksim committed
63
64
65
         do: fetch_token_from_session(conn)
  end

66
67
  @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
68

69
  defp fetch_token_str([token | tail]) do
Maksim's avatar
fix/273    
Maksim committed
70
71
72
73
    trimmed_token = String.trim(token)

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