Session token setting on token exchange. Auth-related refactoring.
[akkoma] / lib / pleroma / helpers / auth_helper.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.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 def drop_auth_info(conn) do
24 conn
25 |> assign(:user, nil)
26 |> assign(:token, nil)
27 end
28
29 def get_session_token(%Conn{} = conn) do
30 get_session(conn, @oauth_token_session_key)
31 end
32
33 def put_session_token(%Conn{} = conn, token) when is_binary(token) do
34 put_session(conn, @oauth_token_session_key, token)
35 end
36
37 def delete_session_token(%Conn{} = conn) do
38 delete_session(conn, @oauth_token_session_key)
39 end
40 end