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,480 @@
1
+ # Functions to discover OpenID endpoints from identifiers.
2
+
3
+ require 'uri'
4
+ require 'openid/util'
5
+ require 'openid/fetchers'
6
+ require 'openid/urinorm'
7
+ require 'openid/message'
8
+ require 'openid/yadis/discovery'
9
+ require 'openid/yadis/xrds'
10
+ require 'openid/yadis/xri'
11
+ require 'openid/yadis/services'
12
+ require 'openid/yadis/filters'
13
+ require 'openid/consumer/html_parse'
14
+ require 'openid/yadis/xrires'
15
+
16
+ module OpenID
17
+
18
+ OPENID_1_0_NS = 'http://openid.net/xmlns/1.0'
19
+ OPENID_IDP_2_0_TYPE = 'http://specs.openid.net/auth/2.0/server'
20
+ OPENID_2_0_TYPE = 'http://specs.openid.net/auth/2.0/signon'
21
+ OPENID_1_1_TYPE = 'http://openid.net/signon/1.1'
22
+ OPENID_1_0_TYPE = 'http://openid.net/signon/1.0'
23
+
24
+ OPENID_1_0_MESSAGE_NS = OPENID1_NS
25
+ OPENID_2_0_MESSAGE_NS = OPENID2_NS
26
+
27
+ # Object representing an OpenID service endpoint.
28
+ class OpenIDServiceEndpoint
29
+
30
+ # OpenID service type URIs, listed in order of preference. The
31
+ # ordering of this list affects yadis and XRI service discovery.
32
+ OPENID_TYPE_URIS = [
33
+ OPENID_IDP_2_0_TYPE,
34
+
35
+ OPENID_2_0_TYPE,
36
+ OPENID_1_1_TYPE,
37
+ OPENID_1_0_TYPE,
38
+ ]
39
+
40
+ # the verified identifier.
41
+ attr_accessor :claimed_id
42
+
43
+ # For XRI, the persistent identifier.
44
+ attr_accessor :canonical_id
45
+
46
+ attr_accessor :server_url, :type_uris, :local_id, :used_yadis
47
+
48
+ def initialize
49
+ @claimed_id = nil
50
+ @server_url = nil
51
+ @type_uris = []
52
+ @local_id = nil
53
+ @canonical_id = nil
54
+ @used_yadis = false # whether this came from an XRDS
55
+ @display_identifier = nil
56
+ end
57
+
58
+ def display_identifier
59
+ @display_identifier || @claimed_id
60
+ end
61
+
62
+ def display_identifier=(display_identifier)
63
+ @display_identifier = display_identifier
64
+ end
65
+
66
+ def uses_extension(extension_uri)
67
+ return @type_uris.member?(extension_uri)
68
+ end
69
+
70
+ def preferred_namespace
71
+ if (@type_uris.member?(OPENID_IDP_2_0_TYPE) or
72
+ @type_uris.member?(OPENID_2_0_TYPE))
73
+ return OPENID_2_0_MESSAGE_NS
74
+ else
75
+ return OPENID_1_0_MESSAGE_NS
76
+ end
77
+ end
78
+
79
+ def supports_type(type_uri)
80
+ # Does this endpoint support this type?
81
+ #
82
+ # I consider C{/server} endpoints to implicitly support C{/signon}.
83
+ (
84
+ @type_uris.member?(type_uri) or
85
+ (type_uri == OPENID_2_0_TYPE and is_op_identifier())
86
+ )
87
+ end
88
+
89
+ def compatibility_mode
90
+ return preferred_namespace() != OPENID_2_0_MESSAGE_NS
91
+ end
92
+
93
+ def is_op_identifier
94
+ return @type_uris.member?(OPENID_IDP_2_0_TYPE)
95
+ end
96
+
97
+ def parse_service(yadis_url, uri, type_uris, service_element)
98
+ # Set the state of this object based on the contents of the
99
+ # service element.
100
+ @type_uris = type_uris
101
+ @server_url = uri
102
+ @used_yadis = true
103
+
104
+ if !is_op_identifier()
105
+ # XXX: This has crappy implications for Service elements that
106
+ # contain both 'server' and 'signon' Types. But that's a
107
+ # pathological configuration anyway, so I don't think I care.
108
+ @local_id = OpenID.find_op_local_identifier(service_element,
109
+ @type_uris)
110
+ @claimed_id = yadis_url
111
+ end
112
+ end
113
+
114
+ def get_local_id
115
+ # Return the identifier that should be sent as the
116
+ # openid.identity parameter to the server.
117
+
118
+ # I looked at this conditional and thought "ah-hah! there's the
119
+ # bug!" but Python actually makes that one big expression
120
+ # somehow, i.e. "x is x is x" is not the same thing as "(x is
121
+ # x) is x". That's pretty weird, dude. -- kmt, 1/07
122
+ if @local_id.nil? and @canonical_id.nil?
123
+ return @claimed_id
124
+ else
125
+ return (@local_id or @canonical_id)
126
+ end
127
+ end
128
+
129
+ def self.from_basic_service_endpoint(endpoint)
130
+ # Create a new instance of this class from the endpoint object
131
+ # passed in.
132
+ #
133
+ # @return: nil or OpenIDServiceEndpoint for this endpoint object"""
134
+
135
+ type_uris = endpoint.match_types(OPENID_TYPE_URIS)
136
+
137
+ # If any Type URIs match and there is an endpoint URI specified,
138
+ # then this is an OpenID endpoint
139
+ if (!type_uris.nil? and !type_uris.empty?) and !endpoint.uri.nil?
140
+ openid_endpoint = self.new
141
+ openid_endpoint.parse_service(
142
+ endpoint.yadis_url,
143
+ endpoint.uri,
144
+ endpoint.type_uris,
145
+ endpoint.service_element)
146
+ else
147
+ openid_endpoint = nil
148
+ end
149
+
150
+ return openid_endpoint
151
+ end
152
+
153
+ def self.from_html(uri, html)
154
+ # Parse the given document as HTML looking for an OpenID <link
155
+ # rel=...>
156
+ #
157
+ # @rtype: [OpenIDServiceEndpoint]
158
+
159
+ discovery_types = [
160
+ [OPENID_2_0_TYPE, 'openid2.provider', 'openid2.local_id'],
161
+ [OPENID_1_1_TYPE, 'openid.server', 'openid.delegate'],
162
+ ]
163
+
164
+ link_attrs = OpenID.parse_link_attrs(html)
165
+ services = []
166
+ discovery_types.each { |type_uri, op_endpoint_rel, local_id_rel|
167
+
168
+ op_endpoint_url = OpenID.find_first_href(link_attrs, op_endpoint_rel)
169
+
170
+ if !op_endpoint_url
171
+ next
172
+ end
173
+
174
+ service = self.new
175
+ service.claimed_id = uri
176
+ service.local_id = OpenID.find_first_href(link_attrs, local_id_rel)
177
+ service.server_url = op_endpoint_url
178
+ service.type_uris = [type_uri]
179
+
180
+ services << service
181
+ }
182
+
183
+ return services
184
+ end
185
+
186
+ def self.from_xrds(uri, xrds)
187
+ # Parse the given document as XRDS looking for OpenID services.
188
+ #
189
+ # @rtype: [OpenIDServiceEndpoint]
190
+ #
191
+ # @raises L{XRDSError}: When the XRDS does not parse.
192
+ return Yadis::apply_filter(uri, xrds, self)
193
+ end
194
+
195
+ def self.from_discovery_result(discoveryResult)
196
+ # Create endpoints from a DiscoveryResult.
197
+ #
198
+ # @type discoveryResult: L{DiscoveryResult}
199
+ #
200
+ # @rtype: list of L{OpenIDServiceEndpoint}
201
+ #
202
+ # @raises L{XRDSError}: When the XRDS does not parse.
203
+ if discoveryResult.is_xrds()
204
+ meth = self.method('from_xrds')
205
+ else
206
+ meth = self.method('from_html')
207
+ end
208
+
209
+ return meth.call(discoveryResult.normalized_uri,
210
+ discoveryResult.response_text)
211
+ end
212
+
213
+ def self.from_op_endpoint_url(op_endpoint_url)
214
+ # Construct an OP-Identifier OpenIDServiceEndpoint object for
215
+ # a given OP Endpoint URL
216
+ #
217
+ # @param op_endpoint_url: The URL of the endpoint
218
+ # @rtype: OpenIDServiceEndpoint
219
+ service = self.new
220
+ service.server_url = op_endpoint_url
221
+ service.type_uris = [OPENID_IDP_2_0_TYPE]
222
+ return service
223
+ end
224
+
225
+ def to_s
226
+ return sprintf("<%s server_url=%s claimed_id=%s " +
227
+ "local_id=%s canonical_id=%s used_yadis=%s>",
228
+ self.class, @server_url, @claimed_id,
229
+ @local_id, @canonical_id, @used_yadis)
230
+ end
231
+ end
232
+
233
+ def self.find_op_local_identifier(service_element, type_uris)
234
+ # Find the OP-Local Identifier for this xrd:Service element.
235
+ #
236
+ # This considers openid:Delegate to be a synonym for xrd:LocalID
237
+ # if both OpenID 1.X and OpenID 2.0 types are present. If only
238
+ # OpenID 1.X is present, it returns the value of
239
+ # openid:Delegate. If only OpenID 2.0 is present, it returns the
240
+ # value of xrd:LocalID. If there is more than one LocalID tag and
241
+ # the values are different, it raises a DiscoveryFailure. This is
242
+ # also triggered when the xrd:LocalID and openid:Delegate tags are
243
+ # different.
244
+
245
+ # XXX: Test this function on its own!
246
+
247
+ # Build the list of tags that could contain the OP-Local
248
+ # Identifier
249
+ local_id_tags = []
250
+ if type_uris.member?(OPENID_1_1_TYPE) or
251
+ type_uris.member?(OPENID_1_0_TYPE)
252
+ # local_id_tags << Yadis::nsTag(OPENID_1_0_NS, 'openid', 'Delegate')
253
+ service_element.add_namespace('openid', OPENID_1_0_NS)
254
+ local_id_tags << "openid:Delegate"
255
+ end
256
+
257
+ if type_uris.member?(OPENID_2_0_TYPE)
258
+ # local_id_tags.append(Yadis::nsTag(XRD_NS_2_0, 'xrd', 'LocalID'))
259
+ service_element.add_namespace('xrd', Yadis::XRD_NS_2_0)
260
+ local_id_tags << "xrd:LocalID"
261
+ end
262
+
263
+ # Walk through all the matching tags and make sure that they all
264
+ # have the same value
265
+ local_id = nil
266
+ local_id_tags.each { |local_id_tag|
267
+ service_element.each_element(local_id_tag) { |local_id_element|
268
+ if local_id.nil?
269
+ local_id = local_id_element.text
270
+ elsif local_id != local_id_element.text
271
+ format = 'More than one %s tag found in one service element'
272
+ message = sprintf(format, local_id_tag)
273
+ raise DiscoveryFailure.new(message, nil)
274
+ end
275
+ }
276
+ }
277
+
278
+ return local_id
279
+ end
280
+
281
+ def self.normalize_url(url)
282
+ # Normalize a URL, converting normalization failures to
283
+ # DiscoveryFailure
284
+ begin
285
+ normalized = URINorm.urinorm(url)
286
+ rescue ArgumentError => why
287
+ raise DiscoveryFailure.new(sprintf('Normalizing identifier: %s', why[0]), nil)
288
+ else
289
+ defragged = URI::parse(normalized)
290
+ defragged.fragment = nil
291
+ return defragged.normalize.to_s
292
+ end
293
+ end
294
+
295
+ def self.best_matching_service(service, preferred_types)
296
+ # Return the index of the first matching type, or something higher
297
+ # if no type matches.
298
+ #
299
+ # This provides an ordering in which service elements that contain
300
+ # a type that comes earlier in the preferred types list come
301
+ # before service elements that come later. If a service element
302
+ # has more than one type, the most preferred one wins.
303
+ preferred_types.each_with_index { |value, index|
304
+ if service.type_uris.member?(value)
305
+ return index
306
+ end
307
+ }
308
+
309
+ return preferred_types.length
310
+ end
311
+
312
+ def self.arrange_by_type(service_list, preferred_types)
313
+ # Rearrange service_list in a new list so services are ordered by
314
+ # types listed in preferred_types. Return the new list.
315
+
316
+ # Build a list with the service elements in tuples whose
317
+ # comparison will prefer the one with the best matching service
318
+ prio_services = []
319
+
320
+ service_list.each_with_index { |s, index|
321
+ prio_services << [best_matching_service(s, preferred_types), index, s]
322
+ }
323
+
324
+ prio_services.sort!
325
+
326
+ # Now that the services are sorted by priority, remove the sort
327
+ # keys from the list.
328
+ (0...prio_services.length).each { |i|
329
+ prio_services[i] = prio_services[i][2]
330
+ }
331
+
332
+ return prio_services
333
+ end
334
+
335
+ def self.get_op_or_user_services(openid_services)
336
+ # Extract OP Identifier services. If none found, return the rest,
337
+ # sorted with most preferred first according to
338
+ # OpenIDServiceEndpoint.openid_type_uris.
339
+ #
340
+ # openid_services is a list of OpenIDServiceEndpoint objects.
341
+ #
342
+ # Returns a list of OpenIDServiceEndpoint objects.
343
+
344
+ op_services = arrange_by_type(openid_services, [OPENID_IDP_2_0_TYPE])
345
+
346
+ openid_services = arrange_by_type(openid_services,
347
+ OpenIDServiceEndpoint::OPENID_TYPE_URIS)
348
+
349
+ if !op_services.empty?
350
+ return op_services
351
+ else
352
+ return openid_services
353
+ end
354
+ end
355
+
356
+ def self.discover_yadis(uri)
357
+ # Discover OpenID services for a URI. Tries Yadis and falls back
358
+ # on old-style <link rel='...'> discovery if Yadis fails.
359
+ #
360
+ # @param uri: normalized identity URL
361
+ # @type uri: str
362
+ #
363
+ # @return: (claimed_id, services)
364
+ # @rtype: (str, list(OpenIDServiceEndpoint))
365
+ #
366
+ # @raises DiscoveryFailure: when discovery fails.
367
+
368
+ # Might raise a yadis.discover.DiscoveryFailure if no document
369
+ # came back for that URI at all. I don't think falling back to
370
+ # OpenID 1.0 discovery on the same URL will help, so don't bother
371
+ # to catch it.
372
+ response = Yadis.discover(uri)
373
+
374
+ yadis_url = response.normalized_uri
375
+ body = response.response_text
376
+
377
+ begin
378
+ openid_services = OpenIDServiceEndpoint.from_xrds(yadis_url, body)
379
+ rescue Yadis::XRDSError
380
+ # Does not parse as a Yadis XRDS file
381
+ openid_services = []
382
+ end
383
+
384
+ if openid_services.empty?
385
+ # Either not an XRDS or there are no OpenID services.
386
+
387
+ if response.is_xrds
388
+ # if we got the Yadis content-type or followed the Yadis
389
+ # header, re-fetch the document without following the Yadis
390
+ # header, with no Accept header.
391
+ return self.discover_no_yadis(uri)
392
+ end
393
+
394
+ # Try to parse the response as HTML.
395
+ # <link rel="...">
396
+ openid_services = OpenIDServiceEndpoint.from_html(yadis_url, body)
397
+ end
398
+
399
+ return [yadis_url, self.get_op_or_user_services(openid_services)]
400
+ end
401
+
402
+ def self.discover_xri(iname)
403
+ endpoints = []
404
+
405
+ begin
406
+ canonical_id, services = Yadis::XRI::ProxyResolver.new().query(
407
+ iname, OpenIDServiceEndpoint::OPENID_TYPE_URIS)
408
+
409
+ if canonical_id.nil?
410
+ raise Yadis::XRDSError.new(sprintf('No CanonicalID found for XRI %s', iname))
411
+ end
412
+
413
+ flt = Yadis.make_filter(OpenIDServiceEndpoint)
414
+
415
+ services.each { |service_element|
416
+ endpoints += flt.get_service_endpoints(iname, service_element)
417
+ }
418
+ rescue Yadis::XRDSError => why
419
+ Util.log('xrds error on ' + iname + ': ' + why.to_s)
420
+ end
421
+
422
+ endpoints.each { |endpoint|
423
+ # Is there a way to pass this through the filter to the endpoint
424
+ # constructor instead of tacking it on after?
425
+ endpoint.canonical_id = canonical_id
426
+ endpoint.claimed_id = canonical_id
427
+ endpoint.display_identifier = iname
428
+ }
429
+
430
+ # FIXME: returned xri should probably be in some normal form
431
+ return [iname, self.get_op_or_user_services(endpoints)]
432
+ end
433
+
434
+ def self.discover_no_yadis(uri)
435
+ http_resp = OpenID.fetch(uri)
436
+ if http_resp.code != "200"
437
+ raise DiscoveryFailure.new(
438
+ "HTTP Response status from identity URL host is not \"200\". "\
439
+ "Got status #{http_resp.code.inspect}", http_resp)
440
+ end
441
+
442
+ claimed_id = http_resp.final_url
443
+ openid_services = OpenIDServiceEndpoint.from_html(
444
+ claimed_id, http_resp.body)
445
+ return [claimed_id, openid_services]
446
+ end
447
+
448
+ def self.discover_uri(uri)
449
+ # Hack to work around URI parsing for URls with *no* scheme.
450
+ if uri.index("://").nil?
451
+ uri = 'http://' + uri
452
+ end
453
+
454
+ begin
455
+ parsed = URI::parse(uri)
456
+ rescue URI::InvalidURIError => why
457
+ raise DiscoveryFailure.new("URI is not valid: #{why.message}", nil)
458
+ end
459
+
460
+ if !parsed.scheme.nil? and !parsed.scheme.empty?
461
+ if !['http', 'https'].member?(parsed.scheme)
462
+ raise DiscoveryFailure.new(
463
+ "URI scheme #{parsed.scheme} is not HTTP or HTTPS", nil)
464
+ end
465
+ end
466
+
467
+ uri = self.normalize_url(uri)
468
+ claimed_id, openid_services = self.discover_yadis(uri)
469
+ claimed_id = self.normalize_url(claimed_id)
470
+ return [claimed_id, openid_services]
471
+ end
472
+
473
+ def self.discover(identifier)
474
+ if Yadis::XRI::identifier_scheme(identifier) == :xri
475
+ normalized_identifier, services = discover_xri(identifier)
476
+ else
477
+ return discover_uri(identifier)
478
+ end
479
+ end
480
+ end
@@ -0,0 +1,123 @@
1
+ module OpenID
2
+ class Consumer
3
+
4
+ # A set of discovered services, for tracking which providers have
5
+ # been attempted for an OpenID identifier
6
+ class DiscoveredServices
7
+ attr_reader :current
8
+
9
+ def initialize(starting_url, yadis_url, services)
10
+ @starting_url = starting_url
11
+ @yadis_url = yadis_url
12
+ @services = services.dup
13
+ @current = nil
14
+ end
15
+
16
+ def next
17
+ @current = @services.shift
18
+ end
19
+
20
+ def for_url?(url)
21
+ [@starting_url, @yadis_url].member?(url)
22
+ end
23
+
24
+ def started?
25
+ !@current.nil?
26
+ end
27
+
28
+ def empty?
29
+ @services.empty?
30
+ end
31
+ end
32
+
33
+ # Manages calling discovery and tracking which endpoints have
34
+ # already been attempted.
35
+ class DiscoveryManager
36
+ def initialize(session, url, session_key_suffix=nil)
37
+ @url = url
38
+
39
+ @session = session
40
+ @session_key_suffix = session_key_suffix || 'auth'
41
+ end
42
+
43
+ def get_next_service
44
+ manager = get_manager
45
+ if !manager.nil? && manager.empty?
46
+ destroy_manager
47
+ manager = nil
48
+ end
49
+
50
+ if manager.nil?
51
+ yadis_url, services = yield @url
52
+ manager = create_manager(yadis_url, services)
53
+ end
54
+
55
+ if !manager.nil?
56
+ service = manager.next
57
+ store(manager)
58
+ else
59
+ service = nil
60
+ end
61
+
62
+ return service
63
+ end
64
+
65
+ def cleanup(force=false)
66
+ manager = get_manager(force)
67
+ if !manager.nil?
68
+ service = manager.current
69
+ destroy_manager(force)
70
+ else
71
+ service = nil
72
+ end
73
+ return service
74
+ end
75
+
76
+ protected
77
+
78
+ def get_manager(force=false)
79
+ manager = load
80
+ if force || manager.nil? || manager.for_url?(@url)
81
+ return manager
82
+ else
83
+ return nil
84
+ end
85
+ end
86
+
87
+ def create_manager(yadis_url, services)
88
+ manager = get_manager
89
+ if !manager.nil?
90
+ raise StandardError, "There is already a manager for #{yadis_url}"
91
+ end
92
+ if services.empty?
93
+ return nil
94
+ end
95
+ manager = DiscoveredServices.new(@url, yadis_url, services)
96
+ store(manager)
97
+ return manager
98
+ end
99
+
100
+ def destroy_manager(force=false)
101
+ if !get_manager(force).nil?
102
+ destroy!
103
+ end
104
+ end
105
+
106
+ def session_key
107
+ 'OpenID::Consumer::DiscoveredServices::' + @session_key_suffix
108
+ end
109
+
110
+ def store(manager)
111
+ @session[session_key] = manager
112
+ end
113
+
114
+ def load
115
+ @session[session_key]
116
+ end
117
+
118
+ def destroy!
119
+ @session[session_key] = nil
120
+ end
121
+ end
122
+ end
123
+ end