Fix oban tags
[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 @oauth_user_session_key :oauth_user
13
14 @doc """
15 Skips OAuth permissions (scopes) checks, assigns nil `:token`.
16 Intended to be used with explicit authentication and only when OAuth token cannot be determined.
17 """
18 def skip_oauth(conn) do
19 conn
20 |> assign(:token, nil)
21 |> OAuthScopesPlug.skip_plug()
22 end
23
24 @doc "Drops authentication info from connection"
25 def drop_auth_info(conn) do
26 # To simplify debugging, setting a private variable on `conn` if auth info is dropped
27 conn
28 |> assign(:user, nil)
29 |> assign(:token, nil)
30 |> put_private(:authentication_ignored, true)
31 end
32
33 @doc "Gets OAuth token string from session"
34 def get_session_token(%Conn{} = conn) do
35 get_session(conn, @oauth_token_session_key)
36 end
37
38 @doc "Updates OAuth token string in session"
39 def put_session_token(%Conn{} = conn, token) when is_binary(token) do
40 put_session(conn, @oauth_token_session_key, token)
41 end
42
43 @doc "Deletes OAuth token string from session"
44 def delete_session_token(%Conn{} = conn) do
45 delete_session(conn, @oauth_token_session_key)
46 end
47
48 def put_session_user(%Conn{} = conn, user) do
49 put_session(conn, @oauth_user_session_key, user)
50 end
51
52 def delete_session_user(%Conn{} = conn) do
53 delete_session(conn, @oauth_user_session_key)
54 end
55
56 def get_session_user(%Conn{} = conn) do
57 get_session(conn, @oauth_user_session_key)
58 end
59 end