1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.OAuth.Token.Strategy.RefreshToken do
7 Functions for dealing with refresh token strategy.
12 alias Pleroma.Web.OAuth.Token
13 alias Pleroma.Web.OAuth.Token.Strategy.Revoke
16 Will grant access token by refresh token.
18 @spec grant(Token.t()) :: {:ok, Token.t()} | {:error, any()}
20 access_token = Repo.preload(token, [:user, :app])
23 Repo.transaction(fn ->
25 app: access_token.app,
26 user: access_token.user,
27 scopes: access_token.scopes
31 |> revoke_access_token()
32 |> create_access_token(token_params)
36 {:ok, {:error, reason}} -> {:error, reason}
37 {:ok, {:ok, token}} -> {:ok, token}
38 {:error, reason} -> {:error, reason}
42 defp revoke_access_token(token) do
46 defp create_access_token({:error, error}, _), do: {:error, error}
48 defp create_access_token({:ok, token}, %{app: app, user: user} = token_params) do
49 Token.create(app, user, add_refresh_token(token_params, token.refresh_token))
52 defp add_refresh_token(params, token) do
53 case Config.get([:oauth2, :issue_new_refresh_token], false) do
54 true -> Map.put(params, :refresh_token, token)