File: client_spec.rb

package info (click to toggle)
ruby-rack-oauth2 2.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 560 kB
  • sloc: ruby: 4,013; makefile: 4
file content (530 lines) | stat: -rw-r--r-- 16,647 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
require 'spec_helper.rb'

describe Rack::OAuth2::Client do
  let(:client_id) { 'client_id' }
  let(:client_secret) { 'client_secret' }
  let :client do
    Rack::OAuth2::Client.new(
      identifier: client_id,
      secret: client_secret,
      host: 'server.example.com',
      redirect_uri: 'https://client.example.com/callback',
      revocation_endpoint: '/oauth2/revoke'
    )
  end
  subject { client }

  its(:identifier) { should == 'client_id' }
  its(:secret)     { should == 'client_secret' }
  its(:authorization_endpoint) { should == '/oauth2/authorize' }
  its(:token_endpoint)         { should == '/oauth2/token' }
  its(:revocation_endpoint)    { should == '/oauth2/revoke' }

  context 'when identifier is missing' do
    it do
      expect { Rack::OAuth2::Client.new }.to raise_error AttrRequired::AttrMissing
    end
  end

  describe '#authorization_uri' do
    subject { client.authorization_uri }
    it { should include 'https://server.example.com/oauth2/authorize' }
    it { should include 'client_id=client_id' }
    it { should include 'redirect_uri=https%3A%2F%2Fclient.example.com%2Fcallback' }
    it { should include 'response_type=code' }

    context 'when endpoints are absolute URIs' do
      before do
        client.authorization_endpoint = 'https://server2.example.com/oauth/authorize'
        client.token_endpoint = 'https://server2.example.com/oauth/token'
      end
      it { should include 'https://server2.example.com/oauth/authorize' }
    end

    context 'when scheme is specified' do
      before { client.scheme = 'http' }
      it { should include 'http://server.example.com/oauth2/authorize' }
    end

    context 'when response_type is token' do
      subject { client.authorization_uri(response_type: :token) }
      it { should include 'response_type=token' }
    end

    context 'when response_type is an Array' do
      subject { client.authorization_uri(response_type: [:token, :code]) }
      it { should include 'response_type=token%20code' }
    end

    context 'when scope is given' do
      subject { client.authorization_uri(scope: [:scope1, :scope2]) }
      it { should include 'scope=scope1%20scope2' }
    end
  end

  describe '#authorization_code=' do
    before  { client.authorization_code = 'code' }
    subject { client.instance_variable_get('@grant') }
    it { should be_instance_of Rack::OAuth2::Client::Grant::AuthorizationCode }
  end

  describe '#resource_owner_credentials=' do
    before  { client.resource_owner_credentials = 'username', 'password' }
    subject { client.instance_variable_get('@grant') }
    it { should be_instance_of Rack::OAuth2::Client::Grant::Password }
  end

  describe '#refresh_token=' do
    before  { client.refresh_token = 'refresh_token' }
    subject { client.instance_variable_get('@grant') }
    it { should be_instance_of Rack::OAuth2::Client::Grant::RefreshToken }
  end

  describe '#access_token!' do
    subject { client.access_token! }

    context '*args handling' do
      describe 'client authentication method' do
        before do
          client.authorization_code = 'code'
        end

        it 'should be Basic auth as default' do
          mock_response(
            :post,
            'https://server.example.com/oauth2/token',
            'tokens/bearer',
            request_header: {
              'Authorization' => 'Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ='
            }
          )
          client.access_token!
        end

        context 'when Basic auth method is used' do
          context 'when client_id is a url' do
            let(:client_id) { 'https://client.example.com'}

            it 'should be encoded in "application/x-www-form-urlencoded"' do
              mock_response(
                :post,
                'https://server.example.com/oauth2/token',
                'tokens/bearer',
                request_header: {
                  'Authorization' => 'Basic aHR0cHMlM0ElMkYlMkZjbGllbnQuZXhhbXBsZS5jb206Y2xpZW50X3NlY3JldA=='
                }
              )
              client.access_token!
            end
          end
        end

        context 'when basic_without_www_form_urlencode method is used' do
          context 'when client_id is a url' do
             let(:client_id) { 'https://client.example.com'}

             it 'should be encoded in "application/x-www-form-urlencoded"' do
               mock_response(
                 :post,
                 'https://server.example.com/oauth2/token',
                 'tokens/bearer',
                 request_header: {
                   'Authorization' => 'Basic aHR0cHM6Ly9jbGllbnQuZXhhbXBsZS5jb206Y2xpZW50X3NlY3JldA=='
                 }
               )
               client.access_token! :basic_without_www_form_urlencode
             end
           end
        end

        context 'when jwt_bearer auth method specified' do
          context 'when client_secret is given' do
            it 'should be JWT bearer client assertion w/ auto-generated HS256-signed JWT assertion' do
              mock_response(
                :post,
                'https://server.example.com/oauth2/token',
                'tokens/bearer',
                params: {
                  client_assertion: /^eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9\..+/, # NOTE: HS256
                  client_assertion_type: Rack::OAuth2::URN::ClientAssertionType::JWT_BEARER,
                  code: 'code',
                  grant_type: 'authorization_code',
                  redirect_uri: 'https://client.example.com/callback'
                }
              )
              client.access_token! :jwt_bearer
            end
          end

          context 'when private_key is given' do
            context 'when RSA key' do
              let :client do
                Rack::OAuth2::Client.new(
                  identifier: 'client_id',
                  private_key: OpenSSL::PKey::RSA.generate(2048),
                  host: 'server.example.com',
                  redirect_uri: 'https://client.example.com/callback'
                )
              end

              it 'should be JWT bearer client assertion w/ auto-generated RS256-signed JWT assertion' do
                mock_response(
                  :post,
                  'https://server.example.com/oauth2/token',
                  'tokens/bearer',
                  params: {
                    client_assertion: /^eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9\..+/, # NOTE: RS256
                    client_assertion_type: Rack::OAuth2::URN::ClientAssertionType::JWT_BEARER,
                    code: 'code',
                    grant_type: 'authorization_code',
                    redirect_uri: 'https://client.example.com/callback'
                  }
                )
                client.access_token! :jwt_bearer
              end
            end

            context 'when EC key' do
              let :client do
                Rack::OAuth2::Client.new(
                  identifier: 'client_id',
                  private_key: OpenSSL::PKey::EC.generate('prime256v1'),
                  host: 'server.example.com',
                  redirect_uri: 'https://client.example.com/callback'
                )
              end

              it 'should be JWT bearer client assertion w/ auto-generated ES256-signed JWT assertion' do
                mock_response(
                  :post,
                  'https://server.example.com/oauth2/token',
                  'tokens/bearer',
                  params: {
                    client_assertion: /^eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9\..+/, # NOTE: ES256
                    client_assertion_type: Rack::OAuth2::URN::ClientAssertionType::JWT_BEARER,
                    code: 'code',
                    grant_type: 'authorization_code',
                    redirect_uri: 'https://client.example.com/callback'
                  }
                )
                client.access_token! :jwt_bearer
              end
            end
          end

          context 'when client_assertion is explicitly given' do
            let :client do
              Rack::OAuth2::Client.new(
                identifier: 'client_id',
                host: 'server.example.com',
                redirect_uri: 'https://client.example.com/callback'
              )
            end

            it 'should be JWT bearer client assertion w/ specified assertion' do
              mock_response(
                :post,
                'https://server.example.com/oauth2/token',
                'tokens/bearer',
                params: {
                  client_assertion: 'any.jwt.assertion',
                  client_assertion_type: Rack::OAuth2::URN::ClientAssertionType::JWT_BEARER,
                  code: 'code',
                  grant_type: 'authorization_code',
                  redirect_uri: 'https://client.example.com/callback'
                }
              )
              client.access_token! :jwt_bearer, client_assertion: 'any.jwt.assertion'
            end
          end
        end

        context 'when other auth method specified' do
          it 'should be body params' do
            mock_response(
              :post,
              'https://server.example.com/oauth2/token',
              'tokens/bearer',
              params: {
                client_id: 'client_id',
                client_secret: 'client_secret',
                code: 'code',
                grant_type: 'authorization_code',
                redirect_uri: 'https://client.example.com/callback'
              }
            )
            client.access_token! :client_auth_body
          end
        end

        context 'when auth method is specified as Hash' do
          it 'should be removed before sending request' do
            mock_response(
              :post,
              'https://server.example.com/oauth2/token',
              'tokens/bearer',
              params: {
                client_id: 'client_id',
                client_secret: 'client_secret',
                code: 'code',
                grant_type: 'authorization_code',
                redirect_uri: 'https://client.example.com/callback'
              }
            )
            client.access_token! client_auth_method: :body
          end
        end
      end

      describe 'scopes' do
        context 'when scope option given' do
          it 'should specify given scope' do
            mock_response(
              :post,
              'https://server.example.com/oauth2/token',
              'tokens/bearer',
              params: {
                grant_type: 'client_credentials',
                scope: 'a b'
              }
            )
            client.access_token! scope: [:a, :b]
          end
        end
      end

      describe 'unknown params' do
        it 'should be included in body params' do
          mock_response(
            :post,
            'https://server.example.com/oauth2/token',
            'tokens/bearer',
            params: {
              grant_type: 'client_credentials',
              resource: 'something'
            }
          )
          client.access_token! resource: :something
        end
      end
    end

    context 'local_http_config handling' do
      it do
        mock_response(
          :post,
          'https://server.example.com/oauth2/token',
          'tokens/bearer',
          request_header: {
            'Authorization' => 'Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=',
            'X-Foo' => 'bar'
          }
        )
        client.access_token! do |request|
          request.headers['X-Foo'] = 'bar'
        end
      end
    end

    context 'when bearer token is given' do
      before do
        client.authorization_code = 'code'
        mock_response(
          :post,
          'https://server.example.com/oauth2/token',
          'tokens/bearer'
        )
      end
      it { should be_instance_of Rack::OAuth2::AccessToken::Bearer }
      its(:token_type) { should == :bearer }
      its(:access_token) { should == 'access_token' }
      its(:refresh_token) { should == 'refresh_token' }
      its(:expires_in) { should == 3600 }

      context 'when token type is "Bearer", not "bearer"' do
        before do
          client.authorization_code = 'code'
          mock_response(
            :post,
            'https://server.example.com/oauth2/token',
            'tokens/_Bearer'
          )
        end
        it { should be_instance_of Rack::OAuth2::AccessToken::Bearer }
        its(:token_type) { should == :bearer }
      end
    end

    context 'when unknown-type token is given' do
      before do
        client.authorization_code = 'code'
        mock_response(
          :post,
          'https://server.example.com/oauth2/token',
          'tokens/unknown'
        )
      end
      it do
        expect { client.access_token! }.to raise_error(StandardError, 'Unknown Token Type')
      end
    end

    context 'when error response is given' do
      before do
        mock_response(
          :post,
          'https://server.example.com/oauth2/token',
          'errors/invalid_request',
          status: 400
        )
      end
      it do
        expect { client.access_token! }.to raise_error Rack::OAuth2::Client::Error
      end
    end

    context 'when no body given' do
      context 'when error given' do
        before do
          mock_response(
            :post,
            'https://server.example.com/oauth2/token',
            'blank',
            format: 'txt',
            status: 400
          )
        end
        it do
          expect { client.access_token! }.to raise_error Rack::OAuth2::Client::Error
        end
      end
    end
  end

  describe '#revoke!' do
    context 'local_http_config handling' do
      it do
        mock_response(
          :post,
          'https://server.example.com/oauth2/revoke',
          'blank',
          format: 'txt',
          status: 200,
          body: {
            token: 'access_token',
            token_type_hint: 'access_token'
          },
          request_header: {
            'Authorization' => 'Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=',
            'X-Foo' => 'bar'
          }
        )
        client.revoke!(access_token: 'access_token') do |request|
          request.headers['X-Foo'] = 'bar'
        end
      end
    end

    context 'when access_token given' do
      before do
        mock_response(
          :post,
          'https://server.example.com/oauth2/revoke',
          'blank',
          format: 'txt',
          status: 200,
          body: {
            token: 'access_token',
            token_type_hint: 'access_token'
          }
        )
      end
      it do
        client.revoke!(access_token: 'access_token').should == :success
      end
    end

    context 'when refresh_token given' do
      before do
        mock_response(
          :post,
          'https://server.example.com/oauth2/revoke',
          'blank',
          format: 'txt',
          status: 200,
          body: {
            token: 'refresh_token',
            token_type_hint: 'refresh_token'
          }
        )
      end

      context 'as argument' do
        it do
          client.revoke!(refresh_token: 'refresh_token').should == :success
        end
      end

      context 'as grant' do
        it do
          client.refresh_token = 'refresh_token'
          client.revoke!
        end
      end
    end

    context 'when error response given' do
      before do
        mock_response(
          :post,
          'https://server.example.com/oauth2/revoke',
          'errors/invalid_request',
          status: 400
        )
      end

      it do
        expect do
          client.revoke! access_token: 'access_token'
        end.to raise_error Rack::OAuth2::Client::Error
      end
    end

    context 'when no token given' do
      it do
        expect do
          client.revoke!
        end.to raise_error ArgumentError
      end
    end
  end

  context 'when no host info' do
    let :client do
      Rack::OAuth2::Client.new(
        identifier: 'client_id',
        secret: 'client_secret',
        redirect_uri: 'https://client.example.com/callback',
        revocation_endpoint: '/oauth2/revoke'
      )
    end

    describe '#authorization_uri' do
      it do
        expect { client.authorization_uri }.to raise_error 'No Host Info'
      end
    end

    describe '#access_token!' do
      it do
        expect { client.access_token! }.to raise_error 'No Host Info'
      end
    end

    describe '#revoke!' do
      it do
        expect { client.revoke! access_token: 'access_token' }.to raise_error 'No Host Info'
      end
    end
  end
end