remove `unread_conversation_count` from User
[akkoma] / lib / pleroma / web / plugs / o_auth_plug.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Plugs.OAuthPlug do
6 import Plug.Conn
7 import Ecto.Query
8
9 alias Pleroma.Repo
10 alias Pleroma.User
11 alias Pleroma.Web.OAuth.App
12 alias Pleroma.Web.OAuth.Token
13
14 @realm_reg Regex.compile!("Bearer\:?\s+(.*)$", "i")
15
16 def init(options), do: options
17
18 def call(%{assigns: %{user: %User{}}} = conn, _), do: conn
19
20 def call(%{params: %{"access_token" => access_token}} = conn, _) do
21 with {:ok, user, token_record} <- fetch_user_and_token(access_token) do
22 conn
23 |> assign(:token, token_record)
24 |> assign(:user, user)
25 else
26 _ ->
27 # token found, but maybe only with app
28 with {:ok, app, token_record} <- fetch_app_and_token(access_token) do
29 conn
30 |> assign(:token, token_record)
31 |> assign(:app, app)
32 else
33 _ -> conn
34 end
35 end
36 end
37
38 def call(conn, _) do
39 case fetch_token_str(conn) do
40 {:ok, token} ->
41 with {:ok, user, token_record} <- fetch_user_and_token(token) do
42 conn
43 |> assign(:token, token_record)
44 |> assign(:user, user)
45 else
46 _ ->
47 # token found, but maybe only with app
48 with {:ok, app, token_record} <- fetch_app_and_token(token) do
49 conn
50 |> assign(:token, token_record)
51 |> assign(:app, app)
52 else
53 _ -> conn
54 end
55 end
56
57 _ ->
58 conn
59 end
60 end
61
62 # Gets user by token
63 #
64 @spec fetch_user_and_token(String.t()) :: {:ok, User.t(), Token.t()} | nil
65 defp fetch_user_and_token(token) do
66 query =
67 from(t in Token,
68 where: t.token == ^token,
69 join: user in assoc(t, :user),
70 preload: [user: user]
71 )
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}
76 end
77 end
78
79 @spec fetch_app_and_token(String.t()) :: {:ok, App.t(), Token.t()} | nil
80 defp fetch_app_and_token(token) do
81 query =
82 from(t in Token, where: t.token == ^token, join: app in assoc(t, :app), preload: [app: app])
83
84 with %Token{app: app} = token_record <- Repo.one(query) do
85 {:ok, app, token_record}
86 end
87 end
88
89 # Gets token from session by :oauth_token key
90 #
91 @spec fetch_token_from_session(Plug.Conn.t()) :: :no_token_found | {:ok, String.t()}
92 defp fetch_token_from_session(conn) do
93 case get_session(conn, :oauth_token) do
94 nil -> :no_token_found
95 token -> {:ok, token}
96 end
97 end
98
99 # Gets token from headers
100 #
101 @spec fetch_token_str(Plug.Conn.t()) :: :no_token_found | {:ok, String.t()}
102 defp fetch_token_str(%Plug.Conn{} = conn) do
103 headers = get_req_header(conn, "authorization")
104
105 with :no_token_found <- fetch_token_str(headers),
106 do: fetch_token_from_session(conn)
107 end
108
109 @spec fetch_token_str(Keyword.t()) :: :no_token_found | {:ok, String.t()}
110 defp fetch_token_str([]), do: :no_token_found
111
112 defp fetch_token_str([token | tail]) do
113 trimmed_token = String.trim(token)
114
115 case Regex.run(@realm_reg, trimmed_token) do
116 [_, match] -> {:ok, String.trim(match)}
117 _ -> fetch_token_str(tail)
118 end
119 end
120 end