Skip to content

Commit

Permalink
Add helper method alias to JwtPayload similar to ShopifyApp::JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
zzooeeyy committed Apr 22, 2024
1 parent eaaa96d commit 6c9c2f8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api
- [#1314](https://github.com/Shopify/shopify-api-ruby/pull/1314)
- Add new session util method `SessionUtils::session_id_from_shopify_id_token`
- `SessionUtils::current_session_id` now accepts shopify Id token in the format of `Bearer this_token` or just `this_token`
- [#1315](https://github.com/Shopify/shopify-api-ruby/pull/1315) Add helper/alias methods to `ShopifyAPI::Auth::JwtPayload`:
- `shopify_domain` alias for `shop` - returns the sanitized shop domain
- `shopify_user_id` - returns the user Id (`sub`) as an Integer value
- `expires_at` alias for `exp` - returns the expiration time

## 14.2.0
- [#1309](https://github.com/Shopify/shopify-api-ruby/pull/1309) Add `Session#copy_attributes_from` method
Expand Down
8 changes: 8 additions & 0 deletions lib/shopify_api/auth/jwt_payload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class JwtPayload
sig { returns(Integer) }
attr_reader :exp, :nbf, :iat

alias_method :expire_at, :exp

sig { params(token: String).void }
def initialize(token)
payload_hash = begin
Expand Down Expand Up @@ -43,6 +45,12 @@ def initialize(token)
def shop
@dest.gsub("https://", "")
end
alias_method :shopify_domain, :shop

sig { returns(Integer) }
def shopify_user_id
@sub.to_i
end

# TODO: Remove before releasing v11
sig { params(shop: String).returns(T::Boolean) }
Expand Down
9 changes: 9 additions & 0 deletions test/auth/jwt_payload_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def test_decode_jwt_payload_succeeds_with_valid_token
jti: decoded.jti,
sid: decoded.sid,
})

# Helper methods
assert_equal(decoded.expire_at, @jwt_payload[:exp])
assert_equal("test-shop.myshopify.io", decoded.shopify_domain)
assert_equal("test-shop.myshopify.io", decoded.shop)
assert_equal(1, decoded.shopify_user_id)
end

def test_decode_jwt_payload_succeeds_with_spin_domain
Expand All @@ -56,6 +62,9 @@ def test_decode_jwt_payload_succeeds_with_spin_domain
jti: decoded.jti,
sid: decoded.sid,
})

assert_equal("test-shop.other.spin.dev", decoded.shopify_domain)
assert_equal("test-shop.other.spin.dev", decoded.shop)
end

def test_decode_jwt_payload_fails_with_wrong_key
Expand Down

0 comments on commit 6c9c2f8

Please sign in to comment.