886117d155fe5b1ed2d2cd908fc2994db8d0cb15
[akkoma] / lib / pleroma / web / o_auth / token.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.Web.OAuth.Token do
6 use Ecto.Schema
7
8 import Ecto.Changeset
9
10 alias Pleroma.Repo
11 alias Pleroma.User
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
16
17 @type t :: %__MODULE__{}
18
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)
25 belongs_to(:app, App)
26
27 timestamps()
28 end
29
30 def lifespan do
31 Pleroma.Config.get!([:oauth2, :token_expires_in])
32 end
33
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
37 token
38 |> Query.get_by_token()
39 |> Repo.find_resource()
40 end
41
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()
48 end
49
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()
57 end
58
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{}
64
65 create(
66 app,
67 user,
68 %{scopes: auth.scopes}
69 )
70 end
71 end
72
73 defp put_token(changeset) do
74 changeset
75 |> change(%{token: Token.Utils.generate_token()})
76 |> validate_required([:token])
77 |> unique_constraint(:token)
78 end
79
80 defp put_refresh_token(changeset, attrs) do
81 refresh_token = Map.get(attrs, :refresh_token, Token.Utils.generate_token())
82
83 changeset
84 |> change(%{refresh_token: refresh_token})
85 |> validate_required([:refresh_token])
86 |> unique_constraint(:refresh_token)
87 end
88
89 defp put_valid_until(changeset, attrs) do
90 valid_until =
91 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), lifespan()))
92
93 changeset
94 |> change(%{valid_until: valid_until})
95 |> validate_required([:valid_until])
96 end
97
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(%{
103 token_id: token.id,
104 valid_until: DateTime.from_naive!(token.valid_until, "Etc/UTC"),
105 mod: __MODULE__
106 })
107 end
108
109 {:ok, token}
110 end
111 end
112
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)
118 |> put_token()
119 |> put_refresh_token(attrs)
120 |> Repo.insert()
121 end
122
123 def delete_user_tokens(%User{id: user_id}) do
124 Query.get_by_user(user_id)
125 |> Repo.delete_all()
126 end
127
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)
131 |> Repo.delete_all()
132 end
133
134 def get_user_tokens(%User{id: user_id}) do
135 Query.get_by_user(user_id)
136 |> Query.preload([:app])
137 |> Repo.all()
138 end
139
140 def is_expired?(%__MODULE__{valid_until: valid_until}) do
141 NaiveDateTime.diff(NaiveDateTime.utc_now(), valid_until) > 0
142 end
143
144 def is_expired?(_), do: false
145 end