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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Added who approved a connected application
Damian Legawiec2d4b1a8a1a1a
diff --git a/packages/admin-sdk/src/types/generated/OauthApplication.ts b/packages/admin-sdk/src/types/generated/OauthApplication.tsindex 024029dbbc..d6af68d6ea 100644--- a/packages/admin-sdk/src/types/generated/OauthApplication.ts+++ b/packages/admin-sdk/src/types/generated/OauthApplication.ts@@ -1,4 +1,4 @@-// Typelizer digest cbcc75348711134a8d4fd2380ad60943+// Typelizer digest 8c2fb32a386af3dcd58e8300c3882d44//// DO NOT MODIFY: This file was automatically generated by Typelizer.@@ -10,6 +10,8 @@ interface OauthApplication {updated_at: string;scopes: Array<string>;last_used_at: string | null;+ authorized_at: string | null;+ authorized_by: string | null;}export default OauthApplication;diff --git a/packages/admin-sdk/src/zod/generated/OauthApplication.ts b/packages/admin-sdk/src/zod/generated/OauthApplication.tsindex d4e9d20406..47ac87cc1e 100644--- a/packages/admin-sdk/src/zod/generated/OauthApplication.ts+++ b/packages/admin-sdk/src/zod/generated/OauthApplication.ts@@ -9,6 +9,8 @@ export const OauthApplicationSchema = z.object({updated_at: z.string(),scopes: z.array(z.string()),last_used_at: z.string().nullable(),+ authorized_at: z.string().nullable(),+ authorized_by: z.string().nullable(),});export type OauthApplication = z.infer<typeof OauthApplicationSchema>;diff --git a/packages/dashboard/src/locales/ar.json b/packages/dashboard/src/locales/ar.jsonindex a7fc6f8d9c..3df97dab14 100644--- a/packages/dashboard/src/locales/ar.json+++ b/packages/dashboard/src/locales/ar.json@@ -4130,7 +4130,8 @@"table": {"name": "التطبيق","access": "الوصول",- "last_used": "آخر استخدام"+ "last_used": "آخر استخدام",+ "authorized_by": "وافق عليه"},"revoke_confirm": {"title": "إلغاء الوصول؟",diff --git a/packages/dashboard/src/locales/de.json b/packages/dashboard/src/locales/de.jsonindex 12e8bfe7c8..789d70ca5d 100644--- a/packages/dashboard/src/locales/de.json+++ b/packages/dashboard/src/locales/de.json@@ -4102,7 +4102,8 @@"table": {"name": "Anwendung","access": "Zugriff",- "last_used": "Zuletzt verwendet"+ "last_used": "Zuletzt verwendet",+ "authorized_by": "Genehmigt von"},"revoke_confirm": {"title": "Zugriff widerrufen?",diff --git a/packages/dashboard/src/locales/en.json b/packages/dashboard/src/locales/en.jsonindex 8d040e7479..8be4d298e2 100644--- a/packages/dashboard/src/locales/en.json+++ b/packages/dashboard/src/locales/en.json@@ -4102,7 +4102,8 @@"table": {"name": "Application","access": "Access",- "last_used": "Last used"+ "last_used": "Last used",+ "authorized_by": "Approved by"},"revoke_confirm": {"title": "Revoke access?",diff --git a/packages/dashboard/src/locales/es.json b/packages/dashboard/src/locales/es.jsonindex 76f9d340cc..84a6f64892 100644--- a/packages/dashboard/src/locales/es.json+++ b/packages/dashboard/src/locales/es.json@@ -728,7 +728,8 @@"table": {"name": "Aplicación","access": "Acceso",- "last_used": "Último uso"+ "last_used": "Último uso",+ "authorized_by": "Aprobado por"},"revoke_confirm": {"title": "¿Revocar el acceso?",diff --git a/packages/dashboard/src/locales/fr.json b/packages/dashboard/src/locales/fr.jsonindex fef75fbbe7..9f88cb120c 100644--- a/packages/dashboard/src/locales/fr.json+++ b/packages/dashboard/src/locales/fr.json@@ -4102,7 +4102,8 @@"table": {"name": "Application","access": "Accès",- "last_used": "Dernière utilisation"+ "last_used": "Dernière utilisation",+ "authorized_by": "Autorisé par"},"revoke_confirm": {"title": "Révoquer l’accès ?",diff --git a/packages/dashboard/src/locales/pl.json b/packages/dashboard/src/locales/pl.jsonindex c4c4652e5a..61fcda05cb 100644--- a/packages/dashboard/src/locales/pl.json+++ b/packages/dashboard/src/locales/pl.json@@ -4156,7 +4156,8 @@"table": {"name": "Aplikacja","access": "Dostęp",- "last_used": "Ostatnio użyta"+ "last_used": "Ostatnio użyta",+ "authorized_by": "Zatwierdzone przez"},"revoke_confirm": {"title": "Odebrać dostęp?",diff --git a/packages/dashboard/src/locales/zh-CN.json b/packages/dashboard/src/locales/zh-CN.jsonindex 870712c5ec..43c6d73eec 100644--- a/packages/dashboard/src/locales/zh-CN.json+++ b/packages/dashboard/src/locales/zh-CN.json@@ -4092,7 +4092,8 @@"table": {"name": "应用","access": "访问权限",- "last_used": "上次使用"+ "last_used": "上次使用",+ "authorized_by": "批准人"},"revoke_confirm": {"title": "撤销访问权限?",diff --git a/packages/dashboard/src/tables/oauth-applications.tsx b/packages/dashboard/src/tables/oauth-applications.tsxindex 7e18bb7b50..92366b463e 100644--- a/packages/dashboard/src/tables/oauth-applications.tsx+++ b/packages/dashboard/src/tables/oauth-applications.tsx@@ -23,6 +23,22 @@ defineTable<OauthApplication>('oauth-applications', {default: true,render: (application) => <ScopeList scopes={application.scopes} />,},+ {+ key: 'authorized_by',+ label: i18n.t('admin.pages.settings.connected_apps.table.authorized_by'),+ default: true,+ render: (application) =>+ application.authorized_by ? (+ <span className="flex flex-col">+ <span>{application.authorized_by}</span>+ <span className="text-muted-foreground text-xs">+ <RelativeTime iso={application.authorized_at} />+ </span>+ </span>+ ) : (+ <span className="text-muted-foreground">—</span>+ ),+ },{key: 'last_used_at',label: i18n.t('admin.pages.settings.connected_apps.table.last_used'),diff --git a/spree/api/app/controllers/spree/api/v3/admin/oauth/applications_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/oauth/applications_controller.rbindex a1a607b127..722fb9964e 100644--- a/spree/api/app/controllers/spree/api/v3/admin/oauth/applications_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/oauth/applications_controller.rb@@ -16,12 +16,12 @@ module Spreedef index# Only applications somebody actually authorized — a registered- # client nobody connected is not a connection. Tokens are- # preloaded because the serializer reads each application's- # scopes and last use from them.+ # client nobody connected is not a connection. Tokens and their+ # owners are preloaded because the serializer reads each+ # application's scopes, last use and who approved it from them.scope = current_store.oauth_applications.where(id: connected_application_ids).- includes(:live_access_tokens).+ includes(live_access_tokens: :resource_owner).order(:name)@pagy, applications = pagy(scope, limit: params[:limit] || 25)diff --git a/spree/api/app/serializers/spree/api/v3/admin/oauth_application_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/oauth_application_serializer.rbindex ac28fb4073..70c7a93ba0 100644--- a/spree/api/app/serializers/spree/api/v3/admin/oauth_application_serializer.rb+++ b/spree/api/app/serializers/spree/api/v3/admin/oauth_application_serializer.rb@@ -15,6 +15,8 @@ module Spreetypelize name: :string,scopes: [:string, multi: true],last_used_at: [:string, nullable: true],+ authorized_at: [:string, nullable: true],+ authorized_by: [:string, nullable: true],redirect_uri: [:string, nullable: true]attributes :name, :redirect_uri, created_at: :iso8601, updated_at: :iso8601@@ -26,6 +28,17 @@ module Spreeattribute :last_used_at do |application|application.last_used_at&.iso8601end++ attribute :authorized_at do |application|+ application.authorized_at&.iso8601+ end++ # Who allowed this, in the words a person is known by. On the screen+ # that decides whether to revoke, the permission alone is half the+ # story.+ attribute :authorized_by do |application|+ application.authorized_by.try(:actor_label)+ endendendenddiff --git a/spree/api/spec/requests/spree/api/v3/admin/oauth/applications_spec.rb b/spree/api/spec/requests/spree/api/v3/admin/oauth/applications_spec.rbnew file mode 100644index 0000000000..c9caa6b0ba--- /dev/null+++ b/spree/api/spec/requests/spree/api/v3/admin/oauth/applications_spec.rb@@ -0,0 +1,81 @@+require 'spec_helper'++# The screen this feeds exists to decide whether to take access away, so it+# has to answer both halves: what the application may do, and who allowed it.+RSpec.describe 'Admin connected applications', type: :request do+ let(:store) { @default_store }+ let(:admin) { create(:admin_user) }+ let(:api_key) { create(:api_key, :secret, store: store, scopes: ['write_all']) }+ let(:application) do+ store.oauth_applications.create!(+ name: 'Claude', redirect_uri: 'https://example.test/cb', confidential: false+ )+ end++ before { Spree::Api::Oauth.register_resource(:mcp, '/api/v3/admin/mcp') }++ def token_for(owner, scopes: 'read_products')+ Spree::OauthAccessToken.create!(+ application: application, resource_owner: owner, scopes: scopes,+ expires_in: 2.hours.to_i, resource: Spree::Api::Oauth.resource_identifier(:mcp)+ )+ end++ def list+ get '/api/v3/admin/oauth/applications', headers: { 'X-Spree-API-Key' => api_key.plaintext_token }+ JSON.parse(response.body)+ end++ it 'names who approved the application and when' do+ token = token_for(admin)++ row = list['data'].first++ expect(row['authorized_by']).to eq(admin.actor_label)+ expect(Time.iso8601(row['authorized_at'])).to be_within(2.seconds).of(token.created_at)+ end++ # A later token refreshes an existing grant; it does not change who gave+ # it, so the oldest live consent is the one still in force.+ it 'keeps naming the original approver after a refresh' do+ first = token_for(admin)+ token_for(create(:admin_user))++ expect(Time.iso8601(list['data'].first['authorized_at'])).+ to be_within(2.seconds).of(first.created_at)+ end++ it 'lists only applications somebody actually connected' do+ store.oauth_applications.create!(+ name: 'Never connected', redirect_uri: 'https://example.test/cb', confidential: false+ )+ token_for(admin)++ expect(list['data'].map { |row| row['name'] }).to contain_exactly('Claude')+ end++ it 'reports what the live tokens grant, not the registration' do+ token_for(admin, scopes: 'read_products write_orders')++ expect(list['data'].first['scopes']).to include('write_orders', 'read_products')+ end++ it 'pages, so a store with many connections is not served in one response' do+ token_for(admin)++ expect(list['meta']).to include('page', 'count', 'pages')+ end++ describe 'revoking' do+ it 'stops the application working without deleting its registration' do+ raw = token_for(admin).token++ delete "/api/v3/admin/oauth/applications/#{application.prefixed_id}",+ headers: { 'X-Spree-API-Key' => api_key.plaintext_token }++ expect(response).to have_http_status(:no_content)+ expect(Spree::OauthAccessToken.by_token(raw)).not_to be_accessible+ expect(store.oauth_applications.find_by(name: 'Claude')).to be_present+ end+ end+enddiff --git a/spree/core/app/models/spree/oauth_application.rb b/spree/core/app/models/spree/oauth_application.rbindex 5586ba6ae1..441f0133b4 100644--- a/spree/core/app/models/spree/oauth_application.rb+++ b/spree/core/app/models/spree/oauth_application.rb@@ -48,5 +48,29 @@ module Spreedef last_used_atlive_access_tokens.map(&:created_at).maxend++ # Who approved this application's access, and when.+ #+ # Read from the oldest live token, because that is the consent still in+ # force — a later token refreshes an existing grant rather than replacing+ # who gave it. On the screen that decides whether to revoke, "Claude can+ # read your orders" is only half the story without the person who allowed+ # it.+ #+ # @return [Object, nil] a {Spree.admin_user_class} record+ def authorized_by+ oldest_live_token&.resource_owner+ end++ # @return [ActiveSupport::TimeWithZone, nil]+ def authorized_at+ oldest_live_token&.created_at+ end++ private++ def oldest_live_token+ live_access_tokens.min_by(&:created_at)+ endendend