Module: ShopifyApp::TokenExchange

Extended by:
ActiveSupport::Concern
Includes:
AdminAPI::WithTokenRefetch, EmbeddedApp, SanitizedParams
Defined in:
lib/shopify_app/controller_concerns/token_exchange.rb

Constant Summary collapse

INVALID_SHOPIFY_ID_TOKEN_ERRORS =
[
  ShopifyAPI::Errors::MissingJwtTokenError,
  ShopifyAPI::Errors::InvalidJwtTokenError,
].freeze

Instance Method Summary collapse

Methods included from AdminAPI::WithTokenRefetch

#with_token_refetch

Instance Method Details

#activate_shopify_session(&block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/shopify_app/controller_concerns/token_exchange.rb', line 19

def activate_shopify_session(&block)
  retrieve_session_from_token_exchange if current_shopify_session.blank? || should_exchange_expired_token?

  ShopifyApp::Logger.debug("Activating Shopify session")
  ShopifyAPI::Context.activate_session(current_shopify_session)
  with_token_refetch(current_shopify_session, shopify_id_token, &block)
rescue *INVALID_SHOPIFY_ID_TOKEN_ERRORS => e
  respond_to_invalid_shopify_id_token(e)
ensure
  ShopifyApp::Logger.debug("Deactivating session")
  ShopifyAPI::Context.deactivate_session
end

#current_shopify_domainObject



49
50
51
52
53
# File 'lib/shopify_app/controller_concerns/token_exchange.rb', line 49

def current_shopify_domain
  sanitized_shop_name || current_shopify_session&.shop
rescue *INVALID_SHOPIFY_ID_TOKEN_ERRORS => e
  respond_to_invalid_shopify_id_token(e)
end

#current_shopify_sessionObject



36
37
38
39
40
# File 'lib/shopify_app/controller_concerns/token_exchange.rb', line 36

def current_shopify_session
  return unless current_shopify_session_id

  @current_shopify_session ||= ShopifyApp::SessionRepository.load_session(current_shopify_session_id)
end

#current_shopify_session_idObject



42
43
44
45
46
47
# File 'lib/shopify_app/controller_concerns/token_exchange.rb', line 42

def current_shopify_session_id
  @current_shopify_session_id ||= ShopifyAPI::Utils::SessionUtils.session_id_from_shopify_id_token(
    id_token: shopify_id_token,
    online: online_token_configured?,
  )
end

#should_exchange_expired_token?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/shopify_app/controller_concerns/token_exchange.rb', line 32

def should_exchange_expired_token?
  ShopifyApp.configuration.check_session_expiry_date && current_shopify_session.expired?
end