Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Add API key scopes
Uku Tahtd473accf408a
diff --git a/lib/plausible/auth/api_key.ex b/lib/plausible/auth/api_key.exindex 13c700d3ed..c07f1ee24d 100644--- a/lib/plausible/auth/api_key.ex+++ b/lib/plausible/auth/api_key.ex@@ -5,6 +5,7 @@ defmodule Plausible.Auth.ApiKey do@required [:user_id, :key, :name]schema "api_keys" dofield :name, :string+ field :scopes, {:array, :string}, default: ["stats:read:*"]field :key, :string, virtual: truefield :key_hash, :stringdiff --git a/lib/plausible_web/plugs/authorize_sites_api.ex b/lib/plausible_web/plugs/authorize_sites_api.exindex da2c108889..46484499dc 100644--- a/lib/plausible_web/plugs/authorize_sites_api.ex+++ b/lib/plausible_web/plugs/authorize_sites_api.ex@@ -21,14 +21,20 @@ defmodule PlausibleWeb.AuthorizeSitesApiPlug do{:error, :invalid_api_key} ->unauthorized(conn,- "Invalid API key. Please make sure you're using a valid API key with access to the site you've requested."+ "Invalid API key. Please make sure you're using a valid API key with access to the resource you've requested.")endenddefp verify_access(api_key) dohashed_key = ApiKey.do_hash(api_key)- found_key = Repo.get_by(ApiKey, key_hash: hashed_key)++ found_key =+ Repo.one(+ from a in ApiKey,+ where: a.key_hash == ^hashed_key,+ where: fragment("? @> ?", a.scopes, ["sites:provision:*"])+ )cond dofound_key -> {:ok, found_key}diff --git a/priv/repo/migrations/20210409082603_add_api_key_scopes.exs b/priv/repo/migrations/20210409082603_add_api_key_scopes.exsnew file mode 100644index 0000000000..ab3ea600eb--- /dev/null+++ b/priv/repo/migrations/20210409082603_add_api_key_scopes.exs@@ -0,0 +1,24 @@+defmodule Plausible.Repo.Migrations.AddApiKeyScopes do+ use Ecto.Migration++ def up do+ alter table(:api_keys) do+ add :scopes, {:array, :text}+ end++ execute "UPDATE api_keys SET scopes='{stats:read:*}'"++ alter table(:api_keys) do+ modify :scopes, {:array, :text}, null: false+ end++ # https://stackoverflow.com/a/4059785+ create index(:api_keys, [:scopes], using: "GIN")+ end++ def down do+ alter table(:api_keys) do+ remove :scopes+ end+ end+enddiff --git a/test/plausible_web/controllers/api/external_sites_controller_test.exs b/test/plausible_web/controllers/api/external_sites_controller_test.exsindex 9a33cb3d3e..f5cdabf973 100644--- a/test/plausible_web/controllers/api/external_sites_controller_test.exs+++ b/test/plausible_web/controllers/api/external_sites_controller_test.exs@@ -2,7 +2,12 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest douse PlausibleWeb.ConnCaseimport Plausible.TestUtils- setup [:create_user, :create_api_key, :use_api_key]+ setup %{conn: conn} do+ user = insert(:user)+ api_key = insert(:api_key, user: user, scopes: ["sites:provision:*"])+ conn = Plug.Conn.put_req_header(conn, "authorization", "Bearer #{api_key.key}")+ {:ok, user: user, api_key: api_key, conn: conn}+ enddescribe "POST /api/v1/sites" dotest "can create a site", %{conn: conn} do@@ -41,6 +46,20 @@ defmodule PlausibleWeb.Api.ExternalSitesControllerTest do"error" => "domain can't be blank"}end++ test "cannot access with a bad API key scope", %{conn: conn, user: user} do+ api_key = insert(:api_key, user: user, scopes: ["stats:read:*"])++ conn =+ conn+ |> Plug.Conn.put_req_header("authorization", "Bearer #{api_key.key}")+ |> post("/api/v1/sites", %{"site" => %{"domain" => "domain.com"}})++ assert json_response(conn, 401) == %{+ "error" =>+ "Invalid API key. Please make sure you're using a valid API key with access to the resource you've requested."+ }+ endenddescribe "PUT /api/v1/sites/shared-links/:link_name" do