forked from entp/ruby-openid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_server.rb
2455 lines (2134 loc) · 85.7 KB
/
test_server.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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require "test_helper"
require 'openid/server'
require 'openid/cryptutil'
require 'openid/association'
require 'openid/util'
require 'openid/message'
require 'openid/store/memory'
require 'openid/dh'
require 'openid/consumer/associationmanager'
require 'support/test_util'
require 'uri'
# In general, if you edit or add tests here, try to move in the
# direction of testing smaller units. For testing the external
# interfaces, we'll be developing an implementation-agnostic testing
# suite.
# for more, see /etc/ssh/moduli
module OpenID
ALT_MODULUS = 0xCAADDDEC1667FC68B5FA15D53C4E1532DD24561A1A2D47A12C01ABEA1E00731F6921AAC40742311FDF9E634BB7131BEE1AF240261554389A910425E044E88C8359B010F5AD2B80E29CB1A5B027B19D9E01A6F63A6F45E5D7ED2FF6A2A0085050A7D0CF307C3DB51D2490355907B4427C23A98DF1EB8ABEF2BA209BB7AFFE86A7
ALT_GEN = 5
class CatchLogs
def catchlogs_setup
@old_logger = Util.logger
Util.logger = self.method('got_log_message')
@messages = []
end
def got_log_message(message)
@messages << message
end
def teardown
Util.logger = @old_logger
end
end
class TestProtocolError < Test::Unit::TestCase
def test_browserWithReturnTo
return_to = "http://rp.unittest/consumer"
# will be a ProtocolError raised by Decode or
# CheckIDRequest.answer
args = Message.from_post_args({
'openid.mode' => 'monkeydance',
'openid.identity' => 'http://wagu.unittest/',
'openid.return_to' => return_to,
})
e = Server::ProtocolError.new(args, "plucky")
assert(e.has_return_to)
expected_args = {
'openid.mode' => 'error',
'openid.error' => 'plucky',
}
rt_base, result_args = e.encode_to_url.split('?', 2)
result_args = Util.parse_query(result_args)
assert_equal(result_args, expected_args)
end
def test_browserWithReturnTo_OpenID2_GET
return_to = "http://rp.unittest/consumer"
# will be a ProtocolError raised by Decode or
# CheckIDRequest.answer
args = Message.from_post_args({
'openid.ns' => OPENID2_NS,
'openid.mode' => 'monkeydance',
'openid.identity' => 'http://wagu.unittest/',
'openid.claimed_id' => 'http://wagu.unittest/',
'openid.return_to' => return_to,
})
e = Server::ProtocolError.new(args, "plucky")
assert(e.has_return_to)
expected_args = {
'openid.ns' => OPENID2_NS,
'openid.mode' => 'error',
'openid.error' => 'plucky',
}
rt_base, result_args = e.encode_to_url.split('?', 2)
result_args = Util.parse_query(result_args)
assert_equal(result_args, expected_args)
end
def test_browserWithReturnTo_OpenID2_POST
return_to = "http://rp.unittest/consumer" + ('x' * OPENID1_URL_LIMIT)
# will be a ProtocolError raised by Decode or
# CheckIDRequest.answer
args = Message.from_post_args({
'openid.ns' => OPENID2_NS,
'openid.mode' => 'monkeydance',
'openid.identity' => 'http://wagu.unittest/',
'openid.claimed_id' => 'http://wagu.unittest/',
'openid.return_to' => return_to,
})
e = Server::ProtocolError.new(args, "plucky")
assert(e.has_return_to)
expected_args = {
'openid.ns' => OPENID2_NS,
'openid.mode' => 'error',
'openid.error' => 'plucky',
}
assert(e.which_encoding == Server::ENCODE_HTML_FORM)
assert(e.to_form_markup == e.to_message.to_form_markup(
args.get_arg(OPENID_NS, 'return_to')))
end
def test_browserWithReturnTo_OpenID1_exceeds_limit
return_to = "http://rp.unittest/consumer" + ('x' * OPENID1_URL_LIMIT)
# will be a ProtocolError raised by Decode or
# CheckIDRequest.answer
args = Message.from_post_args({
'openid.mode' => 'monkeydance',
'openid.identity' => 'http://wagu.unittest/',
'openid.return_to' => return_to,
})
e = Server::ProtocolError.new(args, "plucky")
assert(e.has_return_to)
expected_args = {
'openid.mode' => 'error',
'openid.error' => 'plucky',
}
assert(e.which_encoding == Server::ENCODE_URL)
rt_base, result_args = e.encode_to_url.split('?', 2)
result_args = Util.parse_query(result_args)
assert_equal(result_args, expected_args)
end
def test_noReturnTo
# will be a ProtocolError raised by Decode or
# CheckIDRequest.answer
args = Message.from_post_args({
'openid.mode' => 'zebradance',
'openid.identity' => 'http://wagu.unittest/',
})
e = Server::ProtocolError.new(args, "waffles")
assert(!e.has_return_to)
expected = "error:waffles\nmode:error\n"
assert_equal(e.encode_to_kvform, expected)
end
def test_no_message
e = Server::ProtocolError.new(nil, "no message")
assert(e.get_return_to.nil?)
assert_equal(e.which_encoding, nil)
end
def test_which_encoding_no_message
e = Server::ProtocolError.new(nil, "no message")
assert(e.which_encoding.nil?)
end
end
class TestDecode < Test::Unit::TestCase
def setup
@claimed_id = 'http://de.legating.de.coder.unittest/'
@id_url = "http://decoder.am.unittest/"
@rt_url = "http://rp.unittest/foobot/?qux=zam"
@tr_url = "http://rp.unittest/"
@assoc_handle = "{assoc}{handle}"
@op_endpoint = 'http://endpoint.unittest/encode'
@store = Store::Memory.new()
@server = Server::Server.new(@store, @op_endpoint)
@decode = Server::Decoder.new(@server).method('decode')
end
def test_none
args = {}
r = @decode.call(args)
assert_equal(r, nil)
end
def test_irrelevant
args = {
'pony' => 'spotted',
'sreg.mutant_power' => 'decaffinator',
}
assert_raise(Server::ProtocolError) {
@decode.call(args)
}
end
def test_bad
args = {
'openid.mode' => 'twos-compliment',
'openid.pants' => 'zippered',
}
assert_raise(Server::ProtocolError) {
@decode.call(args)
}
end
def test_dictOfLists
args = {
'openid.mode' => ['checkid_setup'],
'openid.identity' => @id_url,
'openid.assoc_handle' => @assoc_handle,
'openid.return_to' => @rt_url,
'openid.trust_root' => @tr_url,
}
begin
result = @decode.call(args)
rescue ArgumentError => err
assert(!err.to_s.index('values').nil?, err.to_s)
else
flunk("Expected ArgumentError, but got result #{result}")
end
end
def test_checkidImmediate
args = {
'openid.mode' => 'checkid_immediate',
'openid.identity' => @id_url,
'openid.assoc_handle' => @assoc_handle,
'openid.return_to' => @rt_url,
'openid.trust_root' => @tr_url,
# should be ignored
'openid.some.extension' => 'junk',
}
r = @decode.call(args)
assert(r.is_a?(Server::CheckIDRequest))
assert_equal(r.mode, "checkid_immediate")
assert_equal(r.immediate, true)
assert_equal(r.identity, @id_url)
assert_equal(r.trust_root, @tr_url)
assert_equal(r.return_to, @rt_url)
assert_equal(r.assoc_handle, @assoc_handle)
end
def test_checkidImmediate_constructor
r = Server::CheckIDRequest.new(@id_url, @rt_url, nil,
@rt_url, true, @assoc_handle)
assert(r.mode == 'checkid_immediate')
assert(r.immediate)
end
def test_checkid_missing_return_to_and_trust_root
args = {
'openid.ns' => OPENID2_NS,
'openid.mode' => 'checkid_setup',
'openid.identity' => @id_url,
'openid.claimed_id' => @id_url,
'openid.assoc_handle' => @assoc_handle,
}
assert_raise(Server::ProtocolError) {
m = Message.from_post_args(args)
Server::CheckIDRequest.from_message(m, @op_endpoint)
}
end
def test_checkid_id_select
args = {
'openid.ns' => OPENID2_NS,
'openid.mode' => 'checkid_setup',
'openid.identity' => IDENTIFIER_SELECT,
'openid.claimed_id' => IDENTIFIER_SELECT,
'openid.assoc_handle' => @assoc_handle,
'openid.return_to' => @rt_url,
'openid.realm' => @tr_url,
}
m = Message.from_post_args(args)
req = Server::CheckIDRequest.from_message(m, @op_endpoint)
assert(req.id_select)
end
def test_checkid_not_id_select
args = {
'openid.ns' => OPENID2_NS,
'openid.mode' => 'checkid_setup',
'openid.assoc_handle' => @assoc_handle,
'openid.return_to' => @rt_url,
'openid.realm' => @tr_url,
}
id_args = [
{'openid.claimed_id' => IDENTIFIER_SELECT,
'openid.identity' => 'http://bogus.com/'},
{'openid.claimed_id' => 'http://bogus.com/',
'openid.identity' => 'http://bogus.com/'},
]
id_args.each { |id|
m = Message.from_post_args(args.merge(id))
req = Server::CheckIDRequest.from_message(m, @op_endpoint)
assert(!req.id_select)
}
end
def test_checkidSetup
args = {
'openid.mode' => 'checkid_setup',
'openid.identity' => @id_url,
'openid.assoc_handle' => @assoc_handle,
'openid.return_to' => @rt_url,
'openid.trust_root' => @tr_url,
}
r = @decode.call(args)
assert(r.is_a?(Server::CheckIDRequest))
assert_equal(r.mode, "checkid_setup")
assert_equal(r.immediate, false)
assert_equal(r.identity, @id_url)
assert_equal(r.trust_root, @tr_url)
assert_equal(r.return_to, @rt_url)
end
def test_checkidSetupOpenID2
args = {
'openid.ns' => OPENID2_NS,
'openid.mode' => 'checkid_setup',
'openid.identity' => @id_url,
'openid.claimed_id' => @claimed_id,
'openid.assoc_handle' => @assoc_handle,
'openid.return_to' => @rt_url,
'openid.realm' => @tr_url,
}
r = @decode.call(args)
assert(r.is_a?(Server::CheckIDRequest))
assert_equal(r.mode, "checkid_setup")
assert_equal(r.immediate, false)
assert_equal(r.identity, @id_url)
assert_equal(r.claimed_id, @claimed_id)
assert_equal(r.trust_root, @tr_url)
assert_equal(r.return_to, @rt_url)
end
def test_checkidSetupNoClaimedIDOpenID2
args = {
'openid.ns' => OPENID2_NS,
'openid.mode' => 'checkid_setup',
'openid.identity' => @id_url,
'openid.assoc_handle' => @assoc_handle,
'openid.return_to' => @rt_url,
'openid.realm' => @tr_url,
}
assert_raise(Server::ProtocolError) {
@decode.call(args)
}
end
def test_checkidSetupNoIdentityOpenID2
args = {
'openid.ns' => OPENID2_NS,
'openid.mode' => 'checkid_setup',
'openid.assoc_handle' => @assoc_handle,
'openid.return_to' => @rt_url,
'openid.realm' => @tr_url,
}
r = @decode.call(args)
assert(r.is_a?(Server::CheckIDRequest))
assert_equal(r.mode, "checkid_setup")
assert_equal(r.immediate, false)
assert_equal(r.identity, nil)
assert_equal(r.trust_root, @tr_url)
assert_equal(r.return_to, @rt_url)
end
def test_checkidSetupNoReturnOpenID1
# Make sure an OpenID 1 request cannot be decoded if it lacks a
# return_to.
args = {
'openid.mode' => 'checkid_setup',
'openid.identity' => @id_url,
'openid.assoc_handle' => @assoc_handle,
'openid.trust_root' => @tr_url,
}
assert_raise(Server::ProtocolError) {
@decode.call(args)
}
end
def test_checkidSetupNoReturnOpenID2
# Make sure an OpenID 2 request with no return_to can be decoded,
# and make sure a response to such a request raises
# NoReturnToError.
args = {
'openid.ns' => OPENID2_NS,
'openid.mode' => 'checkid_setup',
'openid.identity' => @id_url,
'openid.claimed_id' => @id_url,
'openid.assoc_handle' => @assoc_handle,
'openid.realm' => @tr_url,
}
req = @decode.call(args)
assert(req.is_a?(Server::CheckIDRequest))
assert_raise(Server::NoReturnToError) {
req.answer(false)
}
assert_raise(Server::NoReturnToError) {
req.encode_to_url('bogus')
}
assert_raise(Server::NoReturnToError) {
req.cancel_url
}
end
def test_checkidSetupRealmRequiredOpenID2
# Make sure that an OpenID 2 request which lacks return_to cannot
# be decoded if it lacks a realm. Spec => This value
# (openid.realm) MUST be sent if openid.return_to is omitted.
args = {
'openid.ns' => OPENID2_NS,
'openid.mode' => 'checkid_setup',
'openid.identity' => @id_url,
'openid.assoc_handle' => @assoc_handle,
}
assert_raise(Server::ProtocolError) {
@decode.call(args)
}
end
def test_checkidSetupBadReturn
args = {
'openid.mode' => 'checkid_setup',
'openid.identity' => @id_url,
'openid.assoc_handle' => @assoc_handle,
'openid.return_to' => 'not a url',
}
begin
result = @decode.call(args)
rescue Server::ProtocolError => err
assert(err.openid_message)
else
flunk("Expected ProtocolError, instead returned with #{result}")
end
end
def test_checkidSetupUntrustedReturn
args = {
'openid.mode' => 'checkid_setup',
'openid.identity' => @id_url,
'openid.assoc_handle' => @assoc_handle,
'openid.return_to' => @rt_url,
'openid.trust_root' => 'http://not-the-return-place.unittest/',
}
begin
result = @decode.call(args)
rescue Server::UntrustedReturnURL => err
assert(err.openid_message, err.to_s)
else
flunk("Expected UntrustedReturnURL, instead returned with #{result}")
end
end
def test_checkidSetupUntrustedReturn_Constructor
assert_raise(Server::UntrustedReturnURL) {
Server::CheckIDRequest.new(@id_url, @rt_url, nil,
'http://not-the-return-place.unittest/',
false, @assoc_handle)
}
end
def test_checkidSetupMalformedReturnURL_Constructor
assert_raise(Server::MalformedReturnURL) {
Server::CheckIDRequest.new(@id_url, 'bogus://return.url', nil,
'http://trustroot.com/',
false, @assoc_handle)
}
end
def test_checkAuth
args = {
'openid.mode' => 'check_authentication',
'openid.assoc_handle' => '{dumb}{handle}',
'openid.sig' => 'sigblob',
'openid.signed' => 'identity,return_to,response_nonce,mode',
'openid.identity' => 'signedval1',
'openid.return_to' => 'signedval2',
'openid.response_nonce' => 'signedval3',
'openid.baz' => 'unsigned',
}
r = @decode.call(args)
assert(r.is_a?(Server::CheckAuthRequest))
assert_equal(r.mode, 'check_authentication')
assert_equal(r.sig, 'sigblob')
end
def test_checkAuthMissingSignature
args = {
'openid.mode' => 'check_authentication',
'openid.assoc_handle' => '{dumb}{handle}',
'openid.signed' => 'foo,bar,mode',
'openid.foo' => 'signedval1',
'openid.bar' => 'signedval2',
'openid.baz' => 'unsigned',
}
assert_raise(Server::ProtocolError) {
@decode.call(args)
}
end
def test_checkAuthAndInvalidate
args = {
'openid.mode' => 'check_authentication',
'openid.assoc_handle' => '{dumb}{handle}',
'openid.invalidate_handle' => '[[SMART_handle]]',
'openid.sig' => 'sigblob',
'openid.signed' => 'identity,return_to,response_nonce,mode',
'openid.identity' => 'signedval1',
'openid.return_to' => 'signedval2',
'openid.response_nonce' => 'signedval3',
'openid.baz' => 'unsigned',
}
r = @decode.call(args)
assert(r.is_a?(Server::CheckAuthRequest))
assert_equal(r.invalidate_handle, '[[SMART_handle]]')
end
def test_associateDH
args = {
'openid.mode' => 'associate',
'openid.session_type' => 'DH-SHA1',
'openid.dh_consumer_public' => "Rzup9265tw==",
}
r = @decode.call(args)
assert(r.is_a?(Server::AssociateRequest))
assert_equal(r.mode, "associate")
assert_equal(r.session.session_type, "DH-SHA1")
assert_equal(r.assoc_type, "HMAC-SHA1")
assert(r.session.consumer_pubkey)
end
def test_associateDHMissingKey
# Trying DH assoc w/o public key
args = {
'openid.mode' => 'associate',
'openid.session_type' => 'DH-SHA1',
}
# Using DH-SHA1 without supplying dh_consumer_public is an error.
assert_raise(Server::ProtocolError) {
@decode.call(args)
}
end
def test_associateDHpubKeyNotB64
args = {
'openid.mode' => 'associate',
'openid.session_type' => 'DH-SHA1',
'openid.dh_consumer_public' => "donkeydonkeydonkey",
}
assert_raise(Server::ProtocolError) {
@decode.call(args)
}
end
def test_associateDHModGen
# test dh with non-default but valid values for dh_modulus and
# dh_gen
args = {
'openid.mode' => 'associate',
'openid.session_type' => 'DH-SHA1',
'openid.dh_consumer_public' => "Rzup9265tw==",
'openid.dh_modulus' => CryptUtil.num_to_base64(ALT_MODULUS),
'openid.dh_gen' => CryptUtil.num_to_base64(ALT_GEN) ,
}
r = @decode.call(args)
assert(r.is_a?(Server::AssociateRequest))
assert_equal(r.mode, "associate")
assert_equal(r.session.session_type, "DH-SHA1")
assert_equal(r.assoc_type, "HMAC-SHA1")
assert_equal(r.session.dh.modulus, ALT_MODULUS)
assert_equal(r.session.dh.generator, ALT_GEN)
assert(r.session.consumer_pubkey)
end
def test_associateDHCorruptModGen
# test dh with non-default but valid values for dh_modulus and
# dh_gen
args = {
'openid.mode' => 'associate',
'openid.session_type' => 'DH-SHA1',
'openid.dh_consumer_public' => "Rzup9265tw==",
'openid.dh_modulus' => 'pizza',
'openid.dh_gen' => 'gnocchi',
}
assert_raise(Server::ProtocolError) {
@decode.call(args)
}
end
def test_associateDHMissingGen
args = {
'openid.mode' => 'associate',
'openid.session_type' => 'DH-SHA1',
'openid.dh_consumer_public' => "Rzup9265tw==",
'openid.dh_modulus' => 'pizza',
}
assert_raise(Server::ProtocolError) {
@decode.call(args)
}
end
def test_associateDHMissingMod
args = {
'openid.mode' => 'associate',
'openid.session_type' => 'DH-SHA1',
'openid.dh_consumer_public' => "Rzup9265tw==",
'openid.dh_gen' => 'pizza',
}
assert_raise(Server::ProtocolError) {
@decode.call(args)
}
end
# def test_associateDHInvalidModGen(self):
# # test dh with properly encoded values that are not a valid
# # modulus/generator combination.
# args = {
# 'openid.mode': 'associate',
# 'openid.session_type': 'DH-SHA1',
# 'openid.dh_consumer_public': "Rzup9265tw==",
# 'openid.dh_modulus': cryptutil.longToBase64(9),
# 'openid.dh_gen': cryptutil.longToBase64(27) ,
# }
# self.failUnlessRaises(server.ProtocolError, self.decode, args)
# test_associateDHInvalidModGen.todo = "low-priority feature"
def test_associateWeirdSession
args = {
'openid.mode' => 'associate',
'openid.session_type' => 'FLCL6',
'openid.dh_consumer_public' => "YQ==\n",
}
assert_raise(Server::ProtocolError) {
@decode.call(args)
}
end
def test_associatePlain
args = {
'openid.mode' => 'associate',
}
r = @decode.call(args)
assert(r.is_a?(Server::AssociateRequest))
assert_equal(r.mode, "associate")
assert_equal(r.session.session_type, "no-encryption")
assert_equal(r.assoc_type, "HMAC-SHA1")
end
def test_nomode
args = {
'openid.session_type' => 'DH-SHA1',
'openid.dh_consumer_public' => "my public keeey",
}
assert_raise(Server::ProtocolError) {
@decode.call(args)
}
end
def test_invalidns
args = {'openid.ns' => 'Vegetables',
'openid.mode' => 'associate'}
begin
r = @decode.call(args)
rescue Server::ProtocolError => err
assert(err.openid_message)
assert(err.to_s.index('Vegetables'))
end
end
end
class BogusEncoder < Server::Encoder
def encode(response)
return "BOGUS"
end
end
class BogusDecoder < Server::Decoder
def decode(query)
return "BOGUS"
end
end
class TestEncode < Test::Unit::TestCase
def setup
@encoder = Server::Encoder.new
@encode = @encoder.method('encode')
@op_endpoint = 'http://endpoint.unittest/encode'
@store = Store::Memory.new
@server = Server::Server.new(@store, @op_endpoint)
end
def test_id_res_OpenID2_GET
# Check that when an OpenID 2 response does not exceed the OpenID
# 1 message size, a GET response (i.e., redirect) is issued.
request = Server::CheckIDRequest.new(
'http://bombom.unittest/',
'http://burr.unittest/999',
@server.op_endpoint,
'http://burr.unittest/',
false,
nil)
request.message = Message.new(OPENID2_NS)
response = Server::OpenIDResponse.new(request)
response.fields = Message.from_openid_args({
'ns' => OPENID2_NS,
'mode' => 'id_res',
'identity' => request.identity,
'claimed_id' => request.identity,
'return_to' => request.return_to,
})
assert(!response.render_as_form)
assert(response.which_encoding == Server::ENCODE_URL)
webresponse = @encode.call(response)
assert(webresponse.headers.member?('location'))
end
def test_id_res_OpenID2_POST
# Check that when an OpenID 2 response exceeds the OpenID 1
# message size, a POST response (i.e., an HTML form) is returned.
request = Server::CheckIDRequest.new(
'http://bombom.unittest/',
'http://burr.unittest/999',
@server.op_endpoint,
'http://burr.unittest/',
false,
nil)
request.message = Message.new(OPENID2_NS)
response = Server::OpenIDResponse.new(request)
response.fields = Message.from_openid_args({
'ns' => OPENID2_NS,
'mode' => 'id_res',
'identity' => request.identity,
'claimed_id' => request.identity,
'return_to' => 'x' * OPENID1_URL_LIMIT,
})
assert(response.render_as_form)
assert(response.encode_to_url.length > OPENID1_URL_LIMIT)
assert(response.which_encoding == Server::ENCODE_HTML_FORM)
webresponse = @encode.call(response)
assert_equal(webresponse.body, response.to_form_markup)
end
def test_to_form_markup
request = Server::CheckIDRequest.new(
'http://bombom.unittest/',
'http://burr.unittest/999',
@server.op_endpoint,
'http://burr.unittest/',
false,
nil)
request.message = Message.new(OPENID2_NS)
response = Server::OpenIDResponse.new(request)
response.fields = Message.from_openid_args({
'ns' => OPENID2_NS,
'mode' => 'id_res',
'identity' => request.identity,
'claimed_id' => request.identity,
'return_to' => 'x' * OPENID1_URL_LIMIT,
})
form_markup = response.to_form_markup({'foo'=>'bar'})
assert(/ foo="bar"/ =~ form_markup, form_markup)
end
def test_to_html
request = Server::CheckIDRequest.new(
'http://bombom.unittest/',
'http://burr.unittest/999',
@server.op_endpoint,
'http://burr.unittest/',
false,
nil)
request.message = Message.new(OPENID2_NS)
response = Server::OpenIDResponse.new(request)
response.fields = Message.from_openid_args({
'ns' => OPENID2_NS,
'mode' => 'id_res',
'identity' => request.identity,
'claimed_id' => request.identity,
'return_to' => 'x' * OPENID1_URL_LIMIT,
})
html = response.to_html
assert(html)
end
def test_id_res_OpenID1_exceeds_limit
# Check that when an OpenID 1 response exceeds the OpenID 1
# message size, a GET response is issued. Technically, this
# shouldn't be permitted by the library, but this test is in place
# to preserve the status quo for OpenID 1.
request = Server::CheckIDRequest.new(
'http://bombom.unittest/',
'http://burr.unittest/999',
@server.op_endpoint,
'http://burr.unittest/',
false,
nil)
request.message = Message.new(OPENID1_NS)
response = Server::OpenIDResponse.new(request)
response.fields = Message.from_openid_args({
'mode' => 'id_res',
'identity' => request.identity,
'return_to' => 'x' * OPENID1_URL_LIMIT,
})
assert(!response.render_as_form)
assert(response.encode_to_url.length > OPENID1_URL_LIMIT)
assert(response.which_encoding == Server::ENCODE_URL)
webresponse = @encode.call(response)
assert_equal(webresponse.headers['location'], response.encode_to_url)
end
def test_id_res
request = Server::CheckIDRequest.new(
'http://bombom.unittest/',
'http://burr.unittest/999',
@server.op_endpoint,
'http://burr.unittest/',
false, nil)
request.message = Message.new(OPENID1_NS)
response = Server::OpenIDResponse.new(request)
response.fields = Message.from_openid_args({
'mode' => 'id_res',
'identity' => request.identity,
'return_to' => request.return_to,
})
webresponse = @encode.call(response)
assert_equal(webresponse.code, Server::HTTP_REDIRECT)
assert(webresponse.headers.member?('location'))
location = webresponse.headers['location']
assert(location.starts_with?(request.return_to),
sprintf("%s does not start with %s",
location, request.return_to))
# argh.
q2 = Util.parse_query(URI::parse(location).query)
expected = response.fields.to_post_args
assert_equal(q2, expected)
end
def test_cancel
request = Server::CheckIDRequest.new(
'http://bombom.unittest/',
'http://burr.unittest/999',
@server.op_endpoint,
'http://burr.unittest/',
false, nil)
request.message = Message.new(OPENID2_NS)
response = Server::OpenIDResponse.new(request)
response.fields = Message.from_openid_args({
'mode' => 'cancel',
})
webresponse = @encode.call(response)
assert_equal(webresponse.code, Server::HTTP_REDIRECT)
assert(webresponse.headers.member?('location'))
end
def test_cancel_to_form
request = Server::CheckIDRequest.new(
'http://bombom.unittest/',
'http://burr.unittest/999',
@server.op_endpoint,
'http://burr.unittest/',
false, nil)
request.message = Message.new(OPENID2_NS)
response = Server::OpenIDResponse.new(request)
response.fields = Message.from_openid_args({
'mode' => 'cancel',
})
form = response.to_form_markup
assert(form.index(request.return_to))
end
def test_assocReply
msg = Message.new(OPENID2_NS)
msg.set_arg(OPENID2_NS, 'session_type', 'no-encryption')
request = Server::AssociateRequest.from_message(msg)
response = Server::OpenIDResponse.new(request)
response.fields = Message.from_post_args(
{'openid.assoc_handle' => "every-zig"})
webresponse = @encode.call(response)
body = "assoc_handle:every-zig\n"
assert_equal(webresponse.code, Server::HTTP_OK)
assert_equal(webresponse.headers, {})
assert_equal(webresponse.body, body)
end
def test_checkauthReply
request = Server::CheckAuthRequest.new('a_sock_monkey',
'siggggg',
[])
request.message = Message.new(OPENID2_NS)
response = Server::OpenIDResponse.new(request)
response.fields = Message.from_openid_args({
'is_valid' => 'true',
'invalidate_handle' => 'xXxX:xXXx'
})
body = "invalidate_handle:xXxX:xXXx\nis_valid:true\n"
webresponse = @encode.call(response)
assert_equal(webresponse.code, Server::HTTP_OK)
assert_equal(webresponse.headers, {})
assert_equal(webresponse.body, body)
end
def test_unencodableError
args = Message.from_post_args({
'openid.identity' => 'http://limu.unittest/',
})
e = Server::ProtocolError.new(args, "wet paint")
assert_raise(Server::EncodingError) {
@encode.call(e)
}
end
def test_encodableError
args = Message.from_post_args({
'openid.mode' => 'associate',
'openid.identity' => 'http://limu.unittest/',
})
body="error:snoot\nmode:error\n"
webresponse = @encode.call(Server::ProtocolError.new(args, "snoot"))
assert_equal(webresponse.code, Server::HTTP_ERROR)
assert_equal(webresponse.headers, {})
assert_equal(webresponse.body, body)
end
end
class TestSigningEncode < Test::Unit::TestCase
def setup
@_dumb_key = Server::Signatory._dumb_key
@_normal_key = Server::Signatory._normal_key
@store = Store::Memory.new()
@server = Server::Server.new(@store, "http://signing.unittest/enc")
@request = Server::CheckIDRequest.new(
'http://bombom.unittest/',
'http://burr.unittest/999',
@server.op_endpoint,
'http://burr.unittest/',
false, nil)
@request.message = Message.new(OPENID2_NS)
@response = Server::OpenIDResponse.new(@request)
@response.fields = Message.from_openid_args({
'mode' => 'id_res',
'identity' => @request.identity,
'return_to' => @request.return_to,
})
@signatory = Server::Signatory.new(@store)
@encoder = Server::SigningEncoder.new(@signatory)
@encode = @encoder.method('encode')
end
def test_idres
assoc_handle = '{bicycle}{shed}'
@store.store_association(
@_normal_key,
Association.from_expires_in(60, assoc_handle,
'sekrit', 'HMAC-SHA1'))
@request.assoc_handle = assoc_handle
webresponse = @encode.call(@response)
assert_equal(webresponse.code, Server::HTTP_REDIRECT)
assert(webresponse.headers.member?('location'))
location = webresponse.headers['location']
query = Util.parse_query(URI::parse(location).query)
assert(query.member?('openid.sig'))
assert(query.member?('openid.assoc_handle'))
assert(query.member?('openid.signed'))
end
def test_idresDumb
webresponse = @encode.call(@response)
assert_equal(webresponse.code, Server::HTTP_REDIRECT)
assert(webresponse.headers.has_key?('location'))
location = webresponse.headers['location']
query = Util.parse_query(URI::parse(location).query)
assert(query.member?('openid.sig'))
assert(query.member?('openid.assoc_handle'))
assert(query.member?('openid.signed'))
end
def test_forgotStore
@encoder.signatory = nil
assert_raise(ArgumentError) {
@encode.call(@response)
}
end
def test_cancel
request = Server::CheckIDRequest.new(
'http://bombom.unittest/',
'http://burr.unittest/999',
@server.op_endpoint,
'http://burr.unittest/',
false, nil)