spree_api 6.0.0.rc3 → 6.0.0.rc4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/controllers/concerns/spree/api/v3/admin/email_template_lookup.rb +6 -1
- data/app/controllers/concerns/spree/api/v3/admin/reporting_authorization.rb +17 -8
- data/app/controllers/concerns/spree/api/v3/admin/store_context.rb +7 -0
- data/app/controllers/concerns/spree/api/v3/admin_authentication.rb +61 -0
- data/app/controllers/concerns/spree/api/v3/error_handler.rb +49 -0
- data/app/controllers/concerns/spree/api/v3/params_normalizer.rb +76 -7
- data/app/controllers/concerns/spree/api/v3/scoped_authorization.rb +60 -3
- data/app/controllers/spree/api/v3/admin/companies/tax_exemption_certificates_controller.rb +11 -5
- data/app/controllers/spree/api/v3/admin/concerns/external_references.rb +3 -0
- data/app/controllers/spree/api/v3/admin/exports_controller.rb +15 -3
- data/app/controllers/spree/api/v3/admin/imports_controller.rb +12 -3
- data/app/controllers/spree/api/v3/admin/markets_controller.rb +3 -1
- data/app/controllers/spree/api/v3/admin/me_controller.rb +21 -1
- data/app/controllers/spree/api/v3/admin/oauth/application_tokens_controller.rb +56 -0
- data/app/controllers/spree/api/v3/admin/oauth/applications_controller.rb +55 -0
- data/app/controllers/spree/api/v3/admin/oauth/authorizations_controller.rb +294 -0
- data/app/controllers/spree/api/v3/admin/order_groups_controller.rb +1 -1
- data/app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb +8 -0
- data/app/controllers/spree/api/v3/admin/orders/items_controller.rb +1 -0
- data/app/controllers/spree/api/v3/admin/orders/returns_controller.rb +7 -0
- data/app/controllers/spree/api/v3/admin/orders_controller.rb +9 -2
- data/app/controllers/spree/api/v3/admin/purchase_orders_controller.rb +1 -0
- data/app/controllers/spree/api/v3/admin/reporting_controller.rb +20 -14
- data/app/controllers/spree/api/v3/admin/stock_levels_controller.rb +1 -1
- data/app/controllers/spree/api/v3/admin/tags_controller.rb +8 -5
- data/app/controllers/spree/api/v3/admin/tax_exemption_reasons_controller.rb +58 -0
- data/app/controllers/spree/api/v3/admin/tax_rates_controller.rb +0 -1
- data/app/controllers/spree/api/v3/admin/translations/batches_controller.rb +17 -11
- data/app/controllers/spree/api/v3/oauth/authorizations_controller.rb +61 -0
- data/app/controllers/spree/api/v3/oauth/metadata_controller.rb +57 -0
- data/app/controllers/spree/api/v3/oauth/server_metadata_controller.rb +57 -0
- data/app/controllers/spree/api/v3/resource_controller.rb +1 -0
- data/app/controllers/spree/api/v3/store/carts_controller.rb +4 -5
- data/app/request_schemas/spree/api/v3/admin/email_template_preview_request_schema.rb +4 -0
- data/app/serializers/spree/api/v3/admin/fulfillment_serializer.rb +2 -0
- data/app/serializers/spree/api/v3/admin/line_item_serializer.rb +2 -0
- data/app/serializers/spree/api/v3/admin/oauth_access_token_serializer.rb +62 -0
- data/app/serializers/spree/api/v3/admin/oauth_application_serializer.rb +56 -0
- data/app/serializers/spree/api/v3/admin/oauth_consent_request_serializer.rb +25 -0
- data/app/serializers/spree/api/v3/admin/oauth_protected_resource_serializer.rb +27 -0
- data/app/serializers/spree/api/v3/admin/oauth_redirect_serializer.rb +17 -0
- data/app/serializers/spree/api/v3/admin/payment_capture_event_serializer.rb +16 -0
- data/app/serializers/spree/api/v3/admin/payment_serializer.rb +12 -1
- data/app/serializers/spree/api/v3/admin/return_serializer.rb +2 -0
- data/app/serializers/spree/api/v3/admin/tax_exemption_certificate_serializer.rb +7 -3
- data/app/serializers/spree/api/v3/admin/tax_exemption_reason_serializer.rb +20 -0
- data/app/serializers/spree/api/v3/admin/tax_rate_serializer.rb +1 -2
- data/app/serializers/spree/api/v3/admin/webhook_delivery_serializer.rb +4 -1
- data/app/serializers/spree/api/v3/base_serializer.rb +14 -0
- data/config/routes.rb +61 -1
- data/lib/spree/api/agent_membership_map.rb +73 -0
- data/lib/spree/api/agent_resource_map.rb +350 -0
- data/lib/spree/api/agent_workflow_endpoints.rb +113 -0
- data/lib/spree/api/agent_write_schemas.rb +119 -0
- data/lib/spree/api/dependencies.rb +4 -3
- data/lib/spree/api/engine.rb +40 -0
- data/lib/spree/api/extension_fields.rb +167 -0
- data/lib/spree/api/oauth.rb +243 -0
- data/lib/spree/api.rb +8 -0
- data/lib/typelizer/open_enums.rb +17 -3
- metadata +26 -6
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
module Spree
|
|
2
|
+
module Api
|
|
3
|
+
# The writable attribute schema for each admin resource, read from the
|
|
4
|
+
# generated OpenAPI document.
|
|
5
|
+
#
|
|
6
|
+
# Not from the controllers: most of them declare their writable attributes
|
|
7
|
+
# inside `permitted_params`, where the list is a `params.permit` call that
|
|
8
|
+
# cannot be read without a request. `admin.yaml` already carries the same
|
|
9
|
+
# list — generated from the integration specs, reviewed as a diff on every
|
|
10
|
+
# API change — with the types, descriptions and examples a model needs to
|
|
11
|
+
# fill a form in correctly. Reading it means the generic write tools offer
|
|
12
|
+
# exactly the documented endpoint and nothing else.
|
|
13
|
+
#
|
|
14
|
+
# Loaded once, lazily: the document is well over a megabyte, and an
|
|
15
|
+
# installation with no agent client should never pay for parsing it.
|
|
16
|
+
module AgentWriteSchemas
|
|
17
|
+
# A collection endpoint under the admin namespace: `/api/v3/admin/markets`
|
|
18
|
+
# but not `/api/v3/admin/markets/{id}` or a nested child.
|
|
19
|
+
COLLECTION_PATH = %r{\A/api/v3/admin/(?<key>[a-z0-9_]+)\z}
|
|
20
|
+
|
|
21
|
+
class << self
|
|
22
|
+
# The writable properties for one resource.
|
|
23
|
+
#
|
|
24
|
+
# @param key [String] the resource key, e.g. "markets"
|
|
25
|
+
# @return [Hash] JSON Schema properties, empty when the resource has no
|
|
26
|
+
# documented write
|
|
27
|
+
def for(key)
|
|
28
|
+
schemas[key.to_s] || {}
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# @return [Array<String>] the attribute names a write may set
|
|
32
|
+
def attribute_names(key)
|
|
33
|
+
self.for(key).keys
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Where the generated document lives, relative to this engine. Resolved
|
|
37
|
+
# lazily: the engine class does not exist yet while this file loads.
|
|
38
|
+
#
|
|
39
|
+
# @return [Pathname]
|
|
40
|
+
def spec_path
|
|
41
|
+
Spree::Api::Engine.root.join('..', '..', 'docs', 'api-reference', 'admin.yaml')
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# The parsed document, for a reader that needs more of it than the
|
|
45
|
+
# write bodies — the workflow endpoint map wants the member actions'
|
|
46
|
+
# bodies too, and parsing a megabyte of YAML twice is wasteful.
|
|
47
|
+
#
|
|
48
|
+
# @return [Hash]
|
|
49
|
+
def document
|
|
50
|
+
@document ||= begin
|
|
51
|
+
path = spec_path
|
|
52
|
+
File.exist?(path) ? (YAML.unsafe_load_file(path) || {}) : {}
|
|
53
|
+
rescue StandardError => e
|
|
54
|
+
Rails.logger.warn("[Spree] could not read the admin API document: #{e.class}: #{e.message}")
|
|
55
|
+
{}
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def schemas
|
|
62
|
+
@schemas ||= load_schemas
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# A missing or unparseable document is not a boot failure: the generic
|
|
66
|
+
# write tools simply offer nothing, which is the safe direction. An
|
|
67
|
+
# installation running from the packaged gems has no docs/ directory at
|
|
68
|
+
# all.
|
|
69
|
+
def load_schemas
|
|
70
|
+
path = spec_path
|
|
71
|
+
return {} unless File.exist?(path)
|
|
72
|
+
|
|
73
|
+
document = YAML.unsafe_load_file(path)
|
|
74
|
+
extract(document)
|
|
75
|
+
rescue StandardError => e
|
|
76
|
+
Rails.logger.warn("[Spree] could not read admin API write schemas: #{e.class}: #{e.message}")
|
|
77
|
+
{}
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def extract(document)
|
|
81
|
+
Array(document['paths']).to_h.each_with_object({}) do |(path, operations), result|
|
|
82
|
+
match = COLLECTION_PATH.match(path)
|
|
83
|
+
next if match.nil?
|
|
84
|
+
|
|
85
|
+
properties = write_properties(operations)
|
|
86
|
+
result[match[:key]] = properties if properties.present?
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# The create body where there is one, because it documents every
|
|
91
|
+
# settable attribute; an update body usually repeats a subset.
|
|
92
|
+
def write_properties(operations)
|
|
93
|
+
body = operations.dig('post', 'requestBody', 'content', 'application/json', 'schema')
|
|
94
|
+
body ||= operations.dig('patch', 'requestBody', 'content', 'application/json', 'schema')
|
|
95
|
+
|
|
96
|
+
resolve_schema(body)&.dig('properties').presence
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
public
|
|
100
|
+
|
|
101
|
+
# A request body is declared once per endpoint and published as a
|
|
102
|
+
# component, so the operation carries a `$ref` to it rather than the
|
|
103
|
+
# properties themselves. Followed a few hops, which is as deep as the
|
|
104
|
+
# generated document nests them.
|
|
105
|
+
#
|
|
106
|
+
# @param schema [Hash, nil] a schema that may be a reference
|
|
107
|
+
# @return [Hash, nil] the schema it names
|
|
108
|
+
def resolve_schema(schema, depth = 0)
|
|
109
|
+
return unless schema.is_a?(Hash)
|
|
110
|
+
return schema unless schema.key?('$ref')
|
|
111
|
+
return if depth > 3
|
|
112
|
+
|
|
113
|
+
name = schema['$ref'].to_s.split('/').last
|
|
114
|
+
resolve_schema(document.dig('components', 'schemas', name), depth + 1)
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
@@ -32,13 +32,14 @@ module Spree
|
|
|
32
32
|
exchange exchange_line_item claim claim_line_item package_type policy tax_category tax_rate
|
|
33
33
|
tax_identifier company company_membership company_invitation catalog catalog_price catalog_price_tier
|
|
34
34
|
catalog_product price_list_product catalog_assignment catalog_quantity_rule catalog_product_term
|
|
35
|
-
catalog_order_minimum tax_exemption_certificate admin_user actor seller_team_member product_submission
|
|
35
|
+
catalog_order_minimum tax_exemption_certificate tax_exemption_reason admin_user actor seller_team_member product_submission
|
|
36
36
|
seller seller_requirement seller_requirement_submission seller_requirement_status commission_rate
|
|
37
|
-
commission_rule commission_line seller_transfer seller_payout seller_balance payment_split address
|
|
37
|
+
commission_rule commission_line seller_transfer seller_payout seller_balance payment_split payment_capture_event address
|
|
38
38
|
channel order_routing_rule product_publication market delivery_method delivery_method_service
|
|
39
39
|
stock_location stock_reservation delivery_rate freight_summary freight_summary_line payment_method
|
|
40
40
|
credit_card store_credit store_credit_event customer_group payment_source digital_asset digital_link
|
|
41
|
-
store setup_task update_status api_key
|
|
41
|
+
store setup_task update_status api_key oauth_application oauth_access_token oauth_consent_request oauth_redirect
|
|
42
|
+
oauth_protected_resource allowed_origin webhook_endpoint webhook_event webhook_delivery invitation
|
|
42
43
|
invitation_acceptance_link role permission export saved_report import import_row import_mapping promotion
|
|
43
44
|
promotion_action promotion_rule coupon_code price_adjustment_tier price_list price_rule
|
|
44
45
|
resource_translations email_template email_template_draft email_template_revision email_template_preview
|
data/lib/spree/api/engine.rb
CHANGED
|
@@ -2,6 +2,11 @@ require 'rails/engine'
|
|
|
2
2
|
|
|
3
3
|
require_relative 'dependencies'
|
|
4
4
|
require_relative 'configuration'
|
|
5
|
+
require_relative 'agent_write_schemas'
|
|
6
|
+
require_relative 'agent_resource_map'
|
|
7
|
+
require_relative 'agent_membership_map'
|
|
8
|
+
require_relative 'agent_workflow_endpoints'
|
|
9
|
+
require_relative 'oauth'
|
|
5
10
|
|
|
6
11
|
module Spree
|
|
7
12
|
module Api
|
|
@@ -14,11 +19,46 @@ module Spree
|
|
|
14
19
|
Spree::Api::Dependencies = Spree::Api::ApiDependencies.new
|
|
15
20
|
end
|
|
16
21
|
|
|
22
|
+
# Applied from the engine so an installation gets a correct, locked-down
|
|
23
|
+
# authorization server by adding the gem.
|
|
24
|
+
#
|
|
25
|
+
# Before host initializers, not after: `Doorkeeper.configure` replaces
|
|
26
|
+
# the whole configuration, so running later would silently discard a
|
|
27
|
+
# host's own `config/initializers/doorkeeper.rb`. This way the host's
|
|
28
|
+
# file wins, which is what "configure it yourself" has to mean.
|
|
29
|
+
initializer 'spree.api.oauth', before: :load_config_initializers do |_app|
|
|
30
|
+
Spree::Api::Oauth.configure!
|
|
31
|
+
|
|
32
|
+
# The Admin API as a whole, for an app that works across it rather
|
|
33
|
+
# than through one endpoint. A token naming this audience is accepted
|
|
34
|
+
# on every admin controller; one naming a narrower resource — the MCP
|
|
35
|
+
# endpoint, say — is not, so a token taken from an MCP client cannot
|
|
36
|
+
# be replayed against the rest of the API.
|
|
37
|
+
Spree::Api::Oauth.register_resource(:admin, '/api/v3/admin')
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Extensions register their permission scopes from config initializers,
|
|
41
|
+
# which run after the configuration above — so the grantable list is
|
|
42
|
+
# re-read once they have.
|
|
43
|
+
config.after_initialize do
|
|
44
|
+
Spree::Api::Oauth.refresh_scopes!
|
|
45
|
+
end
|
|
46
|
+
|
|
17
47
|
initializer 'spree.api.request_size_limit' do |app|
|
|
18
48
|
require_relative 'middleware/request_size_limit'
|
|
19
49
|
app.middleware.insert_before Rack::Runtime, Spree::Api::Middleware::RequestSizeLimit
|
|
20
50
|
end
|
|
21
51
|
|
|
52
|
+
# The resources agents may read and write are derived from the admin
|
|
53
|
+
# controllers themselves (see Spree::Api::AgentResourceMap). Marked
|
|
54
|
+
# stale on every reload rather than rebuilt: deriving it eager-loads the
|
|
55
|
+
# application, and doing that on each file save would turn Spree's
|
|
56
|
+
# development reload from lazy into fully eager for everyone, whether or
|
|
57
|
+
# not they run an agent client. The first caller pays instead.
|
|
58
|
+
config.to_prepare do
|
|
59
|
+
Spree::Api::AgentResourceMap.stale!
|
|
60
|
+
end
|
|
61
|
+
|
|
22
62
|
# Add API event subscribers
|
|
23
63
|
config.after_initialize do
|
|
24
64
|
Spree.subscribers << Spree::WebhookEventSubscriber
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
module Spree
|
|
2
|
+
module Api
|
|
3
|
+
# Read-only fields an extension adds to a core serializer's responses —
|
|
4
|
+
# the read side of +additional_permitted_attributes+.
|
|
5
|
+
#
|
|
6
|
+
# A field is registered against a serializer's dependency name, not its
|
|
7
|
+
# class, so a host app that replaces the serializer through +Spree.api+
|
|
8
|
+
# still renders it. An attribute writes its value as it is; an association
|
|
9
|
+
# renders a related record, or a list of them, with the extension's own
|
|
10
|
+
# serializer. Fields are left out of the generated SDK types: the
|
|
11
|
+
# extension that adds one declares its type.
|
|
12
|
+
#
|
|
13
|
+
# @example
|
|
14
|
+
# Spree.api.extension_fields.attribute(:admin_product_serializer, method: :warranty_text)
|
|
15
|
+
# Spree.api.extension_fields.association(
|
|
16
|
+
# :admin_product_serializer, one: :warranty,
|
|
17
|
+
# serializer: 'MyExtension::WarrantySerializer', expand: true
|
|
18
|
+
# )
|
|
19
|
+
class ExtensionFields
|
|
20
|
+
# What attributes and associations share: a name, whether it waits for
|
|
21
|
+
# an expand, and where its value comes from.
|
|
22
|
+
class Field
|
|
23
|
+
attr_reader :name
|
|
24
|
+
|
|
25
|
+
def initialize(name:, expand:, value:)
|
|
26
|
+
@name = name
|
|
27
|
+
@expand = expand
|
|
28
|
+
@value = value
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# @return [Boolean] whether the field is shown only when the request expands it
|
|
32
|
+
def expand?
|
|
33
|
+
@expand
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Run inside the core serializer, so it sees the request's expands.
|
|
37
|
+
# @return [Proc]
|
|
38
|
+
def reader
|
|
39
|
+
field = self
|
|
40
|
+
@reader ||= proc do |object|
|
|
41
|
+
next Alba::REMOVE_KEY if field.expand? && !expand?(field.name)
|
|
42
|
+
|
|
43
|
+
field.render(field.value_for(object), params: nested_params(field.name))
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# An extension cannot add a method to a core model, so +value+ can
|
|
48
|
+
# supply the value from the record instead.
|
|
49
|
+
#
|
|
50
|
+
# @param record [Object] the record the core serializer is writing
|
|
51
|
+
# @return [Object]
|
|
52
|
+
def value_for(record)
|
|
53
|
+
@value ? @value.call(record) : record.public_send(name)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# A value written as it is.
|
|
58
|
+
class Attribute < Field
|
|
59
|
+
# @return [Object]
|
|
60
|
+
def render(value, **)
|
|
61
|
+
value
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# A related record, or a list of them, written by the extension's
|
|
66
|
+
# serializer, which also receives the request's nested expands.
|
|
67
|
+
class Association < Field
|
|
68
|
+
def initialize(serializer:, **)
|
|
69
|
+
super(**)
|
|
70
|
+
@serializer = serializer
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# @return [Hash, Array<Hash>, nil]
|
|
74
|
+
def render(value, params:)
|
|
75
|
+
return if value.nil?
|
|
76
|
+
|
|
77
|
+
serializer_class.new(value, params: params).serializable_hash
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
def serializer_class
|
|
83
|
+
@serializer.is_a?(String) ? @serializer.constantize : @serializer
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
class DuplicateField < ArgumentError; end
|
|
88
|
+
|
|
89
|
+
def initialize
|
|
90
|
+
@fields = Hash.new { |hash, key| hash[key] = {} }
|
|
91
|
+
@class_readers = {}
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Either names the record's method that gives the value, or names the
|
|
95
|
+
# field and supplies its value:
|
|
96
|
+
#
|
|
97
|
+
# attribute(:admin_product_serializer, method: :warranty_text)
|
|
98
|
+
# attribute(:admin_product_serializer, :warranty_months, value: ->(product) { ... })
|
|
99
|
+
#
|
|
100
|
+
# @param dependency [Symbol] the serializer's dependency name, e.g. +:admin_product_serializer+
|
|
101
|
+
# @param name [Symbol, String, nil] the response key of a field whose value is given by +value+
|
|
102
|
+
# @param method [Symbol, String, nil] the response key, and the record's method that gives the value
|
|
103
|
+
# @param expand [Boolean] true to show the field only when the request expands it
|
|
104
|
+
# @param value [#call, nil] gives the value from the record; required with +name+, refused with +method+
|
|
105
|
+
# @raise [ArgumentError] when the dependency is not a serializer, or the name and value do not match up
|
|
106
|
+
# @raise [DuplicateField] when the serializer already has a field of that name
|
|
107
|
+
# @return [void]
|
|
108
|
+
def attribute(dependency, name = nil, method: nil, expand: false, value: nil)
|
|
109
|
+
raise ArgumentError, 'Pass either a name or method:' unless name.present? ^ method.present?
|
|
110
|
+
raise ArgumentError, 'A named attribute needs value:' if name.present? && value.nil?
|
|
111
|
+
raise ArgumentError, 'An attribute reading method: takes no value:' if method.present? && value
|
|
112
|
+
|
|
113
|
+
add(dependency, Attribute.new(name: (name || method).to_sym, expand: expand, value: value))
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# @param dependency [Symbol] the serializer's dependency name, e.g. +:admin_product_serializer+
|
|
117
|
+
# @param serializer [Class, String] writes each related record
|
|
118
|
+
# @param one [Symbol, String, nil] the response key of a single related record, read from the record's method of that name
|
|
119
|
+
# @param many [Symbol, String, nil] the same, for a list of related records
|
|
120
|
+
# @param expand [Boolean] true to show the field only when the request expands it
|
|
121
|
+
# @param value [#call, nil] gives the related record, or records, instead of calling the method
|
|
122
|
+
# @raise [ArgumentError] when the dependency is not a serializer, or not exactly one of +one+ and +many+ is given
|
|
123
|
+
# @raise [DuplicateField] when the serializer already has a field of that name
|
|
124
|
+
# @return [void]
|
|
125
|
+
def association(dependency, serializer:, one: nil, many: nil, expand: false, value: nil)
|
|
126
|
+
raise ArgumentError, 'Pass either one: or many:' unless one.present? ^ many.present?
|
|
127
|
+
|
|
128
|
+
add(dependency, Association.new(name: (one || many).to_sym, serializer: serializer, expand: expand, value: value))
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# @return [void]
|
|
132
|
+
def unregister(dependency, name)
|
|
133
|
+
@fields[dependency.to_sym].delete(name.to_sym)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# The fields of every dependency that currently resolves to this class.
|
|
137
|
+
#
|
|
138
|
+
# @param serializer_class [Class]
|
|
139
|
+
# @return [Array<Field>]
|
|
140
|
+
def for(serializer_class)
|
|
141
|
+
@fields.each_with_object([]) do |(dependency, fields), result|
|
|
142
|
+
# The dependency's cached reader, skipping Spree.api's method_missing on every render.
|
|
143
|
+
next if fields.empty? || Spree::Api::Dependencies.public_send(@class_readers[dependency]) != serializer_class
|
|
144
|
+
|
|
145
|
+
result.concat(fields.values)
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
private
|
|
150
|
+
|
|
151
|
+
def add(dependency, field)
|
|
152
|
+
dependency = dependency.to_sym
|
|
153
|
+
# Current names only: a pre-rename key would be resolved through its
|
|
154
|
+
# deprecation warning on every render.
|
|
155
|
+
unless dependency.to_s.end_with?('_serializer') && Spree::Api::Dependencies.class::INJECTION_POINTS.include?(dependency)
|
|
156
|
+
raise ArgumentError, "#{dependency} is not an API serializer"
|
|
157
|
+
end
|
|
158
|
+
if @fields[dependency].key?(field.name) || Spree.api.public_send(dependency)._attributes.key?(field.name)
|
|
159
|
+
raise DuplicateField, "#{dependency} already has a field named #{field.name}"
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
@fields[dependency][field.name] = field
|
|
163
|
+
@class_readers[dependency] ||= :"#{dependency}_class"
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
end
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
module Spree
|
|
2
|
+
module Api
|
|
3
|
+
# OAuth 2.1 authorization server for the Spree API.
|
|
4
|
+
#
|
|
5
|
+
# Spree is the authorization server here, not a client of one: a
|
|
6
|
+
# self-hosted store has no external identity provider to delegate to, and
|
|
7
|
+
# requiring one would exclude the installations this exists for. This is
|
|
8
|
+
# the opposite direction from SSO login, which makes Spree an OIDC client
|
|
9
|
+
# so staff can sign in through a provider; the two share a spec family and
|
|
10
|
+
# almost no code.
|
|
11
|
+
#
|
|
12
|
+
# The first consumer is the Admin MCP server, whose consumer clients
|
|
13
|
+
# (hosted connectors) accept no other credential. A marketplace app or a
|
|
14
|
+
# merchant-approved integration is the same flow with a different client,
|
|
15
|
+
# which is why this lives in `spree_api` rather than in any one gem that
|
|
16
|
+
# happens to need it.
|
|
17
|
+
module Oauth
|
|
18
|
+
# Protected resources, each registered by the surface that owns it as a
|
|
19
|
+
# path relative to this application's own origin.
|
|
20
|
+
#
|
|
21
|
+
# Spree::Api::Oauth.register_resource(:admin, '/api/v3/admin')
|
|
22
|
+
#
|
|
23
|
+
# A path rather than a full URL because the identifier must equal the
|
|
24
|
+
# URL the client was actually pointed at: the origin comes from the
|
|
25
|
+
# request where there is one, and from the application's configured
|
|
26
|
+
# URL options otherwise. A store's own `url` column is deliberately not
|
|
27
|
+
# consulted — it describes the storefront, which is frequently neither
|
|
28
|
+
# the host nor the scheme the Admin API answers on.
|
|
29
|
+
# Whether a redirect URI points at the machine the client runs on.
|
|
30
|
+
#
|
|
31
|
+
# Decided by parsing the host as an address, the same way Doorkeeper
|
|
32
|
+
# does when it ignores the port of a loopback redirect — so `localhost`
|
|
33
|
+
# is deliberately not loopback here either, and a registration must use
|
|
34
|
+
# the IP literal for an ephemeral port to match.
|
|
35
|
+
#
|
|
36
|
+
# @param uri [URI, String]
|
|
37
|
+
# @return [Boolean]
|
|
38
|
+
def self.loopback_uri?(uri)
|
|
39
|
+
host = uri.respond_to?(:host) ? uri.host : URI.parse(uri.to_s).host
|
|
40
|
+
return false if host.blank?
|
|
41
|
+
|
|
42
|
+
IPAddr.new(host).loopback?
|
|
43
|
+
rescue StandardError
|
|
44
|
+
false
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
mattr_accessor :resources, default: {}
|
|
48
|
+
|
|
49
|
+
class << self
|
|
50
|
+
# @param key [Symbol]
|
|
51
|
+
# @param path [String] absolute path this resource answers on
|
|
52
|
+
def register_resource(key, path)
|
|
53
|
+
resources[key.to_sym] = path
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# @param origin [String, nil] scheme and host the client used
|
|
57
|
+
# @return [Array<String>] every resource identifier this installation
|
|
58
|
+
# serves
|
|
59
|
+
def resource_identifiers(origin = nil)
|
|
60
|
+
base = origin.presence || application_origin
|
|
61
|
+
resources.values.map { |path| "#{base}#{path}" }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# @param key [Symbol]
|
|
65
|
+
# @param path [String] absolute path this resource answers on
|
|
66
|
+
def register_resource(key, path)
|
|
67
|
+
resources[key.to_sym] = path
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# @param origin [String, nil] scheme and host the client used
|
|
71
|
+
# @return [Array<String>] every resource identifier this installation
|
|
72
|
+
# serves
|
|
73
|
+
def resource_identifiers(origin = nil)
|
|
74
|
+
base = origin.presence || application_origin
|
|
75
|
+
resources.values.map { |path| "#{base}#{path}" }
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# @param key [Symbol]
|
|
79
|
+
# @param origin [String, nil] scheme and host the client used
|
|
80
|
+
# @return [String, nil]
|
|
81
|
+
def resource_identifier(key, origin = nil)
|
|
82
|
+
path = resources[key.to_sym]
|
|
83
|
+
return if path.nil?
|
|
84
|
+
|
|
85
|
+
"#{origin.presence || application_origin}#{path}"
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Where this application answers, from the URL options a deployment
|
|
89
|
+
# already has to set for mailer links to work.
|
|
90
|
+
#
|
|
91
|
+
# @return [String]
|
|
92
|
+
def application_origin
|
|
93
|
+
options = Rails.application.routes.default_url_options
|
|
94
|
+
host = options[:host]
|
|
95
|
+
return '' if host.blank?
|
|
96
|
+
|
|
97
|
+
origin = "#{options[:protocol].presence || 'https'}://#{host}"
|
|
98
|
+
options[:port].present? ? "#{origin}:#{options[:port]}" : origin
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# The grantable scope list, re-read after host and extension
|
|
102
|
+
# initializers have run.
|
|
103
|
+
#
|
|
104
|
+
# `optional_scopes` builds its value eagerly, and an extension
|
|
105
|
+
# registers its scopes from a config initializer — which runs after
|
|
106
|
+
# {.configure!}, because a host's own `doorkeeper.rb` has to be able
|
|
107
|
+
# to win. Without this second pass the vocabulary would be whatever
|
|
108
|
+
# core shipped, and an extension's keys could never be granted.
|
|
109
|
+
def refresh_scopes!
|
|
110
|
+
::Doorkeeper.config.instance_variable_set(
|
|
111
|
+
:@optional_scopes,
|
|
112
|
+
::Doorkeeper::OAuth::Scopes.from_array(staff_scope_keys)
|
|
113
|
+
)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# Scopes that govern credentials rather than commerce, and so are
|
|
117
|
+
# never offered to a delegated client.
|
|
118
|
+
#
|
|
119
|
+
# Granting an agent `write_oauth_applications` would let it connect another
|
|
120
|
+
# agent; `write_api_keys` would let it mint a key that outlives the
|
|
121
|
+
# grant it was given. Neither is an escalation a merchant could
|
|
122
|
+
# reasonably be asked to judge on a consent screen, and the tools
|
|
123
|
+
# withhold the models anyway — so the scopes should not be offered in
|
|
124
|
+
# the first place, rather than offered and quietly inert.
|
|
125
|
+
CREDENTIAL_SCOPES = %w[oauth_applications api_keys integrations webhooks staff].freeze
|
|
126
|
+
|
|
127
|
+
# @return [Array<String>]
|
|
128
|
+
def staff_scope_keys
|
|
129
|
+
Spree.permissions.grantable_keys(Spree::PermissionConfiguration::STAFF_AUDIENCE).
|
|
130
|
+
reject { |key| CREDENTIAL_SCOPES.any? { |scope| key.to_s.end_with?("_#{scope}") } }
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Whether every requested audience names a resource this application
|
|
134
|
+
# actually serves.
|
|
135
|
+
#
|
|
136
|
+
# Compared by path rather than by full URL: the origin a client used
|
|
137
|
+
# is whatever it was pointed at, and a deployment behind a proxy or
|
|
138
|
+
# reachable at several hostnames would otherwise refuse its own
|
|
139
|
+
# tokens.
|
|
140
|
+
#
|
|
141
|
+
# @param requested [Array<String>] the `resource` values asked for
|
|
142
|
+
# @return [Boolean]
|
|
143
|
+
def indicators_valid?(requested, _client = nil)
|
|
144
|
+
return false if requested.blank?
|
|
145
|
+
|
|
146
|
+
paths = resources.values
|
|
147
|
+
requested.all? do |indicator|
|
|
148
|
+
path = URI.parse(indicator.to_s).path
|
|
149
|
+
paths.include?(path)
|
|
150
|
+
rescue URI::InvalidURIError
|
|
151
|
+
false
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def configure!
|
|
156
|
+
::Doorkeeper.configure do
|
|
157
|
+
orm :active_record
|
|
158
|
+
|
|
159
|
+
application_class 'Spree::OauthApplication'
|
|
160
|
+
access_grant_class 'Spree::OauthAccessGrant'
|
|
161
|
+
access_token_class 'Spree::OauthAccessToken'
|
|
162
|
+
|
|
163
|
+
# Consent is Spree's own, at
|
|
164
|
+
# `Spree::Api::V3::Admin::Oauth::AuthorizationsController`, and
|
|
165
|
+
# Doorkeeper's authorization controllers are not mounted — so
|
|
166
|
+
# this is only reached if a host app mounts them. Failing loudly
|
|
167
|
+
# beats a NoMethodError inside a before_action, which
|
|
168
|
+
# `handle_auth_errors :raise` would turn into a 500 on an
|
|
169
|
+
# authorization endpoint.
|
|
170
|
+
resource_owner_authenticator do
|
|
171
|
+
raise "Doorkeeper's own consent screens are not mounted in Spree. " \
|
|
172
|
+
'Consent is served by the Admin API at /api/v3/admin/oauth/authorize.'
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# An admin user today, a customer when a storefront client needs
|
|
176
|
+
# one — recorded polymorphically so either can own a grant.
|
|
177
|
+
use_polymorphic_resource_owner
|
|
178
|
+
|
|
179
|
+
# Authorization code only. A password grant asks a client to
|
|
180
|
+
# handle someone's password, and client credentials identify no
|
|
181
|
+
# person, so neither belongs on a surface whose authority is a
|
|
182
|
+
# user's own permissions.
|
|
183
|
+
grant_flows %w[authorization_code]
|
|
184
|
+
|
|
185
|
+
# Mandatory, not merely supported: these are public clients, and
|
|
186
|
+
# without PKCE an intercepted code is enough to obtain a token.
|
|
187
|
+
force_pkce
|
|
188
|
+
# S256 only — `plain` is in the gem's defaults and protects
|
|
189
|
+
# nothing against an intercepted code.
|
|
190
|
+
pkce_code_challenge_methods %w[S256]
|
|
191
|
+
|
|
192
|
+
# A redirect URI is where authorization codes are delivered, so a
|
|
193
|
+
# plain-http one hands them to anyone on the path — except a
|
|
194
|
+
# loopback one, which never leaves the machine. A terminal client
|
|
195
|
+
# listens on an ephemeral local port and has no certificate for
|
|
196
|
+
# it, so RFC 8252 §7.3 makes that the recommended shape, and
|
|
197
|
+
# refusing it would lock out Claude Code and Codex entirely.
|
|
198
|
+
#
|
|
199
|
+
# Passed as a callable so the exemption is per URI rather than
|
|
200
|
+
# per environment: everything else still has to be https.
|
|
201
|
+
force_ssl_in_redirect_uri do |uri|
|
|
202
|
+
next false if Rails.env.development?
|
|
203
|
+
|
|
204
|
+
!Spree::Api::Oauth.loopback_uri?(uri)
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# RFC 8707. Registering the validator is what binds an audience
|
|
208
|
+
# into the token; left nil, the `resource` parameter is ignored
|
|
209
|
+
# and a token minted for another service would be accepted.
|
|
210
|
+
resource_indicator_validator(lambda do |requested, _client|
|
|
211
|
+
Spree::Api::Oauth.indicators_valid?(requested)
|
|
212
|
+
end)
|
|
213
|
+
|
|
214
|
+
# Spree's own permission catalog is the scope vocabulary, so a
|
|
215
|
+
# grant carries the same keys an API key would and the tool
|
|
216
|
+
# registry needs no translation. Seeded here and re-read in
|
|
217
|
+
# `refresh_scopes!` once extensions have registered theirs.
|
|
218
|
+
optional_scopes(*Spree::Api::Oauth.staff_scope_keys)
|
|
219
|
+
|
|
220
|
+
use_refresh_token
|
|
221
|
+
# One live token per authorization, so re-consenting does not
|
|
222
|
+
# leave the previous one usable.
|
|
223
|
+
revoke_previous_authorization_code_token
|
|
224
|
+
# No `fallback: :plain`: every token this server writes is
|
|
225
|
+
# hashed, so a plaintext lookup could never match and keeping
|
|
226
|
+
# the path alive only invites a future migration to rely on it.
|
|
227
|
+
hash_token_secrets
|
|
228
|
+
access_token_expires_in 2.hours
|
|
229
|
+
# Refresh rotation with replay detection comes from the
|
|
230
|
+
# `previous_refresh_token` column the migration adds — Doorkeeper
|
|
231
|
+
# enables it by the column's presence, not by a setting.
|
|
232
|
+
|
|
233
|
+
api_only
|
|
234
|
+
handle_auth_errors :raise
|
|
235
|
+
# Never from a query parameter: the spec forbids it, and a URL
|
|
236
|
+
# reaches logs and proxies.
|
|
237
|
+
access_token_methods :from_bearer_authorization
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
end
|
data/lib/spree/api.rb
CHANGED
|
@@ -3,6 +3,7 @@ require 'pagy'
|
|
|
3
3
|
require 'typelizer'
|
|
4
4
|
require 'typelizer/proc_resource_resolution'
|
|
5
5
|
require 'typelizer/open_enums'
|
|
6
|
+
require 'spree/api/extension_fields'
|
|
6
7
|
|
|
7
8
|
module Spree
|
|
8
9
|
module Api
|
|
@@ -40,6 +41,13 @@ module Spree
|
|
|
40
41
|
api_dependency?(base_name) || super
|
|
41
42
|
end
|
|
42
43
|
|
|
44
|
+
# Fields extensions add to core serializers' responses.
|
|
45
|
+
#
|
|
46
|
+
# @return [Spree::Api::ExtensionFields]
|
|
47
|
+
def extension_fields
|
|
48
|
+
@extension_fields ||= Spree::Api::ExtensionFields.new
|
|
49
|
+
end
|
|
50
|
+
|
|
43
51
|
# Direct access to raw dependencies object for backwards compatibility
|
|
44
52
|
def dependencies
|
|
45
53
|
Spree::Api::Dependencies
|
data/lib/typelizer/open_enums.rb
CHANGED
|
@@ -11,16 +11,30 @@ module Typelizer
|
|
|
11
11
|
# ISO lists are the exception: a named type several serializers and the
|
|
12
12
|
# request types share, but closed, since nothing outside the standard can
|
|
13
13
|
# add a country or a currency, and markets only accept the locales
|
|
14
|
-
# `Spree::Locales::ALL` lists.
|
|
14
|
+
# `Spree::Locales::ALL` lists. The SDKs define them once, as values and
|
|
15
|
+
# types, in `@spree/sdk-core` (generated from the API reference by
|
|
16
|
+
# `pnpm generate:iso-codes`), so each SDK's `Enums.ts` re-exports that
|
|
17
|
+
# definition instead of carrying a copy of the list.
|
|
15
18
|
module OpenEnums
|
|
16
19
|
CLOSED_TYPE_NAMES = %w[CountryCode CurrencyCode LocaleCode].freeze
|
|
17
20
|
|
|
18
21
|
module Property
|
|
19
22
|
def enum_definition(...)
|
|
20
23
|
definition = super
|
|
21
|
-
return definition
|
|
24
|
+
return definition unless definition
|
|
25
|
+
return "type { #{enum_type_name} } from '@spree/sdk-core'" if CLOSED_TYPE_NAMES.include?(enum_type_name)
|
|
22
26
|
|
|
23
|
-
|
|
27
|
+
"#{definition} | (string & {})"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Typelizer skips rewriting a file whose digest is unchanged, and the
|
|
31
|
+
# digest covers a property's values but not the definition above. Folding
|
|
32
|
+
# the re-export in makes a checkout that still carries the old inline
|
|
33
|
+
# list regenerate.
|
|
34
|
+
def fingerprint
|
|
35
|
+
return super unless CLOSED_TYPE_NAMES.include?(enum_type_name)
|
|
36
|
+
|
|
37
|
+
[super, enum_definition].inspect
|
|
24
38
|
end
|
|
25
39
|
end
|
|
26
40
|
|