-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
access_token_spec.rb
720 lines (597 loc) · 25.5 KB
/
access_token_spec.rb
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
# frozen_string_literal: true
require "spec_helper"
RSpec.describe Doorkeeper::AccessToken do
subject(:access_token) { FactoryBot.build(:access_token) }
it { expect(access_token).to be_valid }
it_behaves_like "an accessible token"
it_behaves_like "a revocable token"
it_behaves_like "a unique token" do
let(:factory_name) { :access_token }
end
module CustomGeneratorArgs
def self.generate; end
end
describe "#generate_token" do
it "generates a token using the default method" do
FactoryBot.create :access_token
token = FactoryBot.create :access_token
expect(token.token).to be_a(String)
end
context "with hashing enabled" do
let(:token) { FactoryBot.create :access_token }
include_context "with token hashing enabled"
it "holds a volatile plaintext token when created" do
expect(token.plaintext_token).to be_a(String)
expect(token.token)
.to eq(hashed_or_plain_token_func.call(token.plaintext_token))
# Finder method only finds the hashed token
loaded = described_class.find_by(token: token.token)
expect(loaded).to eq(token)
expect(loaded.plaintext_token).to be_nil
expect(loaded.token).to eq(token.token)
end
it "does not find_by plain text tokens" do
expect(described_class.find_by(token: token.plaintext_token)).to be_nil
end
describe "with having a plain text token" do
let(:plain_text_token) { "plain text token" }
let(:access_token) { FactoryBot.create :access_token }
before do
# Assume we have a plain text token from before activating the option
access_token.update_column(:token, plain_text_token)
end
context "without fallback lookup" do
it "does not provide lookups with either through by_token" do
expect(described_class.by_token(plain_text_token)).to eq(nil)
expect(described_class.by_token(access_token.token)).to eq(nil)
# And it does not touch the token
access_token.reload
expect(access_token.token).to eq(plain_text_token)
end
end
context "with fallback lookup" do
include_context "with token hashing and fallback lookup enabled"
it "upgrades a plain token when falling back to it" do
# Side-effect: This will automatically upgrade the token
expect(described_class).to receive(:upgrade_fallback_value).and_call_original
expect(described_class.by_token(plain_text_token))
.to have_attributes(
resource_owner_id: access_token.resource_owner_id,
application_id: access_token.application_id,
scopes: access_token.scopes,
)
# Will find subsequently by hashing the token
expect(described_class.by_token(plain_text_token))
.to have_attributes(
resource_owner_id: access_token.resource_owner_id,
application_id: access_token.application_id,
scopes: access_token.scopes,
)
# Not all the ORM support :id PK
if access_token.respond_to?(:id)
expect(described_class.by_token(plain_text_token).id).to eq(access_token.id)
end
# And it modifies the token value
access_token.reload
expect(access_token.token).not_to eq(plain_text_token)
expect(described_class.find_by(token: plain_text_token)).to eq(nil)
expect(described_class.find_by(token: access_token.token)).not_to be_nil
end
end
end
end
it "generates a token using a custom object" do
eigenclass = class << CustomGeneratorArgs; self; end
eigenclass.class_eval do
remove_method :generate
end
module CustomGeneratorArgs
def self.generate(opts = {})
id = opts[:resource_owner_id] || opts[:resource_owner]&.id
"custom_generator_token_#{id}"
end
end
Doorkeeper.configure do
orm DOORKEEPER_ORM
access_token_generator "CustomGeneratorArgs"
end
owner = FactoryBot.create :resource_owner
token = FactoryBot.create :access_token,
resource_owner_id: owner.id,
resource_owner_type: owner.class.name
expect(token.token).to match(/custom_generator_token_\d+/)
end
it "allows the custom generator to access the application details" do
eigenclass = class << CustomGeneratorArgs; self; end
eigenclass.class_eval do
remove_method :generate
end
module CustomGeneratorArgs
def self.generate(opts = {})
"custom_generator_token_#{opts[:application].name}"
end
end
Doorkeeper.configure do
orm DOORKEEPER_ORM
access_token_generator "CustomGeneratorArgs"
end
token = FactoryBot.create :access_token
expect(token.token).to match(/custom_generator_token_Application \d+/)
end
it "allows the custom generator to access the scopes" do
eigenclass = class << CustomGeneratorArgs; self; end
eigenclass.class_eval do
remove_method :generate
end
module CustomGeneratorArgs
def self.generate(opts = {})
"custom_generator_token_#{opts[:scopes].count}_#{opts[:scopes]}"
end
end
Doorkeeper.configure do
orm DOORKEEPER_ORM
access_token_generator "CustomGeneratorArgs"
end
token = FactoryBot.create :access_token, scopes: "public write"
expect(token.token).to eq "custom_generator_token_2_public write"
end
it "allows the custom generator to access the expiry length" do
eigenclass = class << CustomGeneratorArgs; self; end
eigenclass.class_eval do
remove_method :generate
end
module CustomGeneratorArgs
def self.generate(opts = {})
"custom_generator_token_#{opts[:expires_in]}"
end
end
Doorkeeper.configure do
orm DOORKEEPER_ORM
access_token_generator "CustomGeneratorArgs"
end
token = FactoryBot.create :access_token
expect(token.token).to eq "custom_generator_token_7200"
end
it "allows the custom generator to access the created time" do
module CustomGeneratorArgs
def self.generate(opts = {})
"custom_generator_token_#{opts[:created_at].to_i}"
end
end
Doorkeeper.configure do
orm DOORKEEPER_ORM
access_token_generator "CustomGeneratorArgs"
end
token = FactoryBot.create :access_token
created_at = token.created_at
expect(token.token).to eq "custom_generator_token_#{created_at.to_i}"
end
it "allows the custom generator to access the custom attributes" do
module CustomGeneratorArgs
def self.generate(opts = {})
"custom_generator_token_#{opts[:tenant_name]}"
end
end
Doorkeeper.configure do
orm DOORKEEPER_ORM
access_token_generator "CustomGeneratorArgs"
custom_access_token_attributes [:tenant_name]
end
token = FactoryBot.create :access_token, tenant_name: "Tenant 1"
expect(token.token).to eq "custom_generator_token_Tenant 1"
end
it "raises an error if the custom object does not support generate" do
module NoGenerate
end
Doorkeeper.configure do
orm DOORKEEPER_ORM
access_token_generator "NoGenerate"
end
expect { FactoryBot.create :access_token }.to(
raise_error(Doorkeeper::Errors::UnableToGenerateToken),
)
end
it "raises original error if something went wrong in custom generator" do
eigenclass = class << CustomGeneratorArgs; self; end
eigenclass.class_eval do
remove_method :generate
end
module CustomGeneratorArgs
def self.generate(_opts = {})
raise LoadError, "custom behaviour"
end
end
Doorkeeper.configure do
orm DOORKEEPER_ORM
access_token_generator "CustomGeneratorArgs"
end
expect { FactoryBot.create :access_token }.to(
raise_error(LoadError),
)
end
it "raises an error if the custom object does not exist" do
Doorkeeper.configure do
orm DOORKEEPER_ORM
access_token_generator "Doorkeeper::NotReal"
end
expect { FactoryBot.create :access_token }.to(
raise_error(Doorkeeper::Errors::TokenGeneratorNotFound, /NotReal/),
)
end
end
describe "refresh_token" do
it "has empty refresh token if it was not required" do
token = FactoryBot.create :access_token
expect(token.refresh_token).to be_nil
end
it "generates a refresh token if it was requested" do
token = FactoryBot.create :access_token, use_refresh_token: true
expect(token.refresh_token).not_to be_nil
end
it "is not valid if token exists" do
token1 = FactoryBot.create :access_token, use_refresh_token: true
token2 = FactoryBot.create :access_token, use_refresh_token: true
token2.refresh_token = token1.refresh_token
expect(token2).not_to be_valid
end
it "expects database to raise an error if refresh tokens are the same" do
token1 = FactoryBot.create :access_token, use_refresh_token: true
token2 = FactoryBot.create :access_token, use_refresh_token: true
expect do
token2.refresh_token = token1.refresh_token
token2.save(validate: false)
end.to raise_error(uniqueness_error)
end
context "with hashing enabled" do
include_context "with token hashing enabled"
let(:token) { FactoryBot.create :access_token, use_refresh_token: true }
it "holds a volatile refresh token when created" do
expect(token.plaintext_refresh_token).to be_a(String)
expect(token.refresh_token)
.to eq(hashed_or_plain_token_func.call(token.plaintext_refresh_token))
# Finder method only finds the hashed token
loaded = described_class.find_by(refresh_token: token.refresh_token)
expect(loaded).to eq(token)
expect(loaded.plaintext_refresh_token).to be_nil
expect(loaded.refresh_token).to eq(token.refresh_token)
end
it "does not find_by plain text refresh tokens" do
expect(described_class.find_by(refresh_token: token.plaintext_refresh_token)).to be_nil
end
describe "with having a plain text token" do
let(:plain_refresh_token) { "plain refresh token" }
let(:access_token) { FactoryBot.create :access_token }
before do
# Assume we have a plain text token from before activating the option
access_token.update_column(:refresh_token, plain_refresh_token)
end
context "without fallback lookup" do
it "does not provide lookups with either through by_token" do
expect(described_class.by_refresh_token(plain_refresh_token)).to eq(nil)
expect(described_class.by_refresh_token(access_token.refresh_token)).to eq(nil)
# And it does not touch the token
access_token.reload
expect(access_token.refresh_token).to eq(plain_refresh_token)
end
end
context "with fallback lookup" do
include_context "with token hashing and fallback lookup enabled"
it "upgrades a plain token when falling back to it" do
# Side-effect: This will automatically upgrade the token
expect(described_class).to receive(:upgrade_fallback_value).and_call_original
expect(described_class.by_refresh_token(plain_refresh_token))
.to have_attributes(
token: access_token.token,
resource_owner_id: access_token.resource_owner_id,
application_id: access_token.application_id,
)
# Will find subsequently by hashing the token
expect(described_class.by_refresh_token(plain_refresh_token))
.to have_attributes(
token: access_token.token,
resource_owner_id: access_token.resource_owner_id,
application_id: access_token.application_id,
)
# Not all the ORM support :id PK
if access_token.respond_to?(:id)
expect(described_class.by_refresh_token(plain_refresh_token).id).to eq(access_token.id)
end
# And it modifies the token value
access_token.reload
expect(access_token.refresh_token).not_to eq(plain_refresh_token)
expect(described_class.find_by(refresh_token: plain_refresh_token)).to eq(nil)
expect(described_class.find_by(refresh_token: access_token.refresh_token)).not_to be_nil
end
end
end
end
end
describe "validations" do
it "is valid without resource_owner_id" do
# For client credentials flow
access_token.resource_owner_id = nil
expect(access_token).to be_valid
end
it "is valid without application_id" do
# For resource owner credentials flow
access_token.application_id = nil
expect(access_token).to be_valid
end
end
describe "#same_credential?" do
context "with default parameters" do
let(:resource_owner) { FactoryBot.create(:resource_owner) }
let(:resource_owner_id) { resource_owner.id }
let(:application) { FactoryBot.create :application }
let(:default_attributes) do
{
application: application,
resource_owner_id: resource_owner_id,
resource_owner_type: resource_owner.class.name,
}
end
let(:access_token1) { FactoryBot.create :access_token, default_attributes }
context "when the second token has the same owner and same app" do
let(:access_token2) { FactoryBot.create :access_token, default_attributes }
it "success" do
expect(access_token1).to be_same_credential(access_token2)
end
end
context "when the second token has same owner and different app" do
let(:other_application) { FactoryBot.create :application }
let(:access_token2) do
FactoryBot.create :access_token,
application: other_application,
resource_owner_id: resource_owner_id,
resource_owner_type: resource_owner.class.name
end
it "fails" do
expect(access_token1).not_to be_same_credential(access_token2)
end
end
context "when the second token has different owner and different app" do
let(:other_application) { FactoryBot.create :application }
let(:access_token2) do
FactoryBot.create :access_token,
application: other_application,
resource_owner_id: resource_owner.id + 1
end
it "fails" do
expect(access_token1).not_to be_same_credential(access_token2)
end
end
context "when the second token has different owner and same app" do
let(:access_token2) do
FactoryBot.create :access_token,
application: application,
resource_owner_id: resource_owner.id + 1
end
it "fails" do
expect(access_token1).not_to be_same_credential(access_token2)
end
end
end
end
describe "#acceptable?" do
context "when token is not accessible" do
let(:token) { FactoryBot.create(:access_token, created_at: 6.hours.ago) }
it "returns false" do
expect(token.acceptable?(nil)).to be false
end
end
context "when token has the incorrect scopes" do
let(:token) { FactoryBot.create(:access_token) }
it "returns false" do
expect(token.acceptable?(["public"])).to be false
end
end
context "when token is acceptable with the correct scopes" do
let(:token) do
token = FactoryBot.create(:access_token)
token[:scopes] = "public"
token
end
it "returns true" do
expect(token.acceptable?(["public"])).to be true
end
end
end
describe ".revoke_all_for" do
let(:resource_owner) { FactoryBot.create :resource_owner }
let(:application) { FactoryBot.create :application }
let(:default_attributes) do
{
application: application,
resource_owner_id: resource_owner.id,
resource_owner_type: resource_owner.class.name,
}
end
it "revokes all tokens for given application and resource owner" do
FactoryBot.create :access_token, default_attributes
described_class.revoke_all_for(application.id, resource_owner)
expect(described_class.all).to all(be_revoked)
end
it "matches application" do
access_token_for_different_app = FactoryBot.create(
:access_token,
default_attributes.merge(application: FactoryBot.create(:application)),
)
described_class.revoke_all_for(application.id, resource_owner)
expect(access_token_for_different_app.reload).not_to be_revoked
end
it "matches resource owner" do
access_token_for_different_owner = FactoryBot.create(
:access_token,
default_attributes.merge(resource_owner_id: resource_owner.id + 1),
)
described_class.revoke_all_for(application.id, resource_owner)
expect(access_token_for_different_owner.reload).not_to be_revoked
end
end
describe ".matching_token_for" do
let(:resource_owner) { FactoryBot.create :resource_owner }
let(:resource_owner_id) { resource_owner.id }
let(:application) { FactoryBot.create :application }
let(:scopes) { Doorkeeper::OAuth::Scopes.from_string("public write") }
let(:default_attributes) do
{
application: application,
resource_owner_id: resource_owner_id,
resource_owner_type: resource_owner.class.name,
scopes: scopes.to_s,
}
end
before do
default_scopes_exist(*scopes.all)
end
it "returns only one token" do
token = FactoryBot.create :access_token, default_attributes
last_token = described_class.matching_token_for(application, resource_owner, scopes)
expect(last_token).to eq(token)
end
it "accepts nil as resource owner" do
token = FactoryBot.create :access_token,
default_attributes.merge(resource_owner_id: nil, resource_owner_type: nil)
last_token = described_class.matching_token_for(application, nil, scopes)
expect(last_token).to eq(token)
end
it "excludes revoked tokens" do
FactoryBot.create :access_token, default_attributes.merge(revoked_at: 1.day.ago)
last_token = described_class.matching_token_for(application, resource_owner_id, scopes)
expect(last_token).to be_nil
end
it "excludes tokens with a different application" do
FactoryBot.create :access_token, default_attributes.merge(application: FactoryBot.create(:application))
last_token = described_class.matching_token_for(application, resource_owner_id, scopes)
expect(last_token).to be_nil
end
it "excludes tokens with a different resource owner" do
FactoryBot.create :access_token, default_attributes.merge(resource_owner_id: resource_owner.id + 1)
last_token = described_class.matching_token_for(application, resource_owner_id, scopes)
expect(last_token).to be_nil
end
it "excludes tokens with fewer scopes" do
FactoryBot.create :access_token, default_attributes.merge(scopes: "public")
last_token = described_class.matching_token_for(application, resource_owner_id, scopes)
expect(last_token).to be_nil
end
it "excludes tokens with different scopes" do
FactoryBot.create :access_token, default_attributes.merge(scopes: "public email")
last_token = described_class.matching_token_for(application, resource_owner, scopes)
expect(last_token).to be_nil
end
it "excludes tokens with additional scopes" do
FactoryBot.create :access_token, default_attributes.merge(scopes: "public write email")
last_token = described_class.matching_token_for(application, resource_owner, scopes)
expect(last_token).to be_nil
end
it "excludes tokens with scopes that are not present in server scopes" do
FactoryBot.create :access_token, default_attributes.merge(
application: application, scopes: "public read",
)
last_token = described_class.matching_token_for(application, resource_owner, scopes)
expect(last_token).to be_nil
end
it "excludes tokens with scopes that are not present in application scopes" do
application = FactoryBot.create :application, scopes: "private read"
FactoryBot.create :access_token, default_attributes.merge(
application: application,
)
last_token = described_class.matching_token_for(application, resource_owner, scopes)
expect(last_token).to be_nil
end
it "does not match token if empty scope requested and token/app scopes present" do
application = FactoryBot.create :application, scopes: "sample:scope"
app_params = {
application_id: application.id, scopes: "sample:scope",
resource_owner_id: resource_owner.id,
resource_owner_type: resource_owner.class.name,
}
FactoryBot.create :access_token, app_params
empty_scopes = Doorkeeper::OAuth::Scopes.from_string("")
last_token = described_class.matching_token_for(application, resource_owner.id, empty_scopes)
expect(last_token).to be_nil
end
it "matches token if empty scope requested and no token scopes present" do
empty_scopes = Doorkeeper::OAuth::Scopes.from_string("")
token = FactoryBot.create :access_token, default_attributes.merge(scopes: empty_scopes)
last_token = described_class.matching_token_for(application, resource_owner.id, empty_scopes)
expect(last_token).to eq(token)
end
it "returns the last matching token" do
FactoryBot.create :access_token, default_attributes.merge(created_at: 1.day.ago)
matching_token = FactoryBot.create :access_token, default_attributes
FactoryBot.create :access_token, default_attributes.merge(scopes: "public")
last_token = described_class.matching_token_for(application, resource_owner_id, scopes)
expect(last_token).to eq(matching_token)
end
context "when custom access token attributes are used" do
before do
Doorkeeper.configure do
orm DOORKEEPER_ORM
custom_access_token_attributes [:tenant_name]
end
default_scopes_exist(*scopes.all)
end
let(:custom_attributes) { { tenant_name: "Me" } }
it "returns a token when attributes match" do
token = FactoryBot.create :access_token, default_attributes.merge(custom_attributes)
last_token = described_class.matching_token_for(
application, resource_owner, scopes, custom_attributes: custom_attributes)
expect(last_token).to eq(token)
end
it "does not return a token if attributes don't match" do
token = FactoryBot.create :access_token, default_attributes.merge(custom_attributes)
last_token = described_class.matching_token_for(application, resource_owner, scopes, custom_attributes: { tenant_id: 'different' })
expect(last_token).to eq(nil)
end
it "ignores custom attributes if a nil value is passed" do
token = FactoryBot.create :access_token, default_attributes.merge(custom_attributes)
last_token = described_class.matching_token_for(application, resource_owner, scopes, custom_attributes: nil)
expect(last_token).to eq(token)
end
end
end
describe "#as_json" do
let(:token) { FactoryBot.create(:access_token) }
let(:token_hash) do
{
resource_owner_id: token.resource_owner_id,
scope: token.scopes,
expires_in: token.expires_in_seconds,
application: { uid: token.application.uid },
created_at: token.created_at.to_i,
}
end
it "returns as_json hash" do
hash = token_hash
if Doorkeeper.configuration.polymorphic_resource_owner?
hash[:resource_owner_type] = token.resource_owner_type
end
expect(token.as_json).to match(hash)
end
describe "#not_expired" do
let(:resource_owner) { FactoryBot.create(:doorkeeper_testing_user) }
let(:application) { FactoryBot.create(:application) }
let(:attrs) { { resource_owner_id: resource_owner.id, application_id: application.id } }
let!(:active_token1) { FactoryBot.create(:access_token, attrs.merge(expires_in: 2000)) }
let!(:active_token2) { FactoryBot.create(:access_token, attrs.merge(expires_in: 2)) }
let!(:active_token3) { FactoryBot.create(:access_token, attrs.merge(expires_in: 10, created_at: Time.current - 5.seconds)) }
let!(:active_token4) { FactoryBot.create(:access_token, attrs.merge(expires_in: nil)) }
let!(:not_active_token1) { FactoryBot.create(:access_token, attrs.merge(expires_in: 2, created_at: Time.current - 2.seconds)) }
let!(:not_active_token2) { FactoryBot.create(:access_token, attrs.merge(expires_in: 10, created_at: Time.current - 12.seconds)) }
let!(:not_active_token3) { FactoryBot.create(:access_token, attrs.merge(expires_in: 10_000, revoked_at: Time.current)) }
before do
Timecop.freeze(Time.current)
end
after do
Timecop.return
end
it "returns only non expired tokens" do
expired_tokens = described_class.not_expired
expect(expired_tokens.size).to be(4)
expect(expired_tokens).to match_array([active_token1, active_token2, active_token3, active_token4])
end
end
end
end