diff options
author | Alex Gleason <alex@alexgleason.me> | 2020-12-17 09:04:43 -0600 |
---|---|---|
committer | Alex Gleason <alex@alexgleason.me> | 2020-12-17 09:05:36 -0600 |
commit | 80891e83d8df25b742a321d2c837e38c805e6582 (patch) | |
tree | 915321403b8a1639fa317b72d438b8a61a0eb090 /lib/pleroma/helpers | |
parent | 4999efad3f0bb0a70a5be5d5c2b970ab564668ca (diff) | |
parent | 8b8c317c0ff1534d3296369ba315b8919e8dc55c (diff) | |
download | pleroma-80891e83d8df25b742a321d2c837e38c805e6582.tar.gz |
Merge remote-tracking branch 'upstream/develop' into registration-workflow
Diffstat (limited to 'lib/pleroma/helpers')
-rw-r--r-- | lib/pleroma/helpers/auth_helper.ex | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/pleroma/helpers/auth_helper.ex b/lib/pleroma/helpers/auth_helper.ex new file mode 100644 index 000000000..8f87b38be --- /dev/null +++ b/lib/pleroma/helpers/auth_helper.ex @@ -0,0 +1,46 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Helpers.AuthHelper do + alias Pleroma.Web.Plugs.OAuthScopesPlug + alias Plug.Conn + + import Plug.Conn + + @oauth_token_session_key :oauth_token + + @doc """ + Skips OAuth permissions (scopes) checks, assigns nil `:token`. + Intended to be used with explicit authentication and only when OAuth token cannot be determined. + """ + def skip_oauth(conn) do + conn + |> assign(:token, nil) + |> OAuthScopesPlug.skip_plug() + end + + @doc "Drops authentication info from connection" + def drop_auth_info(conn) do + # To simplify debugging, setting a private variable on `conn` if auth info is dropped + conn + |> assign(:user, nil) + |> assign(:token, nil) + |> put_private(:authentication_ignored, true) + end + + @doc "Gets OAuth token string from session" + def get_session_token(%Conn{} = conn) do + get_session(conn, @oauth_token_session_key) + end + + @doc "Updates OAuth token string in session" + def put_session_token(%Conn{} = conn, token) when is_binary(token) do + put_session(conn, @oauth_token_session_key, token) + end + + @doc "Deletes OAuth token string from session" + def delete_session_token(%Conn{} = conn) do + delete_session(conn, @oauth_token_session_key) + end +end |