Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Implement routes for discovering /mcp and the OAuth endpoints (#6656)

Artur Pataac320452c07a
diff --git a/lib/plausible/auth/scopes.ex b/lib/plausible/auth/scopes.ex
new file mode 100644
index 0000000000..e066bf3a35
--- /dev/null
+++ b/lib/plausible/auth/scopes.ex
@@ -0,0 +1,12 @@
+defmodule Plausible.Auth.Scopes do
+ @moduledoc """
+ This module enumerates the scopes that any particular
+ authorization grant can be limited to.
+ """
+
+ @stats_read_all "stats:read:*"
+ def stats_read(), do: @stats_read_all
+
+ @sites_read_all "sites:read:*"
+ def sites_read(), do: @sites_read_all
+end
diff --git a/lib/plausible/oauth/protected_resources.ex b/lib/plausible/oauth/protected_resources.ex
new file mode 100644
index 0000000000..5e126e2c10
--- /dev/null
+++ b/lib/plausible/oauth/protected_resources.ex
@@ -0,0 +1,12 @@
+defmodule Plausible.OAuth.ProtectedResources do
+ @moduledoc """
+ The protected resources the OAuth authorization server will issue tokens for.
+ Each authorization grant is associated with a particular resource.
+ """
+
+ def mcp(),
+ do: %{
+ resource_path: "/mcp",
+ scopes_supported: [Plausible.Auth.Scopes.sites_read()]
+ }
+end
diff --git a/lib/plausible_web/controllers/oauth/metadata_controller.ex b/lib/plausible_web/controllers/oauth/metadata_controller.ex
index cf70ecd9f2..1d1b77e1a6 100644
--- a/lib/plausible_web/controllers/oauth/metadata_controller.ex
+++ b/lib/plausible_web/controllers/oauth/metadata_controller.ex
@@ -5,26 +5,42 @@ defmodule PlausibleWeb.OAuth.MetadataController do
use PlausibleWeb, :controller
+ alias PlausibleWeb.Endpoint
+
+ @mcp Plausible.OAuth.ProtectedResources.mcp()
+
@doc """
Serves the [RFC 9728 Protected Resource Metadata](https://www.rfc-editor.org/rfc/rfc9728.html#name-protected-resource-metadata)
naming `/mcp` as the resource and pointing at the authorization server that
protects it.
"""
- def protected_resource(conn, _params) do
- not_implemented(conn)
+ def mcp_protected_resource(conn, _params) do
+ json(conn, %{
+ resource: Endpoint.url() <> @mcp.resource_path,
+ authorization_servers: [Endpoint.url()],
+ scopes_supported: @mcp.scopes_supported,
+ bearer_methods_supported: ["header"]
+ })
end
+ @authorization_endpoint "/login/oauth/authorize"
+ @token_endpoint "/login/oauth/token"
+
@doc """
Serves the [RFC 8414 Authorization Server Metadata](https://www.rfc-editor.org/rfc/rfc8414.html#section-2)
advertising the authorize and token endpoints and the grants they accept.
"""
def authorization_server(conn, _params) do
- not_implemented(conn)
- end
-
- defp not_implemented(conn) do
- conn
- |> put_status(501)
- |> json(%{error: "not_implemented"})
+ json(conn, %{
+ issuer: Endpoint.url(),
+ authorization_endpoint: Endpoint.url() <> @authorization_endpoint,
+ token_endpoint: Endpoint.url() <> @token_endpoint,
+ response_types_supported: ["code"],
+ grant_types_supported: ["authorization_code"],
+ code_challenge_methods_supported: ["S256"],
+ token_endpoint_auth_methods_supported: ["none"],
+ scopes_supported: @mcp.scopes_supported,
+ client_id_metadata_document_supported: true
+ })
end
end
diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex
index 3035ed8840..127038318b 100644
--- a/lib/plausible_web/router.ex
+++ b/lib/plausible_web/router.ex
@@ -113,8 +113,7 @@ defmodule PlausibleWeb.Router do
scope "/.well-known", PlausibleWeb do
pipe_through :external_api
- get "/oauth-protected-resource", OAuth.MetadataController, :protected_resource
- get "/oauth-protected-resource/mcp", OAuth.MetadataController, :protected_resource
+ get "/oauth-protected-resource/mcp", OAuth.MetadataController, :mcp_protected_resource
get "/oauth-authorization-server", OAuth.MetadataController, :authorization_server
end
diff --git a/test/plausible_web/controllers/oauth/metadata_controller_test.exs b/test/plausible_web/controllers/oauth/metadata_controller_test.exs
new file mode 100644
index 0000000000..5081bcdf70
--- /dev/null
+++ b/test/plausible_web/controllers/oauth/metadata_controller_test.exs
@@ -0,0 +1,38 @@
+defmodule PlausibleWeb.OAuth.MetadataControllerTest do
+ use PlausibleWeb.ConnCase, async: true
+
+ describe "GET /.well-known/oauth-authorization-server" do
+ test "advertises the endpoints, grants and CIMD support", %{conn: conn} do
+ issuer = PlausibleWeb.Endpoint.url()
+
+ resp = conn |> get("/.well-known/oauth-authorization-server") |> json_response(200)
+
+ assert_matches ^strict_map(%{
+ "issuer" => ^issuer,
+ "authorization_endpoint" => ^(issuer <> "/login/oauth/authorize"),
+ "token_endpoint" => ^(issuer <> "/login/oauth/token"),
+ "response_types_supported" => ["code"],
+ "grant_types_supported" => ["authorization_code"],
+ "code_challenge_methods_supported" => ["S256"],
+ "token_endpoint_auth_methods_supported" => ["none"],
+ "scopes_supported" => ["sites:read:*"],
+ "client_id_metadata_document_supported" => true
+ }) = resp
+ end
+ end
+
+ describe "GET /.well-known/oauth-protected-resource/mcp" do
+ test "names /mcp as the resource and points at the authorization server", %{conn: conn} do
+ issuer = PlausibleWeb.Endpoint.url()
+
+ resp = conn |> get("/.well-known/oauth-protected-resource/mcp") |> json_response(200)
+
+ assert_matches ^strict_map(%{
+ "resource" => ^(issuer <> "/mcp"),
+ "authorization_servers" => [^issuer],
+ "bearer_methods_supported" => ["header"],
+ "scopes_supported" => ["sites:read:*"]
+ }) = resp
+ end
+ end
+end