-
Notifications
You must be signed in to change notification settings - Fork 698
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1884 from Shopify/readd_deprecated_jwt_files
Re-add deprecated files to prevent major release
- Loading branch information
Showing
7 changed files
with
138 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module ShopifyApp | ||
class JWTMiddleware | ||
TOKEN_REGEX = /^Bearer (.+)$/ | ||
ID_TOKEN_QUERY_PARAM = "id_token" | ||
|
||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
return call_next(env) unless ShopifyApp.configuration.embedded_app? | ||
|
||
token = token_from_authorization_header(env) || token_from_query_string(env) | ||
return call_next(env) unless token | ||
|
||
set_env_variables(token, env) | ||
call_next(env) | ||
end | ||
|
||
private | ||
|
||
def call_next(env) | ||
@app.call(env) | ||
end | ||
|
||
def token_from_authorization_header(env) | ||
env["HTTP_AUTHORIZATION"]&.match(TOKEN_REGEX)&.[](1) | ||
end | ||
|
||
def token_from_query_string(env) | ||
Rack::Utils.parse_nested_query(env["QUERY_STRING"])[ID_TOKEN_QUERY_PARAM] | ||
end | ||
|
||
def set_env_variables(token, env) | ||
jwt = ShopifyAPI::Auth::JwtPayload.new(token) | ||
|
||
env["jwt.token"] = token | ||
env["jwt.shopify_domain"] = jwt.shopify_domain | ||
env["jwt.shopify_user_id"] = jwt.shopify_user_id | ||
env["jwt.expire_at"] = jwt.expire_at | ||
rescue ShopifyAPI::Errors::InvalidJwtTokenError | ||
# ShopifyApp::JWT did not raise any exceptions, ensuring behaviour does not change | ||
nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# frozen_string_literal: true | ||
|
||
module ShopifyApp | ||
class JWT | ||
WARN_EXCEPTIONS = [ | ||
::JWT::DecodeError, | ||
::JWT::ExpiredSignature, | ||
::JWT::ImmatureSignature, | ||
::JWT::VerificationError, | ||
::ShopifyApp::InvalidAudienceError, | ||
::ShopifyApp::InvalidDestinationError, | ||
::ShopifyApp::MismatchedHostsError, | ||
] | ||
|
||
def initialize(token) | ||
warn_deprecation | ||
@token = token | ||
set_payload | ||
end | ||
|
||
def shopify_domain | ||
@payload && ShopifyApp::Utils.sanitize_shop_domain(@payload["dest"]) | ||
end | ||
|
||
def shopify_user_id | ||
@payload["sub"].to_i if @payload && @payload["sub"] | ||
end | ||
|
||
def expire_at | ||
@payload["exp"].to_i if @payload && @payload["exp"] | ||
end | ||
|
||
private | ||
|
||
def set_payload | ||
payload, _ = parse_token_data(ShopifyApp.configuration&.secret, ShopifyApp.configuration&.old_secret) | ||
@payload = validate_payload(payload) | ||
rescue *WARN_EXCEPTIONS | ||
nil | ||
end | ||
|
||
def parse_token_data(secret, old_secret) | ||
::JWT.decode(@token, secret, true, { algorithm: "HS256" }) | ||
rescue ::JWT::VerificationError | ||
raise unless old_secret | ||
|
||
::JWT.decode(@token, old_secret, true, { algorithm: "HS256" }) | ||
end | ||
|
||
def validate_payload(payload) | ||
dest_host = ShopifyApp::Utils.sanitize_shop_domain(payload["dest"]) | ||
iss_host = ShopifyApp::Utils.sanitize_shop_domain(payload["iss"]) | ||
api_key = ShopifyApp.configuration.api_key | ||
|
||
raise ::ShopifyApp::InvalidAudienceError, | ||
"'aud' claim does not match api_key" unless payload["aud"] == api_key | ||
raise ::ShopifyApp::InvalidDestinationError, "'dest' claim host not a valid shopify host" unless dest_host | ||
|
||
raise ::ShopifyApp::MismatchedHostsError, | ||
"'dest' claim host does not match 'iss' claim host" unless dest_host == iss_host | ||
|
||
payload | ||
end | ||
|
||
def warn_deprecation | ||
message = <<~EOS | ||
"ShopifyApp::JWT will be deprecated, use ShopifyAPI::Auth::JwtPayload to parse JWT token instead." | ||
EOS | ||
|
||
ShopifyApp::Logger.deprecated(message, "23.0.0") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters