Skip to content

Commit 4bc8538

Browse files
committed
Make sub and sid jwt claims optional
The generated session token on the checkout ui extensions doesn't contain: - The `sid` claim - The `sub` claim if the customer is not logged in Making them mandatory causes an `Expected type String, got type NilClass` error
1 parent b2836db commit 4bc8538

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api
44
## Unreleased
55

66
- [#1362](https://github.com/Shopify/shopify-api-ruby/pull/1362) Add support for client credentials grant
7+
- [#1369](https://github.com/Shopify/shopify-api-ruby/pull/1369) Make `sub` and `sid` jwt claims optional (Checkout ui extension support)
78

89
## 14.8.0
910

lib/shopify_api/auth/jwt_payload.rb

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ def initialize(token)
3030
@iss = T.let(payload_hash["iss"], String)
3131
@dest = T.let(payload_hash["dest"], String)
3232
@aud = T.let(payload_hash["aud"], String)
33-
@sub = T.let(payload_hash["sub"], String)
33+
@sub = T.let(payload_hash["sub"], T.nilable(String))
3434
@exp = T.let(payload_hash["exp"], Integer)
3535
@nbf = T.let(payload_hash["nbf"], Integer)
3636
@iat = T.let(payload_hash["iat"], Integer)
3737
@jti = T.let(payload_hash["jti"], String)
38-
@sid = T.let(payload_hash["sid"], String)
38+
@sid = T.let(payload_hash["sid"], T.nilable(String))
3939

4040
raise ShopifyAPI::Errors::InvalidJwtTokenError,
4141
"Session token had invalid API key" unless @aud == Context.api_key
@@ -47,19 +47,11 @@ def shop
4747
end
4848
alias_method :shopify_domain, :shop
4949

50-
sig { returns(Integer) }
50+
sig { returns(T.nilable(Integer)) }
5151
def shopify_user_id
52-
@sub.to_i
53-
end
52+
return unless @sub
5453

55-
# TODO: Remove before releasing v11
56-
sig { params(shop: String).returns(T::Boolean) }
57-
def validate_shop(shop)
58-
Context.logger.warn(
59-
"Deprecation notice: ShopifyAPI::Auth::JwtPayload.validate_shop no longer checks the given shop and always " \
60-
"returns true. It will be removed in v11.",
61-
)
62-
true
54+
@sub.tr("^0-9", "").to_i
6355
end
6456

6557
alias_method :eql?, :==

test/auth/jwt_payload_test.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,55 @@ def test_decode_jwt_payload_succeeds_with_not_before_in_the_future_within_10s_le
139139
sid: decoded.sid,
140140
})
141141
end
142+
143+
def test_decode_jwt_payload_coming_from_checkout_ui_extension
144+
payload = @jwt_payload.dup
145+
payload[:sid] = nil
146+
jwt_token = JWT.encode(payload, ShopifyAPI::Context.api_secret_key, "HS256")
147+
decoded = ShopifyAPI::Auth::JwtPayload.new(jwt_token)
148+
assert_equal(payload,
149+
{
150+
iss: decoded.iss,
151+
dest: decoded.dest,
152+
aud: decoded.aud,
153+
sub: decoded.sub,
154+
exp: decoded.exp,
155+
nbf: decoded.nbf,
156+
iat: decoded.iat,
157+
jti: decoded.jti,
158+
sid: decoded.sid,
159+
})
160+
161+
assert_equal(decoded.expire_at, @jwt_payload[:exp])
162+
assert_equal("test-shop.myshopify.io", decoded.shopify_domain)
163+
assert_equal("test-shop.myshopify.io", decoded.shop)
164+
assert_equal(1, decoded.shopify_user_id)
165+
end
166+
167+
def test_decode_jwt_payload_coming_from_checkout_ui_extension_without_user_logged_in
168+
payload = @jwt_payload.dup
169+
payload[:sid] = nil
170+
payload[:sub] = nil
171+
jwt_token = JWT.encode(payload, ShopifyAPI::Context.api_secret_key, "HS256")
172+
decoded = ShopifyAPI::Auth::JwtPayload.new(jwt_token)
173+
assert_equal(payload,
174+
{
175+
iss: decoded.iss,
176+
dest: decoded.dest,
177+
aud: decoded.aud,
178+
sub: decoded.sub,
179+
exp: decoded.exp,
180+
nbf: decoded.nbf,
181+
iat: decoded.iat,
182+
jti: decoded.jti,
183+
sid: decoded.sid,
184+
})
185+
186+
assert_equal(decoded.expire_at, @jwt_payload[:exp])
187+
assert_equal("test-shop.myshopify.io", decoded.shopify_domain)
188+
assert_equal("test-shop.myshopify.io", decoded.shop)
189+
assert_nil(decoded.shopify_user_id)
190+
end
142191
end
143192
end
144193
end

0 commit comments

Comments
 (0)