Class: Spree::Api::V3::Admin::ApiKeysController

Inherits:
ResourceController show all
Defined in:
app/controllers/spree/api/v3/admin/api_keys_controller.rb

Constant Summary

Constants inherited from BaseController

BaseController::RATE_LIMIT_RESPONSE

Constants included from Idempotent

Idempotent::IDEMPOTENCY_HEADER, Idempotent::IDEMPOTENCY_TTL, Idempotent::MAX_KEY_LENGTH, Idempotent::MUTATING_METHODS

Constants included from ErrorHandler

ErrorHandler::ERROR_CODES

Constants included from JwtAuthentication

JwtAuthentication::JWT_AUDIENCE_ADMIN, JwtAuthentication::JWT_AUDIENCE_STORE, JwtAuthentication::JWT_ISSUER, JwtAuthentication::USER_TYPE_ADMIN, JwtAuthentication::USER_TYPE_CUSTOMER

Instance Method Summary collapse

Methods inherited from ResourceController

#destroy, #index, #show, #update

Methods included from Spree::Api::V3::ApiKeyAuthentication

#authenticate_api_key!, #authenticate_secret_key!

Methods included from JwtAuthentication

#authenticate_user, #require_authentication!

Instance Method Details

#createObject

POST /api/v3/admin/api_keys Prevents scope amplification: a key minted via a secret API key can only carry scopes that key already holds. A JWT admin is governed by CanCanCan (not scopes) and may grant any valid scope — so when a JWT user authenticated the request, ‘current_ability` ignores the API key (see AdminAuthentication#current_ability) and we skip the scope cap too, even if an `X-Spree-Api-Key` header was also sent.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/spree/api/v3/admin/api_keys_controller.rb', line 18

def create
  if scope_limited_principal? && (excess = requested_scopes.reject { |s| current_api_key.has_scope?(s) }).any?
    return render_error(
      code: ERROR_CODES[:access_denied],
      message: "Cannot grant scopes beyond your own: #{excess.join(', ')}",
      status: :forbidden,
      details: { excess_scopes: excess }
    )
  end

  super
end

#revokeObject

PATCH /api/v3/admin/api_keys/:id/revoke Marks the key revoked rather than deleting it — the row stays so audit logs and ‘created_by`/`revoked_by` remain queryable. Hard deletion is available via `destroy` for cleanup.



35
36
37
38
39
40
41
# File 'app/controllers/spree/api/v3/admin/api_keys_controller.rb', line 35

def revoke
  @resource = find_resource
  authorize!(:update, @resource)

  @resource.revoke!(try_spree_current_user)
  render json: serialize_resource(@resource)
end