1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.OAuth.Token do
12 alias Pleroma.Web.OAuth.App
13 alias Pleroma.Web.OAuth.Authorization
14 alias Pleroma.Web.OAuth.Token
15 alias Pleroma.Web.OAuth.Token.Query
17 @type t :: %__MODULE__{}
19 schema "oauth_tokens" do
20 field(:token, :string)
21 field(:refresh_token, :string)
22 field(:scopes, {:array, :string}, default: [])
23 field(:valid_until, :naive_datetime_usec)
24 belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
31 Pleroma.Config.get!([:oauth2, :token_expires_in])
34 @doc "Gets token by unique access token"
35 @spec get_by_token(String.t()) :: {:ok, t()} | {:error, :not_found}
36 def get_by_token(token) do
38 |> Query.get_by_token()
39 |> Repo.find_resource()
42 @doc "Gets token for app by access token"
43 @spec get_by_token(App.t(), String.t()) :: {:ok, t()} | {:error, :not_found}
44 def get_by_token(%App{id: app_id} = _app, token) do
45 Query.get_by_app(app_id)
46 |> Query.get_by_token(token)
47 |> Repo.find_resource()
50 @doc "Gets token for app by refresh token"
51 @spec get_by_refresh_token(App.t(), String.t()) :: {:ok, t()} | {:error, :not_found}
52 def get_by_refresh_token(%App{id: app_id} = _app, token) do
53 Query.get_by_app(app_id)
54 |> Query.get_by_refresh_token(token)
55 |> Query.preload([:user])
56 |> Repo.find_resource()
59 @spec exchange_token(App.t(), Authorization.t()) :: {:ok, Token.t()} | {:error, Changeset.t()}
60 def exchange_token(app, auth) do
61 with {:ok, auth} <- Authorization.use_token(auth),
62 true <- auth.app_id == app.id do
63 user = if auth.user_id, do: User.get_cached_by_id(auth.user_id), else: %User{}
68 %{scopes: auth.scopes}
73 defp put_token(changeset) do
75 |> change(%{token: Token.Utils.generate_token()})
76 |> validate_required([:token])
77 |> unique_constraint(:token)
80 defp put_refresh_token(changeset, attrs) do
81 refresh_token = Map.get(attrs, :refresh_token, Token.Utils.generate_token())
84 |> change(%{refresh_token: refresh_token})
85 |> validate_required([:refresh_token])
86 |> unique_constraint(:refresh_token)
89 defp put_valid_until(changeset, attrs) do
91 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), lifespan()))
94 |> change(%{valid_until: valid_until})
95 |> validate_required([:valid_until])
98 @spec create(App.t(), User.t(), map()) :: {:ok, Token} | {:error, Changeset.t()}
99 def create(%App{} = app, %User{} = user, attrs \\ %{}) do
100 with {:ok, token} <- do_create(app, user, attrs) do
101 if Pleroma.Config.get([:oauth2, :clean_expired_tokens]) do
102 Pleroma.Workers.PurgeExpiredToken.enqueue(%{
104 valid_until: DateTime.from_naive!(token.valid_until, "Etc/UTC"),
113 defp do_create(app, user, attrs) do
114 %__MODULE__{user_id: user.id, app_id: app.id}
115 |> cast(%{scopes: attrs[:scopes] || app.scopes}, [:scopes])
116 |> validate_required([:scopes, :app_id])
117 |> put_valid_until(attrs)
119 |> put_refresh_token(attrs)
123 def delete_user_tokens(%User{id: user_id}) do
124 Query.get_by_user(user_id)
128 def delete_user_token(%User{id: user_id}, token_id) do
129 Query.get_by_user(user_id)
130 |> Query.get_by_id(token_id)
134 def get_user_tokens(%User{id: user_id}) do
135 Query.get_by_user(user_id)
136 |> Query.preload([:app])
140 def is_expired?(%__MODULE__{valid_until: valid_until}) do
141 NaiveDateTime.diff(NaiveDateTime.utc_now(), valid_until) > 0
144 def is_expired?(_), do: false