nov-ruby-openid 2.1.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (203) hide show
  1. data/CHANGELOG +215 -0
  2. data/CHANGES-2.1.0 +36 -0
  3. data/INSTALL +47 -0
  4. data/LICENSE +210 -0
  5. data/NOTICE +2 -0
  6. data/README +81 -0
  7. data/Rakefile +98 -0
  8. data/UPGRADE +127 -0
  9. data/VERSION +1 -0
  10. data/contrib/google/ruby-openid-apps-discovery-1.0.gem +0 -0
  11. data/contrib/google/ruby-openid-apps-discovery-1.01.gem +0 -0
  12. data/examples/README +32 -0
  13. data/examples/active_record_openid_store/README +58 -0
  14. data/examples/active_record_openid_store/XXX_add_open_id_store_to_db.rb +24 -0
  15. data/examples/active_record_openid_store/XXX_upgrade_open_id_store.rb +26 -0
  16. data/examples/active_record_openid_store/init.rb +8 -0
  17. data/examples/active_record_openid_store/lib/association.rb +10 -0
  18. data/examples/active_record_openid_store/lib/nonce.rb +3 -0
  19. data/examples/active_record_openid_store/lib/open_id_setting.rb +4 -0
  20. data/examples/active_record_openid_store/lib/openid_ar_store.rb +57 -0
  21. data/examples/active_record_openid_store/test/store_test.rb +212 -0
  22. data/examples/discover +49 -0
  23. data/examples/rails_openid/README +153 -0
  24. data/examples/rails_openid/Rakefile +10 -0
  25. data/examples/rails_openid/app/controllers/application.rb +4 -0
  26. data/examples/rails_openid/app/controllers/consumer_controller.rb +122 -0
  27. data/examples/rails_openid/app/controllers/login_controller.rb +45 -0
  28. data/examples/rails_openid/app/controllers/server_controller.rb +265 -0
  29. data/examples/rails_openid/app/helpers/application_helper.rb +3 -0
  30. data/examples/rails_openid/app/helpers/login_helper.rb +2 -0
  31. data/examples/rails_openid/app/helpers/server_helper.rb +9 -0
  32. data/examples/rails_openid/app/views/consumer/index.rhtml +81 -0
  33. data/examples/rails_openid/app/views/layouts/server.rhtml +68 -0
  34. data/examples/rails_openid/app/views/login/index.rhtml +56 -0
  35. data/examples/rails_openid/app/views/server/decide.rhtml +26 -0
  36. data/examples/rails_openid/config/boot.rb +19 -0
  37. data/examples/rails_openid/config/database.yml +74 -0
  38. data/examples/rails_openid/config/environment.rb +54 -0
  39. data/examples/rails_openid/config/environments/development.rb +19 -0
  40. data/examples/rails_openid/config/environments/production.rb +19 -0
  41. data/examples/rails_openid/config/environments/test.rb +19 -0
  42. data/examples/rails_openid/config/routes.rb +24 -0
  43. data/examples/rails_openid/doc/README_FOR_APP +2 -0
  44. data/examples/rails_openid/public/.htaccess +40 -0
  45. data/examples/rails_openid/public/404.html +8 -0
  46. data/examples/rails_openid/public/500.html +8 -0
  47. data/examples/rails_openid/public/dispatch.cgi +12 -0
  48. data/examples/rails_openid/public/dispatch.fcgi +26 -0
  49. data/examples/rails_openid/public/dispatch.rb +12 -0
  50. data/examples/rails_openid/public/favicon.ico +0 -0
  51. data/examples/rails_openid/public/images/openid_login_bg.gif +0 -0
  52. data/examples/rails_openid/public/javascripts/controls.js +750 -0
  53. data/examples/rails_openid/public/javascripts/dragdrop.js +584 -0
  54. data/examples/rails_openid/public/javascripts/effects.js +854 -0
  55. data/examples/rails_openid/public/javascripts/prototype.js +1785 -0
  56. data/examples/rails_openid/public/robots.txt +1 -0
  57. data/examples/rails_openid/script/about +3 -0
  58. data/examples/rails_openid/script/breakpointer +3 -0
  59. data/examples/rails_openid/script/console +3 -0
  60. data/examples/rails_openid/script/destroy +3 -0
  61. data/examples/rails_openid/script/generate +3 -0
  62. data/examples/rails_openid/script/performance/benchmarker +3 -0
  63. data/examples/rails_openid/script/performance/profiler +3 -0
  64. data/examples/rails_openid/script/plugin +3 -0
  65. data/examples/rails_openid/script/process/reaper +3 -0
  66. data/examples/rails_openid/script/process/spawner +3 -0
  67. data/examples/rails_openid/script/process/spinner +3 -0
  68. data/examples/rails_openid/script/runner +3 -0
  69. data/examples/rails_openid/script/server +3 -0
  70. data/examples/rails_openid/test/functional/login_controller_test.rb +18 -0
  71. data/examples/rails_openid/test/functional/server_controller_test.rb +18 -0
  72. data/examples/rails_openid/test/test_helper.rb +28 -0
  73. data/lib/hmac/hmac.rb +112 -0
  74. data/lib/hmac/sha1.rb +11 -0
  75. data/lib/hmac/sha2.rb +25 -0
  76. data/lib/openid.rb +20 -0
  77. data/lib/openid/association.rb +249 -0
  78. data/lib/openid/consumer.rb +395 -0
  79. data/lib/openid/consumer/associationmanager.rb +344 -0
  80. data/lib/openid/consumer/checkid_request.rb +186 -0
  81. data/lib/openid/consumer/discovery.rb +497 -0
  82. data/lib/openid/consumer/discovery_manager.rb +123 -0
  83. data/lib/openid/consumer/html_parse.rb +134 -0
  84. data/lib/openid/consumer/idres.rb +523 -0
  85. data/lib/openid/consumer/responses.rb +148 -0
  86. data/lib/openid/cryptutil.rb +115 -0
  87. data/lib/openid/dh.rb +89 -0
  88. data/lib/openid/extension.rb +39 -0
  89. data/lib/openid/extensions/ax.rb +539 -0
  90. data/lib/openid/extensions/oauth.rb +91 -0
  91. data/lib/openid/extensions/pape.rb +179 -0
  92. data/lib/openid/extensions/sreg.rb +277 -0
  93. data/lib/openid/extensions/ui.rb +53 -0
  94. data/lib/openid/extras.rb +11 -0
  95. data/lib/openid/fetchers.rb +258 -0
  96. data/lib/openid/kvform.rb +136 -0
  97. data/lib/openid/kvpost.rb +58 -0
  98. data/lib/openid/message.rb +553 -0
  99. data/lib/openid/protocolerror.rb +8 -0
  100. data/lib/openid/server.rb +1544 -0
  101. data/lib/openid/store/filesystem.rb +271 -0
  102. data/lib/openid/store/interface.rb +75 -0
  103. data/lib/openid/store/memcache.rb +107 -0
  104. data/lib/openid/store/memory.rb +84 -0
  105. data/lib/openid/store/nonce.rb +68 -0
  106. data/lib/openid/trustroot.rb +349 -0
  107. data/lib/openid/urinorm.rb +75 -0
  108. data/lib/openid/util.rb +110 -0
  109. data/lib/openid/yadis/accept.rb +148 -0
  110. data/lib/openid/yadis/constants.rb +21 -0
  111. data/lib/openid/yadis/discovery.rb +153 -0
  112. data/lib/openid/yadis/filters.rb +205 -0
  113. data/lib/openid/yadis/htmltokenizer.rb +305 -0
  114. data/lib/openid/yadis/parsehtml.rb +45 -0
  115. data/lib/openid/yadis/services.rb +42 -0
  116. data/lib/openid/yadis/xrds.rb +155 -0
  117. data/lib/openid/yadis/xri.rb +90 -0
  118. data/lib/openid/yadis/xrires.rb +99 -0
  119. data/setup.rb +1551 -0
  120. data/test/data/accept.txt +124 -0
  121. data/test/data/dh.txt +29 -0
  122. data/test/data/example-xrds.xml +14 -0
  123. data/test/data/linkparse.txt +587 -0
  124. data/test/data/n2b64 +650 -0
  125. data/test/data/test1-discover.txt +137 -0
  126. data/test/data/test1-parsehtml.txt +152 -0
  127. data/test/data/test_discover/malformed_meta_tag.html +19 -0
  128. data/test/data/test_discover/openid.html +11 -0
  129. data/test/data/test_discover/openid2.html +11 -0
  130. data/test/data/test_discover/openid2_xrds.xml +12 -0
  131. data/test/data/test_discover/openid2_xrds_no_local_id.xml +11 -0
  132. data/test/data/test_discover/openid_1_and_2.html +11 -0
  133. data/test/data/test_discover/openid_1_and_2_xrds.xml +16 -0
  134. data/test/data/test_discover/openid_1_and_2_xrds_bad_delegate.xml +17 -0
  135. data/test/data/test_discover/openid_and_yadis.html +12 -0
  136. data/test/data/test_discover/openid_no_delegate.html +10 -0
  137. data/test/data/test_discover/openid_utf8.html +11 -0
  138. data/test/data/test_discover/yadis_0entries.xml +12 -0
  139. data/test/data/test_discover/yadis_2_bad_local_id.xml +15 -0
  140. data/test/data/test_discover/yadis_2entries_delegate.xml +22 -0
  141. data/test/data/test_discover/yadis_2entries_idp.xml +21 -0
  142. data/test/data/test_discover/yadis_another_delegate.xml +14 -0
  143. data/test/data/test_discover/yadis_idp.xml +12 -0
  144. data/test/data/test_discover/yadis_idp_delegate.xml +13 -0
  145. data/test/data/test_discover/yadis_no_delegate.xml +11 -0
  146. data/test/data/test_xrds/=j3h.2007.11.14.xrds +25 -0
  147. data/test/data/test_xrds/README +12 -0
  148. data/test/data/test_xrds/delegated-20060809-r1.xrds +34 -0
  149. data/test/data/test_xrds/delegated-20060809-r2.xrds +34 -0
  150. data/test/data/test_xrds/delegated-20060809.xrds +34 -0
  151. data/test/data/test_xrds/no-xrd.xml +7 -0
  152. data/test/data/test_xrds/not-xrds.xml +2 -0
  153. data/test/data/test_xrds/prefixsometimes.xrds +34 -0
  154. data/test/data/test_xrds/ref.xrds +109 -0
  155. data/test/data/test_xrds/sometimesprefix.xrds +34 -0
  156. data/test/data/test_xrds/spoof1.xrds +25 -0
  157. data/test/data/test_xrds/spoof2.xrds +25 -0
  158. data/test/data/test_xrds/spoof3.xrds +37 -0
  159. data/test/data/test_xrds/status222.xrds +9 -0
  160. data/test/data/test_xrds/subsegments.xrds +58 -0
  161. data/test/data/test_xrds/valid-populated-xrds.xml +39 -0
  162. data/test/data/trustroot.txt +153 -0
  163. data/test/data/urinorm.txt +79 -0
  164. data/test/discoverdata.rb +131 -0
  165. data/test/test_accept.rb +170 -0
  166. data/test/test_association.rb +266 -0
  167. data/test/test_associationmanager.rb +917 -0
  168. data/test/test_ax.rb +690 -0
  169. data/test/test_checkid_request.rb +294 -0
  170. data/test/test_consumer.rb +257 -0
  171. data/test/test_cryptutil.rb +119 -0
  172. data/test/test_dh.rb +86 -0
  173. data/test/test_discover.rb +852 -0
  174. data/test/test_discovery_manager.rb +262 -0
  175. data/test/test_extension.rb +46 -0
  176. data/test/test_extras.rb +35 -0
  177. data/test/test_fetchers.rb +565 -0
  178. data/test/test_filters.rb +270 -0
  179. data/test/test_idres.rb +963 -0
  180. data/test/test_kvform.rb +165 -0
  181. data/test/test_kvpost.rb +65 -0
  182. data/test/test_linkparse.rb +101 -0
  183. data/test/test_message.rb +1116 -0
  184. data/test/test_nonce.rb +89 -0
  185. data/test/test_oauth.rb +175 -0
  186. data/test/test_openid_yadis.rb +178 -0
  187. data/test/test_pape.rb +247 -0
  188. data/test/test_parsehtml.rb +80 -0
  189. data/test/test_responses.rb +63 -0
  190. data/test/test_server.rb +2457 -0
  191. data/test/test_sreg.rb +479 -0
  192. data/test/test_stores.rb +298 -0
  193. data/test/test_trustroot.rb +113 -0
  194. data/test/test_ui.rb +93 -0
  195. data/test/test_urinorm.rb +35 -0
  196. data/test/test_util.rb +145 -0
  197. data/test/test_xrds.rb +169 -0
  198. data/test/test_xri.rb +48 -0
  199. data/test/test_xrires.rb +63 -0
  200. data/test/test_yadis_discovery.rb +220 -0
  201. data/test/testutil.rb +127 -0
  202. data/test/util.rb +53 -0
  203. metadata +336 -0
@@ -0,0 +1,294 @@
1
+ require "openid/consumer/checkid_request"
2
+ require "openid/message"
3
+ require "test/unit"
4
+ require "testutil"
5
+ require "util"
6
+
7
+ module OpenID
8
+ class Consumer
9
+ class CheckIDRequest
10
+ class DummyEndpoint
11
+ attr_accessor :preferred_namespace, :local_id, :server_url,
12
+ :is_op_identifier, :claimed_id
13
+
14
+ def initialize
15
+ @preferred_namespace = nil
16
+ @local_id = nil
17
+ @server_url = nil
18
+ @is_op_identifier = false
19
+ end
20
+
21
+ def get_local_id
22
+ @local_id
23
+ end
24
+
25
+ def compatibility_mode
26
+ @preferred_namespace == OPENID1_NS
27
+ end
28
+ end
29
+
30
+ module CheckIDTestMixin
31
+ include TestUtil
32
+
33
+ def setup
34
+ @endpoint = DummyEndpoint.new
35
+ @endpoint.local_id = 'http://server.unittest/joe'
36
+ @endpoint.claimed_id = 'http://joe.vanity.example/'
37
+ @endpoint.server_url = 'http://server.unittest/'
38
+ @endpoint.preferred_namespace = preferred_namespace
39
+ @realm = 'http://example/'
40
+ @return_to = 'http://example/return/'
41
+ @assoc = GoodAssoc.new
42
+ @checkid_req = CheckIDRequest.new(@assoc, @endpoint)
43
+ end
44
+
45
+ def assert_has_identifiers(msg, local_id, claimed_id)
46
+ assert_openid_value_equal(msg, 'identity', local_id)
47
+ assert_openid_value_equal(msg, 'claimed_id', claimed_id)
48
+ end
49
+
50
+ def assert_openid_key_exists(msg, key)
51
+ assert(msg.get_arg(OPENID_NS, key),
52
+ "#{key} not present in #{msg.get_args(OPENID_NS).inspect}")
53
+ end
54
+
55
+ def assert_openid_key_absent(msg, key)
56
+ assert(msg.get_arg(OPENID_NS, key).nil?)
57
+ end
58
+
59
+ def assert_openid_value_equal(msg, key, expected)
60
+ actual = msg.get_arg(OPENID_NS, key, NO_DEFAULT)
61
+ error_text = ("Expected #{expected.inspect} for openid.#{key} "\
62
+ "but got #{actual.inspect}: #{msg.inspect}")
63
+ assert_equal(expected, actual, error_text)
64
+ end
65
+
66
+ def assert_anonymous(msg)
67
+ ['claimed_id', 'identity'].each do |key|
68
+ assert_openid_key_absent(msg, key)
69
+ end
70
+ end
71
+
72
+ def assert_has_required_fields(msg)
73
+ internal_message = @checkid_req.instance_variable_get(:@message)
74
+ assert_equal(preferred_namespace,
75
+ internal_message.get_openid_namespace)
76
+
77
+ assert_equal(preferred_namespace, msg.get_openid_namespace)
78
+ assert_openid_value_equal(msg, 'mode', expected_mode)
79
+
80
+ # Implement these in subclasses because they depend on
81
+ # protocol differences!
82
+ assert_has_realm(msg)
83
+ assert_identifiers_present(msg)
84
+ end
85
+
86
+ # TESTS
87
+
88
+ def test_check_no_assoc_handle
89
+ @checkid_req.instance_variable_set('@assoc', nil)
90
+ msg = assert_log_matches("Generated checkid") {
91
+ @checkid_req.get_message(@realm, @return_to, immediate)
92
+ }
93
+ assert_openid_key_absent(msg, 'assoc_handle')
94
+ end
95
+
96
+ def test_check_with_assoc_handle
97
+ msg = assert_log_matches("Generated checkid") {
98
+ @checkid_req.get_message(@realm, @return_to, immediate)
99
+ }
100
+
101
+ assert_openid_value_equal(msg, 'assoc_handle', @assoc.handle)
102
+ end
103
+
104
+ def test_add_extension_arg
105
+ @checkid_req.add_extension_arg('bag:', 'color', 'brown')
106
+ @checkid_req.add_extension_arg('bag:', 'material', 'paper')
107
+ assert(@checkid_req.message.namespaces.member?('bag:'))
108
+ assert_equal(@checkid_req.message.get_args('bag:'),
109
+ {'color' => 'brown', 'material' => 'paper'})
110
+
111
+ msg = assert_log_matches("Generated checkid") {
112
+ @checkid_req.get_message(@realm, @return_to, immediate)
113
+ }
114
+
115
+ # XXX: this depends on the way that Message assigns
116
+ # namespaces. Really it doesn't care that it has alias "0",
117
+ # but that is tested anyway
118
+ post_args = msg.to_post_args()
119
+ assert_equal('brown', post_args['openid.ext0.color'])
120
+ assert_equal('paper', post_args['openid.ext0.material'])
121
+ end
122
+
123
+ def test_standard
124
+ msg = assert_log_matches('Generated checkid') {
125
+ @checkid_req.get_message(@realm, @return_to, immediate)
126
+ }
127
+ assert_has_identifiers(msg, @endpoint.local_id, @endpoint.claimed_id)
128
+ end
129
+
130
+ def test_send_redirect?
131
+ silence_logging {
132
+ url = @checkid_req.redirect_url(@realm, @return_to, immediate)
133
+ assert(url.length < OPENID1_URL_LIMIT)
134
+ assert(@checkid_req.send_redirect?(@realm, @return_to, immediate))
135
+
136
+ @return_to << '/foo' * 1000
137
+ url = @checkid_req.redirect_url(@realm, @return_to, immediate)
138
+ assert(url.length > OPENID1_URL_LIMIT)
139
+ actual = @checkid_req.send_redirect?(@realm, @return_to, immediate)
140
+ expected = preferred_namespace != OPENID2_NS
141
+ assert_equal(expected, actual)
142
+ }
143
+ end
144
+ end
145
+
146
+ class TestCheckIDRequestOpenID2 < Test::Unit::TestCase
147
+ include CheckIDTestMixin
148
+
149
+ def immediate
150
+ false
151
+ end
152
+
153
+ def expected_mode
154
+ 'checkid_setup'
155
+ end
156
+
157
+ def preferred_namespace
158
+ OPENID2_NS
159
+ end
160
+
161
+ # check presence of proper realm key and absence of the wrong
162
+ # one.
163
+ def assert_has_realm(msg)
164
+ assert_openid_value_equal(msg, 'realm', @realm)
165
+ assert_openid_key_absent(msg, 'trust_root')
166
+ end
167
+
168
+ def assert_identifiers_present(msg)
169
+ identity_present = msg.has_key?(OPENID_NS, 'identity')
170
+ claimed_present = msg.has_key?(OPENID_NS, 'claimed_id')
171
+
172
+ assert_equal(claimed_present, identity_present)
173
+ end
174
+
175
+ # OpenID Checkid_Requests should be able to set 'anonymous' to true.
176
+ def test_set_anonymous_works_for_openid2
177
+ assert(@checkid_req.message.is_openid2)
178
+ assert_nothing_raised {@checkid_req.anonymous = true}
179
+ assert_nothing_raised {@checkid_req.anonymous = false}
180
+ end
181
+
182
+ def test_user_anonymous_ignores_identfier
183
+ @checkid_req.anonymous = true
184
+ msg = assert_log_matches('Generated checkid') {
185
+ @checkid_req.get_message(@realm, @return_to, immediate)
186
+ }
187
+ assert_has_required_fields(msg)
188
+ assert_anonymous(msg)
189
+ end
190
+
191
+ def test_op_anonymous_ignores_identifier
192
+ @endpoint.is_op_identifier = true
193
+ @checkid_req.anonymous = true
194
+ msg = assert_log_matches('Generated checkid') {
195
+ @checkid_req.get_message(@realm, @return_to, immediate)
196
+ }
197
+ assert_has_required_fields(msg)
198
+ assert_anonymous(msg)
199
+ end
200
+
201
+ def test_op_identifier_sends_identifier_select
202
+ @endpoint.is_op_identifier = true
203
+ msg = assert_log_matches('Generated checkid') {
204
+ @checkid_req.get_message(@realm, @return_to, immediate)
205
+ }
206
+ assert_has_required_fields(msg)
207
+ assert_has_identifiers(msg, IDENTIFIER_SELECT, IDENTIFIER_SELECT)
208
+ end
209
+ end
210
+
211
+ class TestCheckIDRequestOpenID1 < Test::Unit::TestCase
212
+ include CheckIDTestMixin
213
+
214
+ def immediate
215
+ false
216
+ end
217
+
218
+ def preferred_namespace
219
+ OPENID1_NS
220
+ end
221
+
222
+ def expected_mode
223
+ 'checkid_setup'
224
+ end
225
+
226
+ # Make sure claimed_is is *absent* in request.
227
+ def assert_has_identifiers(msg, op_specific_id, claimed_id)
228
+ assert_openid_value_equal(msg, 'identity', op_specific_id)
229
+ assert_openid_key_absent(msg, 'claimed_id')
230
+ end
231
+
232
+ def assert_identifiers_present(msg)
233
+ assert_openid_key_absent(msg, 'claimed_id')
234
+ assert(msg.has_key?(OPENID_NS, 'identity'))
235
+ end
236
+
237
+ # check presence of proper realm key and absence of the wrong
238
+ # one.
239
+ def assert_has_realm(msg)
240
+ assert_openid_value_equal(msg, 'trust_root', @realm)
241
+ assert_openid_key_absent(msg, 'realm')
242
+ end
243
+
244
+ # TESTS
245
+
246
+ # OpenID 1 requests MUST NOT be able to set anonymous to true
247
+ def test_set_anonymous_fails_for_openid1
248
+ assert(@checkid_req.message.is_openid1)
249
+ assert_raises(ArgumentError) {
250
+ @checkid_req.anonymous = true
251
+ }
252
+ assert_nothing_raised{
253
+ @checkid_req.anonymous = false
254
+ }
255
+ end
256
+
257
+ # Identfier select SHOULD NOT be sent, but this pathway is in
258
+ # here in case some special discovery stuff is done to trigger
259
+ # it with OpenID 1. If it is triggered, it will send
260
+ # identifier_select just like OpenID 2.
261
+ def test_identifier_select
262
+ @endpoint.is_op_identifier = true
263
+ msg = assert_log_matches('Generated checkid') {
264
+ @checkid_req.get_message(@realm, @return_to, immediate)
265
+ }
266
+ assert_has_required_fields(msg)
267
+ assert_equal(IDENTIFIER_SELECT,
268
+ msg.get_arg(OPENID1_NS, 'identity'))
269
+ end
270
+
271
+ end
272
+
273
+ class TestCheckIDRequestOpenID1Immediate < TestCheckIDRequestOpenID1
274
+ def immediate
275
+ true
276
+ end
277
+
278
+ def expected_mode
279
+ 'checkid_immediate'
280
+ end
281
+ end
282
+
283
+ class TestCheckid_RequestOpenID2Immediate < TestCheckIDRequestOpenID2
284
+ def immediate
285
+ true
286
+ end
287
+
288
+ def expected_mode
289
+ 'checkid_immediate'
290
+ end
291
+ end
292
+ end
293
+ end
294
+ end
@@ -0,0 +1,257 @@
1
+ require "openid/consumer"
2
+ require "test/unit"
3
+ require "testutil"
4
+
5
+ module OpenID
6
+ class Consumer
7
+ module TestConsumer
8
+ class TestLastEndpoint < Test::Unit::TestCase
9
+ def test_set_get
10
+ session = {}
11
+ consumer = Consumer.new(session, nil)
12
+ consumer.send(:last_requested_endpoint=, :endpoint)
13
+ ep = consumer.send(:last_requested_endpoint)
14
+ assert_equal(:endpoint, ep)
15
+ ep = consumer.send(:last_requested_endpoint)
16
+ assert_equal(:endpoint, ep)
17
+ consumer.send(:cleanup_last_requested_endpoint)
18
+ ep = consumer.send(:last_requested_endpoint)
19
+ assert_equal(nil, ep)
20
+ end
21
+ end
22
+
23
+ class TestBegin < Test::Unit::TestCase
24
+ attr_accessor :user_input, :anonymous, :services,
25
+ :discovered_identifier, :checkid_request, :service
26
+
27
+ def setup
28
+ @discovered_identifier = 'http://discovered/'
29
+ @user_input = 'user.input'
30
+ @service = :service
31
+ @services = [@service]
32
+ @session = {}
33
+ @anonymous = false
34
+ @checkid_request = :checkid_request
35
+ end
36
+
37
+ def consumer
38
+ test = self
39
+ consumer = Consumer.new(@session, nil)
40
+ consumer.extend(InstanceDefExtension)
41
+ consumer.instance_def(:discover) do |identifier|
42
+ test.assert_equal(test.user_input, identifier)
43
+ [test.discovered_identifier, test.services]
44
+ end
45
+ consumer.instance_def(:begin_without_discovery) do
46
+ |service, sent_anonymous|
47
+ test.assert_equal(test.service, service)
48
+ test.assert_equal(test.anonymous, sent_anonymous)
49
+ test.checkid_request
50
+ end
51
+ consumer
52
+ end
53
+
54
+ def test_begin
55
+ checkid_request = consumer.begin(@user_input, @anonymous)
56
+ assert_equal(:checkid_request, checkid_request)
57
+ assert_equal(['OpenID::Consumer::DiscoveredServices::'\
58
+ 'OpenID::Consumer::'], @session.keys.sort!)
59
+ end
60
+
61
+ def test_begin_failure
62
+ @services = []
63
+ assert_raises(DiscoveryFailure) {
64
+ consumer.begin(@user_input, @anonymous)
65
+ }
66
+ end
67
+
68
+ def test_begin_fallback
69
+ @services = [:service1, :service2]
70
+ consumer = self.consumer
71
+ @service = :service1
72
+ consumer.begin(@user_input, @anonymous)
73
+ @service = :service2
74
+ consumer.begin(@user_input, @anonymous)
75
+ @service = :service1
76
+ consumer.begin(@user_input, @anonymous)
77
+ @service = :service2
78
+ consumer.begin(@user_input, @anonymous)
79
+ end
80
+ end
81
+
82
+ class TestBeginWithoutDiscovery < Test::Unit::TestCase
83
+ attr_reader :assoc
84
+ def setup
85
+ @session = {}
86
+ @assoc = :assoc
87
+ @service = OpenIDServiceEndpoint.new
88
+ @claimed_id = 'http://claimed.id/'
89
+ @service.claimed_id = @claimed_id
90
+ @anonymous = false
91
+ end
92
+
93
+ def consumer
94
+ test = self
95
+ assoc_manager = Object.new
96
+ assoc_manager.extend(InstanceDefExtension)
97
+ assoc_manager.instance_def(:get_association) do
98
+ test.assoc
99
+ end
100
+
101
+ consumer = Consumer.new(@session, nil)
102
+ consumer.extend(InstanceDefExtension)
103
+ consumer.instance_def(:association_manager) do |service|
104
+ assoc_manager
105
+ end
106
+ consumer
107
+ end
108
+
109
+ def call_begin_without_discovery
110
+ result = consumer.begin_without_discovery(@service, @anonymous)
111
+ assert(result.instance_of?(CheckIDRequest))
112
+ assert_equal(@anonymous, result.anonymous)
113
+ assert_equal(@service, consumer.send(:last_requested_endpoint))
114
+ assert_equal(result.instance_variable_get(:@assoc), @assoc)
115
+ return result
116
+ end
117
+
118
+ def cid_name
119
+ Consumer.openid1_return_to_claimed_id_name
120
+ end
121
+
122
+ def nonce_name
123
+ Consumer.openid1_return_to_nonce_name
124
+ end
125
+
126
+ def test_begin_without_openid1
127
+ result = call_begin_without_discovery
128
+
129
+ assert_equal(@claimed_id, result.return_to_args[cid_name])
130
+ assert_equal([cid_name, nonce_name].sort!,
131
+ result.return_to_args.keys.sort!)
132
+ end
133
+
134
+ def test_begin_without_openid1_anonymous
135
+ @anonymous = true
136
+ assert_raises(ArgumentError) {
137
+ call_begin_without_discovery
138
+ }
139
+ end
140
+
141
+ def test_begin_without_openid2
142
+ @service.type_uris = [OPENID_2_0_TYPE]
143
+ result = call_begin_without_discovery
144
+
145
+ assert(result.return_to_args.empty?)
146
+ end
147
+
148
+ def test_begin_without_openid2_anonymous
149
+ @anonymous = true
150
+ @service.type_uris = [OPENID_2_0_TYPE]
151
+ result = call_begin_without_discovery
152
+
153
+ assert(result.return_to_args.empty?)
154
+ end
155
+ end
156
+
157
+ class TestComplete < Test::Unit::TestCase
158
+ def setup
159
+ @session = {}
160
+ @consumer = Consumer.new(@session, nil)
161
+ end
162
+
163
+ def test_bad_mode
164
+ response = @consumer.complete({'openid.ns' => OPENID2_NS,
165
+ 'openid.mode' => 'bad'}, nil)
166
+ assert_equal(FAILURE, response.status)
167
+ end
168
+
169
+ def test_missing_mode
170
+ response = @consumer.complete({'openid.ns' => OPENID2_NS}, nil)
171
+ assert_equal(FAILURE, response.status)
172
+ end
173
+
174
+ def test_cancel
175
+ response = @consumer.complete({'openid.mode' => 'cancel'}, nil)
176
+ assert_equal(CANCEL, response.status)
177
+ end
178
+
179
+ def test_setup_needed_openid1
180
+ response = @consumer.complete({'openid.mode' => 'setup_needed'}, nil)
181
+ assert_equal(FAILURE, response.status)
182
+ end
183
+
184
+ def test_setup_needed_openid2
185
+ args = {'openid.ns' => OPENID2_NS, 'openid.mode' => 'setup_needed'}
186
+ response = @consumer.complete(args, nil)
187
+ assert_equal(SETUP_NEEDED, response.status)
188
+ end
189
+
190
+ def test_idres_setup_needed_openid1
191
+ setup_url = 'http://setup.url/'
192
+ args = {
193
+ 'openid.user_setup_url' => setup_url,
194
+ 'openid.mode' => 'id_res',
195
+ }
196
+ response = @consumer.complete(args, nil)
197
+ assert_equal(SETUP_NEEDED, response.status)
198
+ end
199
+
200
+ def test_error
201
+ contact = 'me'
202
+ reference = 'thing thing'
203
+ args = {
204
+ 'openid.mode' => 'error',
205
+ 'openid.contact' => contact,
206
+ 'openid.reference' => reference,
207
+ }
208
+ response = @consumer.complete(args, nil)
209
+ assert_equal(FAILURE, response.status)
210
+ assert_equal(contact, response.contact)
211
+ assert_equal(reference, response.reference)
212
+
213
+ args['openid.ns'] = OPENID2_NS
214
+ response = @consumer.complete(args, nil)
215
+ assert_equal(FAILURE, response.status)
216
+ assert_equal(contact, response.contact)
217
+ assert_equal(reference, response.reference)
218
+ end
219
+
220
+ def test_idres_openid1
221
+ args = {
222
+ 'openid.mode' => 'id_res',
223
+ }
224
+
225
+ endpoint = OpenIDServiceEndpoint.new
226
+ endpoint.claimed_id = :test_claimed_id
227
+
228
+ idres = Object.new
229
+ idres.extend(InstanceDefExtension)
230
+ idres.instance_def(:endpoint){endpoint}
231
+ idres.instance_def(:signed_fields){:test_signed_fields}
232
+
233
+ test = self
234
+ @consumer.extend(InstanceDefExtension)
235
+ @consumer.instance_def(:handle_idres) {|message, return_to|
236
+ test.assert_equal(args, message.to_post_args)
237
+ test.assert_equal(:test_return_to, return_to)
238
+ idres
239
+ }
240
+
241
+ response = @consumer.complete(args, :test_return_to)
242
+ assert_equal(SUCCESS, response.status, response.message)
243
+ assert_equal(:test_claimed_id, response.identity_url)
244
+ assert_equal(endpoint, response.endpoint)
245
+
246
+ error_message = "In Soviet Russia, id_res handles you!"
247
+ @consumer.instance_def(:handle_idres) {|message, return_to|
248
+ raise ProtocolError, error_message
249
+ }
250
+ response = @consumer.complete(args, :test_return_to)
251
+ assert_equal(FAILURE, response.status)
252
+ assert_equal(error_message, response.message)
253
+ end
254
+ end
255
+ end
256
+ end
257
+ end