ruby-openid 1.1.4 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (207) hide show
  1. data/INSTALL +0 -9
  2. data/README +21 -22
  3. data/UPGRADE +117 -0
  4. data/admin/runtests.rb +36 -0
  5. data/examples/README +13 -21
  6. data/examples/active_record_openid_store/README +8 -3
  7. data/examples/active_record_openid_store/XXX_add_open_id_store_to_db.rb +4 -8
  8. data/examples/active_record_openid_store/XXX_upgrade_open_id_store.rb +26 -0
  9. data/examples/active_record_openid_store/lib/association.rb +2 -0
  10. data/examples/active_record_openid_store/lib/openid_ar_store.rb +22 -47
  11. data/examples/active_record_openid_store/test/store_test.rb +78 -48
  12. data/examples/discover +46 -0
  13. data/examples/{rails_server → rails_openid}/README +0 -0
  14. data/examples/{rails_server → rails_openid}/Rakefile +0 -0
  15. data/examples/{rails_server → rails_openid}/app/controllers/application.rb +0 -0
  16. data/examples/rails_openid/app/controllers/consumer_controller.rb +115 -0
  17. data/examples/{rails_server → rails_openid}/app/controllers/login_controller.rb +10 -2
  18. data/examples/rails_openid/app/controllers/server_controller.rb +265 -0
  19. data/examples/{rails_server → rails_openid}/app/helpers/application_helper.rb +0 -0
  20. data/examples/{rails_server → rails_openid}/app/helpers/login_helper.rb +0 -0
  21. data/examples/{rails_server → rails_openid}/app/helpers/server_helper.rb +0 -0
  22. data/examples/rails_openid/app/views/consumer/index.rhtml +81 -0
  23. data/examples/rails_openid/app/views/consumer/start.rhtml +8 -0
  24. data/examples/{rails_server → rails_openid}/app/views/layouts/server.rhtml +0 -0
  25. data/examples/{rails_server → rails_openid}/app/views/login/index.rhtml +1 -1
  26. data/examples/rails_openid/app/views/server/decide.rhtml +26 -0
  27. data/examples/{rails_server → rails_openid}/config/boot.rb +0 -0
  28. data/examples/{rails_server → rails_openid}/config/database.yml +0 -0
  29. data/examples/{rails_server → rails_openid}/config/environment.rb +0 -0
  30. data/examples/{rails_server → rails_openid}/config/environments/development.rb +0 -0
  31. data/examples/{rails_server → rails_openid}/config/environments/production.rb +0 -0
  32. data/examples/{rails_server → rails_openid}/config/environments/test.rb +0 -0
  33. data/examples/{rails_server → rails_openid}/config/routes.rb +2 -1
  34. data/examples/{rails_server → rails_openid}/doc/README_FOR_APP +0 -0
  35. data/examples/{rails_server → rails_openid}/public/404.html +0 -0
  36. data/examples/{rails_server → rails_openid}/public/500.html +0 -0
  37. data/examples/{rails_server → rails_openid}/public/dispatch.cgi +0 -0
  38. data/examples/{rails_server → rails_openid}/public/dispatch.fcgi +0 -0
  39. data/examples/{rails_server → rails_openid}/public/dispatch.rb +0 -0
  40. data/examples/{rails_server → rails_openid}/public/favicon.ico +0 -0
  41. data/examples/rails_openid/public/images/openid_login_bg.gif +0 -0
  42. data/examples/{rails_server → rails_openid}/public/javascripts/controls.js +0 -0
  43. data/examples/{rails_server → rails_openid}/public/javascripts/dragdrop.js +0 -0
  44. data/examples/{rails_server → rails_openid}/public/javascripts/effects.js +0 -0
  45. data/examples/{rails_server → rails_openid}/public/javascripts/prototype.js +0 -0
  46. data/examples/{rails_server → rails_openid}/public/robots.txt +0 -0
  47. data/examples/{rails_server → rails_openid}/script/about +0 -0
  48. data/examples/{rails_server → rails_openid}/script/breakpointer +0 -0
  49. data/examples/{rails_server → rails_openid}/script/console +0 -0
  50. data/examples/{rails_server → rails_openid}/script/destroy +0 -0
  51. data/examples/{rails_server → rails_openid}/script/generate +0 -0
  52. data/examples/{rails_server → rails_openid}/script/performance/benchmarker +0 -0
  53. data/examples/{rails_server → rails_openid}/script/performance/profiler +0 -0
  54. data/examples/{rails_server → rails_openid}/script/plugin +0 -0
  55. data/examples/{rails_server → rails_openid}/script/process/reaper +0 -0
  56. data/examples/{rails_server → rails_openid}/script/process/spawner +0 -0
  57. data/examples/{rails_server → rails_openid}/script/process/spinner +0 -0
  58. data/examples/{rails_server → rails_openid}/script/runner +0 -0
  59. data/examples/{rails_server → rails_openid}/script/server +0 -0
  60. data/examples/{rails_server → rails_openid}/test/functional/login_controller_test.rb +0 -0
  61. data/examples/{rails_server → rails_openid}/test/functional/server_controller_test.rb +0 -0
  62. data/examples/{rails_server → rails_openid}/test/test_helper.rb +0 -0
  63. data/lib/{hmac.rb → hmac/hmac.rb} +0 -0
  64. data/lib/{hmac-sha1.rb → hmac/sha1.rb} +1 -1
  65. data/lib/{hmac-sha2.rb → hmac/sha2.rb} +1 -1
  66. data/lib/openid/association.rb +213 -73
  67. data/lib/openid/consumer/associationmanager.rb +338 -0
  68. data/lib/openid/consumer/checkid_request.rb +175 -0
  69. data/lib/openid/consumer/discovery.rb +480 -0
  70. data/lib/openid/consumer/discovery_manager.rb +123 -0
  71. data/lib/openid/consumer/html_parse.rb +136 -0
  72. data/lib/openid/consumer/idres.rb +525 -0
  73. data/lib/openid/consumer/responses.rb +133 -0
  74. data/lib/openid/consumer.rb +280 -807
  75. data/lib/openid/cryptutil.rb +85 -0
  76. data/lib/openid/dh.rb +60 -23
  77. data/lib/openid/extension.rb +31 -0
  78. data/lib/openid/extensions/ax.rb +506 -0
  79. data/lib/openid/extensions/pape.rb +182 -0
  80. data/lib/openid/extensions/sreg.rb +275 -0
  81. data/lib/openid/extras.rb +11 -0
  82. data/lib/openid/fetchers.rb +132 -93
  83. data/lib/openid/kvform.rb +133 -0
  84. data/lib/openid/kvpost.rb +56 -0
  85. data/lib/openid/message.rb +534 -0
  86. data/lib/openid/protocolerror.rb +6 -0
  87. data/lib/openid/server.rb +1215 -666
  88. data/lib/openid/store/filesystem.rb +271 -0
  89. data/lib/openid/store/interface.rb +75 -0
  90. data/lib/openid/store/memory.rb +84 -0
  91. data/lib/openid/store/nonce.rb +68 -0
  92. data/lib/openid/trustroot.rb +314 -87
  93. data/lib/openid/urinorm.rb +37 -34
  94. data/lib/openid/util.rb +42 -220
  95. data/lib/openid/yadis/accept.rb +148 -0
  96. data/lib/openid/yadis/constants.rb +21 -0
  97. data/lib/openid/yadis/discovery.rb +153 -0
  98. data/lib/openid/yadis/filters.rb +205 -0
  99. data/lib/openid/{htmltokenizer.rb → yadis/htmltokenizer.rb} +1 -54
  100. data/lib/openid/yadis/parsehtml.rb +36 -0
  101. data/lib/openid/yadis/services.rb +42 -0
  102. data/lib/openid/yadis/xrds.rb +171 -0
  103. data/lib/openid/yadis/xri.rb +90 -0
  104. data/lib/openid/yadis/xrires.rb +106 -0
  105. data/lib/openid.rb +1 -4
  106. data/test/data/accept.txt +124 -0
  107. data/test/data/dh.txt +29 -0
  108. data/test/data/example-xrds.xml +14 -0
  109. data/test/data/linkparse.txt +587 -0
  110. data/test/data/n2b64 +650 -0
  111. data/test/data/test1-discover.txt +137 -0
  112. data/test/data/test1-parsehtml.txt +128 -0
  113. data/test/data/test_discover/openid.html +11 -0
  114. data/test/data/test_discover/openid2.html +11 -0
  115. data/test/data/test_discover/openid2_xrds.xml +12 -0
  116. data/test/data/test_discover/openid2_xrds_no_local_id.xml +11 -0
  117. data/test/data/test_discover/openid_1_and_2.html +11 -0
  118. data/test/data/test_discover/openid_1_and_2_xrds.xml +16 -0
  119. data/test/data/test_discover/openid_1_and_2_xrds_bad_delegate.xml +17 -0
  120. data/test/data/test_discover/openid_and_yadis.html +12 -0
  121. data/test/data/test_discover/openid_no_delegate.html +10 -0
  122. data/test/data/test_discover/yadis_0entries.xml +12 -0
  123. data/test/data/test_discover/yadis_2_bad_local_id.xml +15 -0
  124. data/test/data/test_discover/yadis_2entries_delegate.xml +22 -0
  125. data/test/data/test_discover/yadis_2entries_idp.xml +21 -0
  126. data/test/data/test_discover/yadis_another_delegate.xml +14 -0
  127. data/test/data/test_discover/yadis_idp.xml +12 -0
  128. data/test/data/test_discover/yadis_idp_delegate.xml +13 -0
  129. data/test/data/test_discover/yadis_no_delegate.xml +11 -0
  130. data/test/data/test_xrds/=j3h.2007.11.14.xrds +25 -0
  131. data/test/data/test_xrds/README +12 -0
  132. data/test/data/test_xrds/delegated-20060809-r1.xrds +34 -0
  133. data/test/data/test_xrds/delegated-20060809-r2.xrds +34 -0
  134. data/test/data/test_xrds/delegated-20060809.xrds +34 -0
  135. data/test/data/test_xrds/no-xrd.xml +7 -0
  136. data/test/data/test_xrds/not-xrds.xml +2 -0
  137. data/test/data/test_xrds/prefixsometimes.xrds +34 -0
  138. data/test/data/test_xrds/ref.xrds +109 -0
  139. data/test/data/test_xrds/sometimesprefix.xrds +34 -0
  140. data/test/data/test_xrds/spoof1.xrds +25 -0
  141. data/test/data/test_xrds/spoof2.xrds +25 -0
  142. data/test/data/test_xrds/spoof3.xrds +37 -0
  143. data/test/data/test_xrds/status222.xrds +9 -0
  144. data/test/data/test_xrds/valid-populated-xrds.xml +39 -0
  145. data/test/data/trustroot.txt +147 -0
  146. data/test/discoverdata.rb +131 -0
  147. data/test/test_accept.rb +170 -0
  148. data/test/test_association.rb +266 -0
  149. data/test/test_associationmanager.rb +899 -0
  150. data/test/test_ax.rb +587 -0
  151. data/test/test_checkid_request.rb +297 -0
  152. data/test/test_consumer.rb +257 -0
  153. data/test/test_cryptutil.rb +117 -0
  154. data/test/test_dh.rb +86 -0
  155. data/test/test_discover.rb +772 -0
  156. data/test/test_discovery_manager.rb +262 -0
  157. data/test/test_extras.rb +35 -0
  158. data/test/test_fetchers.rb +472 -0
  159. data/test/test_filters.rb +270 -0
  160. data/test/test_idres.rb +816 -0
  161. data/test/test_kvform.rb +165 -0
  162. data/test/test_kvpost.rb +65 -0
  163. data/test/test_linkparse.rb +101 -0
  164. data/test/test_message.rb +1058 -0
  165. data/test/test_nonce.rb +89 -0
  166. data/test/test_openid_yadis.rb +178 -0
  167. data/test/test_pape.rb +233 -0
  168. data/test/test_parsehtml.rb +80 -0
  169. data/test/test_responses.rb +63 -0
  170. data/test/test_server.rb +2270 -0
  171. data/test/test_sreg.rb +479 -0
  172. data/test/test_stores.rb +269 -0
  173. data/test/test_trustroot.rb +112 -0
  174. data/test/{urinorm.rb → test_urinorm.rb} +6 -3
  175. data/test/test_util.rb +144 -0
  176. data/test/test_xrds.rb +160 -0
  177. data/test/test_xri.rb +48 -0
  178. data/test/test_xrires.rb +63 -0
  179. data/test/test_yadis_discovery.rb +207 -0
  180. data/test/testutil.rb +116 -0
  181. data/test/util.rb +47 -50
  182. metadata +233 -143
  183. data/examples/consumer.rb +0 -290
  184. data/examples/rails_openid_login_generator/openid_login_generator-0.1.gem +0 -0
  185. data/examples/rails_server/app/controllers/server_controller.rb +0 -190
  186. data/examples/rails_server/app/views/server/decide.rhtml +0 -11
  187. data/examples/rails_server/public/images/rails.png +0 -0
  188. data/lib/hmac-md5.rb +0 -11
  189. data/lib/hmac-rmd160.rb +0 -11
  190. data/lib/openid/discovery.rb +0 -122
  191. data/lib/openid/filestore.rb +0 -315
  192. data/lib/openid/parse.rb +0 -23
  193. data/lib/openid/service.rb +0 -147
  194. data/lib/openid/stores.rb +0 -178
  195. data/test/assoc.rb +0 -38
  196. data/test/consumer.rb +0 -376
  197. data/test/data/brian.xrds +0 -16
  198. data/test/data/brianellin.mylid.xrds +0 -42
  199. data/test/dh.rb +0 -20
  200. data/test/extensions.rb +0 -30
  201. data/test/linkparse.rb +0 -305
  202. data/test/runtests.rb +0 -22
  203. data/test/server2.rb +0 -1053
  204. data/test/service.rb +0 -47
  205. data/test/storetestcase.rb +0 -172
  206. data/test/teststore.rb +0 -47
  207. data/test/trustroot.rb +0 -117
@@ -0,0 +1,772 @@
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_html1
277
+ services = _discover('text/html',
278
+ read_data_file('test_discover/openid.html', false),
279
+ 1)
280
+
281
+ _checkService(services[0],
282
+ "http://www.myopenid.com/server",
283
+ @id_url,
284
+ 'http://smoker.myopenid.com/',
285
+ nil,
286
+ ['1.1'],
287
+ false)
288
+ end
289
+
290
+ def test_html1Fragment
291
+ # Ensure that the Claimed Identifier does not have a fragment if
292
+ # one is supplied in the User Input.
293
+ content_type = 'text/html'
294
+ data = read_data_file('test_discover/openid.html', false)
295
+ expected_services = 1
296
+
297
+ @documents[@id_url] = [content_type, data]
298
+ expected_id = @id_url
299
+ @id_url = @id_url + '#fragment'
300
+ id_url, services = OpenID.discover(@id_url)
301
+
302
+ assert_equal(expected_services, services.length)
303
+ assert_equal(expected_id, id_url)
304
+
305
+ _checkService(services[0],
306
+ "http://www.myopenid.com/server",
307
+ expected_id,
308
+ 'http://smoker.myopenid.com/',
309
+ nil,
310
+ ['1.1'],
311
+ false)
312
+ end
313
+
314
+ def test_html2
315
+ services = _discover('text/html',
316
+ read_data_file('test_discover/openid2.html', false),
317
+ 1)
318
+
319
+ _checkService(services[0],
320
+ "http://www.myopenid.com/server",
321
+ @id_url,
322
+ 'http://smoker.myopenid.com/',
323
+ nil,
324
+ ['2.0'],
325
+ false)
326
+ end
327
+
328
+ def test_html1And2
329
+ services = _discover(
330
+ 'text/html',
331
+ read_data_file('test_discover/openid_1_and_2.html', false),
332
+ 2)
333
+
334
+ services.zip(['2.0', '1.1']).each { |s, t|
335
+ _checkService(s,
336
+ "http://www.myopenid.com/server",
337
+ @id_url,
338
+ 'http://smoker.myopenid.com/',
339
+ nil,
340
+ [t],
341
+ false)
342
+ }
343
+ end
344
+
345
+ def test_yadisEmpty
346
+ services = _discover('application/xrds+xml',
347
+ read_data_file('test_discover/yadis_0entries.xml', false),
348
+ 0)
349
+ end
350
+
351
+ def test_htmlEmptyYadis
352
+ # HTML document has discovery information, but points to an
353
+ # empty Yadis document. The XRDS document pointed to by
354
+ # "openid_and_yadis.html"
355
+ @documents[@id_url + 'xrds'] = ['application/xrds+xml',
356
+ read_data_file('test_discover/yadis_0entries.xml', false)]
357
+
358
+ services = _discover('text/html',
359
+ read_data_file('test_discover/openid_and_yadis.html', false),
360
+ 1)
361
+
362
+ _checkService(services[0],
363
+ "http://www.myopenid.com/server",
364
+ @id_url,
365
+ 'http://smoker.myopenid.com/',
366
+ nil,
367
+ ['1.1'],
368
+ false)
369
+ end
370
+
371
+ def test_yadis1NoDelegate
372
+ services = _discover('application/xrds+xml',
373
+ read_data_file('test_discover/yadis_no_delegate.xml', false),
374
+ 1)
375
+
376
+ _checkService(services[0],
377
+ "http://www.myopenid.com/server",
378
+ @id_url,
379
+ @id_url,
380
+ nil,
381
+ ['1.0'],
382
+ true)
383
+ end
384
+
385
+ def test_yadis2NoLocalID
386
+ services = _discover('application/xrds+xml',
387
+ read_data_file('test_discover/openid2_xrds_no_local_id.xml', false),
388
+ 1)
389
+
390
+ _checkService(services[0],
391
+ "http://www.myopenid.com/server",
392
+ @id_url,
393
+ @id_url,
394
+ nil,
395
+ ['2.0'],
396
+ true)
397
+ end
398
+
399
+ def test_yadis2
400
+ services = _discover('application/xrds+xml',
401
+ read_data_file('test_discover/openid2_xrds.xml', false),
402
+ 1)
403
+
404
+ _checkService(services[0],
405
+ "http://www.myopenid.com/server",
406
+ @id_url,
407
+ 'http://smoker.myopenid.com/',
408
+ nil,
409
+ ['2.0'],
410
+ true)
411
+ end
412
+
413
+ def test_yadis2OP
414
+ services = _discover('application/xrds+xml',
415
+ read_data_file('test_discover/yadis_idp.xml', false),
416
+ 1)
417
+
418
+ _checkService(services[0],
419
+ "http://www.myopenid.com/server",
420
+ nil, nil, nil,
421
+ ['2.0 OP'],
422
+ true)
423
+ end
424
+
425
+ def test_yadis2OPDelegate
426
+ # The delegate tag isn't meaningful for OP entries.
427
+ services = _discover('application/xrds+xml',
428
+ read_data_file('test_discover/yadis_idp_delegate.xml', false),
429
+ 1)
430
+
431
+ _checkService(services[0],
432
+ "http://www.myopenid.com/server",
433
+ nil, nil, nil,
434
+ ['2.0 OP'],
435
+ true)
436
+ end
437
+
438
+ def test_yadis2BadLocalID
439
+ assert_raise(DiscoveryFailure) {
440
+ _discover('application/xrds+xml',
441
+ read_data_file('test_discover/yadis_2_bad_local_id.xml', false),
442
+ 1)
443
+ }
444
+ end
445
+
446
+ def test_yadis1And2
447
+ services = _discover('application/xrds+xml',
448
+ read_data_file('test_discover/openid_1_and_2_xrds.xml', false),
449
+ 1)
450
+
451
+ _checkService(services[0],
452
+ "http://www.myopenid.com/server",
453
+ @id_url,
454
+ 'http://smoker.myopenid.com/',
455
+ nil,
456
+ ['2.0', '1.1'],
457
+ true)
458
+ end
459
+
460
+ def test_yadis1And2BadLocalID
461
+ assert_raise(DiscoveryFailure) {
462
+ _discover('application/xrds+xml',
463
+ read_data_file('test_discover/openid_1_and_2_xrds_bad_delegate.xml', false),
464
+ 1)
465
+ }
466
+ end
467
+ end
468
+
469
+ class MockFetcherForXRIProxy
470
+
471
+ def initialize(documents, proxy_url=Yadis::XRI::ProxyResolver::DEFAULT_PROXY)
472
+ @documents = documents
473
+ @fetchlog = []
474
+ @proxy_url = nil
475
+ end
476
+
477
+ def fetch(url, body=nil, headers=nil, limit=nil)
478
+ @fetchlog << [url, body, headers]
479
+
480
+ u = URI::parse(url)
481
+ proxy_host = u.host
482
+ xri = u.path
483
+ query = u.query
484
+
485
+ if !headers and !query
486
+ raise ArgumentError.new("No headers or query; you probably didn't " +
487
+ "mean to do that.")
488
+ end
489
+
490
+ if xri.starts_with?('/')
491
+ xri = xri[1..-1]
492
+ end
493
+
494
+ begin
495
+ ctype, body = @documents.fetch(xri)
496
+ rescue IndexError
497
+ status = 404
498
+ ctype = 'text/plain'
499
+ body = ''
500
+ else
501
+ status = 200
502
+ end
503
+
504
+ return HTTPResponse._from_raw_data(status, body,
505
+ {'content-type' => ctype}, url)
506
+ end
507
+ end
508
+
509
+ class TestXRIDiscovery < BaseTestDiscovery
510
+
511
+ include TestDataMixin
512
+ include TestUtil
513
+
514
+ def initialize(*args)
515
+ super(*args)
516
+
517
+ @fetcher_class = MockFetcherForXRIProxy
518
+
519
+ @documents = {'=smoker' => ['application/xrds+xml',
520
+ read_data_file('test_discover/yadis_2entries_delegate.xml', false)],
521
+ '=smoker*bad' => ['application/xrds+xml',
522
+ read_data_file('test_discover/yadis_another_delegate.xml', false)]}
523
+ end
524
+
525
+ def test_xri
526
+ user_xri, services = OpenID.discover_xri('=smoker')
527
+
528
+ _checkService(services[0],
529
+ "http://www.myopenid.com/server",
530
+ Yadis::XRI.make_xri("=!1000"),
531
+ 'http://smoker.myopenid.com/',
532
+ Yadis::XRI.make_xri("=!1000"),
533
+ ['1.0'],
534
+ true,
535
+ '=smoker')
536
+
537
+ _checkService(services[1],
538
+ "http://www.livejournal.com/openid/server.bml",
539
+ Yadis::XRI.make_xri("=!1000"),
540
+ 'http://frank.livejournal.com/',
541
+ Yadis::XRI.make_xri("=!1000"),
542
+ ['1.0'],
543
+ true,
544
+ '=smoker')
545
+ end
546
+
547
+ def test_xriNoCanonicalID
548
+ silence_logging {
549
+ user_xri, services = OpenID.discover_xri('=smoker*bad')
550
+ assert(services.empty?)
551
+ }
552
+ end
553
+
554
+ def test_useCanonicalID
555
+ # When there is no delegate, the CanonicalID should be used with
556
+ # XRI.
557
+ endpoint = OpenIDServiceEndpoint.new()
558
+ endpoint.claimed_id = Yadis::XRI.make_xri("=!1000")
559
+ endpoint.canonical_id = Yadis::XRI.make_xri("=!1000")
560
+ assert_equal(endpoint.get_local_id, Yadis::XRI.make_xri("=!1000"))
561
+ end
562
+ end
563
+
564
+ class TestXRIDiscoveryIDP < BaseTestDiscovery
565
+ include TestDataMixin
566
+
567
+ def initialize(*args)
568
+ super(*args)
569
+
570
+ @fetcher_class = MockFetcherForXRIProxy
571
+
572
+ @documents = {'=smoker' => ['application/xrds+xml',
573
+ read_data_file('test_discover/yadis_2entries_idp.xml', false)] }
574
+ end
575
+
576
+ def test_xri
577
+ user_xri, services = OpenID.discover_xri('=smoker')
578
+ assert(!services.empty?, "Expected services, got zero")
579
+ assert_equal(services[0].server_url,
580
+ "http://www.livejournal.com/openid/server.bml")
581
+ end
582
+ end
583
+
584
+ class TestPreferredNamespace < Test::Unit::TestCase
585
+ def initialize(*args)
586
+ super(*args)
587
+
588
+ @cases = [
589
+ [OPENID1_NS, []],
590
+ [OPENID1_NS, ['http://jyte.com/']],
591
+ [OPENID1_NS, [OPENID_1_0_TYPE]],
592
+ [OPENID1_NS, [OPENID_1_1_TYPE]],
593
+ [OPENID2_NS, [OPENID_2_0_TYPE]],
594
+ [OPENID2_NS, [OPENID_IDP_2_0_TYPE]],
595
+ [OPENID2_NS, [OPENID_2_0_TYPE,
596
+ OPENID_1_0_TYPE]],
597
+ [OPENID2_NS, [OPENID_1_0_TYPE,
598
+ OPENID_2_0_TYPE]],
599
+ ]
600
+ end
601
+
602
+ def test_preferred_namespace
603
+
604
+ @cases.each { |expected_ns, type_uris|
605
+ endpoint = OpenIDServiceEndpoint.new()
606
+ endpoint.type_uris = type_uris
607
+ actual_ns = endpoint.preferred_namespace()
608
+ assert_equal(actual_ns, expected_ns)
609
+ }
610
+ end
611
+ end
612
+
613
+ class TestIsOPIdentifier < Test::Unit::TestCase
614
+ def setup
615
+ @endpoint = OpenIDServiceEndpoint.new()
616
+ end
617
+
618
+ def test_none
619
+ assert(!@endpoint.is_op_identifier())
620
+ end
621
+
622
+ def test_openid1_0
623
+ @endpoint.type_uris = [OPENID_1_0_TYPE]
624
+ assert(!@endpoint.is_op_identifier())
625
+ end
626
+
627
+ def test_openid1_1
628
+ @endpoint.type_uris = [OPENID_1_1_TYPE]
629
+ assert(!@endpoint.is_op_identifier())
630
+ end
631
+
632
+ def test_openid2
633
+ @endpoint.type_uris = [OPENID_2_0_TYPE]
634
+ assert(!@endpoint.is_op_identifier())
635
+ end
636
+
637
+ def test_openid2OP
638
+ @endpoint.type_uris = [OPENID_IDP_2_0_TYPE]
639
+ assert(@endpoint.is_op_identifier())
640
+ end
641
+
642
+ def test_multipleMissing
643
+ @endpoint.type_uris = [OPENID_2_0_TYPE,
644
+ OPENID_1_0_TYPE]
645
+ assert(!@endpoint.is_op_identifier())
646
+ end
647
+
648
+ def test_multiplePresent
649
+ @endpoint.type_uris = [OPENID_2_0_TYPE,
650
+ OPENID_1_0_TYPE,
651
+ OPENID_IDP_2_0_TYPE]
652
+ assert(@endpoint.is_op_identifier())
653
+ end
654
+ end
655
+
656
+ class TestFromOPEndpointURL < Test::Unit::TestCase
657
+ def setup
658
+ @op_endpoint_url = 'http://example.com/op/endpoint'
659
+ @endpoint = OpenIDServiceEndpoint.from_op_endpoint_url(@op_endpoint_url)
660
+ end
661
+
662
+ def test_isOPEndpoint
663
+ assert(@endpoint.is_op_identifier())
664
+ end
665
+
666
+ def test_noIdentifiers
667
+ assert_equal(@endpoint.get_local_id, nil)
668
+ assert_equal(@endpoint.claimed_id, nil)
669
+ end
670
+
671
+ def test_compatibility
672
+ assert(!@endpoint.compatibility_mode())
673
+ end
674
+
675
+ def test_canonical_id
676
+ assert_equal(@endpoint.canonical_id, nil)
677
+ end
678
+
679
+ def test_serverURL
680
+ assert_equal(@endpoint.server_url, @op_endpoint_url)
681
+ end
682
+ end
683
+
684
+ class TestDiscoverFunction < Test::Unit::TestCase
685
+ def test_discover_function
686
+ # XXX these were all different tests in python, but they're
687
+ # combined here so I only have to use with_method_overridden
688
+ # once.
689
+ discoverXRI = Proc.new { |identifier|
690
+ return 'XRI'
691
+ }
692
+
693
+ discoverURI = Proc.new { |identifier|
694
+ return 'URI'
695
+ }
696
+
697
+ OpenID.extend(OverrideMethodMixin)
698
+
699
+ OpenID.with_method_overridden(:discover_uri, discoverURI) do
700
+ OpenID.with_method_overridden(:discover_xri, discoverXRI) do
701
+ assert_equal('URI', OpenID.discover('http://woo!'))
702
+ assert_equal('URI', OpenID.discover('not a URL or XRI'))
703
+ assert_equal('XRI', OpenID.discover('xri://=something'))
704
+ assert_equal('XRI', OpenID.discover('=something'))
705
+ end
706
+ end
707
+ end
708
+ end
709
+
710
+ class TestEndpointSupportsType < Test::Unit::TestCase
711
+ def setup
712
+ @endpoint = OpenIDServiceEndpoint.new()
713
+ end
714
+
715
+ def failUnlessSupportsOnly(*types)
716
+ ['foo',
717
+ OPENID_1_1_TYPE,
718
+ OPENID_1_0_TYPE,
719
+ OPENID_2_0_TYPE,
720
+ OPENID_IDP_2_0_TYPE].each { |t|
721
+ if types.member?(t)
722
+ assert(@endpoint.supports_type(t),
723
+ sprintf("Must support %s", t))
724
+ else
725
+ assert(!@endpoint.supports_type(t),
726
+ sprintf("Shouldn't support %s", t))
727
+ end
728
+ }
729
+ end
730
+
731
+ def test_supportsNothing
732
+ failUnlessSupportsOnly()
733
+ end
734
+
735
+ def test_openid2
736
+ @endpoint.type_uris = [OPENID_2_0_TYPE]
737
+ failUnlessSupportsOnly(OPENID_2_0_TYPE)
738
+ end
739
+
740
+ def test_openid2provider
741
+ @endpoint.type_uris = [OPENID_IDP_2_0_TYPE]
742
+ failUnlessSupportsOnly(OPENID_IDP_2_0_TYPE,
743
+ OPENID_2_0_TYPE)
744
+ end
745
+
746
+ def test_openid1_0
747
+ @endpoint.type_uris = [OPENID_1_0_TYPE]
748
+ failUnlessSupportsOnly(OPENID_1_0_TYPE)
749
+ end
750
+
751
+ def test_openid1_1
752
+ @endpoint.type_uris = [OPENID_1_1_TYPE]
753
+ failUnlessSupportsOnly(OPENID_1_1_TYPE)
754
+ end
755
+
756
+ def test_multiple
757
+ @endpoint.type_uris = [OPENID_1_1_TYPE,
758
+ OPENID_2_0_TYPE]
759
+ failUnlessSupportsOnly(OPENID_1_1_TYPE,
760
+ OPENID_2_0_TYPE)
761
+ end
762
+
763
+ def test_multipleWithProvider
764
+ @endpoint.type_uris = [OPENID_1_1_TYPE,
765
+ OPENID_2_0_TYPE,
766
+ OPENID_IDP_2_0_TYPE]
767
+ failUnlessSupportsOnly(OPENID_1_1_TYPE,
768
+ OPENID_2_0_TYPE,
769
+ OPENID_IDP_2_0_TYPE)
770
+ end
771
+ end
772
+ end