Merge remote-tracking branch 'pleroma/develop' into moderators
[akkoma] / lib / pleroma / helpers / auth_helper.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Helpers.AuthHelper do
6 alias Pleroma.Web.Plugs.OAuthScopesPlug
7 alias Plug.Conn
8
9 import Plug.Conn
10
11 @oauth_token_session_key :oauth_token
12
13 @doc """
14 Skips OAuth permissions (scopes) checks, assigns nil `:token`.
15 Intended to be used with explicit authentication and only when OAuth token cannot be determined.
16 """
17 def skip_oauth(conn) do
18 conn
19 |> assign(:token, nil)
20 |> OAuthScopesPlug.skip_plug()
21 end
22
23 @doc "Drops authentication info from connection"
24 def drop_auth_info(conn) do
25 # To simplify debugging, setting a private variable on `conn` if auth info is dropped
26 conn
27 |> assign(:user, nil)
28 |> assign(:token, nil)
29 |> put_private(:authentication_ignored, true)
30 end
31
32 @doc "Gets OAuth token string from session"
33 def get_session_token(%Conn{} = conn) do
34 get_session(conn, @oauth_token_session_key)
35 end
36
37 @doc "Updates OAuth token string in session"
38 def put_session_token(%Conn{} = conn, token) when is_binary(token) do
39 put_session(conn, @oauth_token_session_key, token)
40 end
41
42 @doc "Deletes OAuth token string from session"
43 def delete_session_token(%Conn{} = conn) do
44 delete_session(conn, @oauth_token_session_key)
45 end
46 end