entp-ruby-openid 2.2

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 (200) hide show
  1. data/CHANGELOG +215 -0
  2. data/INSTALL +47 -0
  3. data/LICENSE +210 -0
  4. data/NOTICE +2 -0
  5. data/README +85 -0
  6. data/UPGRADE +127 -0
  7. data/admin/runtests.rb +45 -0
  8. data/examples/README +32 -0
  9. data/examples/active_record_openid_store/README +58 -0
  10. data/examples/active_record_openid_store/XXX_add_open_id_store_to_db.rb +24 -0
  11. data/examples/active_record_openid_store/XXX_upgrade_open_id_store.rb +26 -0
  12. data/examples/active_record_openid_store/init.rb +8 -0
  13. data/examples/active_record_openid_store/lib/association.rb +10 -0
  14. data/examples/active_record_openid_store/lib/nonce.rb +3 -0
  15. data/examples/active_record_openid_store/lib/open_id_setting.rb +4 -0
  16. data/examples/active_record_openid_store/lib/openid_ar_store.rb +57 -0
  17. data/examples/active_record_openid_store/test/store_test.rb +212 -0
  18. data/examples/discover +49 -0
  19. data/examples/rails_openid/README +153 -0
  20. data/examples/rails_openid/Rakefile +10 -0
  21. data/examples/rails_openid/app/controllers/application.rb +4 -0
  22. data/examples/rails_openid/app/controllers/consumer_controller.rb +125 -0
  23. data/examples/rails_openid/app/controllers/login_controller.rb +45 -0
  24. data/examples/rails_openid/app/controllers/server_controller.rb +265 -0
  25. data/examples/rails_openid/app/helpers/application_helper.rb +3 -0
  26. data/examples/rails_openid/app/helpers/login_helper.rb +2 -0
  27. data/examples/rails_openid/app/helpers/server_helper.rb +9 -0
  28. data/examples/rails_openid/app/views/consumer/index.rhtml +81 -0
  29. data/examples/rails_openid/app/views/layouts/server.rhtml +68 -0
  30. data/examples/rails_openid/app/views/login/index.rhtml +56 -0
  31. data/examples/rails_openid/app/views/server/decide.rhtml +26 -0
  32. data/examples/rails_openid/config/boot.rb +19 -0
  33. data/examples/rails_openid/config/database.yml +74 -0
  34. data/examples/rails_openid/config/environment.rb +54 -0
  35. data/examples/rails_openid/config/environments/development.rb +19 -0
  36. data/examples/rails_openid/config/environments/production.rb +19 -0
  37. data/examples/rails_openid/config/environments/test.rb +19 -0
  38. data/examples/rails_openid/config/routes.rb +24 -0
  39. data/examples/rails_openid/doc/README_FOR_APP +2 -0
  40. data/examples/rails_openid/public/404.html +8 -0
  41. data/examples/rails_openid/public/500.html +8 -0
  42. data/examples/rails_openid/public/dispatch.cgi +12 -0
  43. data/examples/rails_openid/public/dispatch.fcgi +26 -0
  44. data/examples/rails_openid/public/dispatch.rb +12 -0
  45. data/examples/rails_openid/public/favicon.ico +0 -0
  46. data/examples/rails_openid/public/images/openid_login_bg.gif +0 -0
  47. data/examples/rails_openid/public/javascripts/controls.js +750 -0
  48. data/examples/rails_openid/public/javascripts/dragdrop.js +584 -0
  49. data/examples/rails_openid/public/javascripts/effects.js +854 -0
  50. data/examples/rails_openid/public/javascripts/prototype.js +1785 -0
  51. data/examples/rails_openid/public/robots.txt +1 -0
  52. data/examples/rails_openid/script/about +3 -0
  53. data/examples/rails_openid/script/breakpointer +3 -0
  54. data/examples/rails_openid/script/console +3 -0
  55. data/examples/rails_openid/script/destroy +3 -0
  56. data/examples/rails_openid/script/generate +3 -0
  57. data/examples/rails_openid/script/performance/benchmarker +3 -0
  58. data/examples/rails_openid/script/performance/profiler +3 -0
  59. data/examples/rails_openid/script/plugin +3 -0
  60. data/examples/rails_openid/script/process/reaper +3 -0
  61. data/examples/rails_openid/script/process/spawner +3 -0
  62. data/examples/rails_openid/script/process/spinner +3 -0
  63. data/examples/rails_openid/script/runner +3 -0
  64. data/examples/rails_openid/script/server +3 -0
  65. data/examples/rails_openid/test/functional/login_controller_test.rb +18 -0
  66. data/examples/rails_openid/test/functional/server_controller_test.rb +18 -0
  67. data/examples/rails_openid/test/test_helper.rb +28 -0
  68. data/lib/hmac/hmac.rb +112 -0
  69. data/lib/hmac/sha1.rb +11 -0
  70. data/lib/hmac/sha2.rb +25 -0
  71. data/lib/openid.rb +22 -0
  72. data/lib/openid/association.rb +249 -0
  73. data/lib/openid/consumer.rb +395 -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 +497 -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 +150 -0
  81. data/lib/openid/cryptutil.rb +115 -0
  82. data/lib/openid/dh.rb +89 -0
  83. data/lib/openid/extension.rb +39 -0
  84. data/lib/openid/extensions/ax.rb +539 -0
  85. data/lib/openid/extensions/oauth.rb +91 -0
  86. data/lib/openid/extensions/pape.rb +179 -0
  87. data/lib/openid/extensions/sreg.rb +277 -0
  88. data/lib/openid/extras.rb +11 -0
  89. data/lib/openid/fetchers.rb +258 -0
  90. data/lib/openid/kvform.rb +136 -0
  91. data/lib/openid/kvpost.rb +58 -0
  92. data/lib/openid/message.rb +553 -0
  93. data/lib/openid/protocolerror.rb +12 -0
  94. data/lib/openid/server.rb +1544 -0
  95. data/lib/openid/store.rb +10 -0
  96. data/lib/openid/store/filesystem.rb +272 -0
  97. data/lib/openid/store/interface.rb +75 -0
  98. data/lib/openid/store/memcache.rb +109 -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 +119 -0
  104. data/lib/openid/version.rb +3 -0
  105. data/lib/openid/yadis.rb +15 -0
  106. data/lib/openid/yadis/accept.rb +148 -0
  107. data/lib/openid/yadis/constants.rb +21 -0
  108. data/lib/openid/yadis/discovery.rb +153 -0
  109. data/lib/openid/yadis/filters.rb +205 -0
  110. data/lib/openid/yadis/htmltokenizer.rb +305 -0
  111. data/lib/openid/yadis/parsehtml.rb +45 -0
  112. data/lib/openid/yadis/services.rb +42 -0
  113. data/lib/openid/yadis/xrds.rb +155 -0
  114. data/lib/openid/yadis/xri.rb +90 -0
  115. data/lib/openid/yadis/xrires.rb +91 -0
  116. data/test/data/test_discover/openid_utf8.html +11 -0
  117. data/test/support/test_data_mixin.rb +127 -0
  118. data/test/support/test_util.rb +53 -0
  119. data/test/support/yadis_data.rb +131 -0
  120. data/test/support/yadis_data/accept.txt +124 -0
  121. data/test/support/yadis_data/dh.txt +29 -0
  122. data/test/support/yadis_data/example-xrds.xml +14 -0
  123. data/test/support/yadis_data/linkparse.txt +587 -0
  124. data/test/support/yadis_data/n2b64 +650 -0
  125. data/test/support/yadis_data/test1-discover.txt +137 -0
  126. data/test/support/yadis_data/test1-parsehtml.txt +152 -0
  127. data/test/support/yadis_data/test_discover/malformed_meta_tag.html +19 -0
  128. data/test/support/yadis_data/test_discover/openid.html +11 -0
  129. data/test/support/yadis_data/test_discover/openid2.html +11 -0
  130. data/test/support/yadis_data/test_discover/openid2_xrds.xml +12 -0
  131. data/test/support/yadis_data/test_discover/openid2_xrds_no_local_id.xml +11 -0
  132. data/test/support/yadis_data/test_discover/openid_1_and_2.html +11 -0
  133. data/test/support/yadis_data/test_discover/openid_1_and_2_xrds.xml +16 -0
  134. data/test/support/yadis_data/test_discover/openid_1_and_2_xrds_bad_delegate.xml +17 -0
  135. data/test/support/yadis_data/test_discover/openid_and_yadis.html +12 -0
  136. data/test/support/yadis_data/test_discover/openid_no_delegate.html +10 -0
  137. data/test/support/yadis_data/test_discover/openid_utf8.html +11 -0
  138. data/test/support/yadis_data/test_discover/yadis_0entries.xml +12 -0
  139. data/test/support/yadis_data/test_discover/yadis_2_bad_local_id.xml +15 -0
  140. data/test/support/yadis_data/test_discover/yadis_2entries_delegate.xml +22 -0
  141. data/test/support/yadis_data/test_discover/yadis_2entries_idp.xml +21 -0
  142. data/test/support/yadis_data/test_discover/yadis_another_delegate.xml +14 -0
  143. data/test/support/yadis_data/test_discover/yadis_idp.xml +12 -0
  144. data/test/support/yadis_data/test_discover/yadis_idp_delegate.xml +13 -0
  145. data/test/support/yadis_data/test_discover/yadis_no_delegate.xml +11 -0
  146. data/test/support/yadis_data/test_xrds/=j3h.2007.11.14.xrds +25 -0
  147. data/test/support/yadis_data/test_xrds/README +12 -0
  148. data/test/support/yadis_data/test_xrds/delegated-20060809-r1.xrds +34 -0
  149. data/test/support/yadis_data/test_xrds/delegated-20060809-r2.xrds +34 -0
  150. data/test/support/yadis_data/test_xrds/delegated-20060809.xrds +34 -0
  151. data/test/support/yadis_data/test_xrds/no-xrd.xml +7 -0
  152. data/test/support/yadis_data/test_xrds/not-xrds.xml +2 -0
  153. data/test/support/yadis_data/test_xrds/prefixsometimes.xrds +34 -0
  154. data/test/support/yadis_data/test_xrds/ref.xrds +109 -0
  155. data/test/support/yadis_data/test_xrds/sometimesprefix.xrds +34 -0
  156. data/test/support/yadis_data/test_xrds/spoof1.xrds +25 -0
  157. data/test/support/yadis_data/test_xrds/spoof2.xrds +25 -0
  158. data/test/support/yadis_data/test_xrds/spoof3.xrds +37 -0
  159. data/test/support/yadis_data/test_xrds/status222.xrds +9 -0
  160. data/test/support/yadis_data/test_xrds/subsegments.xrds +58 -0
  161. data/test/support/yadis_data/test_xrds/valid-populated-xrds.xml +39 -0
  162. data/test/support/yadis_data/trustroot.txt +153 -0
  163. data/test/support/yadis_data/urinorm.txt +79 -0
  164. data/test/test_accept.rb +170 -0
  165. data/test/test_association.rb +268 -0
  166. data/test/test_associationmanager.rb +918 -0
  167. data/test/test_ax.rb +690 -0
  168. data/test/test_checkid_request.rb +293 -0
  169. data/test/test_consumer.rb +260 -0
  170. data/test/test_cryptutil.rb +119 -0
  171. data/test/test_dh.rb +85 -0
  172. data/test/test_discover.rb +848 -0
  173. data/test/test_discovery_manager.rb +259 -0
  174. data/test/test_extension.rb +46 -0
  175. data/test/test_extras.rb +35 -0
  176. data/test/test_fetchers.rb +554 -0
  177. data/test/test_filters.rb +269 -0
  178. data/test/test_helper.rb +4 -0
  179. data/test/test_idres.rb +961 -0
  180. data/test/test_kvform.rb +164 -0
  181. data/test/test_kvpost.rb +64 -0
  182. data/test/test_linkparse.rb +100 -0
  183. data/test/test_message.rb +1115 -0
  184. data/test/test_nonce.rb +89 -0
  185. data/test/test_oauth.rb +176 -0
  186. data/test/test_openid_yadis.rb +177 -0
  187. data/test/test_pape.rb +248 -0
  188. data/test/test_parsehtml.rb +79 -0
  189. data/test/test_responses.rb +63 -0
  190. data/test/test_server.rb +2455 -0
  191. data/test/test_sreg.rb +479 -0
  192. data/test/test_stores.rb +292 -0
  193. data/test/test_trustroot.rb +111 -0
  194. data/test/test_urinorm.rb +34 -0
  195. data/test/test_util.rb +145 -0
  196. data/test/test_xrds.rb +167 -0
  197. data/test/test_xri.rb +48 -0
  198. data/test/test_xrires.rb +67 -0
  199. data/test/test_yadis_discovery.rb +218 -0
  200. metadata +268 -0
@@ -0,0 +1,89 @@
1
+ require "test_helper"
2
+ require 'openid/store/nonce'
3
+
4
+ module OpenID
5
+ class NonceTestCase < Test::Unit::TestCase
6
+
7
+ NONCE_RE = /\A\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ/
8
+
9
+ def test_mk_nonce
10
+ nonce = Nonce::mk_nonce
11
+ assert(nonce.match(NONCE_RE))
12
+ assert(nonce.size == 26)
13
+ end
14
+
15
+ def test_mk_nonce_time
16
+ nonce = Nonce::mk_nonce(0)
17
+ assert(nonce.match(NONCE_RE))
18
+ assert(nonce.size == 26)
19
+ assert(nonce.match(/^1970-01-01T00:00:00Z/))
20
+ end
21
+
22
+ def test_split
23
+ s = '1970-01-01T00:00:00Z'
24
+ expected_t = 0
25
+ expected_salt = ''
26
+ actual_t, actual_salt = Nonce::split_nonce(s)
27
+ assert_equal(expected_t, actual_t)
28
+ assert_equal(expected_salt, actual_salt)
29
+ end
30
+
31
+ def test_mk_split
32
+ t = 42
33
+ nonce_str = Nonce::mk_nonce(t)
34
+ assert(nonce_str.match(NONCE_RE))
35
+ at, salt = Nonce::split_nonce(nonce_str)
36
+ assert_equal(6, salt.size)
37
+ assert_equal(t, at)
38
+ end
39
+
40
+ def test_bad_split
41
+ cases = [
42
+ '',
43
+ '1970-01-01T00:00:00+1:00',
44
+ '1969-01-01T00:00:00Z',
45
+ '1970-00-01T00:00:00Z',
46
+ '1970.01-01T00:00:00Z',
47
+ 'Thu Sep 7 13:29:31 PDT 2006',
48
+ 'monkeys',
49
+ ]
50
+ cases.each{|c|
51
+ assert_raises(ArgumentError, c.inspect) { Nonce::split_nonce(c) }
52
+ }
53
+ end
54
+
55
+ def test_check_timestamp
56
+ cases = [
57
+ # exact, no allowed skew
58
+ ['1970-01-01T00:00:00Z', 0, 0, true],
59
+
60
+ # exact, large skew
61
+ ['1970-01-01T00:00:00Z', 1000, 0, true],
62
+
63
+ # no allowed skew, one second old
64
+ ['1970-01-01T00:00:00Z', 0, 1, false],
65
+
66
+ # many seconds old, outside of skew
67
+ ['1970-01-01T00:00:00Z', 10, 50, false],
68
+
69
+ # one second old, one second skew allowed
70
+ ['1970-01-01T00:00:00Z', 1, 1, true],
71
+
72
+ # One second in the future, one second skew allowed
73
+ ['1970-01-01T00:00:02Z', 1, 1, true],
74
+
75
+ # two seconds in the future, one second skew allowed
76
+ ['1970-01-01T00:00:02Z', 1, 0, false],
77
+
78
+ # malformed nonce string
79
+ ['monkeys', 0, 0, false],
80
+ ]
81
+
82
+ cases.each{|c|
83
+ (nonce_str, allowed_skew, now, expected) = c
84
+ actual = Nonce::check_timestamp(nonce_str, allowed_skew, now)
85
+ assert_equal(expected, actual, c.inspect)
86
+ }
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,176 @@
1
+ require 'test_helper'
2
+ require 'openid/extensions/oauth'
3
+ require 'openid/message'
4
+ require 'openid/server'
5
+ require 'openid/consumer/responses'
6
+ require 'openid/consumer/discovery'
7
+
8
+ module OpenID
9
+ module OAuthTest
10
+ class OAuthRequestTestCase < Test::Unit::TestCase
11
+ def setup
12
+ @req = OAuth::Request.new
13
+ end
14
+
15
+ def test_construct
16
+ assert_nil(@req.consumer)
17
+ assert_nil(@req.scope)
18
+ assert_equal('oauth', @req.ns_alias)
19
+
20
+ req2 = OAuth::Request.new("CONSUMER","http://sample.com/some_scope")
21
+ assert_equal("CONSUMER",req2.consumer)
22
+ assert_equal("http://sample.com/some_scope",req2.scope)
23
+ end
24
+
25
+ def test_add_consumer
26
+ @req.consumer="CONSUMER"
27
+ assert_equal("CONSUMER",@req.consumer)
28
+ end
29
+
30
+ def test_add_scope
31
+ @req.scope="http://sample.com/some_scope"
32
+ assert_equal("http://sample.com/some_scope",@req.scope)
33
+ end
34
+
35
+ def test_get_extension_args
36
+ assert_equal({}, @req.get_extension_args)
37
+ @req.consumer="CONSUMER"
38
+ assert_equal({'consumer' => 'CONSUMER'}, @req.get_extension_args)
39
+ @req.scope="http://sample.com/some_scope"
40
+ assert_equal({'consumer' => 'CONSUMER', 'scope' => 'http://sample.com/some_scope'}, @req.get_extension_args)
41
+ end
42
+
43
+ def test_parse_extension_args
44
+ args = {'consumer' => 'CONSUMER', 'scope' => 'http://sample.com/some_scope'}
45
+ @req.parse_extension_args(args)
46
+ assert_equal("CONSUMER",@req.consumer)
47
+ assert_equal("http://sample.com/some_scope",@req.scope)
48
+ end
49
+
50
+ def test_parse_extension_args_empty
51
+ @req.parse_extension_args({})
52
+ assert_nil( @req.consumer )
53
+ assert_nil( @req.scope )
54
+ end
55
+
56
+ def test_from_openid_request
57
+ openid_req_msg = Message.from_openid_args({
58
+ 'mode' => 'checkid_setup',
59
+ 'ns' => OPENID2_NS,
60
+ 'ns.oauth' => OAuth::NS_URI,
61
+ 'oauth.consumer' => 'CONSUMER',
62
+ 'oauth.scope' => "http://sample.com/some_scope"
63
+ })
64
+ oid_req = Server::OpenIDRequest.new
65
+ oid_req.message = openid_req_msg
66
+ req = OAuth::Request.from_openid_request(oid_req)
67
+ assert_equal("CONSUMER",req.consumer)
68
+ assert_equal("http://sample.com/some_scope",req.scope)
69
+ end
70
+
71
+ def test_from_openid_request_no_oauth
72
+ message = Message.new
73
+ openid_req = Server::OpenIDRequest.new
74
+ openid_req.message = message
75
+ oauth_req = OAuth::Request.from_openid_request(openid_req)
76
+ assert(oauth_req.nil?)
77
+ end
78
+
79
+ end
80
+
81
+ class DummySuccessResponse
82
+ attr_accessor :message
83
+
84
+ def initialize(message, signed_stuff)
85
+ @message = message
86
+ @signed_stuff = signed_stuff
87
+ end
88
+
89
+ def get_signed_ns(ns_uri)
90
+ return @signed_stuff
91
+ end
92
+
93
+ end
94
+
95
+ class OAuthResponseTestCase < Test::Unit::TestCase
96
+ def setup
97
+ @req = OAuth::Response.new
98
+ end
99
+
100
+ def test_construct
101
+ assert_nil(@req.request_token)
102
+ assert_nil(@req.scope)
103
+
104
+ req2 = OAuth::Response.new("REQUESTTOKEN","http://sample.com/some_scope")
105
+ assert_equal("REQUESTTOKEN",req2.request_token)
106
+ assert_equal("http://sample.com/some_scope",req2.scope)
107
+ end
108
+
109
+ def test_add_request_token
110
+ @req.request_token="REQUESTTOKEN"
111
+ assert_equal("REQUESTTOKEN",@req.request_token)
112
+ end
113
+
114
+ def test_add_scope
115
+ @req.scope="http://sample.com/some_scope"
116
+ assert_equal("http://sample.com/some_scope",@req.scope)
117
+ end
118
+
119
+ def test_get_extension_args
120
+ assert_equal({}, @req.get_extension_args)
121
+ @req.request_token="REQUESTTOKEN"
122
+ assert_equal({'request_token' => 'REQUESTTOKEN'}, @req.get_extension_args)
123
+ @req.scope="http://sample.com/some_scope"
124
+ assert_equal({'request_token' => 'REQUESTTOKEN', 'scope' => 'http://sample.com/some_scope'}, @req.get_extension_args)
125
+ end
126
+
127
+ def test_parse_extension_args
128
+ args = {'request_token' => 'REQUESTTOKEN', 'scope' => 'http://sample.com/some_scope'}
129
+ @req.parse_extension_args(args)
130
+ assert_equal("REQUESTTOKEN",@req.request_token)
131
+ assert_equal("http://sample.com/some_scope",@req.scope)
132
+ end
133
+
134
+ def test_parse_extension_args_empty
135
+ @req.parse_extension_args({})
136
+ assert_nil( @req.request_token )
137
+ assert_nil( @req.scope )
138
+ end
139
+
140
+ def test_from_success_response
141
+
142
+ openid_req_msg = Message.from_openid_args({
143
+ 'mode' => 'id_res',
144
+ 'ns' => OPENID2_NS,
145
+ 'ns.oauth' => OAuth::NS_URI,
146
+ 'ns.oauth' => OAuth::NS_URI,
147
+ 'oauth.request_token' => 'REQUESTTOKEN',
148
+ 'oauth.scope' => "http://sample.com/some_scope"
149
+ })
150
+ signed_stuff = {
151
+ 'request_token' => 'REQUESTTOKEN',
152
+ 'scope' => "http://sample.com/some_scope"
153
+ }
154
+ oid_req = DummySuccessResponse.new(openid_req_msg, signed_stuff)
155
+ req = OAuth::Response.from_success_response(oid_req)
156
+ assert_equal("REQUESTTOKEN",req.request_token)
157
+ assert_equal("http://sample.com/some_scope",req.scope)
158
+ end
159
+
160
+ def test_from_success_response_unsigned
161
+ openid_req_msg = Message.from_openid_args({
162
+ 'mode' => 'id_res',
163
+ 'ns' => OPENID2_NS,
164
+ 'ns.oauth' => OAuth::NS_URI,
165
+ 'oauth.request_token' => 'REQUESTTOKEN',
166
+ 'oauth.scope' => "http://sample.com/some_scope"
167
+ })
168
+ signed_stuff = {}
169
+ endpoint = OpenIDServiceEndpoint.new
170
+ oid_req = Consumer::SuccessResponse.new(endpoint, openid_req_msg, signed_stuff)
171
+ req = OAuth::Response.from_success_response(oid_req)
172
+ assert(req.nil?, req.inspect)
173
+ end
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,177 @@
1
+ require "test_helper"
2
+ require 'openid/consumer/discovery'
3
+ require 'openid/yadis/services'
4
+
5
+ module OpenID
6
+
7
+ XRDS_BOILERPLATE = <<EOF
8
+ <?xml version="1.0" encoding="UTF-8"?>
9
+ <xrds:XRDS xmlns:xrds="xri://$xrds"
10
+ xmlns="xri://$xrd*($v*2.0)"
11
+ xmlns:openid="http://openid.net/xmlns/1.0">
12
+ <XRD>
13
+ %s
14
+ </XRD>
15
+ </xrds:XRDS>
16
+ EOF
17
+
18
+ def self.mkXRDS(services)
19
+ return sprintf(XRDS_BOILERPLATE, services)
20
+ end
21
+
22
+ def self.mkService(uris=nil, type_uris=nil, local_id=nil, dent=" ")
23
+ chunks = [dent, "<Service>\n"]
24
+ dent2 = dent + " "
25
+ if type_uris
26
+ type_uris.each { |type_uri|
27
+ chunks += [dent2 + "<Type>", type_uri, "</Type>\n"]
28
+ }
29
+ end
30
+
31
+ if uris
32
+ uris.each { |uri|
33
+ if uri.is_a?(Array)
34
+ uri, prio = uri
35
+ else
36
+ prio = nil
37
+ end
38
+
39
+ chunks += [dent2, "<URI"]
40
+ if !prio.nil?
41
+ chunks += [" priority='", str(prio), "'"]
42
+ end
43
+ chunks += [">", uri, "</URI>\n"]
44
+ }
45
+ end
46
+
47
+ if local_id
48
+ chunks += [dent2, "<openid:Delegate>", local_id, "</openid:Delegate>\n"]
49
+ end
50
+
51
+ chunks += [dent, "</Service>\n"]
52
+
53
+ return chunks.join("")
54
+ end
55
+
56
+ # Different sets of server URLs for use in the URI tag
57
+ SERVER_URL_OPTIONS = [
58
+ [], # This case should not generate an endpoint object
59
+ ['http://server.url/'],
60
+ ['https://server.url/'],
61
+ ['https://server.url/', 'http://server.url/'],
62
+ ['https://server.url/',
63
+ 'http://server.url/',
64
+ 'http://example.server.url/'],
65
+ ]
66
+
67
+ # Used for generating test data
68
+ def OpenID.subsets(l)
69
+ subsets_list = [[]]
70
+ l.each { |x|
71
+ subsets_list += subsets_list.collect { |t| [x] + t }
72
+ }
73
+
74
+ return subsets_list
75
+ end
76
+
77
+ # A couple of example extension type URIs. These are not at all
78
+ # official, but are just here for testing.
79
+ EXT_TYPES = [
80
+ 'http://janrain.com/extension/blah',
81
+ 'http://openid.net/sreg/1.0',
82
+ ]
83
+
84
+ # Range of valid Delegate tag values for generating test data
85
+ LOCAL_ID_OPTIONS = [
86
+ nil,
87
+ 'http://vanity.domain/',
88
+ 'https://somewhere/yadis/',
89
+ ]
90
+
91
+ class OpenIDYadisTest
92
+ def initialize(uris, type_uris, local_id)
93
+ super()
94
+ @uris = uris
95
+ @type_uris = type_uris
96
+ @local_id = local_id
97
+
98
+ @yadis_url = 'http://unit.test/'
99
+
100
+ # Create an XRDS document to parse
101
+ services = OpenID.mkService(@uris,
102
+ @type_uris,
103
+ @local_id)
104
+ @xrds = OpenID.mkXRDS(services)
105
+ end
106
+
107
+ def runTest(testcase)
108
+ # Parse into endpoint objects that we will check
109
+ endpoints = Yadis.apply_filter(@yadis_url, @xrds, OpenIDServiceEndpoint)
110
+
111
+ # make sure there are the same number of endpoints as URIs. This
112
+ # assumes that the type_uris contains at least one OpenID type.
113
+ testcase.assert_equal(@uris.length, endpoints.length)
114
+
115
+ # So that we can check equality on the endpoint types
116
+ type_uris = @type_uris.dup
117
+ type_uris.sort!
118
+
119
+ seen_uris = []
120
+ endpoints.each { |endpoint|
121
+ seen_uris << endpoint.server_url
122
+
123
+ # All endpoints will have same yadis_url
124
+ testcase.assert_equal(@yadis_url, endpoint.claimed_id)
125
+
126
+ # and local_id
127
+ testcase.assert_equal(@local_id, endpoint.local_id)
128
+
129
+ # and types
130
+ actual_types = endpoint.type_uris.dup
131
+ actual_types.sort!
132
+ testcase.assert_equal(type_uris, actual_types, actual_types.inspect)
133
+ }
134
+
135
+ # So that they will compare equal, because we don't care what
136
+ # order they are in
137
+ seen_uris.sort!
138
+ uris = @uris.dup
139
+ uris.sort!
140
+
141
+ # Make sure we saw all URIs, and saw each one once
142
+ testcase.assert_equal(uris, seen_uris)
143
+ end
144
+ end
145
+
146
+ class OpenIDYadisTests < Test::Unit::TestCase
147
+ def test_openid_yadis
148
+ data = []
149
+
150
+ # All valid combinations of Type tags that should produce an
151
+ # OpenID endpoint
152
+ type_uri_options = []
153
+
154
+ OpenID.subsets([OPENID_1_0_TYPE, OPENID_1_1_TYPE]).each { |ts|
155
+ OpenID.subsets(EXT_TYPES).each { |exts|
156
+ if !ts.empty?
157
+ type_uri_options << exts + ts
158
+ end
159
+ }
160
+ }
161
+
162
+ # All combinations of valid URIs, Type URIs and Delegate tags
163
+ SERVER_URL_OPTIONS.each { |uris|
164
+ type_uri_options.each { |type_uris|
165
+ LOCAL_ID_OPTIONS.each { |local_id|
166
+ data << [uris, type_uris, local_id]
167
+ }
168
+ }
169
+ }
170
+
171
+ data.each { |args|
172
+ t = OpenIDYadisTest.new(*args)
173
+ t.runTest(self)
174
+ }
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,248 @@
1
+ require 'test_helper'
2
+ require 'openid/extensions/pape'
3
+ require 'openid/message'
4
+ require 'openid/server'
5
+ require 'openid/consumer/responses'
6
+
7
+ module OpenID
8
+ module PAPETest
9
+ class PapeRequestTestCase < Test::Unit::TestCase
10
+ def setup
11
+ @req = PAPE::Request.new
12
+ end
13
+
14
+ def test_construct
15
+ assert_equal([], @req.preferred_auth_policies)
16
+ assert_equal(nil, @req.max_auth_age)
17
+ assert_equal('pape', @req.ns_alias)
18
+
19
+ req2 = PAPE::Request.new([PAPE::AUTH_MULTI_FACTOR], 1000)
20
+ assert_equal([PAPE::AUTH_MULTI_FACTOR], req2.preferred_auth_policies)
21
+ assert_equal(1000, req2.max_auth_age)
22
+ end
23
+
24
+ def test_add_policy_uri
25
+ assert_equal([], @req.preferred_auth_policies)
26
+ @req.add_policy_uri(PAPE::AUTH_MULTI_FACTOR)
27
+ assert_equal([PAPE::AUTH_MULTI_FACTOR], @req.preferred_auth_policies)
28
+ @req.add_policy_uri(PAPE::AUTH_MULTI_FACTOR)
29
+ assert_equal([PAPE::AUTH_MULTI_FACTOR], @req.preferred_auth_policies)
30
+ @req.add_policy_uri(PAPE::AUTH_PHISHING_RESISTANT)
31
+ assert_equal([PAPE::AUTH_MULTI_FACTOR, PAPE::AUTH_PHISHING_RESISTANT], @req.preferred_auth_policies)
32
+ @req.add_policy_uri(PAPE::AUTH_MULTI_FACTOR)
33
+ assert_equal([PAPE::AUTH_MULTI_FACTOR, PAPE::AUTH_PHISHING_RESISTANT], @req.preferred_auth_policies)
34
+ end
35
+
36
+ def test_get_extension_args
37
+ assert_equal({'preferred_auth_policies' => ''}, @req.get_extension_args)
38
+ @req.add_policy_uri('http://uri')
39
+ assert_equal({'preferred_auth_policies' => 'http://uri'}, @req.get_extension_args)
40
+ @req.add_policy_uri('http://zig')
41
+ assert_equal({'preferred_auth_policies' => 'http://uri http://zig'}, @req.get_extension_args)
42
+ @req.max_auth_age = 789
43
+ assert_equal({'preferred_auth_policies' => 'http://uri http://zig', 'max_auth_age' => '789'}, @req.get_extension_args)
44
+ end
45
+
46
+ def test_parse_extension_args
47
+ args = {'preferred_auth_policies' => 'http://foo http://bar',
48
+ 'max_auth_age' => '9'}
49
+ @req.parse_extension_args(args)
50
+ assert_equal(9, @req.max_auth_age)
51
+ assert_equal(['http://foo','http://bar'], @req.preferred_auth_policies)
52
+ end
53
+
54
+ def test_parse_extension_args_empty
55
+ @req.parse_extension_args({})
56
+ assert_equal(nil, @req.max_auth_age)
57
+ assert_equal([], @req.preferred_auth_policies)
58
+ end
59
+
60
+ def test_from_openid_request
61
+ openid_req_msg = Message.from_openid_args({
62
+ 'mode' => 'checkid_setup',
63
+ 'ns' => OPENID2_NS,
64
+ 'ns.pape' => PAPE::NS_URI,
65
+ 'pape.preferred_auth_policies' => [PAPE::AUTH_MULTI_FACTOR, PAPE::AUTH_PHISHING_RESISTANT].join(' '),
66
+ 'pape.max_auth_age' => '5476'
67
+ })
68
+ oid_req = Server::OpenIDRequest.new
69
+ oid_req.message = openid_req_msg
70
+ req = PAPE::Request.from_openid_request(oid_req)
71
+ assert_equal([PAPE::AUTH_MULTI_FACTOR, PAPE::AUTH_PHISHING_RESISTANT], req.preferred_auth_policies)
72
+ assert_equal(5476, req.max_auth_age)
73
+ end
74
+
75
+ def test_from_openid_request_no_pape
76
+ message = Message.new
77
+ openid_req = Server::OpenIDRequest.new
78
+ openid_req.message = message
79
+ pape_req = PAPE::Request.from_openid_request(openid_req)
80
+ assert(pape_req.nil?)
81
+ end
82
+
83
+ def test_preferred_types
84
+ @req.add_policy_uri(PAPE::AUTH_PHISHING_RESISTANT)
85
+ @req.add_policy_uri(PAPE::AUTH_MULTI_FACTOR)
86
+ pt = @req.preferred_types([PAPE::AUTH_MULTI_FACTOR,
87
+ PAPE::AUTH_MULTI_FACTOR_PHYSICAL])
88
+ assert_equal([PAPE::AUTH_MULTI_FACTOR], pt)
89
+ end
90
+ end
91
+
92
+ class DummySuccessResponse
93
+ attr_accessor :message
94
+
95
+ def initialize(message, signed_stuff)
96
+ @message = message
97
+ @signed_stuff = signed_stuff
98
+ end
99
+
100
+ def get_signed_ns(ns_uri)
101
+ return @signed_stuff
102
+ end
103
+
104
+ end
105
+
106
+ class PapeResponseTestCase < Test::Unit::TestCase
107
+ def setup
108
+ @req = PAPE::Response.new
109
+ end
110
+
111
+ def test_construct
112
+ assert_equal([], @req.auth_policies)
113
+ assert_equal(nil, @req.auth_time)
114
+ assert_equal('pape', @req.ns_alias)
115
+ assert_equal(nil, @req.nist_auth_level)
116
+
117
+ req2 = PAPE::Response.new([PAPE::AUTH_MULTI_FACTOR], "1983-11-05T12:30:24Z", 3)
118
+ assert_equal([PAPE::AUTH_MULTI_FACTOR], req2.auth_policies)
119
+ assert_equal("1983-11-05T12:30:24Z", req2.auth_time)
120
+ assert_equal(3, req2.nist_auth_level)
121
+ end
122
+
123
+ def test_add_policy_uri
124
+ assert_equal([], @req.auth_policies)
125
+ @req.add_policy_uri(PAPE::AUTH_MULTI_FACTOR)
126
+ assert_equal([PAPE::AUTH_MULTI_FACTOR], @req.auth_policies)
127
+ @req.add_policy_uri(PAPE::AUTH_MULTI_FACTOR)
128
+ assert_equal([PAPE::AUTH_MULTI_FACTOR], @req.auth_policies)
129
+ @req.add_policy_uri(PAPE::AUTH_PHISHING_RESISTANT)
130
+ assert_equal([PAPE::AUTH_MULTI_FACTOR, PAPE::AUTH_PHISHING_RESISTANT], @req.auth_policies)
131
+ @req.add_policy_uri(PAPE::AUTH_MULTI_FACTOR)
132
+ assert_equal([PAPE::AUTH_MULTI_FACTOR, PAPE::AUTH_PHISHING_RESISTANT], @req.auth_policies)
133
+ end
134
+
135
+ def test_get_extension_args
136
+ assert_equal({'auth_policies' => 'none'}, @req.get_extension_args)
137
+ @req.add_policy_uri('http://uri')
138
+ assert_equal({'auth_policies' => 'http://uri'}, @req.get_extension_args)
139
+ @req.add_policy_uri('http://zig')
140
+ assert_equal({'auth_policies' => 'http://uri http://zig'}, @req.get_extension_args)
141
+ @req.auth_time = "1983-11-05T12:30:24Z"
142
+ assert_equal({'auth_policies' => 'http://uri http://zig', 'auth_time' => "1983-11-05T12:30:24Z"}, @req.get_extension_args)
143
+ @req.nist_auth_level = 3
144
+ assert_equal({'auth_policies' => 'http://uri http://zig', 'auth_time' => "1983-11-05T12:30:24Z", 'nist_auth_level' => '3'}, @req.get_extension_args)
145
+ end
146
+
147
+ def test_get_extension_args_error_auth_age
148
+ @req.auth_time = "the beginning of time"
149
+ assert_raises(ArgumentError) { @req.get_extension_args }
150
+ end
151
+
152
+ def test_get_extension_args_error_nist_auth_level
153
+ @req.nist_auth_level = "high as a kite"
154
+ assert_raises(ArgumentError) { @req.get_extension_args }
155
+ @req.nist_auth_level = 5
156
+ assert_raises(ArgumentError) { @req.get_extension_args }
157
+ @req.nist_auth_level = -1
158
+ assert_raises(ArgumentError) { @req.get_extension_args }
159
+ end
160
+
161
+ def test_parse_extension_args
162
+ args = {'auth_policies' => 'http://foo http://bar',
163
+ 'auth_time' => '1983-11-05T12:30:24Z'}
164
+ @req.parse_extension_args(args)
165
+ assert_equal('1983-11-05T12:30:24Z', @req.auth_time)
166
+ assert_equal(['http://foo','http://bar'], @req.auth_policies)
167
+ end
168
+
169
+ def test_parse_extension_args_empty
170
+ @req.parse_extension_args({})
171
+ assert_equal(nil, @req.auth_time)
172
+ assert_equal([], @req.auth_policies)
173
+ end
174
+
175
+ def test_parse_extension_args_strict_bogus1
176
+ args = {'auth_policies' => 'http://foo http://bar',
177
+ 'auth_time' => 'this one time'}
178
+ assert_raises(ArgumentError) {
179
+ @req.parse_extension_args(args, true)
180
+ }
181
+ end
182
+
183
+ def test_parse_extension_args_strict_bogus2
184
+ args = {'auth_policies' => 'http://foo http://bar',
185
+ 'auth_time' => '1983-11-05T12:30:24Z',
186
+ 'nist_auth_level' => 'some'}
187
+ assert_raises(ArgumentError) {
188
+ @req.parse_extension_args(args, true)
189
+ }
190
+ end
191
+
192
+ def test_parse_extension_args_strict_good
193
+ args = {'auth_policies' => 'http://foo http://bar',
194
+ 'auth_time' => '2007-10-11T05:25:18Z',
195
+ 'nist_auth_level' => '0'}
196
+ @req.parse_extension_args(args, true)
197
+ assert_equal(['http://foo','http://bar'], @req.auth_policies)
198
+ assert_equal('2007-10-11T05:25:18Z', @req.auth_time)
199
+ assert_equal(0, @req.nist_auth_level)
200
+ end
201
+
202
+ def test_parse_extension_args_nostrict_bogus
203
+ args = {'auth_policies' => 'http://foo http://bar',
204
+ 'auth_time' => 'some time ago',
205
+ 'nist_auth_level' => 'some'}
206
+ @req.parse_extension_args(args)
207
+ assert_equal(['http://foo','http://bar'], @req.auth_policies)
208
+ assert_equal(nil, @req.auth_time)
209
+ assert_equal(nil, @req.nist_auth_level)
210
+ end
211
+
212
+
213
+ def test_from_success_response
214
+
215
+ openid_req_msg = Message.from_openid_args({
216
+ 'mode' => 'id_res',
217
+ 'ns' => OPENID2_NS,
218
+ 'ns.pape' => PAPE::NS_URI,
219
+ 'pape.auth_policies' => [PAPE::AUTH_MULTI_FACTOR, PAPE::AUTH_PHISHING_RESISTANT].join(' '),
220
+ 'pape.auth_time' => '1983-11-05T12:30:24Z'
221
+ })
222
+ signed_stuff = {
223
+ 'auth_policies' => [PAPE::AUTH_MULTI_FACTOR, PAPE::AUTH_PHISHING_RESISTANT].join(' '),
224
+ 'auth_time' => '1983-11-05T12:30:24Z'
225
+ }
226
+ oid_req = DummySuccessResponse.new(openid_req_msg, signed_stuff)
227
+ req = PAPE::Response.from_success_response(oid_req)
228
+ assert_equal([PAPE::AUTH_MULTI_FACTOR, PAPE::AUTH_PHISHING_RESISTANT], req.auth_policies)
229
+ assert_equal('1983-11-05T12:30:24Z', req.auth_time)
230
+ end
231
+
232
+ def test_from_success_response_unsigned
233
+ openid_req_msg = Message.from_openid_args({
234
+ 'mode' => 'id_res',
235
+ 'ns' => OPENID2_NS,
236
+ 'ns.pape' => PAPE::NS_URI,
237
+ 'pape.auth_policies' => [PAPE::AUTH_MULTI_FACTOR, PAPE::AUTH_PHISHING_RESISTANT].join(' '),
238
+ 'pape.auth_time' => '1983-11-05T12:30:24Z'
239
+ })
240
+ signed_stuff = {}
241
+ endpoint = OpenIDServiceEndpoint.new
242
+ oid_req = Consumer::SuccessResponse.new(endpoint, openid_req_msg, signed_stuff)
243
+ req = PAPE::Response.from_success_response(oid_req)
244
+ assert(req.nil?, req.inspect)
245
+ end
246
+ end
247
+ end
248
+ end