-
Notifications
You must be signed in to change notification settings - Fork 476
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 #1254 from Shopify/token-exchange
Introduce the token exchange API for fetching access tokens
- Loading branch information
Showing
8 changed files
with
443 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module ShopifyAPI | ||
module Auth | ||
module Oauth | ||
class AccessTokenResponse < T::Struct | ||
extend T::Sig | ||
|
||
const :access_token, String | ||
const :scope, String | ||
const :session, T.nilable(String) | ||
const :expires_in, T.nilable(Integer) | ||
const :associated_user, T.nilable(AssociatedUser) | ||
const :associated_user_scope, T.nilable(String) | ||
|
||
sig { returns(T::Boolean) } | ||
def online_token? | ||
!associated_user.nil? | ||
end | ||
|
||
alias_method :eql?, :== | ||
sig { params(other: T.nilable(AccessTokenResponse)).returns(T::Boolean) } | ||
def ==(other) | ||
return false unless other | ||
|
||
access_token == other.access_token && | ||
scope == other.scope && | ||
session == other.session && | ||
expires_in == other.expires_in && | ||
associated_user == other.associated_user && | ||
associated_user_scope == other.associated_user_scope | ||
end | ||
end | ||
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
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,80 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module ShopifyAPI | ||
module Auth | ||
module TokenExchange | ||
extend T::Sig | ||
|
||
TOKEN_EXCHANGE_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:token-exchange" | ||
ID_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:id_token" | ||
|
||
class RequestedTokenType < T::Enum | ||
enums do | ||
ONLINE_ACCESS_TOKEN = new("urn:shopify:params:oauth:token-type:online-access-token") | ||
OFFLINE_ACCESS_TOKEN = new("urn:shopify:params:oauth:token-type:offline-access-token") | ||
end | ||
end | ||
|
||
class << self | ||
extend T::Sig | ||
|
||
sig do | ||
params( | ||
shop: String, | ||
session_token: String, | ||
requested_token_type: RequestedTokenType, | ||
).returns(ShopifyAPI::Auth::Session) | ||
end | ||
def exchange_token(shop:, session_token:, requested_token_type:) | ||
unless ShopifyAPI::Context.setup? | ||
raise ShopifyAPI::Errors::ContextNotSetupError, | ||
"ShopifyAPI::Context not setup, please call ShopifyAPI::Context.setup" | ||
end | ||
raise ShopifyAPI::Errors::UnsupportedOauthError, | ||
"Cannot perform OAuth Token Exchange for private apps." if ShopifyAPI::Context.private? | ||
raise ShopifyAPI::Errors::UnsupportedOauthError, | ||
"Cannot perform OAuth Token Exchange for non embedded apps." unless ShopifyAPI::Context.embedded? | ||
|
||
# Validate the session token content | ||
ShopifyAPI::Auth::JwtPayload.new(session_token) | ||
|
||
shop_session = ShopifyAPI::Auth::Session.new(shop: shop) | ||
body = { | ||
client_id: ShopifyAPI::Context.api_key, | ||
client_secret: ShopifyAPI::Context.api_secret_key, | ||
grant_type: TOKEN_EXCHANGE_GRANT_TYPE, | ||
subject_token: session_token, | ||
subject_token_type: ID_TOKEN_TYPE, | ||
requested_token_type: requested_token_type.serialize, | ||
} | ||
|
||
client = Clients::HttpClient.new(session: shop_session, base_path: "/admin/oauth") | ||
response = begin | ||
client.request( | ||
Clients::HttpRequest.new( | ||
http_method: :post, | ||
path: "access_token", | ||
body: body, | ||
body_type: "application/json", | ||
), | ||
) | ||
rescue ShopifyAPI::Errors::HttpResponseError => error | ||
if error.code == 400 && error.response.body["error"] == "invalid_subject_token" | ||
raise ShopifyAPI::Errors::InvalidJwtTokenError, "Session token was rejected by token exchange" | ||
end | ||
|
||
raise error | ||
end | ||
|
||
session_params = T.cast(response.body, T::Hash[String, T.untyped]).to_h | ||
|
||
Session.from( | ||
shop: shop, | ||
access_token_response: Oauth::AccessTokenResponse.from_hash(session_params), | ||
) | ||
end | ||
end | ||
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,43 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
require_relative "../../test_helper" | ||
|
||
module ShopifyAPITest | ||
module Auth | ||
module Oauth | ||
class AccessTokenResponseTest < Test::Unit::TestCase | ||
def test_online_token_is_false_when_no_associated_user | ||
token_response = ShopifyAPI::Auth::Oauth::AccessTokenResponse.new( | ||
access_token: "token", | ||
scope: "scope1, scope2", | ||
) | ||
|
||
assert(!token_response.online_token?) | ||
end | ||
|
||
def test_online_token_is_true_when_associated_user_is_present | ||
associated_user = ShopifyAPI::Auth::AssociatedUser.new( | ||
id: 902541635, | ||
first_name: "first", | ||
last_name: "last", | ||
email: "[email protected]", | ||
email_verified: true, | ||
account_owner: true, | ||
locale: "en", | ||
collaborator: false, | ||
) | ||
token_response = ShopifyAPI::Auth::Oauth::AccessTokenResponse.new( | ||
access_token: "token", | ||
scope: "scope1, scope2", | ||
expires_in: 1000, | ||
associated_user_scope: "scope1", | ||
associated_user: associated_user, | ||
) | ||
|
||
assert(token_response.online_token?) | ||
end | ||
end | ||
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 |
---|---|---|
|
@@ -79,6 +79,70 @@ def test_temp_with_block_var | |
assert_equal(session, ShopifyAPI::Context.active_session) | ||
end | ||
|
||
def test_from_with_offline_access_token_response | ||
shop = "test-shop" | ||
response = ShopifyAPI::Auth::Oauth::AccessTokenResponse.new( | ||
access_token: "token", | ||
scope: "scope1, scope2", | ||
session: "session", | ||
) | ||
expected_session = ShopifyAPI::Auth::Session.new( | ||
id: "offline_#{shop}", | ||
shop: shop, | ||
access_token: response.access_token, | ||
scope: response.scope, | ||
is_online: false, | ||
associated_user_scope: nil, | ||
associated_user: nil, | ||
expires: nil, | ||
shopify_session_id: response.session, | ||
) | ||
|
||
session = ShopifyAPI::Auth::Session.from(shop: shop, access_token_response: response) | ||
|
||
assert_equal(expected_session, session) | ||
end | ||
|
||
def test_from_with_online_access_token_response | ||
shop = "test-shop" | ||
associated_user = ShopifyAPI::Auth::AssociatedUser.new( | ||
id: 902541635, | ||
first_name: "first", | ||
last_name: "last", | ||
email: "[email protected]", | ||
email_verified: true, | ||
account_owner: true, | ||
locale: "en", | ||
collaborator: false, | ||
) | ||
response = ShopifyAPI::Auth::Oauth::AccessTokenResponse.new( | ||
access_token: "token", | ||
scope: "scope1, scope2", | ||
session: "session", | ||
expires_in: 1000, | ||
associated_user: associated_user, | ||
associated_user_scope: "scope1", | ||
) | ||
time_now = Time.now | ||
expected_session = ShopifyAPI::Auth::Session.new( | ||
id: "#{shop}_#{associated_user.id}", | ||
shop: shop, | ||
access_token: response.access_token, | ||
scope: response.scope, | ||
is_online: false, | ||
associated_user_scope: response.associated_user_scope, | ||
associated_user: associated_user, | ||
expires: time_now + response.expires_in, | ||
shopify_session_id: response.session, | ||
) | ||
|
||
session = Time.stub(:now, time_now) do | ||
ShopifyAPI::Auth::Session.from(shop: shop, access_token_response: response) | ||
end | ||
|
||
assert_equal(expected_session, session) | ||
end | ||
|
||
def teardown | ||
ShopifyAPI::Context.deactivate_session | ||
end | ||
|
Oops, something went wrong.