app_manager 2.7.1 → 2.8.0

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.
@@ -0,0 +1,161 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ module AppManager
5
+ # Raised when no Shopify access token is available for a shop at all.
6
+ class MissingTokenError < Error; end
7
+
8
+ # Default resolver: reads the token column of the configured shop table, which
9
+ # is what the gem has always done. Right for non-expiring offline tokens. It
10
+ # cannot mint a new token, so an app on expiring tokens must configure another.
11
+ class ColumnTokenResolver
12
+ def token(shop_domain)
13
+ value = read_token(find_shop(shop_domain))
14
+ value.to_s.empty? ? nil : value.to_s
15
+ end
16
+
17
+ def refresh(_shop_domain)
18
+ nil
19
+ end
20
+
21
+ private
22
+
23
+ # Read through the model's own accessor (like fresh_shopify_token), so an
24
+ # app that encrypts the column, or wraps it in a custom getter, gets the
25
+ # decrypted value. Only a model without the accessor is read by attribute.
26
+ def read_token(shop)
27
+ return nil if shop.nil?
28
+
29
+ field = AppManager.configuration.field_names['shopify_token'].to_s
30
+ shop.respond_to?(field) ? shop.public_send(field) : shop[field]
31
+ end
32
+
33
+ def find_shop(shop_domain)
34
+ config = AppManager.configuration
35
+ table = config.shopify_table_name
36
+ return nil unless ActiveRecord::Base.connection.tables.include?(table)
37
+
38
+ table.classify.constantize.find_by(config.shopify_domain_field => shop_domain)
39
+ end
40
+ end
41
+
42
+ # For apps whose shop model owns the token and renews it in an instance
43
+ # method, `fresh_shopify_token` (e.g. Shop#fresh_shopify_token). Set
44
+ # config.shopify_token_resolver = AppManager::ModelTokenResolver
45
+ #
46
+ # token -> shop.fresh_shopify_token
47
+ # refresh -> the same call after Shopify rejected the token. If the method
48
+ # accepts a `force:` keyword it is passed as true, so the app can
49
+ # renew regardless of its own expiry bookkeeping; the shop is also
50
+ # reloaded first. The gem replays only if the result differs from
51
+ # the rejected token.
52
+ #
53
+ # Falls back to the token column when the model does not define the method or
54
+ # cannot be found, so the config can be set before the model is migrated.
55
+ class ModelTokenResolver < ColumnTokenResolver
56
+ METHOD = :fresh_shopify_token
57
+
58
+ def token(shop_domain)
59
+ shop = find_shop(shop_domain)
60
+ return super unless shop.respond_to?(METHOD)
61
+
62
+ present(shop.public_send(METHOD)) || super
63
+ rescue StandardError => e
64
+ warn_failure('token', shop_domain, e)
65
+ super
66
+ end
67
+
68
+ def refresh(shop_domain)
69
+ shop = find_shop(shop_domain)
70
+ return nil unless shop.respond_to?(METHOD)
71
+
72
+ present(call_forced(shop))
73
+ rescue StandardError => e
74
+ warn_failure('refresh', shop_domain, e)
75
+ nil
76
+ end
77
+
78
+ private
79
+
80
+ def call_forced(shop)
81
+ accepts_force = shop.method(METHOD).parameters.any? { |type, name| name == :force && %i[key keyreq].include?(type) }
82
+ accepts_force ? shop.public_send(METHOD, force: true) : shop.public_send(METHOD)
83
+ end
84
+
85
+ def present(value)
86
+ value.to_s.empty? ? nil : value.to_s
87
+ end
88
+
89
+ def warn_failure(what, shop_domain, error)
90
+ Rails.logger.warn("App Manager: #{METHOD} #{what} failed for #{shop_domain}: #{error.message}")
91
+ end
92
+ end
93
+
94
+ # The single path for every gem call to a shop's Admin API. It asks the app's
95
+ # resolver for a token and replays once with a replacement if Shopify rejects
96
+ # it. With the default resolver there is no replacement, so nothing changes.
97
+ #
98
+ # A resolver is any object responding to:
99
+ # token(shop_domain) -> a token valid right now, or nil
100
+ # refresh(shop_domain) -> a replacement after Shopify rejected token(), or nil
101
+ class ShopifyApi
102
+ NON_EXPIRING_REJECTED = 'Non-expiring access tokens are no longer accepted'.freeze
103
+
104
+ def self.resolver
105
+ configured = AppManager.configuration.shopify_token_resolver
106
+ configured = configured.constantize if configured.is_a?(String)
107
+ configured = configured.new if configured.is_a?(Class)
108
+ resolver = configured || ColumnTokenResolver.new
109
+ unless resolver.respond_to?(:token) && resolver.respond_to?(:refresh)
110
+ raise ArgumentError, 'AppManager shopify_token_resolver must respond to #token(shop_domain) and #refresh(shop_domain)'
111
+ end
112
+ resolver
113
+ end
114
+
115
+ # Sends a request to /admin/api/{version}/{path}. Returns a Net::HTTPResponse.
116
+ def self.rest(shop_domain, method, path, body: nil, headers: {})
117
+ url = URI("https://#{shop_domain}/admin/api/#{AppManager.configuration.shopify_api_version}/#{path.to_s.sub(%r{\A/}, '')}")
118
+ with_token(shop_domain) { |token| request(url, method, token, body, headers) }
119
+ end
120
+
121
+ # Yields a token, expecting a response back. Replays exactly once, and only
122
+ # with a token that differs from the rejected one: if a fresh token is
123
+ # rejected too, the token is not what is wrong.
124
+ def self.with_token(shop_domain, fallback_token = nil)
125
+ resolver = self.resolver
126
+ token = resolver.token(shop_domain)
127
+ token = fallback_token if token.to_s.empty?
128
+ raise MissingTokenError, "No Shopify access token available for #{shop_domain}" if token.to_s.empty?
129
+
130
+ response = yield(token)
131
+ return response unless token_rejected?(response)
132
+
133
+ replacement = resolver.refresh(shop_domain)
134
+ return response if replacement.to_s.empty? || replacement == token
135
+
136
+ yield(replacement)
137
+ end
138
+
139
+ # A 403 is only a rejection when Shopify says the token type is the problem:
140
+ # a missing scope is also a 403 and a new token would not fix it.
141
+ def self.token_rejected?(response)
142
+ status = response.respond_to?(:code) ? response.code.to_i : 0
143
+ return true if status == 401
144
+
145
+ status == 403 && response.body.to_s.include?(NON_EXPIRING_REJECTED)
146
+ end
147
+
148
+ def self.request(url, method, token, body, headers)
149
+ http = Net::HTTP.new(url.host, url.port)
150
+ http.use_ssl = true
151
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
152
+ req = Net::HTTP.const_get(method.to_s.capitalize).new(url)
153
+ req['X-Shopify-Access-Token'] = token
154
+ req['Accept'] = 'application/json'
155
+ headers.each { |key, value| req[key] = value }
156
+ req.body = body if body
157
+ http.request(req)
158
+ end
159
+ private_class_method :request
160
+ end
161
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AppManager
4
- VERSION = "2.7.1"
4
+ VERSION = "2.8.0"
5
5
  end
data/lib/app_manager.rb CHANGED
@@ -8,10 +8,13 @@ require "app_manager/engine"
8
8
  require "app_manager/client"
9
9
  require "app_manager/actions"
10
10
  require "app_manager/api_cache_handler"
11
+ require "app_manager/exceptions"
12
+ require "app_manager/shopify_api"
11
13
  require "app_manager/graphql_helper"
12
14
  require "app_manager/response_cache"
13
15
  require "app_manager/config"
14
16
  require "app_manager/protection"
17
+ require "app_manager/session_token"
15
18
  require "app_manager/model"
16
19
  require "app_manager/fail_safe"
17
20
  require 'app_manager/railtie' if defined?(Rails)
@@ -2,7 +2,8 @@ AppManager.configure do |config|
2
2
  config.enable_caching = false # Optional, True to enable app-manager api response caching, default is enabled from gem
3
3
  config.expires_in = 1.days # Optional, Example: 30.seconds, 5.minutes or 2.days Default caching is for 1.days from gem
4
4
  config.app_url = '' # App URL like https://volumediscount.hulkapps.dev/ or #https://5044-2409-4052-209a-69da-9d7-925a-9418-a9c3.ngrok.io
5
- config.shopify_api_key = '' # Shopify api key of app
5
+ config.shopify_api_key = '' # Required, Shopify api key of app
6
+ config.shopify_api_secret = ENV['SHOPIFY_API_SECRET'] # Required, Shopify api secret of app. Verifies the billing page's session tokens; use your own env name if it differs
6
7
  config.shopify_api_version = '' # Must be 2022-04 or latest
7
8
  config.shopify_table_name = 'shops' # Table name which is generated by shopify mostly it is 'shops'
8
9
  config.shopify_domain_field = 'shopify_domain' #shopify domain field
@@ -21,6 +22,7 @@ AppManager.configure do |config|
21
22
  'partner_development' => 'partner_development',
22
23
  'total_trial_days' => '' #optional, put a trial days field from your shops table otherwise leave it blank
23
24
  }
25
+ # config.shopify_token_resolver = AppManager::ModelTokenResolver # Optional, for Shopify expiring offline tokens: uses Shop#fresh_shopify_token (see README). Unset reads the shopify_token column
24
26
  config.plan_features = [
25
27
  {
26
28
  "uuid" => "b48a3a6c-c1fb-11ec-9d64-0242ac120002",
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: app_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.1
4
+ version: 2.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rahul Tiwari @ Hulkapps
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-07-24 00:00:00.000000000 Z
11
+ date: 2026-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -72,9 +72,13 @@ executables: []
72
72
  extensions: []
73
73
  extra_rdoc_files: []
74
74
  files:
75
+ - ".cursor/rules/commit-and-pr.mdc"
76
+ - ".github/copilot-instructions.md"
75
77
  - ".github/workflows/ruby.yml"
76
78
  - ".gitignore"
77
79
  - ".rubocop.yml"
80
+ - CHANGELOG.md
81
+ - CLAUDE.md
78
82
  - CODE_OF_CONDUCT.md
79
83
  - Gemfile
80
84
  - Gemfile.lock
@@ -104,6 +108,7 @@ files:
104
108
  - bin/console
105
109
  - bin/setup
106
110
  - config/routes.rb
111
+ - docs/commit-and-pr-standards.md
107
112
  - lib/app_manager.rb
108
113
  - lib/app_manager/actions.rb
109
114
  - lib/app_manager/api_cache_handler.rb
@@ -121,7 +126,9 @@ files:
121
126
  - lib/app_manager/protection.rb
122
127
  - lib/app_manager/railtie.rb
123
128
  - lib/app_manager/response_cache.rb
129
+ - lib/app_manager/session_token.rb
124
130
  - lib/app_manager/set_local_storage.rb
131
+ - lib/app_manager/shopify_api.rb
125
132
  - lib/app_manager/tasks/sync/local_app_manager.rake
126
133
  - lib/app_manager/version.rb
127
134
  - lib/generators/app_manager/install/install_generator.rb