pelle-ruby-openid 2.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (197) 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 +82 -0
  7. data/UPGRADE +127 -0
  8. data/VERSION +1 -0
  9. data/examples/README +32 -0
  10. data/examples/active_record_openid_store/README +58 -0
  11. data/examples/active_record_openid_store/XXX_add_open_id_store_to_db.rb +24 -0
  12. data/examples/active_record_openid_store/XXX_upgrade_open_id_store.rb +26 -0
  13. data/examples/active_record_openid_store/init.rb +8 -0
  14. data/examples/active_record_openid_store/lib/association.rb +10 -0
  15. data/examples/active_record_openid_store/lib/nonce.rb +3 -0
  16. data/examples/active_record_openid_store/lib/open_id_setting.rb +4 -0
  17. data/examples/active_record_openid_store/lib/openid_ar_store.rb +57 -0
  18. data/examples/active_record_openid_store/test/store_test.rb +212 -0
  19. data/examples/discover +49 -0
  20. data/examples/rails_openid/README +153 -0
  21. data/examples/rails_openid/Rakefile +10 -0
  22. data/examples/rails_openid/app/controllers/application.rb +4 -0
  23. data/examples/rails_openid/app/controllers/consumer_controller.rb +122 -0
  24. data/examples/rails_openid/app/controllers/login_controller.rb +45 -0
  25. data/examples/rails_openid/app/controllers/server_controller.rb +265 -0
  26. data/examples/rails_openid/app/helpers/application_helper.rb +3 -0
  27. data/examples/rails_openid/app/helpers/login_helper.rb +2 -0
  28. data/examples/rails_openid/app/helpers/server_helper.rb +9 -0
  29. data/examples/rails_openid/app/views/consumer/index.rhtml +81 -0
  30. data/examples/rails_openid/app/views/layouts/server.rhtml +68 -0
  31. data/examples/rails_openid/app/views/login/index.rhtml +56 -0
  32. data/examples/rails_openid/app/views/server/decide.rhtml +26 -0
  33. data/examples/rails_openid/config/boot.rb +19 -0
  34. data/examples/rails_openid/config/database.yml +74 -0
  35. data/examples/rails_openid/config/environment.rb +54 -0
  36. data/examples/rails_openid/config/environments/development.rb +19 -0
  37. data/examples/rails_openid/config/environments/production.rb +19 -0
  38. data/examples/rails_openid/config/environments/test.rb +19 -0
  39. data/examples/rails_openid/config/routes.rb +24 -0
  40. data/examples/rails_openid/doc/README_FOR_APP +2 -0
  41. data/examples/rails_openid/public/.htaccess +40 -0
  42. data/examples/rails_openid/public/404.html +8 -0
  43. data/examples/rails_openid/public/500.html +8 -0
  44. data/examples/rails_openid/public/dispatch.cgi +12 -0
  45. data/examples/rails_openid/public/dispatch.fcgi +26 -0
  46. data/examples/rails_openid/public/dispatch.rb +12 -0
  47. data/examples/rails_openid/public/favicon.ico +0 -0
  48. data/examples/rails_openid/public/images/openid_login_bg.gif +0 -0
  49. data/examples/rails_openid/public/javascripts/controls.js +750 -0
  50. data/examples/rails_openid/public/javascripts/dragdrop.js +584 -0
  51. data/examples/rails_openid/public/javascripts/effects.js +854 -0
  52. data/examples/rails_openid/public/javascripts/prototype.js +1785 -0
  53. data/examples/rails_openid/public/robots.txt +1 -0
  54. data/examples/rails_openid/script/about +3 -0
  55. data/examples/rails_openid/script/breakpointer +3 -0
  56. data/examples/rails_openid/script/console +3 -0
  57. data/examples/rails_openid/script/destroy +3 -0
  58. data/examples/rails_openid/script/generate +3 -0
  59. data/examples/rails_openid/script/performance/benchmarker +3 -0
  60. data/examples/rails_openid/script/performance/profiler +3 -0
  61. data/examples/rails_openid/script/plugin +3 -0
  62. data/examples/rails_openid/script/process/reaper +3 -0
  63. data/examples/rails_openid/script/process/spawner +3 -0
  64. data/examples/rails_openid/script/process/spinner +3 -0
  65. data/examples/rails_openid/script/runner +3 -0
  66. data/examples/rails_openid/script/server +3 -0
  67. data/examples/rails_openid/test/functional/login_controller_test.rb +18 -0
  68. data/examples/rails_openid/test/functional/server_controller_test.rb +18 -0
  69. data/examples/rails_openid/test/test_helper.rb +28 -0
  70. data/lib/hmac/hmac.rb +112 -0
  71. data/lib/hmac/sha1.rb +11 -0
  72. data/lib/hmac/sha2.rb +25 -0
  73. data/lib/openid/association.rb +249 -0
  74. data/lib/openid/consumer/associationmanager.rb +344 -0
  75. data/lib/openid/consumer/checkid_request.rb +186 -0
  76. data/lib/openid/consumer/discovery.rb +498 -0
  77. data/lib/openid/consumer/discovery_manager.rb +123 -0
  78. data/lib/openid/consumer/html_parse.rb +134 -0
  79. data/lib/openid/consumer/idres.rb +523 -0
  80. data/lib/openid/consumer/responses.rb +148 -0
  81. data/lib/openid/consumer.rb +395 -0
  82. data/lib/openid/cryptutil.rb +97 -0
  83. data/lib/openid/dh.rb +89 -0
  84. data/lib/openid/extension.rb +39 -0
  85. data/lib/openid/extensions/ax.rb +516 -0
  86. data/lib/openid/extensions/oauth.rb +91 -0
  87. data/lib/openid/extensions/pape.rb +179 -0
  88. data/lib/openid/extensions/sreg.rb +277 -0
  89. data/lib/openid/extras.rb +11 -0
  90. data/lib/openid/fetchers.rb +238 -0
  91. data/lib/openid/kvform.rb +136 -0
  92. data/lib/openid/kvpost.rb +58 -0
  93. data/lib/openid/message.rb +553 -0
  94. data/lib/openid/protocolerror.rb +8 -0
  95. data/lib/openid/server.rb +1544 -0
  96. data/lib/openid/store/filesystem.rb +271 -0
  97. data/lib/openid/store/interface.rb +75 -0
  98. data/lib/openid/store/memcache.rb +107 -0
  99. data/lib/openid/store/memory.rb +84 -0
  100. data/lib/openid/store/nonce.rb +68 -0
  101. data/lib/openid/trustroot.rb +349 -0
  102. data/lib/openid/urinorm.rb +75 -0
  103. data/lib/openid/util.rb +110 -0
  104. data/lib/openid/yadis/accept.rb +148 -0
  105. data/lib/openid/yadis/constants.rb +21 -0
  106. data/lib/openid/yadis/discovery.rb +153 -0
  107. data/lib/openid/yadis/filters.rb +205 -0
  108. data/lib/openid/yadis/htmltokenizer.rb +305 -0
  109. data/lib/openid/yadis/parsehtml.rb +45 -0
  110. data/lib/openid/yadis/services.rb +42 -0
  111. data/lib/openid/yadis/xrds.rb +155 -0
  112. data/lib/openid/yadis/xri.rb +90 -0
  113. data/lib/openid/yadis/xrires.rb +106 -0
  114. data/lib/openid.rb +20 -0
  115. data/setup.rb +1551 -0
  116. data/test/data/accept.txt +124 -0
  117. data/test/data/dh.txt +29 -0
  118. data/test/data/example-xrds.xml +14 -0
  119. data/test/data/linkparse.txt +587 -0
  120. data/test/data/n2b64 +650 -0
  121. data/test/data/test1-discover.txt +137 -0
  122. data/test/data/test1-parsehtml.txt +152 -0
  123. data/test/data/test_discover/malformed_meta_tag.html +19 -0
  124. data/test/data/test_discover/openid.html +11 -0
  125. data/test/data/test_discover/openid2.html +11 -0
  126. data/test/data/test_discover/openid2_xrds.xml +12 -0
  127. data/test/data/test_discover/openid2_xrds_no_local_id.xml +11 -0
  128. data/test/data/test_discover/openid_1_and_2.html +11 -0
  129. data/test/data/test_discover/openid_1_and_2_xrds.xml +16 -0
  130. data/test/data/test_discover/openid_1_and_2_xrds_bad_delegate.xml +17 -0
  131. data/test/data/test_discover/openid_and_yadis.html +12 -0
  132. data/test/data/test_discover/openid_no_delegate.html +10 -0
  133. data/test/data/test_discover/yadis_0entries.xml +12 -0
  134. data/test/data/test_discover/yadis_2_bad_local_id.xml +15 -0
  135. data/test/data/test_discover/yadis_2entries_delegate.xml +22 -0
  136. data/test/data/test_discover/yadis_2entries_idp.xml +21 -0
  137. data/test/data/test_discover/yadis_another_delegate.xml +14 -0
  138. data/test/data/test_discover/yadis_idp.xml +12 -0
  139. data/test/data/test_discover/yadis_idp_delegate.xml +13 -0
  140. data/test/data/test_discover/yadis_no_delegate.xml +11 -0
  141. data/test/data/test_xrds/=j3h.2007.11.14.xrds +25 -0
  142. data/test/data/test_xrds/README +12 -0
  143. data/test/data/test_xrds/delegated-20060809-r1.xrds +34 -0
  144. data/test/data/test_xrds/delegated-20060809-r2.xrds +34 -0
  145. data/test/data/test_xrds/delegated-20060809.xrds +34 -0
  146. data/test/data/test_xrds/no-xrd.xml +7 -0
  147. data/test/data/test_xrds/not-xrds.xml +2 -0
  148. data/test/data/test_xrds/prefixsometimes.xrds +34 -0
  149. data/test/data/test_xrds/ref.xrds +109 -0
  150. data/test/data/test_xrds/sometimesprefix.xrds +34 -0
  151. data/test/data/test_xrds/spoof1.xrds +25 -0
  152. data/test/data/test_xrds/spoof2.xrds +25 -0
  153. data/test/data/test_xrds/spoof3.xrds +37 -0
  154. data/test/data/test_xrds/status222.xrds +9 -0
  155. data/test/data/test_xrds/subsegments.xrds +58 -0
  156. data/test/data/test_xrds/valid-populated-xrds.xml +39 -0
  157. data/test/data/trustroot.txt +153 -0
  158. data/test/data/urinorm.txt +79 -0
  159. data/test/discoverdata.rb +131 -0
  160. data/test/test_accept.rb +170 -0
  161. data/test/test_association.rb +266 -0
  162. data/test/test_associationmanager.rb +917 -0
  163. data/test/test_ax.rb +648 -0
  164. data/test/test_checkid_request.rb +294 -0
  165. data/test/test_consumer.rb +257 -0
  166. data/test/test_cryptutil.rb +119 -0
  167. data/test/test_dh.rb +86 -0
  168. data/test/test_discover.rb +838 -0
  169. data/test/test_discovery_manager.rb +262 -0
  170. data/test/test_extension.rb +46 -0
  171. data/test/test_extras.rb +35 -0
  172. data/test/test_fetchers.rb +538 -0
  173. data/test/test_filters.rb +270 -0
  174. data/test/test_idres.rb +963 -0
  175. data/test/test_kvform.rb +165 -0
  176. data/test/test_kvpost.rb +65 -0
  177. data/test/test_linkparse.rb +101 -0
  178. data/test/test_message.rb +1116 -0
  179. data/test/test_nonce.rb +89 -0
  180. data/test/test_oauth.rb +175 -0
  181. data/test/test_openid_yadis.rb +178 -0
  182. data/test/test_pape.rb +247 -0
  183. data/test/test_parsehtml.rb +80 -0
  184. data/test/test_responses.rb +63 -0
  185. data/test/test_server.rb +2457 -0
  186. data/test/test_sreg.rb +479 -0
  187. data/test/test_stores.rb +298 -0
  188. data/test/test_trustroot.rb +113 -0
  189. data/test/test_urinorm.rb +35 -0
  190. data/test/test_util.rb +145 -0
  191. data/test/test_xrds.rb +169 -0
  192. data/test/test_xri.rb +48 -0
  193. data/test/test_xrires.rb +63 -0
  194. data/test/test_yadis_discovery.rb +220 -0
  195. data/test/testutil.rb +127 -0
  196. data/test/util.rb +53 -0
  197. metadata +316 -0
@@ -0,0 +1,838 @@
1
+
2
+ require 'testutil'
3
+ require 'util'
4
+
5
+ require 'test/unit'
6
+ require 'openid/fetchers'
7
+ require 'openid/yadis/discovery'
8
+ require 'openid/consumer/discovery'
9
+ require 'openid/yadis/xrires'
10
+ require 'openid/yadis/xri'
11
+ require 'openid/message'
12
+ require 'openid/util'
13
+
14
+ ### Tests for conditions that trigger DiscoveryFailure
15
+
16
+ module OpenID
17
+ class SimpleMockFetcher
18
+ def initialize(test, responses)
19
+ @test = test
20
+ @responses = responses.dup
21
+ end
22
+
23
+ def fetch(url, body=nil, headers=nil, limit=nil)
24
+ response = @responses.shift
25
+ @test.assert(body.nil?)
26
+ @test.assert_equal(response.final_url, url)
27
+ return response
28
+ end
29
+ end
30
+
31
+ class TestDiscoveryFailure < Test::Unit::TestCase
32
+ def initialize(*args)
33
+ super(*args)
34
+
35
+ @responses = [
36
+ [HTTPResponse._from_raw_data(nil, nil, {}, 'http://network.error/')],
37
+ [HTTPResponse._from_raw_data(404, nil, {}, 'http://not.found/')],
38
+ [HTTPResponse._from_raw_data(400, nil, {}, 'http://bad.request/')],
39
+ [HTTPResponse._from_raw_data(500, nil, {}, 'http://server.error/')],
40
+ [HTTPResponse._from_raw_data(200, nil, {'x-xrds-location' => 'http://xrds.missing/'},
41
+ 'http://header.found/'),
42
+ HTTPResponse._from_raw_data(404, nil, {}, 'http://xrds.missing/')],
43
+ ]
44
+ end
45
+
46
+ def test_discovery_failure
47
+
48
+ @responses.each { |response_set|
49
+ @url = response_set[0].final_url
50
+ OpenID.fetcher = SimpleMockFetcher.new(self, response_set)
51
+
52
+ expected_status = response_set[-1].code
53
+ begin
54
+ OpenID.discover(@url)
55
+ rescue DiscoveryFailure => why
56
+ assert_equal(why.http_response.code, expected_status)
57
+ else
58
+ flunk('Did not raise DiscoveryFailure')
59
+ end
60
+
61
+ OpenID.fetcher = nil
62
+ }
63
+ end
64
+ end
65
+
66
+ ### Tests for raising/catching exceptions from the fetcher through
67
+ ### the discover function
68
+
69
+ class ErrorRaisingFetcher
70
+ # Just raise an exception when fetch is called
71
+
72
+ def initialize(thing_to_raise)
73
+ @thing_to_raise = thing_to_raise
74
+ end
75
+
76
+ def fetch(url, body=nil, headers=nil, limit=nil)
77
+ raise @thing_to_raise
78
+ end
79
+ end
80
+
81
+ class DidFetch < Exception
82
+ # Custom exception just to make sure it's not handled differently
83
+ end
84
+
85
+ class TestFetchException < Test::Unit::TestCase
86
+ # Discovery should only raise DiscoveryFailure
87
+
88
+ def initialize(*args)
89
+ super(*args)
90
+
91
+ @cases = [
92
+ DidFetch.new(),
93
+ Exception.new(),
94
+ ArgumentError.new(),
95
+ RuntimeError.new(),
96
+ ]
97
+ end
98
+
99
+ def test_fetch_exception
100
+ @cases.each { |exc|
101
+ OpenID.fetcher = ErrorRaisingFetcher.new(exc)
102
+ assert_raises(DiscoveryFailure) {
103
+ OpenID.discover('http://doesnt.matter/')
104
+ }
105
+ OpenID.fetcher = nil
106
+ }
107
+ end
108
+ end
109
+
110
+ ### Tests for openid.consumer.discover.discover
111
+
112
+ class TestNormalization < Test::Unit::TestCase
113
+ def test_addingProtocol
114
+ f = ErrorRaisingFetcher.new(RuntimeError.new())
115
+ OpenID.fetcher = f
116
+
117
+ begin
118
+ OpenID.discover('users.stompy.janrain.com:8000/x')
119
+ rescue DiscoveryFailure => why
120
+ assert why.to_s.match("Failed to fetch")
121
+ rescue RuntimeError
122
+ end
123
+
124
+ OpenID.fetcher = nil
125
+ end
126
+ end
127
+
128
+ class DiscoveryMockFetcher
129
+ def initialize(documents)
130
+ @redirect = nil
131
+ @documents = documents
132
+ @fetchlog = []
133
+ end
134
+
135
+ def fetch(url, body=nil, headers=nil, limit=nil)
136
+ @fetchlog << [url, body, headers]
137
+ if @redirect
138
+ final_url = @redirect
139
+ else
140
+ final_url = url
141
+ end
142
+
143
+ begin
144
+ ctype, body = @documents.fetch(url)
145
+ rescue IndexError
146
+ status = 404
147
+ ctype = 'text/plain'
148
+ body = ''
149
+ else
150
+ status = 200
151
+ end
152
+
153
+ return HTTPResponse._from_raw_data(status, body, {'content-type' => ctype}, final_url)
154
+ end
155
+ end
156
+
157
+ class BaseTestDiscovery < Test::Unit::TestCase
158
+ attr_accessor :id_url, :fetcher_class
159
+
160
+ def initialize(*args)
161
+ super(*args)
162
+ @id_url = "http://someuser.unittest/"
163
+ @documents = {}
164
+ @fetcher_class = DiscoveryMockFetcher
165
+ end
166
+
167
+ def _checkService(s, server_url, claimed_id=nil,
168
+ local_id=nil, canonical_id=nil,
169
+ types=nil, used_yadis=false,
170
+ display_identifier=nil)
171
+ assert_equal(server_url, s.server_url)
172
+ if types == ['2.0 OP']
173
+ assert(!claimed_id)
174
+ assert(!local_id)
175
+ assert(!s.claimed_id)
176
+ assert(!s.local_id)
177
+ assert(!s.get_local_id())
178
+ assert(!s.compatibility_mode())
179
+ assert(s.is_op_identifier())
180
+ assert_equal(s.preferred_namespace(),
181
+ OPENID_2_0_MESSAGE_NS)
182
+ else
183
+ assert_equal(claimed_id, s.claimed_id)
184
+ assert_equal(local_id, s.get_local_id())
185
+ end
186
+
187
+ if used_yadis
188
+ assert(s.used_yadis, "Expected to use Yadis")
189
+ else
190
+ assert(!s.used_yadis,
191
+ "Expected to use old-style discovery")
192
+ end
193
+
194
+ openid_types = {
195
+ '1.1' => OPENID_1_1_TYPE,
196
+ '1.0' => OPENID_1_0_TYPE,
197
+ '2.0' => OPENID_2_0_TYPE,
198
+ '2.0 OP' => OPENID_IDP_2_0_TYPE,
199
+ }
200
+
201
+ type_uris = types.collect { |t| openid_types[t] }
202
+
203
+ assert_equal(type_uris, s.type_uris)
204
+ assert_equal(canonical_id, s.canonical_id)
205
+
206
+ if canonical_id.nil?
207
+ assert_equal(claimed_id, s.display_identifier)
208
+ else
209
+ assert_equal(display_identifier, s.display_identifier)
210
+ end
211
+ end
212
+
213
+ def setup
214
+ # @documents = @documents.dup
215
+ @fetcher = @fetcher_class.new(@documents)
216
+ OpenID.fetcher = @fetcher
217
+ end
218
+
219
+ def teardown
220
+ OpenID.fetcher = nil
221
+ end
222
+
223
+ def test_blank
224
+ # XXX to avoid > 0 test requirement
225
+ end
226
+ end
227
+
228
+ # def readDataFile(filename):
229
+ # module_directory = os.path.dirname(os.path.abspath(__file__))
230
+ # filename = os.path.join(
231
+ # module_directory, 'data', 'test_discover', filename)
232
+ # return file(filename).read()
233
+
234
+ class TestDiscovery < BaseTestDiscovery
235
+ include TestDataMixin
236
+
237
+ def _discover(content_type, data,
238
+ expected_services, expected_id=nil)
239
+ if expected_id.nil?
240
+ expected_id = @id_url
241
+ end
242
+
243
+ @documents[@id_url] = [content_type, data]
244
+ id_url, services = OpenID.discover(@id_url)
245
+
246
+ assert_equal(expected_services, services.length)
247
+ assert_equal(expected_id, id_url)
248
+ return services
249
+ end
250
+
251
+ def test_404
252
+ assert_raise(DiscoveryFailure) {
253
+ OpenID.discover(@id_url + '/404')
254
+ }
255
+ end
256
+
257
+ def test_noOpenID
258
+ services = _discover('text/plain',
259
+ "junk", 0)
260
+
261
+ services = _discover(
262
+ 'text/html',
263
+ read_data_file('test_discover/openid_no_delegate.html', false),
264
+ 1)
265
+
266
+ _checkService(
267
+ services[0],
268
+ "http://www.myopenid.com/server",
269
+ @id_url,
270
+ @id_url,
271
+ nil,
272
+ ['1.1'],
273
+ false)
274
+ end
275
+
276
+ def test_malformed_meta_tag
277
+ @id_url = "http://user.myopenid.com/"
278
+
279
+ services = _discover(
280
+ 'text/html',
281
+ read_data_file('test_discover/malformed_meta_tag.html', false),
282
+ 2)
283
+
284
+ _checkService(
285
+ services[0],
286
+ "http://www.myopenid.com/server",
287
+ @id_url,
288
+ @id_url,
289
+ nil,
290
+ ['2.0'],
291
+ false)
292
+
293
+ _checkService(
294
+ services[1],
295
+ "http://www.myopenid.com/server",
296
+ @id_url,
297
+ @id_url,
298
+ nil,
299
+ ['1.1'],
300
+ false)
301
+ end
302
+
303
+ def test_html1
304
+ services = _discover('text/html',
305
+ read_data_file('test_discover/openid.html', false),
306
+ 1)
307
+
308
+ _checkService(services[0],
309
+ "http://www.myopenid.com/server",
310
+ @id_url,
311
+ 'http://smoker.myopenid.com/',
312
+ nil,
313
+ ['1.1'],
314
+ false)
315
+ end
316
+
317
+ def test_html1Fragment
318
+ # Ensure that the Claimed Identifier does not have a fragment if
319
+ # one is supplied in the User Input.
320
+ content_type = 'text/html'
321
+ data = read_data_file('test_discover/openid.html', false)
322
+ expected_services = 1
323
+
324
+ @documents[@id_url] = [content_type, data]
325
+ expected_id = @id_url
326
+ @id_url = @id_url + '#fragment'
327
+ id_url, services = OpenID.discover(@id_url)
328
+
329
+ assert_equal(expected_services, services.length)
330
+ assert_equal(expected_id, id_url)
331
+
332
+ _checkService(services[0],
333
+ "http://www.myopenid.com/server",
334
+ expected_id,
335
+ 'http://smoker.myopenid.com/',
336
+ nil,
337
+ ['1.1'],
338
+ false)
339
+ end
340
+
341
+ def test_html2
342
+ services = _discover('text/html',
343
+ read_data_file('test_discover/openid2.html', false),
344
+ 1)
345
+
346
+ _checkService(services[0],
347
+ "http://www.myopenid.com/server",
348
+ @id_url,
349
+ 'http://smoker.myopenid.com/',
350
+ nil,
351
+ ['2.0'],
352
+ false)
353
+ end
354
+
355
+ def test_html1And2
356
+ services = _discover(
357
+ 'text/html',
358
+ read_data_file('test_discover/openid_1_and_2.html', false),
359
+ 2)
360
+
361
+ services.zip(['2.0', '1.1']).each { |s, t|
362
+ _checkService(s,
363
+ "http://www.myopenid.com/server",
364
+ @id_url,
365
+ 'http://smoker.myopenid.com/',
366
+ nil,
367
+ [t],
368
+ false)
369
+ }
370
+ end
371
+
372
+ def test_yadisEmpty
373
+ services = _discover('application/xrds+xml',
374
+ read_data_file('test_discover/yadis_0entries.xml', false),
375
+ 0)
376
+ end
377
+
378
+ def test_htmlEmptyYadis
379
+ # HTML document has discovery information, but points to an
380
+ # empty Yadis document. The XRDS document pointed to by
381
+ # "openid_and_yadis.html"
382
+ @documents[@id_url + 'xrds'] = ['application/xrds+xml',
383
+ read_data_file('test_discover/yadis_0entries.xml', false)]
384
+
385
+ services = _discover('text/html',
386
+ read_data_file('test_discover/openid_and_yadis.html', false),
387
+ 1)
388
+
389
+ _checkService(services[0],
390
+ "http://www.myopenid.com/server",
391
+ @id_url,
392
+ 'http://smoker.myopenid.com/',
393
+ nil,
394
+ ['1.1'],
395
+ false)
396
+ end
397
+
398
+ def test_yadis1NoDelegate
399
+ services = _discover('application/xrds+xml',
400
+ read_data_file('test_discover/yadis_no_delegate.xml', false),
401
+ 1)
402
+
403
+ _checkService(services[0],
404
+ "http://www.myopenid.com/server",
405
+ @id_url,
406
+ @id_url,
407
+ nil,
408
+ ['1.0'],
409
+ true)
410
+ end
411
+
412
+ def test_yadis2NoLocalID
413
+ services = _discover('application/xrds+xml',
414
+ read_data_file('test_discover/openid2_xrds_no_local_id.xml', false),
415
+ 1)
416
+
417
+ _checkService(services[0],
418
+ "http://www.myopenid.com/server",
419
+ @id_url,
420
+ @id_url,
421
+ nil,
422
+ ['2.0'],
423
+ true)
424
+ end
425
+
426
+ def test_yadis2
427
+ services = _discover('application/xrds+xml',
428
+ read_data_file('test_discover/openid2_xrds.xml', false),
429
+ 1)
430
+
431
+ _checkService(services[0],
432
+ "http://www.myopenid.com/server",
433
+ @id_url,
434
+ 'http://smoker.myopenid.com/',
435
+ nil,
436
+ ['2.0'],
437
+ true)
438
+ end
439
+
440
+ def test_yadis2OP
441
+ services = _discover('application/xrds+xml',
442
+ read_data_file('test_discover/yadis_idp.xml', false),
443
+ 1)
444
+
445
+ _checkService(services[0],
446
+ "http://www.myopenid.com/server",
447
+ nil, nil, nil,
448
+ ['2.0 OP'],
449
+ true)
450
+ end
451
+
452
+ def test_yadis2OPDelegate
453
+ # The delegate tag isn't meaningful for OP entries.
454
+ services = _discover('application/xrds+xml',
455
+ read_data_file('test_discover/yadis_idp_delegate.xml', false),
456
+ 1)
457
+
458
+ _checkService(services[0],
459
+ "http://www.myopenid.com/server",
460
+ nil, nil, nil,
461
+ ['2.0 OP'],
462
+ true)
463
+ end
464
+
465
+ def test_yadis2BadLocalID
466
+ assert_raise(DiscoveryFailure) {
467
+ _discover('application/xrds+xml',
468
+ read_data_file('test_discover/yadis_2_bad_local_id.xml', false),
469
+ 1)
470
+ }
471
+ end
472
+
473
+ def test_yadis1And2
474
+ services = _discover('application/xrds+xml',
475
+ read_data_file('test_discover/openid_1_and_2_xrds.xml', false),
476
+ 1)
477
+
478
+ _checkService(services[0],
479
+ "http://www.myopenid.com/server",
480
+ @id_url,
481
+ 'http://smoker.myopenid.com/',
482
+ nil,
483
+ ['2.0', '1.1'],
484
+ true)
485
+ end
486
+
487
+ def test_yadis1And2BadLocalID
488
+ assert_raise(DiscoveryFailure) {
489
+ _discover('application/xrds+xml',
490
+ read_data_file('test_discover/openid_1_and_2_xrds_bad_delegate.xml', false),
491
+ 1)
492
+ }
493
+ end
494
+ end
495
+
496
+ class MockFetcherForXRIProxy
497
+
498
+ def initialize(documents, proxy_url=Yadis::XRI::ProxyResolver::DEFAULT_PROXY)
499
+ @documents = documents
500
+ @fetchlog = []
501
+ @proxy_url = nil
502
+ end
503
+
504
+ def fetch(url, body=nil, headers=nil, limit=nil)
505
+ @fetchlog << [url, body, headers]
506
+
507
+ u = URI::parse(url)
508
+ proxy_host = u.host
509
+ xri = u.path
510
+ query = u.query
511
+
512
+ if !headers and !query
513
+ raise ArgumentError.new("No headers or query; you probably didn't " +
514
+ "mean to do that.")
515
+ end
516
+
517
+ if xri.starts_with?('/')
518
+ xri = xri[1..-1]
519
+ end
520
+
521
+ begin
522
+ ctype, body = @documents.fetch(xri)
523
+ rescue IndexError
524
+ status = 404
525
+ ctype = 'text/plain'
526
+ body = ''
527
+ else
528
+ status = 200
529
+ end
530
+
531
+ return HTTPResponse._from_raw_data(status, body,
532
+ {'content-type' => ctype}, url)
533
+ end
534
+ end
535
+
536
+ class TestXRIDiscovery < BaseTestDiscovery
537
+
538
+ include TestDataMixin
539
+ include TestUtil
540
+
541
+ def initialize(*args)
542
+ super(*args)
543
+
544
+ @fetcher_class = MockFetcherForXRIProxy
545
+
546
+ @documents = {'=smoker' => ['application/xrds+xml',
547
+ read_data_file('test_discover/yadis_2entries_delegate.xml', false)],
548
+ '=smoker*bad' => ['application/xrds+xml',
549
+ read_data_file('test_discover/yadis_another_delegate.xml', false)]}
550
+ end
551
+
552
+ def test_xri
553
+ user_xri, services = OpenID.discover_xri('=smoker')
554
+
555
+ _checkService(services[0],
556
+ "http://www.myopenid.com/server",
557
+ Yadis::XRI.make_xri("=!1000"),
558
+ 'http://smoker.myopenid.com/',
559
+ Yadis::XRI.make_xri("=!1000"),
560
+ ['1.0'],
561
+ true,
562
+ '=smoker')
563
+
564
+ _checkService(services[1],
565
+ "http://www.livejournal.com/openid/server.bml",
566
+ Yadis::XRI.make_xri("=!1000"),
567
+ 'http://frank.livejournal.com/',
568
+ Yadis::XRI.make_xri("=!1000"),
569
+ ['1.0'],
570
+ true,
571
+ '=smoker')
572
+ end
573
+
574
+ def test_xri_normalize
575
+ user_xri, services = OpenID.discover_xri('xri://=smoker')
576
+
577
+ _checkService(services[0],
578
+ "http://www.myopenid.com/server",
579
+ Yadis::XRI.make_xri("=!1000"),
580
+ 'http://smoker.myopenid.com/',
581
+ Yadis::XRI.make_xri("=!1000"),
582
+ ['1.0'],
583
+ true,
584
+ '=smoker')
585
+
586
+ _checkService(services[1],
587
+ "http://www.livejournal.com/openid/server.bml",
588
+ Yadis::XRI.make_xri("=!1000"),
589
+ 'http://frank.livejournal.com/',
590
+ Yadis::XRI.make_xri("=!1000"),
591
+ ['1.0'],
592
+ true,
593
+ '=smoker')
594
+ end
595
+
596
+ def test_xriNoCanonicalID
597
+ silence_logging {
598
+ user_xri, services = OpenID.discover_xri('=smoker*bad')
599
+ assert(services.empty?)
600
+ }
601
+ end
602
+
603
+ def test_useCanonicalID
604
+ # When there is no delegate, the CanonicalID should be used with
605
+ # XRI.
606
+ endpoint = OpenIDServiceEndpoint.new()
607
+ endpoint.claimed_id = Yadis::XRI.make_xri("=!1000")
608
+ endpoint.canonical_id = Yadis::XRI.make_xri("=!1000")
609
+ assert_equal(endpoint.get_local_id, Yadis::XRI.make_xri("=!1000"))
610
+ end
611
+ end
612
+
613
+ class TestXRIDiscoveryIDP < BaseTestDiscovery
614
+ include TestDataMixin
615
+
616
+ def initialize(*args)
617
+ super(*args)
618
+
619
+ @fetcher_class = MockFetcherForXRIProxy
620
+
621
+ @documents = {'=smoker' => ['application/xrds+xml',
622
+ read_data_file('test_discover/yadis_2entries_idp.xml', false)] }
623
+ end
624
+
625
+ def test_xri
626
+ user_xri, services = OpenID.discover_xri('=smoker')
627
+ assert(!services.empty?, "Expected services, got zero")
628
+ assert_equal(services[0].server_url,
629
+ "http://www.livejournal.com/openid/server.bml")
630
+ end
631
+ end
632
+
633
+ class TestPreferredNamespace < Test::Unit::TestCase
634
+ def initialize(*args)
635
+ super(*args)
636
+
637
+ @cases = [
638
+ [OPENID1_NS, []],
639
+ [OPENID1_NS, ['http://jyte.com/']],
640
+ [OPENID1_NS, [OPENID_1_0_TYPE]],
641
+ [OPENID1_NS, [OPENID_1_1_TYPE]],
642
+ [OPENID2_NS, [OPENID_2_0_TYPE]],
643
+ [OPENID2_NS, [OPENID_IDP_2_0_TYPE]],
644
+ [OPENID2_NS, [OPENID_2_0_TYPE,
645
+ OPENID_1_0_TYPE]],
646
+ [OPENID2_NS, [OPENID_1_0_TYPE,
647
+ OPENID_2_0_TYPE]],
648
+ ]
649
+ end
650
+
651
+ def test_preferred_namespace
652
+
653
+ @cases.each { |expected_ns, type_uris|
654
+ endpoint = OpenIDServiceEndpoint.new()
655
+ endpoint.type_uris = type_uris
656
+ actual_ns = endpoint.preferred_namespace()
657
+ assert_equal(actual_ns, expected_ns)
658
+ }
659
+ end
660
+ end
661
+
662
+ class TestIsOPIdentifier < Test::Unit::TestCase
663
+ def setup
664
+ @endpoint = OpenIDServiceEndpoint.new()
665
+ end
666
+
667
+ def test_none
668
+ assert(!@endpoint.is_op_identifier())
669
+ end
670
+
671
+ def test_openid1_0
672
+ @endpoint.type_uris = [OPENID_1_0_TYPE]
673
+ assert(!@endpoint.is_op_identifier())
674
+ end
675
+
676
+ def test_openid1_1
677
+ @endpoint.type_uris = [OPENID_1_1_TYPE]
678
+ assert(!@endpoint.is_op_identifier())
679
+ end
680
+
681
+ def test_openid2
682
+ @endpoint.type_uris = [OPENID_2_0_TYPE]
683
+ assert(!@endpoint.is_op_identifier())
684
+ end
685
+
686
+ def test_openid2OP
687
+ @endpoint.type_uris = [OPENID_IDP_2_0_TYPE]
688
+ assert(@endpoint.is_op_identifier())
689
+ end
690
+
691
+ def test_multipleMissing
692
+ @endpoint.type_uris = [OPENID_2_0_TYPE,
693
+ OPENID_1_0_TYPE]
694
+ assert(!@endpoint.is_op_identifier())
695
+ end
696
+
697
+ def test_multiplePresent
698
+ @endpoint.type_uris = [OPENID_2_0_TYPE,
699
+ OPENID_1_0_TYPE,
700
+ OPENID_IDP_2_0_TYPE]
701
+ assert(@endpoint.is_op_identifier())
702
+ end
703
+ end
704
+
705
+ class TestFromOPEndpointURL < Test::Unit::TestCase
706
+ def setup
707
+ @op_endpoint_url = 'http://example.com/op/endpoint'
708
+ @endpoint = OpenIDServiceEndpoint.from_op_endpoint_url(@op_endpoint_url)
709
+ end
710
+
711
+ def test_isOPEndpoint
712
+ assert(@endpoint.is_op_identifier())
713
+ end
714
+
715
+ def test_noIdentifiers
716
+ assert_equal(@endpoint.get_local_id, nil)
717
+ assert_equal(@endpoint.claimed_id, nil)
718
+ end
719
+
720
+ def test_compatibility
721
+ assert(!@endpoint.compatibility_mode())
722
+ end
723
+
724
+ def test_canonical_id
725
+ assert_equal(@endpoint.canonical_id, nil)
726
+ end
727
+
728
+ def test_serverURL
729
+ assert_equal(@endpoint.server_url, @op_endpoint_url)
730
+ end
731
+ end
732
+
733
+ class TestDiscoverFunction < Test::Unit::TestCase
734
+ def test_discover_function
735
+ # XXX these were all different tests in python, but they're
736
+ # combined here so I only have to use with_method_overridden
737
+ # once.
738
+ discoverXRI = Proc.new { |identifier|
739
+ return 'XRI'
740
+ }
741
+
742
+ discoverURI = Proc.new { |identifier|
743
+ return 'URI'
744
+ }
745
+
746
+ OpenID.extend(OverrideMethodMixin)
747
+
748
+ OpenID.with_method_overridden(:discover_uri, discoverURI) do
749
+ OpenID.with_method_overridden(:discover_xri, discoverXRI) do
750
+ assert_equal('URI', OpenID.discover('http://woo!'))
751
+ assert_equal('URI', OpenID.discover('not a URL or XRI'))
752
+ assert_equal('XRI', OpenID.discover('xri://=something'))
753
+ assert_equal('XRI', OpenID.discover('=something'))
754
+ end
755
+ end
756
+ end
757
+ end
758
+
759
+ class TestEndpointSupportsType < Test::Unit::TestCase
760
+ def setup
761
+ @endpoint = OpenIDServiceEndpoint.new()
762
+ end
763
+
764
+ def failUnlessSupportsOnly(*types)
765
+ ['foo',
766
+ OPENID_1_1_TYPE,
767
+ OPENID_1_0_TYPE,
768
+ OPENID_2_0_TYPE,
769
+ OPENID_IDP_2_0_TYPE].each { |t|
770
+ if types.member?(t)
771
+ assert(@endpoint.supports_type(t),
772
+ sprintf("Must support %s", t))
773
+ else
774
+ assert(!@endpoint.supports_type(t),
775
+ sprintf("Shouldn't support %s", t))
776
+ end
777
+ }
778
+ end
779
+
780
+ def test_supportsNothing
781
+ failUnlessSupportsOnly()
782
+ end
783
+
784
+ def test_openid2
785
+ @endpoint.type_uris = [OPENID_2_0_TYPE]
786
+ failUnlessSupportsOnly(OPENID_2_0_TYPE)
787
+ end
788
+
789
+ def test_openid2provider
790
+ @endpoint.type_uris = [OPENID_IDP_2_0_TYPE]
791
+ failUnlessSupportsOnly(OPENID_IDP_2_0_TYPE,
792
+ OPENID_2_0_TYPE)
793
+ end
794
+
795
+ def test_openid1_0
796
+ @endpoint.type_uris = [OPENID_1_0_TYPE]
797
+ failUnlessSupportsOnly(OPENID_1_0_TYPE)
798
+ end
799
+
800
+ def test_openid1_1
801
+ @endpoint.type_uris = [OPENID_1_1_TYPE]
802
+ failUnlessSupportsOnly(OPENID_1_1_TYPE)
803
+ end
804
+
805
+ def test_multiple
806
+ @endpoint.type_uris = [OPENID_1_1_TYPE,
807
+ OPENID_2_0_TYPE]
808
+ failUnlessSupportsOnly(OPENID_1_1_TYPE,
809
+ OPENID_2_0_TYPE)
810
+ end
811
+
812
+ def test_multipleWithProvider
813
+ @endpoint.type_uris = [OPENID_1_1_TYPE,
814
+ OPENID_2_0_TYPE,
815
+ OPENID_IDP_2_0_TYPE]
816
+ failUnlessSupportsOnly(OPENID_1_1_TYPE,
817
+ OPENID_2_0_TYPE,
818
+ OPENID_IDP_2_0_TYPE)
819
+ end
820
+ end
821
+
822
+ class TestEndpointDisplayIdentifier < Test::Unit::TestCase
823
+ def test_strip_fragment
824
+ @endpoint = OpenIDServiceEndpoint.new()
825
+ @endpoint.claimed_id = 'http://recycled.invalid/#123'
826
+ assert_equal 'http://recycled.invalid/', @endpoint.display_identifier
827
+ end
828
+ end
829
+
830
+
831
+ class TestNormalizeURL < Test::Unit::TestCase
832
+ def test_no_host
833
+ assert_raise(DiscoveryFailure) {
834
+ OpenID::normalize_url('http:///too-many.invalid/slashes')
835
+ }
836
+ end
837
+ end
838
+ end