Skip to content

Commit 9171f7f

Browse files
committed
Access Token and Refresh Token. (+1 squashed commit)
Squashed commits: [1e07aed] authorize_url
1 parent 9f68c0e commit 9171f7f

File tree

2 files changed

+28
-42
lines changed

2 files changed

+28
-42
lines changed

lib/jira/oauth2_client.rb

+5-11
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ class Oauth2Client < RequestClient
125125
# attr_reader :options
126126

127127
# @param [Hash] options Options as passed from JIRA::Client constructor.
128-
# @option options [String] :site The hostname of the Jira in the role as Resource Server
129-
# @option options [String] :auth_site The hostname of the Authentication Server
128+
# @option options [String] :site The URL of the Jira in the role as Resource Server
129+
# @option options [String] :auth_site The URL of the Authentication Server
130130
# @option options [String] :client_id The OAuth 2.0 client id as registered with the Authentication Server
131131
# @option options [String] :client_secret The OAuth 2.0 client secret as registered with the Authentication Server
132132
# @option options [String] :auth_scheme Way of passing parameters for authentication (defaults to 'request_body')
@@ -147,8 +147,8 @@ class Oauth2Client < RequestClient
147147
def initialize(options)
148148
# @options = init_oauth2_options(options)
149149
init_oauth2_options(options)
150-
if options.has_key?(:access_token_options)
151-
@access_token = access_token_from_options(options[:access_token_options])
150+
if 0 < options.slice(:access_token, :refresh_token).size
151+
@access_token = access_token_from_options(options)
152152
end
153153
nil
154154
end
@@ -198,13 +198,7 @@ def oauth2_client
198198

199199
def access_token_from_options(_options)
200200
@prior_grant_type = 'access_token'
201-
OAuth2::AccessToken.from_hash(oauth2_client, _options)
202-
end
203-
204-
# @private
205-
private def init_access_token(_options)
206-
@prior_grant_type = 'access_token'
207-
hash = { token: _options[:token], refresh_token: _options[:refresh_token] }
201+
hash = { token: _options[:access_token], refresh_token: _options[:refresh_token] }
208202
OAuth2::AccessToken.from_hash(oauth2_client, hash)
209203
end
210204

spec/jira/oauth2_client_spec.rb

+23-31
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,9 @@ def query_params_to_h(uri)
179179
context 'prior to Authentication Request' do
180180
let(:redirect_uri) { 'http://localhost/auth_response' }
181181
subject(:request_client) do
182-
JIRA::Oauth2Client.new(client_id: client_id,
183-
client_secret: client_secret,
184-
site: auth_site,
185-
oauth2_client_options: { site: auth_site, redirect_uri: redirect_uri })
182+
JIRA::Oauth2Client.new(site: auth_site,
183+
client_id: client_id,
184+
client_secret: client_secret)
186185
end
187186

188187
describe '.authorize_url' do
@@ -191,7 +190,7 @@ def query_params_to_h(uri)
191190
context 'default generated CSRF state' do
192191
it 'provides authorization redirect URI' do
193192

194-
authorize_url = request_client.authorize_url
193+
authorize_url = request_client.authorize_url(params: { redirect_uri: redirect_uri })
195194

196195
expect(authorize_url).to_not be_nil
197196
uri = URI.parse(authorize_url)
@@ -208,7 +207,7 @@ def query_params_to_h(uri)
208207
context 'without using CSRF state' do
209208
it 'disables CSRF STATE' do
210209

211-
authorize_url = request_client.authorize_url(state: false)
210+
authorize_url = request_client.authorize_url(state: false, params: { redirect_uri: redirect_uri })
212211

213212
expect(authorize_url).to_not be_nil
214213
uri = URI.parse(authorize_url)
@@ -225,7 +224,7 @@ def query_params_to_h(uri)
225224
context 'using given CSRF state' do
226225
it 'uses given CSRF STATE' do
227226

228-
authorize_url = request_client.authorize_url(state: state_given)
227+
authorize_url = request_client.authorize_url(state: state_given, params: { redirect_uri: redirect_uri })
229228

230229
expect(authorize_url).to_not be_nil
231230
uri = URI.parse(authorize_url)
@@ -253,7 +252,7 @@ def query_params_to_h(uri)
253252

254253
it 'provides authorization redirect URI' do
255254

256-
params = { proxy_uri: proxy_site, proxy_user: proxy_user, proxy_password: proxy_password }
255+
params = { redirect_uri: redirect_uri, proxy_uri: proxy_site, proxy_user: proxy_user, proxy_password: proxy_password }
257256
authorize_url = proxy_request_client.authorize_url(params: params)
258257

259258
expect(authorize_url).to_not be_nil
@@ -277,6 +276,11 @@ def query_params_to_h(uri)
277276
let(:code) { 'Authentication Code String Value' }
278277
let(:token) { 'Access Token String Value' }
279278
let(:refresh_token) { 'Refresh Token String Value' }
279+
subject(:request_client) do
280+
JIRA::Oauth2Client.new(site: site,
281+
client_id: client_id,
282+
client_secret: client_secret)
283+
end
280284
let(:access_token) do
281285
OAuth2::AccessToken.new(request_client.oauth2_client,
282286
token,
@@ -291,7 +295,7 @@ def query_params_to_h(uri)
291295

292296
request_client.get_token(code)
293297

294-
expect(request_client.grant_type).to eq('authorization_code')
298+
expect(request_client.prior_grant_type).to eq('authorization_code')
295299
expect(request_client.token).to eq(token)
296300
expect(request_client.refresh_token).to eq(refresh_token)
297301
end
@@ -309,11 +313,9 @@ def query_params_to_h(uri)
309313
request_client = JIRA::Oauth2Client.new(client_id: client_id,
310314
client_secret: client_secret,
311315
site: auth_site,
312-
access_token_options: {
313-
token: token
314-
})
316+
access_token: token)
315317

316-
expect(request_client.grant_type).to eq('access_token')
318+
expect(request_client.prior_grant_type).to eq('access_token')
317319
expect(request_client.token).to eq(token)
318320
end
319321
end
@@ -328,9 +330,7 @@ def query_params_to_h(uri)
328330
JIRA::Oauth2Client.new(client_id: client_id,
329331
client_secret: client_secret,
330332
site: auth_site,
331-
access_token_options: {
332-
refresh_token: refresh_token
333-
})
333+
refresh_token: refresh_token)
334334
end
335335
let(:access_token_updated) do
336336
OAuth2::AccessToken.new(request_client.oauth2_client,
@@ -346,7 +346,7 @@ def query_params_to_h(uri)
346346

347347
request_client.refresh
348348

349-
expect(request_client.grant_type).to eq('refresh_token')
349+
expect(request_client.prior_grant_type).to eq('refresh_token')
350350
expect(request_client.token).to eq(token_updated)
351351
expect(request_client.refresh_token).to eq(refresh_token_updated)
352352
end
@@ -357,26 +357,21 @@ def query_params_to_h(uri)
357357
let(:oauth2_client) { instance_double(OAuth2::Client) }
358358
let(:token) { 'Access Token String Value' }
359359
let(:refresh_token) { 'Refresh Token String Value' }
360+
let(:redirect_uri) { 'http://localhost/auth_response' }
360361
let(:access_token) do
361362
OAuth2::AccessToken.new(oauth2_client,
362363
token,
363364
{ refresh_token: refresh_token,
364365
expires_in: 3600,
365366
expires_at: (Time.now + 3600).to_i })
366367
end
367-
let(:redirect_uri) { 'http://localhost/auth_response' }
368368
subject(:client) do
369369
JIRA::Client.new(auth_type: :oauth2,
370370
client_id: client_id,
371371
client_secret: client_secret,
372-
site: auth_site,
373-
oauth2_client_options: {
374-
site: site
375-
},
376-
access_token_options: {
377-
token: token,
378-
refresh_token: refresh_token
379-
})
372+
site: site,
373+
access_token: token,
374+
refresh_token: refresh_token)
380375
end
381376
let(:response) do
382377
response = Net::HTTPSuccess.new(1.0, '200', 'OK')
@@ -388,11 +383,7 @@ def query_params_to_h(uri)
388383
it 'initializes the oauth2 client from an Access Token' do
389384
expect(OAuth2::Client).to receive(:new).with(client_id,
390385
client_secret,
391-
{
392-
site: site,
393-
auth_scheme: "request_body",
394-
authorize_url: "/rest/oauth2/latest/authorize"
395-
}).and_return(oauth2_client)
386+
client.request_client.oauth2_client_options).and_return(oauth2_client)
396387

397388
oauth2_client_result = client.request_client.oauth2_client
398389

@@ -412,6 +403,7 @@ def query_params_to_h(uri)
412403

413404
access_token_result = client.request_client.access_token
414405

406+
expect(access_token_result).to_not be_nil
415407
expect(access_token_result.token).to eq(token)
416408
expect(access_token_result.refresh_token).to eq(refresh_token)
417409
end

0 commit comments

Comments
 (0)