Class: Spree::ApiKey

Inherits:
Object
  • Object
show all
Includes:
Actor, SingleStoreResource
Defined in:
app/models/spree/api_key.rb

Constant Summary collapse

KEY_TYPES =
%w[publishable secret].freeze
PREFIXES =
{ 'publishable' => 'pk_', 'secret' => 'sk_' }.freeze
TOKEN_LENGTH =
24
ALIAS_SCOPES =

Convenience aliases expanded at check time (never stored expanded).

%w[read_all write_all].freeze

Constants included from Actor

Spree::Actor::BUILT_IN_KINDS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Actor

#actor_kind, #actor_label

Instance Attribute Details

#skip_admin_notification ⇒ Object

Set when a key is minted as part of setting up a store (the CLI's own read-only key), which is not news to anyone. Never persisted.



85
86
87
# File 'app/models/spree/api_key.rb', line 85

def skip_admin_notification
  @skip_admin_notification
end

Class Method Details

.compute_token_digest(plaintext) ⇒ String

Computes the HMAC-SHA256 hex digest for a given plaintext token.

Parameters:

  • plaintext (String) —

    the raw token value

Returns:

  • (String) —

    the hex-encoded HMAC-SHA256 digest



108
109
110
# File 'app/models/spree/api_key.rb', line 108

def self.compute_token_digest(plaintext)
  OpenSSL::HMAC.hexdigest('SHA256', hmac_secret, plaintext)
end

.find_by_secret_token(plaintext) ⇒ Spree::ApiKey?

Finds an active secret API key by computing the HMAC-SHA256 digest of the provided plaintext token and looking up by token_digest.

Parameters:

  • plaintext (String) —

    the raw secret key (e.g. "sk_abc123...")

Returns:

  • (Spree::ApiKey, nil) —

    the matching active secret key, or nil



97
98
99
100
101
102
# File 'app/models/spree/api_key.rb', line 97

def self.find_by_secret_token(plaintext)
  return nil if plaintext.blank?

  digest = compute_token_digest(plaintext)
  active.secret.find_by(token_digest: digest)
end

.hmac_secret ⇒ String

Returns the HMAC secret used for token hashing.

Returns:

  • (String) —

    the application's secret key base



115
116
117
# File 'app/models/spree/api_key.rb', line 115

def self.hmac_secret
  Rails.application.secret_key_base
end

.known_scopes ⇒ Array<String>

Admin API authorization scopes — the permission catalog keys a store's own back office may hold, plus the aliases. Derived from the catalog so an extension registering a resource (Spree.permissions.register_scope) makes its keys mintable on secret keys with no further wiring. See docs/plans/6.0-admin-rbac.md.

Staff-grantable only: a secret key is issued against a store, so keys that exist for another audience (a seller's own profile or books) are not mintable on one.

Returns:

  • (Array<String>)


26
27
28
# File 'app/models/spree/api_key.rb', line 26

def self.known_scopes
  Spree.permissions.grantable_keys(Spree::PermissionConfiguration::STAFF_AUDIENCE) + ALIAS_SCOPES
end

Instance Method Details

#active? ⇒ Boolean

Returns whether this key has not been revoked.

Returns:

  • (Boolean) —

    whether this key has not been revoked



130
131
132
# File 'app/models/spree/api_key.rb', line 130

def active?
  revoked_at.nil?
end

#has_scope?(scope) ⇒ Boolean

Whether this key carries the given scope. write_* implies the matching read_*; read_all / write_all aliases expand to every read / read+write scope respectively.

Parameters:

  • scope (String)

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
157
# File 'app/models/spree/api_key.rb', line 149

def has_scope?(scope)
  scope = scope.to_s
  return true if scopes.include?(scope)
  return true if scope.start_with?('read_') && scopes.include?("write_#{scope.delete_prefix('read_')}")
  return true if scopes.include?('write_all')
  return true if scope.start_with?('read_') && scopes.include?('read_all')

  false
end

#plaintext_token ⇒ String?

Returns the raw token value. For publishable keys this is the persisted token column. For secret keys it is only available in memory immediately after creation (not persisted).

Returns:

  • (String, nil)


43
44
45
# File 'app/models/spree/api_key.rb', line 43

def plaintext_token
  publishable? ? token : @plaintext_token
end

#publishable? ⇒ Boolean

Returns whether this is a publishable (Store API) key.

Returns:

  • (Boolean) —

    whether this is a publishable (Store API) key



120
121
122
# File 'app/models/spree/api_key.rb', line 120

def publishable?
  key_type == 'publishable'
end

#revoke!(actor = nil) ⇒ Boolean

Revokes this API key by setting revoked_at to the current time.

Parameters:

  • actor (Object, nil) (defaults to: nil) —

    who performed the revocation — an admin user or another API key (see Spree.actor_classes)

Returns:

  • (Boolean) —

    true if the update succeeded



139
140
141
# File 'app/models/spree/api_key.rb', line 139

def revoke!(actor = nil)
  update!(revoked_at: Time.current, revoked_by: actor)
end

#scopes=(value) ⇒ Object



34
35
36
# File 'app/models/spree/api_key.rb', line 34

def scopes=(value)
  super(Array(value).map(&:to_s).reject(&:blank?))
end

#secret? ⇒ Boolean

Returns whether this is a secret (Admin API) key.

Returns:

  • (Boolean) —

    whether this is a secret (Admin API) key



125
126
127
# File 'app/models/spree/api_key.rb', line 125

def secret?
  key_type == 'secret'
end