A platform for community discussion. Free, open, simple.
FEATURE: Add granular API key scopes for gamification score events (#45367)
jbrwc48ba0b0fe33
diff --git a/plugins/discourse-gamification/README.md b/plugins/discourse-gamification/README.mdindex 4ab38152a86..89e60657de8 100644--- a/plugins/discourse-gamification/README.md+++ b/plugins/discourse-gamification/README.md@@ -1,5 +1,23 @@# **Discourse Gamification** Plugin+## Scoped API keys++To award points without granting a global API key, create a **Granular** API key+for a **Single user** who is an admin and select only+`gamification: create_score_events`. Send the key in the `Api-Key` header when+calling `POST /admin/plugins/gamification/score_events.json` with `user_id`, `date`,+`points`, and optionally `description` and `category_id`.++The score events API provides independent scopes:++- `gamification: read_score_events`: `GET /admin/plugins/gamification/score_events.json`+- `gamification: create_score_events`: `POST /admin/plugins/gamification/score_events.json`+- `gamification: update_score_events`: `PUT /admin/plugins/gamification/score_events.json`++These scopes do not grant access to other admin operations or bypass the admin-user+requirement. The create scope allows both awarding and deducting points, so treat+its key as a sensitive credential.+## Category-scoped leaderboardsA leaderboard with its own nonempty **Scorable categories** list counts onlydiff --git a/plugins/discourse-gamification/config/locales/client.en.yml b/plugins/discourse-gamification/config/locales/client.en.ymlindex 9edcbd66cf2..93b526a8554 100644--- a/plugins/discourse-gamification/config/locales/client.en.yml+++ b/plugins/discourse-gamification/config/locales/client.en.yml@@ -1,6 +1,13 @@en:admin_js:admin:+ api:+ scopes:+ descriptions:+ gamification:+ read_score_events: "Read gamification score events."+ create_score_events: "Create gamification score events to award or deduct points."+ update_score_events: "Update existing gamification score events."site_settings:categories:discourse_gamification: "Discourse Gamification"diff --git a/plugins/discourse-gamification/plugin.rb b/plugins/discourse-gamification/plugin.rbindex 03a384ff25f..a55a550f0da 100644--- a/plugins/discourse-gamification/plugin.rb+++ b/plugins/discourse-gamification/plugin.rb@@ -36,6 +36,21 @@ after_initialize do{ use_new_show_route: true },)+ add_api_key_scope(+ :gamification,+ {+ read_score_events: {+ actions: %w[discourse_gamification/admin_gamification_score_event#show],+ },+ create_score_events: {+ actions: %w[discourse_gamification/admin_gamification_score_event#create],+ },+ update_score_events: {+ actions: %w[discourse_gamification/admin_gamification_score_event#update],+ },+ },+ )+require_relative "jobs/scheduled/update_scores_for_ten_days"require_relative "jobs/scheduled/update_scores_for_today"require_relative "jobs/regular/recalculate_scores"diff --git a/plugins/discourse-gamification/spec/requests/admin_gamification_score_event_api_spec.rb b/plugins/discourse-gamification/spec/requests/admin_gamification_score_event_api_spec.rbnew file mode 100644index 00000000000..d61662712f2--- /dev/null+++ b/plugins/discourse-gamification/spec/requests/admin_gamification_score_event_api_spec.rb@@ -0,0 +1,153 @@+# frozen_string_literal: true++RSpec.describe DiscourseGamification::AdminGamificationScoreEventController do+ fab!(:admin)+ fab!(:user)+ let(:api_key) { Fabricate(:api_key, user: admin) }+ let!(:api_key_scope) do+ Fabricate(+ :api_key_scope,+ api_key_id: api_key.id,+ resource: "gamification",+ action: "create_score_events",+ )+ end+ fab!(:score_event) do+ DiscourseGamification::GamificationScoreEvent.create!(user: user, date: Date.current, points: 5)+ end++ let(:headers) { { "Api-Key" => api_key.key } }+ let(:create_params) do+ { user_id: user.id, date: Date.current, points: 10, description: "Contest prize" }+ end++ before { SiteSetting.discourse_gamification_enabled = true }++ describe "#show" do+ it "reads score events with the read scope" do+ api_key_scope.update!(action: "read_score_events")++ get "/admin/plugins/gamification/score_events.json",+ params: {+ user_id: user.id,+ },+ headers: headers++ expect(response.status).to eq(200)+ expect(response.parsed_body["events"].map { |event| event["id"] }).to eq([score_event.id])+ end++ it "rejects a create-only key" do+ get "/admin/plugins/gamification/score_events.json", headers: headers++ expect(response.status).to eq(404)+ end+ end++ describe "#create" do+ it "awards points with the create scope" do+ expect {+ post "/admin/plugins/gamification/score_events.json",+ params: create_params,+ headers: headers+ }.to change { DiscourseGamification::GamificationScoreEvent.count }.by(1)++ expect(response.status).to eq(200)+ expect(response.parsed_body).to include(+ "user_id" => user.id,+ "points" => 10,+ "description" => "Contest prize",+ )+ end++ %w[read_score_events update_score_events].each do |action|+ it "rejects a key with only the #{action} scope" do+ api_key_scope.update!(action: action)++ expect {+ post "/admin/plugins/gamification/score_events.json",+ params: create_params,+ headers: headers+ }.not_to change { DiscourseGamification::GamificationScoreEvent.count }++ expect(response.status).to eq(404)+ end+ end++ %i[user moderator].each do |role|+ it "rejects a #{role}'s key even with the create scope" do+ api_key.update!(user: Fabricate(role))++ expect {+ post "/admin/plugins/gamification/score_events.json",+ params: create_params,+ headers: headers+ }.not_to change { DiscourseGamification::GamificationScoreEvent.count }++ expect(response.status).to eq(role == :moderator ? 403 : 404)+ end+ end++ it "rejects a key with an unrelated scope" do+ api_key_scope.update!(resource: "topics", action: "write")++ expect {+ post "/admin/plugins/gamification/score_events.json",+ params: create_params,+ headers: headers+ }.not_to change { DiscourseGamification::GamificationScoreEvent.count }++ expect(response.status).to eq(404)+ end++ it "rejects requests when the plugin is disabled" do+ SiteSetting.discourse_gamification_enabled = false++ expect {+ post "/admin/plugins/gamification/score_events.json",+ params: create_params,+ headers: headers+ }.not_to change { DiscourseGamification::GamificationScoreEvent.count }++ expect(response.status).to eq(404)+ end+ end++ describe "#update" do+ it "updates score events with the update scope" do+ api_key_scope.update!(action: "update_score_events")++ put "/admin/plugins/gamification/score_events.json",+ params: {+ id: score_event.id,+ points: 20,+ },+ headers: headers++ expect(response.status).to eq(200)+ expect(score_event.reload.points).to eq(20)+ end++ it "rejects a create-only key" do+ put "/admin/plugins/gamification/score_events.json",+ params: {+ id: score_event.id,+ points: 20,+ },+ headers: headers++ expect(response.status).to eq(404)+ expect(score_event.reload.points).to eq(5)+ end+ end++ it "rejects unrelated admin operations with a create-only key" do+ put "/admin/plugins/gamification/recalculate-scores.json", headers: headers++ expect(response.status).to eq(404)++ get "/admin/users.json", headers: headers++ expect(response.status).to eq(404)+ end+end