Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper method alias to JwtPayload similar to ShopifyApp::JWT #1315

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading