X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fweb%2Foauth%2Ftoken.ex;h=ca67632badb52c7147491b7e0b98003406b520f1;hb=b6f915313f59223002a0eff88c1eefb00ca5c8f3;hp=aa3610bb35f26b7d4335214b99f4eb1c263b39fa;hpb=89fbed88212657e3dcd4bbcb2c0718b07802037f;p=akkoma
diff --git a/lib/pleroma/web/oauth/token.ex b/lib/pleroma/web/oauth/token.ex
index aa3610bb3..ca67632ba 100644
--- a/lib/pleroma/web/oauth/token.ex
+++ b/lib/pleroma/web/oauth/token.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2018 Pleroma Authors
+# Copyright © 2017-2019 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.OAuth.Token do
@@ -7,14 +7,18 @@ defmodule Pleroma.Web.OAuth.Token do
import Ecto.Query
- alias Pleroma.{User, Repo}
- alias Pleroma.Web.OAuth.{Token, App, Authorization}
+ alias Pleroma.User
+ alias Pleroma.Repo
+ alias Pleroma.Web.OAuth.Token
+ alias Pleroma.Web.OAuth.App
+ alias Pleroma.Web.OAuth.Authorization
schema "oauth_tokens" do
field(:token, :string)
field(:refresh_token, :string)
+ field(:scopes, {:array, :string}, default: [])
field(:valid_until, :naive_datetime)
- belongs_to(:user, Pleroma.User)
+ belongs_to(:user, Pleroma.User, type: Pleroma.FlakeId)
belongs_to(:app, App)
timestamps()
@@ -23,17 +27,19 @@ defmodule Pleroma.Web.OAuth.Token do
def exchange_token(app, auth) do
with {:ok, auth} <- Authorization.use_token(auth),
true <- auth.app_id == app.id do
- create_token(app, Repo.get(User, auth.user_id))
+ create_token(app, Repo.get(User, auth.user_id), auth.scopes)
end
end
- def create_token(%App{} = app, %User{} = user) do
- token = :crypto.strong_rand_bytes(32) |> Base.url_encode64()
- refresh_token = :crypto.strong_rand_bytes(32) |> Base.url_encode64()
+ def create_token(%App{} = app, %User{} = user, scopes \\ nil) do
+ scopes = scopes || app.scopes
+ token = :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false)
+ refresh_token = :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false)
token = %Token{
token: token,
refresh_token: refresh_token,
+ scopes: scopes,
user_id: user.id,
app_id: app.id,
valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
@@ -44,9 +50,27 @@ defmodule Pleroma.Web.OAuth.Token do
def delete_user_tokens(%User{id: user_id}) do
from(
- t in Pleroma.Web.OAuth.Token,
+ t in Token,
where: t.user_id == ^user_id
)
|> Repo.delete_all()
end
+
+ def delete_user_token(%User{id: user_id}, token_id) do
+ from(
+ t in Token,
+ where: t.user_id == ^user_id,
+ where: t.id == ^token_id
+ )
+ |> Repo.delete_all()
+ end
+
+ def get_user_tokens(%User{id: user_id}) do
+ from(
+ t in Token,
+ where: t.user_id == ^user_id
+ )
+ |> Repo.all()
+ |> Repo.preload(:app)
+ end
end