Skip to content

Commit

Permalink
Use JWT helpers, improve memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpgross committed Jun 17, 2024
1 parent ef54cdc commit 0ee1edd
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/shopify_app/controller_concerns/with_shopify_id_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,31 @@ module WithShopifyIdToken
extend ActiveSupport::Concern

def shopify_id_token
@shopify_id_token ||= id_token_from_authorization_header || id_token_from_url_param
return @shopify_id_token if defined?(@shopify_id_token)

@shopify_id_token = id_token_from_authorization_header || id_token_from_url_param
end

def jwt_payload
@jwt_payload ||= shopify_id_token.present? ? ShopifyAPI::Auth::JwtPayload.new(shopify_id_token) : nil
return @jwt_payload if defined?(@jwt_payload)

@jwt_payload = shopify_id_token.present? ? ShopifyAPI::Auth::JwtPayload.new(shopify_id_token) : nil
end

def jwt_shopify_domain
@jwt_shopify_domain ||= jwt_payload.present? ? ShopifyApp::Utils.sanitize_shop_domain(jwt_payload.dest) : nil
return @jwt_shopify_domain if defined?(@jwt_shopify_domain)

@jwt_shopify_domain = if jwt_payload.present?
ShopifyApp::Utils.sanitize_shop_domain(jwt_payload.shopify_domain)
end
end

def jwt_shopify_user_id
jwt_payload&.sub&.to_i
jwt_payload&.shopify_user_id
end

def jwt_expire_at
expire_at = jwt_payload&.exp&.to_i
expire_at = jwt_payload&.expire_at
return unless expire_at

expire_at - 5.seconds # 5s gap to start fetching new token in advance
Expand Down

0 comments on commit 0ee1edd

Please sign in to comment.