Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Added full-access scopes, and renamed connected apps to connected agents

Damian Legawieca07987cbbcdf
diff --git a/packages/dashboard/src/components/spree/api-keys/api-key-table.tsx b/packages/dashboard/src/components/spree/api-keys/api-key-table.tsx
index 81f9953a26..f09e69fe6b 100644
--- a/packages/dashboard/src/components/spree/api-keys/api-key-table.tsx
+++ b/packages/dashboard/src/components/spree/api-keys/api-key-table.tsx
@@ -33,6 +33,8 @@ import { BanIcon, CheckIcon, CopyIcon, KeyRoundIcon, PencilIcon } from '@spree/d
import type { ReactNode } from 'react'
import { useTranslation } from 'react-i18next'
import { useDeleteApiKey, useRevokeApiKey } from '../../../hooks/use-api-keys'
+import { usePermissionCatalog } from '../../../hooks/use-roles'
+import { permissionKeyLabel } from '../permission-picker'
/**
* The two kinds of key are listed side by side but answer different questions:
@@ -318,6 +320,11 @@ const SCOPE_PREVIEW_COUNT = 3
export function ScopeList({ scopes }: { scopes: string[] }) {
const { t } = useTranslation()
+ const { data: catalog } = usePermissionCatalog()
+
+ // Same shape as the roles table: a filled badge means "everything", an
+ // outlined one means a named permission, so the two read differently at a
+ // glance wherever permissions are listed.
if (scopes.includes('write_all')) {
return <Badge>{t('admin.pages.settings.api_keys.scope_badge.full_access')}</Badge>
}
@@ -334,8 +341,8 @@ export function ScopeList({ scopes }: { scopes: string[] }) {
return (
<div className="flex max-w-xs flex-wrap items-center gap-1">
{preview.map((scope) => (
- <Badge key={scope} className="font-mono text-[10px]">
- {scope}
+ <Badge key={scope} variant="outline">
+ {permissionKeyLabel(t, catalog?.data, scope)}
</Badge>
))}
{overflow.length > 0 && (
@@ -350,14 +357,16 @@ export function ScopeList({ scopes }: { scopes: string[] }) {
count: overflow.length,
})}
>
- <Badge className="font-mono text-[10px] hover:bg-accent">+{overflow.length}</Badge>
+ <Badge variant="secondary" className="hover:bg-accent">
+ +{overflow.length}
+ </Badge>
</button>
</PopoverTrigger>
<PopoverContent align="start" className="w-auto max-w-sm p-2">
<div className="flex max-h-60 flex-col gap-1 overflow-y-auto">
{scopes.map((scope) => (
- <Badge key={scope} className="self-start font-mono text-[10px]">
- {scope}
+ <Badge key={scope} variant="outline" className="self-start">
+ {permissionKeyLabel(t, catalog?.data, scope)}
</Badge>
))}
</div>
diff --git a/packages/dashboard/src/locales/ar.json b/packages/dashboard/src/locales/ar.json
index ce6990677b..33cfb68fff 100644
--- a/packages/dashboard/src/locales/ar.json
+++ b/packages/dashboard/src/locales/ar.json
@@ -4123,12 +4123,12 @@
"commission_tax_title": "الضريبة على العمولة"
},
"connected_apps": {
- "title": "التطبيقات المتصلة",
- "subtitle": "الوكلاء والتطبيقات التي منحها أحد أعضاء الفريق حق الوصول إلى هذا المتجر. يسري الإلغاء فورًا.",
- "empty": "لم يتم ربط أي تطبيق بعد.",
+ "title": "الوكلاء المتصلون",
+ "subtitle": "وكلاء الذكاء الاصطناعي الذين منحهم أحد أعضاء الفريق حق الوصول إلى هذا المتجر. يسري الإلغاء فورًا.",
+ "empty": "لم يتم ربط أي وكيل بعد.",
"revoke": "إلغاء الوصول",
"table": {
- "name": "التطبيق",
+ "name": "الوكيل",
"access": "الوصول",
"authorized_by": "وافق عليه",
"last_authorized": "آخر تفويض"
@@ -5197,7 +5197,7 @@
"package_types": "صناديق وكراتين ومنصات وحاويات",
"suppliers": "من تشتري منه المخزون، وكيفية التواصل معه",
"marketplace": "مدفوعات البائعين، والموافقة على البائعين، ورسائل البائعين، والضريبة على العمولة",
- "connected_apps": "الوكلاء والتطبيقات التي لديها حق الوصول إلى هذا المتجر"
+ "connected_apps": "وكلاء الذكاء الاصطناعي الذين لديهم حق الوصول إلى هذا المتجر"
},
"groups": {
"audit": "التدقيق",
@@ -5236,7 +5236,7 @@
"package_types": "أنواع التغليف",
"suppliers": "الموردون",
"marketplace": "الإعدادات",
- "connected_apps": "التطبيقات المتصلة"
+ "connected_apps": "الوكلاء المتصلون"
},
"breadcrumbs": {
"marketplace": "السوق"
diff --git a/packages/dashboard/src/locales/de.json b/packages/dashboard/src/locales/de.json
index f32b544578..a1354d9977 100644
--- a/packages/dashboard/src/locales/de.json
+++ b/packages/dashboard/src/locales/de.json
@@ -4095,12 +4095,12 @@
"commission_tax_title": "Steuer auf Provision"
},
"connected_apps": {
- "title": "Verbundene Apps",
- "subtitle": "Agenten und Anwendungen, denen ein Teammitglied Zugriff auf diesen Shop gegeben hat. Ein Widerruf wirkt sofort.",
- "empty": "Es wurden noch keine Anwendungen verbunden.",
+ "title": "Verbundene Agenten",
+ "subtitle": "KI-Agenten, denen ein Teammitglied Zugriff auf diesen Shop gegeben hat. Der Widerruf gilt sofort.",
+ "empty": "Es wurden noch keine Agenten verbunden.",
"revoke": "Zugriff widerrufen",
"table": {
- "name": "Anwendung",
+ "name": "Agent",
"access": "Zugriff",
"authorized_by": "Genehmigt von",
"last_authorized": "Letzte Autorisierung"
@@ -5164,7 +5164,7 @@
"package_types": "Kartons, Paletten und Container",
"suppliers": "Von wem Sie Ware einkaufen und wie Sie sie erreichen",
"marketplace": "Auszahlungen, Verkäuferfreigabe, Verkäufer-E-Mails und Steuer auf Provision",
- "connected_apps": "Agenten und Anwendungen mit Zugriff auf diesen Shop"
+ "connected_apps": "KI-Agenten mit Zugriff auf diesen Shop"
},
"groups": {
"audit": "Audit",
@@ -5203,7 +5203,7 @@
"package_types": "Verpackungsarten",
"suppliers": "Lieferanten",
"marketplace": "Einstellungen",
- "connected_apps": "Verbundene Apps"
+ "connected_apps": "Verbundene Agenten"
},
"breadcrumbs": {
"marketplace": "Marktplatz"
diff --git a/packages/dashboard/src/locales/en.json b/packages/dashboard/src/locales/en.json
index fb11cff903..edfa26c17d 100644
--- a/packages/dashboard/src/locales/en.json
+++ b/packages/dashboard/src/locales/en.json
@@ -4095,12 +4095,12 @@
"commission_tax_title": "Commission tax"
},
"connected_apps": {
- "title": "Connected apps",
- "subtitle": "Agents and applications a team member has given access to this store. Revoking takes effect immediately.",
- "empty": "No applications have been connected yet.",
+ "title": "Connected agents",
+ "subtitle": "AI agents a team member has given access to this store. Revoking takes effect immediately.",
+ "empty": "No agents have been connected yet.",
"revoke": "Revoke access",
"table": {
- "name": "Application",
+ "name": "Agent",
"access": "Access",
"authorized_by": "Approved by",
"last_authorized": "Last authorized"
@@ -5164,7 +5164,7 @@
"package_types": "Boxes, cartons, pallets and containers",
"suppliers": "Who you buy stock from, and how to reach them",
"marketplace": "Seller payouts, seller approval, seller emails and commission tax",
- "connected_apps": "Agents and applications with access to this store"
+ "connected_apps": "AI agents with access to this store"
},
"groups": {
"audit": "Audit",
@@ -5203,7 +5203,7 @@
"package_types": "Package types",
"suppliers": "Suppliers",
"marketplace": "Settings",
- "connected_apps": "Connected apps"
+ "connected_apps": "Connected agents"
},
"breadcrumbs": {
"marketplace": "Marketplace"
diff --git a/packages/dashboard/src/locales/es.json b/packages/dashboard/src/locales/es.json
index 75d8cf8b3e..e7333f07f6 100644
--- a/packages/dashboard/src/locales/es.json
+++ b/packages/dashboard/src/locales/es.json
@@ -721,12 +721,12 @@
"commission_tax_title": "Impuesto sobre la comisión"
},
"connected_apps": {
- "title": "Aplicaciones conectadas",
- "subtitle": "Agentes y aplicaciones a los que un miembro del equipo ha dado acceso a esta tienda. La revocación surte efecto de inmediato.",
- "empty": "Todavía no se ha conectado ninguna aplicación.",
+ "title": "Agentes conectados",
+ "subtitle": "Agentes de IA a los que un miembro del equipo ha dado acceso a esta tienda. La revocación surte efecto de inmediato.",
+ "empty": "Aún no se ha conectado ningún agente.",
"revoke": "Revocar acceso",
"table": {
- "name": "Aplicación",
+ "name": "Agente",
"access": "Acceso",
"authorized_by": "Aprobado por",
"last_authorized": "Última autorización"
@@ -4235,7 +4235,7 @@
"package_types": "Cajas, cajones, palets y contenedores",
"suppliers": "A quién le comprás stock y cómo contactarlos",
"marketplace": "Pagos a vendedores, aprobación de vendedores, correos a vendedores e impuesto sobre la comisión",
- "connected_apps": "Agentes y aplicaciones con acceso a esta tienda"
+ "connected_apps": "Agentes de IA con acceso a esta tienda"
},
"groups": {
"audit": "Auditoría",
@@ -4274,7 +4274,7 @@
"package_types": "Tipos de empaque",
"suppliers": "Proveedores",
"marketplace": "Configuración",
- "connected_apps": "Aplicaciones conectadas"
+ "connected_apps": "Agentes conectados"
},
"breadcrumbs": {
"marketplace": "Marketplace"
diff --git a/packages/dashboard/src/locales/fr.json b/packages/dashboard/src/locales/fr.json
index 2492e36e08..1bc0a84b40 100644
--- a/packages/dashboard/src/locales/fr.json
+++ b/packages/dashboard/src/locales/fr.json
@@ -4095,12 +4095,12 @@
"commission_tax_title": "Taxe sur la commission"
},
"connected_apps": {
- "title": "Applications connectées",
- "subtitle": "Agents et applications auxquels un membre de l’équipe a donné accès à cette boutique. La révocation prend effet immédiatement.",
- "empty": "Aucune application n’a encore été connectée.",
+ "title": "Agents connectés",
+ "subtitle": "Agents IA auxquels un membre de l'équipe a donné accès à cette boutique. La révocation prend effet immédiatement.",
+ "empty": "Aucun agent n'a encore été connecté.",
"revoke": "Révoquer l’accès",
"table": {
- "name": "Application",
+ "name": "Agent",
"access": "Accès",
"authorized_by": "Autorisé par",
"last_authorized": "Dernière autorisation"
@@ -5164,7 +5164,7 @@
"package_types": "Cartons, palettes et conteneurs",
"suppliers": "À qui vous achetez votre stock, et comment les joindre",
"marketplace": "Paiements, validation des vendeurs, e-mails vendeurs et taxe sur la commission",
- "connected_apps": "Agents et applications ayant accès à cette boutique"
+ "connected_apps": "Agents IA ayant accès à cette boutique"
},
"groups": {
"audit": "Audit",
@@ -5203,7 +5203,7 @@
"package_types": "Types d'emballage",
"suppliers": "Fournisseurs",
"marketplace": "Paramètres",
- "connected_apps": "Applications connectées"
+ "connected_apps": "Agents connectés"
},
"breadcrumbs": {
"marketplace": "Marketplace"
diff --git a/packages/dashboard/src/locales/pl.json b/packages/dashboard/src/locales/pl.json
index 0b13b8a0ae..7c715b78ee 100644
--- a/packages/dashboard/src/locales/pl.json
+++ b/packages/dashboard/src/locales/pl.json
@@ -4149,12 +4149,12 @@
"commission_tax_title": "Podatek od prowizji"
},
"connected_apps": {
- "title": "Połączone aplikacje",
- "subtitle": "Agenci i aplikacje, którym członek zespołu dał dostęp do tego sklepu. Odebranie dostępu działa natychmiast.",
- "empty": "Nie połączono jeszcze żadnej aplikacji.",
+ "title": "Połączone agenty",
+ "subtitle": "Agenty AI, którym członek zespołu dał dostęp do tego sklepu. Cofnięcie dostępu działa natychmiast.",
+ "empty": "Nie połączono jeszcze żadnego agenta.",
"revoke": "Odbierz dostęp",
"table": {
- "name": "Aplikacja",
+ "name": "Agent",
"access": "Dostęp",
"authorized_by": "Zatwierdzone przez",
"last_authorized": "Ostatnia autoryzacja"
@@ -5228,7 +5228,7 @@
"package_types": "Kartony, palety i kontenery",
"suppliers": "Od kogo kupujesz towar i jak się z nimi skontaktować",
"marketplace": "Wypłaty, zatwierdzanie sprzedawców, e-maile do sprzedawców i podatek od prowizji",
- "connected_apps": "Agenci i aplikacje z dostępem do tego sklepu"
+ "connected_apps": "Agenty AI z dostępem do tego sklepu"
},
"groups": {
"audit": "Audyt",
@@ -5267,7 +5267,7 @@
"package_types": "Rodzaje opakowań",
"suppliers": "Dostawcy",
"marketplace": "Ustawienia",
- "connected_apps": "Połączone aplikacje"
+ "connected_apps": "Połączone agenty"
},
"breadcrumbs": {
"marketplace": "Marketplace"
diff --git a/packages/dashboard/src/locales/zh-CN.json b/packages/dashboard/src/locales/zh-CN.json
index 2993f1c1f3..1de52b201e 100644
--- a/packages/dashboard/src/locales/zh-CN.json
+++ b/packages/dashboard/src/locales/zh-CN.json
@@ -4085,12 +4085,12 @@
"commission_tax_title": "佣金税"
},
"connected_apps": {
- "title": "已连接的应用",
- "subtitle": "团队成员授予此店铺访问权限的代理和应用。撤销将立即生效。",
- "empty": "尚未连接任何应用。",
+ "title": "已连接的代理",
+ "subtitle": "团队成员授予此店铺访问权限的 AI 代理。撤销立即生效。",
+ "empty": "尚未连接任何代理。",
"revoke": "撤销访问权限",
"table": {
- "name": "应用",
+ "name": "代理",
"access": "访问权限",
"authorized_by": "批准人",
"last_authorized": "最后授权时间"
@@ -5153,7 +5153,7 @@
"package_types": "纸箱、外箱、托盘与集装箱",
"suppliers": "你从谁那里采购库存,以及如何联系他们",
"marketplace": "卖家付款、卖家审核、卖家邮件与佣金税",
- "connected_apps": "可访问此店铺的代理和应用"
+ "connected_apps": "有权访问此店铺的 AI 代理"
},
"groups": {
"audit": "审计",
@@ -5192,7 +5192,7 @@
"package_types": "包装类型",
"suppliers": "供应商",
"marketplace": "设置",
- "connected_apps": "已连接的应用"
+ "connected_apps": "已连接的代理"
},
"breadcrumbs": {
"marketplace": "平台"
diff --git a/spree/api/app/controllers/spree/api/v3/admin/oauth/authorizations_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/oauth/authorizations_controller.rb
index aaa1722c05..e4ed34c658 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/oauth/authorizations_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/oauth/authorizations_controller.rb
@@ -122,6 +122,8 @@ module Spree
# anything their own role does not carry.
#
# @return [Array<String>]
+ # Every individual permission the merchant could hand over, which
+ # is what the consent screen draws a row for.
def grantable_scopes
held = current_ability.try(:permission_keys)&.map(&:to_s) || []
requested = Spree.permissions.expand_keys(@pre_auth.scopes.to_a)
@@ -129,6 +131,53 @@ module Spree
(requested & held).sort
end
+ # What the grant records, given what the merchant left ticked.
+ #
+ # An untouched "everything" stays an alias rather than becoming a
+ # list. Enumerating would freeze it: ship a feature with new
+ # permissions and the grant does not name them, so an agent the
+ # merchant gave full access silently cannot use the new tools and
+ # nothing says why. A narrowed selection is a deliberate list and
+ # stays one.
+ def recorded_scopes
+ granted = selected_scopes
+ return granted unless granted.sort == grantable_scopes.sort
+ return granted unless records_as_alias?
+
+ requested = @pre_auth.scopes.to_a
+ return ['write_all'] if requested.include?('write_all')
+
+ ['read_all']
+ end
+
+ # An alias is only safe for someone who holds everything it
+ # stands for.
+ #
+ # It is resolved against the token owner's ability at each
+ # request, so recording it for a limited staffer would widen the
+ # grant the day they are promoted — the standing-ceiling problem
+ # the intersection exists to prevent. A partial holder gets the
+ # list their authority actually covers.
+ def records_as_alias?
+ requested = @pre_auth.scopes.to_a
+ return false unless requested.intersect?(Spree::Api::Oauth::ALIAS_SCOPES)
+
+ held = current_ability.try(:permission_keys)&.map(&:to_s) || []
+ catalog = Spree.permissions.grantable_keys(
+ Spree::PermissionConfiguration::STAFF_AUDIENCE
+ ).map(&:to_s)
+
+ (catalog - held).empty?
+ end
+
+ # What the merchant left ticked, bounded by what they may give.
+ def selected_scopes
+ submitted = Spree.permissions.expand_keys(params[:scope].to_s.split)
+ return grantable_scopes if submitted.empty?
+
+ (submitted & grantable_scopes).sort
+ end
+
# Symbol keys: PreAuthorization reads them that way, and a hash of
# strings silently produces an unauthorizable request.
def pre_authorization_params
@@ -175,7 +224,7 @@ module Spree
# successful connection and the merchant sees the application
# listed as connected.
def require_something_to_grant!
- return if grantable_scopes.any?
+ return if selected_scopes.any?
render_error(
code: Spree::Api::V3::ErrorHandler::ERROR_CODES[:resource_invalid],
@@ -187,7 +236,7 @@ module Spree
def granted_code_request
bounded = ::Doorkeeper::OAuth::PreAuthorization.new(
::Doorkeeper.config,
- pre_authorization_params.merge(scope: grantable_scopes.join(' ')),
+ pre_authorization_params.merge(scope: recorded_scopes.join(' ')),
current_actor
)
diff --git a/spree/api/lib/spree/api/oauth.rb b/spree/api/lib/spree/api/oauth.rb
index 250442d9fe..1d15c3fe67 100644
--- a/spree/api/lib/spree/api/oauth.rb
+++ b/spree/api/lib/spree/api/oauth.rb
@@ -26,6 +26,16 @@ module Spree
# URL options otherwise. A store's own `url` column is deliberately not
# consulted — it describes the storefront, which is frequently neither
# the host nor the scheme the Admin API answers on.
+ # The aliases, offered alongside the individual keys.
+ #
+ # A grant that enumerates every key is frozen at the moment it was
+ # given: ship a feature with new permissions and every existing
+ # connection silently lacks them, with nothing to tell the merchant why
+ # its agent stopped seeing the new tools. `write_all` keeps meaning
+ # "everything" as the catalog grows, which is what a merchant who
+ # ticked "Full access" believed they were agreeing to.
+ ALIAS_SCOPES = %w[read_all write_all].freeze
+
mattr_accessor :resources, default: {}
class << self
@@ -83,7 +93,7 @@ module Spree
# @return [Array<String>]
def staff_scope_keys
- Spree.permissions.grantable_keys(Spree::PermissionConfiguration::STAFF_AUDIENCE)
+ ALIAS_SCOPES + Spree.permissions.grantable_keys(Spree::PermissionConfiguration::STAFF_AUDIENCE)
end
# Whether every requested audience names a resource this application
diff --git a/spree/api/spec/requests/spree/api/v3/admin/oauth/authorizations_spec.rb b/spree/api/spec/requests/spree/api/v3/admin/oauth/authorizations_spec.rb
index 0c1231c6c6..236af76813 100644
--- a/spree/api/spec/requests/spree/api/v3/admin/oauth/authorizations_spec.rb
+++ b/spree/api/spec/requests/spree/api/v3/admin/oauth/authorizations_spec.rb
@@ -74,6 +74,27 @@ RSpec.describe 'Admin OAuth consent', type: :request do
end
end
+ # A grant that enumerates every key is frozen at the moment it was given.
+ # Ship a feature with new permissions and an agent the merchant gave full
+ # access silently cannot use the new tools, with nothing to say why.
+ describe 'a grant of everything the approver holds' do
+ it 'is recorded as the alias, not as the keys it stands for today' do
+ admin = create(:admin_user)
+
+ approve(admin, scope: 'write_all')
+
+ expect(Spree::OauthAccessGrant.order(:id).last.scopes.to_a).to eq(['write_all'])
+ end
+
+ it 'still cannot exceed what the approver holds' do
+ approve(staffer, scope: 'write_all')
+
+ # The staffer holds read_products alone, so "everything" is that one
+ # key — an alias must never widen a grant beyond its grantor.
+ expect(Spree::OauthAccessGrant.order(:id).last.scopes.to_a).to contain_exactly('read_products')
+ end
+ end
+
# The merchant may hand over less than was asked for; that choice has to
# survive, or the checkboxes are decoration.
it 'honours a narrower selection than the client requested' do