aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/web/oauth
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pleroma/web/oauth')
-rw-r--r--lib/pleroma/web/oauth/authorization.ex6
-rw-r--r--lib/pleroma/web/oauth/oauth_controller.ex12
-rw-r--r--lib/pleroma/web/oauth/token.ex8
3 files changed, 15 insertions, 11 deletions
diff --git a/lib/pleroma/web/oauth/authorization.ex b/lib/pleroma/web/oauth/authorization.ex
index f8c65602d..0fbaa902b 100644
--- a/lib/pleroma/web/oauth/authorization.ex
+++ b/lib/pleroma/web/oauth/authorization.ex
@@ -6,12 +6,14 @@ defmodule Pleroma.Web.OAuth.Authorization do
use Ecto.Schema
alias Pleroma.{User, Repo}
+ alias Pleroma.Web.OAuth
alias Pleroma.Web.OAuth.{Authorization, App}
import Ecto.{Changeset, Query}
schema "oauth_authorizations" do
field(:token, :string)
+ field(:scope, :string)
field(:valid_until, :naive_datetime)
field(:used, :boolean, default: false)
belongs_to(:user, Pleroma.User, type: Pleroma.FlakeId)
@@ -20,7 +22,8 @@ defmodule Pleroma.Web.OAuth.Authorization do
timestamps()
end
- def create_authorization(%App{} = app, %User{} = user) do
+ def create_authorization(%App{} = app, %User{} = user, scope \\ nil) do
+ scopes = OAuth.parse_scopes(scope || app.scopes)
token = :crypto.strong_rand_bytes(32) |> Base.url_encode64()
authorization = %Authorization{
@@ -28,6 +31,7 @@ defmodule Pleroma.Web.OAuth.Authorization do
used: false,
user_id: user.id,
app_id: app.id,
+ scope: Enum.join(scopes, " "),
valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
}
diff --git a/lib/pleroma/web/oauth/oauth_controller.ex b/lib/pleroma/web/oauth/oauth_controller.ex
index 8ec963c79..15345d4ba 100644
--- a/lib/pleroma/web/oauth/oauth_controller.ex
+++ b/lib/pleroma/web/oauth/oauth_controller.ex
@@ -38,7 +38,7 @@ defmodule Pleroma.Web.OAuth.OAuthController do
{:auth_active, true} <- {:auth_active, User.auth_active?(user)},
%App{} = app <- Repo.get_by(App, client_id: client_id),
true <- redirect_uri in String.split(app.redirect_uris),
- {:ok, auth} <- Authorization.create_authorization(app, user) do
+ {:ok, auth} <- Authorization.create_authorization(app, user, params["scope"]) do
# Special case: Local MastodonFE.
redirect_uri =
if redirect_uri == "." do
@@ -81,8 +81,6 @@ defmodule Pleroma.Web.OAuth.OAuthController do
end
end
- # TODO
- # - proper scope handling
def token_exchange(conn, %{"grant_type" => "authorization_code"} = params) do
with %App{} = app <- get_app_from_request(conn, params),
fixed_token = fix_padding(params["code"]),
@@ -96,7 +94,7 @@ defmodule Pleroma.Web.OAuth.OAuthController do
refresh_token: token.refresh_token,
created_at: DateTime.to_unix(inserted_at),
expires_in: 60 * 10,
- scope: "read write follow"
+ scope: token.scope
}
json(conn, response)
@@ -107,8 +105,6 @@ defmodule Pleroma.Web.OAuth.OAuthController do
end
end
- # TODO
- # - investigate a way to verify the user wants to grant read/write/follow once scope handling is done
def token_exchange(
conn,
%{"grant_type" => "password", "username" => name, "password" => password} = params
@@ -117,14 +113,14 @@ defmodule Pleroma.Web.OAuth.OAuthController do
%User{} = user <- User.get_by_nickname_or_email(name),
true <- Pbkdf2.checkpw(password, user.password_hash),
{:auth_active, true} <- {:auth_active, User.auth_active?(user)},
- {:ok, auth} <- Authorization.create_authorization(app, user),
+ {:ok, auth} <- Authorization.create_authorization(app, user, params["scope"]),
{:ok, token} <- Token.exchange_token(app, auth) do
response = %{
token_type: "Bearer",
access_token: token.token,
refresh_token: token.refresh_token,
expires_in: 60 * 10,
- scope: "read write follow"
+ scope: token.scope
}
json(conn, response)
diff --git a/lib/pleroma/web/oauth/token.ex b/lib/pleroma/web/oauth/token.ex
index 4e01b123b..61f43ed5a 100644
--- a/lib/pleroma/web/oauth/token.ex
+++ b/lib/pleroma/web/oauth/token.ex
@@ -8,11 +8,13 @@ defmodule Pleroma.Web.OAuth.Token do
import Ecto.Query
alias Pleroma.{User, Repo}
+ alias Pleroma.Web.OAuth
alias Pleroma.Web.OAuth.{Token, App, Authorization}
schema "oauth_tokens" do
field(:token, :string)
field(:refresh_token, :string)
+ field(:scope, :string)
field(:valid_until, :naive_datetime)
belongs_to(:user, Pleroma.User, type: Pleroma.FlakeId)
belongs_to(:app, App)
@@ -23,17 +25,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.scope)
end
end
- def create_token(%App{} = app, %User{} = user) do
+ def create_token(%App{} = app, %User{} = user, scope \\ nil) do
+ scopes = OAuth.parse_scopes(scope || app.scopes)
token = :crypto.strong_rand_bytes(32) |> Base.url_encode64()
refresh_token = :crypto.strong_rand_bytes(32) |> Base.url_encode64()
token = %Token{
token: token,
refresh_token: refresh_token,
+ scope: Enum.join(scopes, " "),
user_id: user.id,
app_id: app.id,
valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)