Skip to content

Commit

Permalink
Add refresh_token for token and session objects
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaansyed committed Aug 14, 2024
1 parent c60a49b commit 6ada57e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/shopify_api/auth/oauth/access_token_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class AccessTokenResponse < T::Struct
const :expires_in, T.nilable(Integer)
const :associated_user, T.nilable(AssociatedUser)
const :associated_user_scope, T.nilable(String)
const :refresh_token, T.nilable(String)

sig { returns(T::Boolean) }
def online_token?
Expand All @@ -29,7 +30,8 @@ def ==(other)
session == other.session &&
expires_in == other.expires_in &&
associated_user == other.associated_user &&
associated_user_scope == other.associated_user_scope
associated_user_scope == other.associated_user_scope &&
refresh_token == other.refresh_token
end
end
end
Expand Down
12 changes: 10 additions & 2 deletions lib/shopify_api/auth/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class Session
sig { returns(T.nilable(String)) }
attr_accessor :shopify_session_id

sig { returns(T.nilable(String)) }
attr_accessor :refresh_token

sig { returns(T::Boolean) }
def online?
@is_online
Expand All @@ -52,10 +55,11 @@ def expired?
is_online: T.nilable(T::Boolean),
associated_user: T.nilable(AssociatedUser),
shopify_session_id: T.nilable(String),
refresh_token: T.nilable(String),
).void
end
def initialize(shop:, id: nil, state: nil, access_token: "", scope: [], associated_user_scope: nil, expires: nil,
is_online: nil, associated_user: nil, shopify_session_id: nil)
is_online: nil, associated_user: nil, shopify_session_id: nil, refresh_token: nil)
@id = T.let(id || SecureRandom.uuid, String)
@shop = shop
@state = state
Expand All @@ -68,6 +72,7 @@ def initialize(shop:, id: nil, state: nil, access_token: "", scope: [], associat
@associated_user = associated_user
@is_online = T.let(is_online || !associated_user.nil?, T::Boolean)
@shopify_session_id = shopify_session_id
@refresh_token = refresh_token
end

class << self
Expand All @@ -91,6 +96,7 @@ def temp(shop:, access_token:, &blk)

sig { params(shop: String, access_token_response: Oauth::AccessTokenResponse).returns(Session) }
def from(shop:, access_token_response:)
puts "ACCESS_TOKEN_RESPONSE #{access_token_response.inspect}"
is_online = access_token_response.online_token?

if is_online
Expand All @@ -112,6 +118,7 @@ def from(shop:, access_token_response:)
associated_user: associated_user,
expires: expires,
shopify_session_id: access_token_response.session,
refresh_token: access_token_response.refresh_token
)
end

Expand Down Expand Up @@ -150,7 +157,8 @@ def ==(other)
(!(expires.nil? ^ other.expires.nil?) && (expires.nil? || expires.to_i == other.expires.to_i)) &&
online? == other.online? &&
associated_user == other.associated_user &&
shopify_session_id == other.shopify_session_id
shopify_session_id == other.shopify_session_id &&
refresh_token == other.refresh_token

else
false
Expand Down
2 changes: 1 addition & 1 deletion lib/shopify_api/auth/token_exchange.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def exchange_token(shop:, session_token:, requested_token_type:)

Session.from(
shop: shop,
access_token_response: Oauth::AccessTokenResponse.from_hash(session_params),
access_token_response: Oauth::AccessTokenResponse.from_hash(session_params.merge({"refresh_token" => "cool-refresh"})),
)
end
end
Expand Down

0 comments on commit 6ada57e

Please sign in to comment.