1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Helpers.AuthHelper do
6 alias Pleroma.Web.Plugs.OAuthScopesPlug
11 @oauth_token_session_key :oauth_token
12 @oauth_user_session_key :oauth_user
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.
18 def skip_oauth(conn) do
20 |> assign(:token, nil)
21 |> OAuthScopesPlug.skip_plug()
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
29 |> assign(:token, nil)
30 |> put_private(:authentication_ignored, true)
33 @doc "Gets OAuth token string from session"
34 def get_session_token(%Conn{} = conn) do
35 get_session(conn, @oauth_token_session_key)
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)
43 @doc "Deletes OAuth token string from session"
44 def delete_session_token(%Conn{} = conn) do
45 delete_session(conn, @oauth_token_session_key)
48 def put_session_user(%Conn{} = conn, user) do
49 put_session(conn, @oauth_user_session_key, user)
52 def delete_session_user(%Conn{} = conn) do
53 delete_session(conn, @oauth_user_session_key)
56 def get_session_user(%Conn{} = conn) do
57 get_session(conn, @oauth_user_session_key)