Skip to content

Commit 6c9c2f8

Browse files
committed
Add helper method alias to JwtPayload similar to ShopifyApp::JWT
1 parent eaaa96d commit 6c9c2f8

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api
77
- [#1314](https://github.com/Shopify/shopify-api-ruby/pull/1314)
88
- Add new session util method `SessionUtils::session_id_from_shopify_id_token`
99
- `SessionUtils::current_session_id` now accepts shopify Id token in the format of `Bearer this_token` or just `this_token`
10+
- [#1315](https://github.com/Shopify/shopify-api-ruby/pull/1315) Add helper/alias methods to `ShopifyAPI::Auth::JwtPayload`:
11+
- `shopify_domain` alias for `shop` - returns the sanitized shop domain
12+
- `shopify_user_id` - returns the user Id (`sub`) as an Integer value
13+
- `expires_at` alias for `exp` - returns the expiration time
1014

1115
## 14.2.0
1216
- [#1309](https://github.com/Shopify/shopify-api-ruby/pull/1309) Add `Session#copy_attributes_from` method

lib/shopify_api/auth/jwt_payload.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class JwtPayload
1515
sig { returns(Integer) }
1616
attr_reader :exp, :nbf, :iat
1717

18+
alias_method :expire_at, :exp
19+
1820
sig { params(token: String).void }
1921
def initialize(token)
2022
payload_hash = begin
@@ -43,6 +45,12 @@ def initialize(token)
4345
def shop
4446
@dest.gsub("https://", "")
4547
end
48+
alias_method :shopify_domain, :shop
49+
50+
sig { returns(Integer) }
51+
def shopify_user_id
52+
@sub.to_i
53+
end
4654

4755
# TODO: Remove before releasing v11
4856
sig { params(shop: String).returns(T::Boolean) }

test/auth/jwt_payload_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ def test_decode_jwt_payload_succeeds_with_valid_token
3636
jti: decoded.jti,
3737
sid: decoded.sid,
3838
})
39+
40+
# Helper methods
41+
assert_equal(decoded.expire_at, @jwt_payload[:exp])
42+
assert_equal("test-shop.myshopify.io", decoded.shopify_domain)
43+
assert_equal("test-shop.myshopify.io", decoded.shop)
44+
assert_equal(1, decoded.shopify_user_id)
3945
end
4046

4147
def test_decode_jwt_payload_succeeds_with_spin_domain
@@ -56,6 +62,9 @@ def test_decode_jwt_payload_succeeds_with_spin_domain
5662
jti: decoded.jti,
5763
sid: decoded.sid,
5864
})
65+
66+
assert_equal("test-shop.other.spin.dev", decoded.shopify_domain)
67+
assert_equal("test-shop.other.spin.dev", decoded.shop)
5968
end
6069

6170
def test_decode_jwt_payload_fails_with_wrong_key

0 commit comments

Comments
 (0)