A platform for community discussion. Free, open, simple.
DEV: Require at least one scope for API key granular mode (#31253)
Ted Johansson3d11e3ca10ee
diff --git a/app/assets/javascripts/admin/addon/components/admin-config-areas/api-keys-new.gjs b/app/assets/javascripts/admin/addon/components/admin-config-areas/api-keys-new.gjsindex f4ec63694fa..5ab2243986c 100644--- a/app/assets/javascripts/admin/addon/components/admin-config-areas/api-keys-new.gjs+++ b/app/assets/javascripts/admin/addon/components/admin-config-areas/api-keys-new.gjs@@ -10,6 +10,7 @@ import DButton from "discourse/components/d-button";import Form from "discourse/components/form";import { ajax } from "discourse/lib/ajax";import { popupAjaxError } from "discourse/lib/ajax-error";+import { bind } from "discourse/lib/decorators";import { i18n } from "discourse-i18n";import ApiKeyUrlsModal from "admin/components/modal/api-key-urls";import EmailGroupUserChooser from "select-kit/components/email-group-user-chooser";@@ -79,7 +80,10 @@ export default class AdminConfigAreasApiKeysNew extends Component {@actionasync save(data) {- const payload = { description: data.description };+ const payload = {+ description: data.description,+ scope_mode: data.scope_mode,+ };if (data.user_mode === "single") {payload.username = data.user;@@ -123,6 +127,21 @@ export default class AdminConfigAreasApiKeysNew extends Component {return enabledScopes.flat();}+ @bind+ atLeastOneGranularScope(data, { addError, removeError }) {+ removeError("scopes");++ if (+ data.scope_mode === "granular" &&+ this.#selectedScopes(data.scopes).length === 0+ ) {+ addError("scopes", {+ title: i18n("admin.api.scopes.title"),+ message: i18n("admin.api.scopes.one_or_more"),+ });+ }+ }+@actionasync showURLs(urls) {await this.modal.show(ApiKeyUrlsModal, {@@ -165,6 +184,7 @@ export default class AdminConfigAreasApiKeysNew extends Component {<Form@onSubmit={{this.save}}@data={{this.formData}}+ @validate={{this.atLeastOneGranularScope}}as |form transientData|><form.Fielddiff --git a/app/assets/javascripts/admin/addon/models/api-key.js b/app/assets/javascripts/admin/addon/models/api-key.jsindex 785220552ba..eb414b990fc 100644--- a/app/assets/javascripts/admin/addon/models/api-key.js+++ b/app/assets/javascripts/admin/addon/models/api-key.js@@ -55,7 +55,12 @@ export default class ApiKey extends RestModel {}createProperties() {- return this.getProperties("description", "username", "scopes");+ return this.getProperties(+ "description",+ "username",+ "scopes",+ "scope_mode"+ );}@discourseComputed()diff --git a/app/controllers/admin/api_controller.rb b/app/controllers/admin/api_controller.rbindex 04ed1684c92..4bc15407627 100644--- a/app/controllers/admin/api_controller.rb+++ b/app/controllers/admin/api_controller.rb@@ -75,6 +75,7 @@ class Admin::ApiController < Admin::AdminControllerApiKey.transaction doapi_key.created_by = current_userapi_key.api_key_scopes = build_scopes+ api_key.scope_mode = params.dig(:key, :scope_mode)if username = params.require(:key).permit(:username)[:username].presenceapi_key.user = User.find_by_username(username)raise Discourse::NotFound unless api_key.userdiff --git a/app/models/api_key.rb b/app/models/api_key.rbindex e86d27d2a95..993408b0323 100644--- a/app/models/api_key.rb+++ b/app/models/api_key.rb@@ -4,6 +4,8 @@ class ApiKey < ActiveRecord::Baseclass KeyAccessError < StandardErrorend+ attr_accessor :scope_mode+has_many :api_key_scopesbelongs_to :userbelongs_to :created_by, class_name: "User"@@ -18,6 +20,7 @@ class ApiKey < ActiveRecord::Baseendvalidates :description, length: { maximum: 255 }+ validate :at_least_one_granular_scopeafter_initialize :generate_key@@ -114,6 +117,17 @@ class ApiKey < ActiveRecord::Base# using update_column to avoid the AR transactionupdate_column(:last_used_at, now)end++ private++ def at_least_one_granular_scope+ if scope_mode == "granular" && api_key_scopes.empty?+ errors.add(+ :api_key_scopes,+ I18n.t("activerecord.errors.models.api_key.base.at_least_one_granular_scope"),+ )+ end+ endend# == Schema Informationdiff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 9df27d09079..cd03168a0a6 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -5478,6 +5478,7 @@ en:When using scopes, you can restrict an API key to a specific set of endpoints.You can also define which parameters will be allowed. Use commas to separate multiple values.title: Scopes+ one_or_more: At least one scope must be selected.granular: Granularread_only: Read-onlyglobal: Globaldiff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex a97816195ff..a5491b2046a 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -847,6 +847,9 @@ en:attributes:linkable_type:invalid: "is not valid"+ api_key:+ base:+ at_least_one_granular_scope: "at least one must be selected"uncategorized_category_name: "Uncategorized"diff --git a/spec/models/api_key_spec.rb b/spec/models/api_key_spec.rbindex 8de0dbc54d3..03a2012610f 100644--- a/spec/models/api_key_spec.rb+++ b/spec/models/api_key_spec.rb@@ -8,6 +8,15 @@ RSpec.describe ApiKey doit { is_expected.to belong_to :created_by }it { is_expected.to validate_length_of(:description).is_at_most(255) }+ it "validates at least one scope for granular mode" do+ api_key = ApiKey.new+ api_key.scope_mode = "granular"++ api_key.validate++ expect(api_key.errors).to contain_exactly("Api key scopes at least one must be selected")+ end+it "generates a key when saving" doapi_key = ApiKey.newapi_key.save!