Skip to content

Commit a2e33fe

Browse files
committed
Allow new access token class extending AccessToken class to get access token instance of the its own class when refreshed
1 parent 96daaf5 commit a2e33fe

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/oauth2/access_token.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ def expired?
7979
#
8080
# @return [AccessToken] a new AccessToken
8181
# @note options should be carried over to the new AccessToken
82-
def refresh!(params = {})
82+
def refresh!(params = {}, access_token_opts = {}, access_token_class = self.class)
8383
raise('A refresh_token is not available') unless refresh_token
8484
params[:grant_type] = 'refresh_token'
8585
params[:refresh_token] = refresh_token
86-
new_token = @client.get_token(params)
86+
new_token = @client.get_token(params, access_token_opts, access_token_class)
8787
new_token.options = options
8888
new_token.refresh_token = refresh_token unless new_token.refresh_token
8989
new_token

spec/oauth2/access_token_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,22 @@ def assert_initialized_token(target) # rubocop:disable Metrics/AbcSize
154154
:expires_in => 600,
155155
:param_name => 'o_param')
156156
end
157+
let(:new_access) do
158+
NewAccessToken = Class.new(AccessToken)
159+
NewAccessToken.new(client, token, :refresh_token => 'abaca')
160+
end
157161

158162
it 'returns a refresh token with appropriate values carried over' do
159163
refreshed = access.refresh!
160164
expect(access.client).to eq(refreshed.client)
161165
expect(access.options[:param_name]).to eq(refreshed.options[:param_name])
162166
end
163167

168+
it 'returns a refresh token of the same access token class' do
169+
refreshed = new_access.refresh!
170+
expect(new_access.class).to eq(refreshed.class)
171+
end
172+
164173
context 'with a nil refresh_token in the response' do
165174
let(:refresh_body) { MultiJson.encode(:access_token => 'refreshed_foo', :expires_in => 600, :refresh_token => nil) }
166175

0 commit comments

Comments
 (0)