PageRenderTime 57ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/boomhammer/vendor/gems/ruby-openid-2.1.4/test/test_server.rb

http://strd6.googlecode.com/
Ruby | 2457 lines | 1972 code | 322 blank | 163 comment | 26 complexity | 1c5d2f9de227427638e28300cb536adc MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. require 'openid/server'
  2. require 'openid/cryptutil'
  3. require 'openid/association'
  4. require 'openid/util'
  5. require 'openid/message'
  6. require 'openid/store/memory'
  7. require 'openid/dh'
  8. require 'openid/consumer/associationmanager'
  9. require 'util'
  10. require "testutil"
  11. require 'test/unit'
  12. require 'uri'
  13. # In general, if you edit or add tests here, try to move in the
  14. # direction of testing smaller units. For testing the external
  15. # interfaces, we'll be developing an implementation-agnostic testing
  16. # suite.
  17. # for more, see /etc/ssh/moduli
  18. module OpenID
  19. ALT_MODULUS = 0xCAADDDEC1667FC68B5FA15D53C4E1532DD24561A1A2D47A12C01ABEA1E00731F6921AAC40742311FDF9E634BB7131BEE1AF240261554389A910425E044E88C8359B010F5AD2B80E29CB1A5B027B19D9E01A6F63A6F45E5D7ED2FF6A2A0085050A7D0CF307C3DB51D2490355907B4427C23A98DF1EB8ABEF2BA209BB7AFFE86A7
  20. ALT_GEN = 5
  21. class CatchLogs
  22. def catchlogs_setup
  23. @old_logger = Util.logger
  24. Util.logger = self.method('got_log_message')
  25. @messages = []
  26. end
  27. def got_log_message(message)
  28. @messages << message
  29. end
  30. def teardown
  31. Util.logger = @old_logger
  32. end
  33. end
  34. class TestProtocolError < Test::Unit::TestCase
  35. def test_browserWithReturnTo
  36. return_to = "http://rp.unittest/consumer"
  37. # will be a ProtocolError raised by Decode or
  38. # CheckIDRequest.answer
  39. args = Message.from_post_args({
  40. 'openid.mode' => 'monkeydance',
  41. 'openid.identity' => 'http://wagu.unittest/',
  42. 'openid.return_to' => return_to,
  43. })
  44. e = Server::ProtocolError.new(args, "plucky")
  45. assert(e.has_return_to)
  46. expected_args = {
  47. 'openid.mode' => 'error',
  48. 'openid.error' => 'plucky',
  49. }
  50. rt_base, result_args = e.encode_to_url.split('?', 2)
  51. result_args = Util.parse_query(result_args)
  52. assert_equal(result_args, expected_args)
  53. end
  54. def test_browserWithReturnTo_OpenID2_GET
  55. return_to = "http://rp.unittest/consumer"
  56. # will be a ProtocolError raised by Decode or
  57. # CheckIDRequest.answer
  58. args = Message.from_post_args({
  59. 'openid.ns' => OPENID2_NS,
  60. 'openid.mode' => 'monkeydance',
  61. 'openid.identity' => 'http://wagu.unittest/',
  62. 'openid.claimed_id' => 'http://wagu.unittest/',
  63. 'openid.return_to' => return_to,
  64. })
  65. e = Server::ProtocolError.new(args, "plucky")
  66. assert(e.has_return_to)
  67. expected_args = {
  68. 'openid.ns' => OPENID2_NS,
  69. 'openid.mode' => 'error',
  70. 'openid.error' => 'plucky',
  71. }
  72. rt_base, result_args = e.encode_to_url.split('?', 2)
  73. result_args = Util.parse_query(result_args)
  74. assert_equal(result_args, expected_args)
  75. end
  76. def test_browserWithReturnTo_OpenID2_POST
  77. return_to = "http://rp.unittest/consumer" + ('x' * OPENID1_URL_LIMIT)
  78. # will be a ProtocolError raised by Decode or
  79. # CheckIDRequest.answer
  80. args = Message.from_post_args({
  81. 'openid.ns' => OPENID2_NS,
  82. 'openid.mode' => 'monkeydance',
  83. 'openid.identity' => 'http://wagu.unittest/',
  84. 'openid.claimed_id' => 'http://wagu.unittest/',
  85. 'openid.return_to' => return_to,
  86. })
  87. e = Server::ProtocolError.new(args, "plucky")
  88. assert(e.has_return_to)
  89. expected_args = {
  90. 'openid.ns' => OPENID2_NS,
  91. 'openid.mode' => 'error',
  92. 'openid.error' => 'plucky',
  93. }
  94. assert(e.which_encoding == Server::ENCODE_HTML_FORM)
  95. assert(e.to_form_markup == e.to_message.to_form_markup(
  96. args.get_arg(OPENID_NS, 'return_to')))
  97. end
  98. def test_browserWithReturnTo_OpenID1_exceeds_limit
  99. return_to = "http://rp.unittest/consumer" + ('x' * OPENID1_URL_LIMIT)
  100. # will be a ProtocolError raised by Decode or
  101. # CheckIDRequest.answer
  102. args = Message.from_post_args({
  103. 'openid.mode' => 'monkeydance',
  104. 'openid.identity' => 'http://wagu.unittest/',
  105. 'openid.return_to' => return_to,
  106. })
  107. e = Server::ProtocolError.new(args, "plucky")
  108. assert(e.has_return_to)
  109. expected_args = {
  110. 'openid.mode' => 'error',
  111. 'openid.error' => 'plucky',
  112. }
  113. assert(e.which_encoding == Server::ENCODE_URL)
  114. rt_base, result_args = e.encode_to_url.split('?', 2)
  115. result_args = Util.parse_query(result_args)
  116. assert_equal(result_args, expected_args)
  117. end
  118. def test_noReturnTo
  119. # will be a ProtocolError raised by Decode or
  120. # CheckIDRequest.answer
  121. args = Message.from_post_args({
  122. 'openid.mode' => 'zebradance',
  123. 'openid.identity' => 'http://wagu.unittest/',
  124. })
  125. e = Server::ProtocolError.new(args, "waffles")
  126. assert(!e.has_return_to)
  127. expected = "error:waffles\nmode:error\n"
  128. assert_equal(e.encode_to_kvform, expected)
  129. end
  130. def test_no_message
  131. e = Server::ProtocolError.new(nil, "no message")
  132. assert(e.get_return_to.nil?)
  133. assert_equal(e.which_encoding, nil)
  134. end
  135. def test_which_encoding_no_message
  136. e = Server::ProtocolError.new(nil, "no message")
  137. assert(e.which_encoding.nil?)
  138. end
  139. end
  140. class TestDecode < Test::Unit::TestCase
  141. def setup
  142. @claimed_id = 'http://de.legating.de.coder.unittest/'
  143. @id_url = "http://decoder.am.unittest/"
  144. @rt_url = "http://rp.unittest/foobot/?qux=zam"
  145. @tr_url = "http://rp.unittest/"
  146. @assoc_handle = "{assoc}{handle}"
  147. @op_endpoint = 'http://endpoint.unittest/encode'
  148. @store = Store::Memory.new()
  149. @server = Server::Server.new(@store, @op_endpoint)
  150. @decode = Server::Decoder.new(@server).method('decode')
  151. end
  152. def test_none
  153. args = {}
  154. r = @decode.call(args)
  155. assert_equal(r, nil)
  156. end
  157. def test_irrelevant
  158. args = {
  159. 'pony' => 'spotted',
  160. 'sreg.mutant_power' => 'decaffinator',
  161. }
  162. assert_raise(Server::ProtocolError) {
  163. @decode.call(args)
  164. }
  165. end
  166. def test_bad
  167. args = {
  168. 'openid.mode' => 'twos-compliment',
  169. 'openid.pants' => 'zippered',
  170. }
  171. assert_raise(Server::ProtocolError) {
  172. @decode.call(args)
  173. }
  174. end
  175. def test_dictOfLists
  176. args = {
  177. 'openid.mode' => ['checkid_setup'],
  178. 'openid.identity' => @id_url,
  179. 'openid.assoc_handle' => @assoc_handle,
  180. 'openid.return_to' => @rt_url,
  181. 'openid.trust_root' => @tr_url,
  182. }
  183. begin
  184. result = @decode.call(args)
  185. rescue ArgumentError => err
  186. assert(!err.to_s.index('values').nil?, err)
  187. else
  188. flunk("Expected ArgumentError, but got result #{result}")
  189. end
  190. end
  191. def test_checkidImmediate
  192. args = {
  193. 'openid.mode' => 'checkid_immediate',
  194. 'openid.identity' => @id_url,
  195. 'openid.assoc_handle' => @assoc_handle,
  196. 'openid.return_to' => @rt_url,
  197. 'openid.trust_root' => @tr_url,
  198. # should be ignored
  199. 'openid.some.extension' => 'junk',
  200. }
  201. r = @decode.call(args)
  202. assert(r.is_a?(Server::CheckIDRequest))
  203. assert_equal(r.mode, "checkid_immediate")
  204. assert_equal(r.immediate, true)
  205. assert_equal(r.identity, @id_url)
  206. assert_equal(r.trust_root, @tr_url)
  207. assert_equal(r.return_to, @rt_url)
  208. assert_equal(r.assoc_handle, @assoc_handle)
  209. end
  210. def test_checkidImmediate_constructor
  211. r = Server::CheckIDRequest.new(@id_url, @rt_url, nil,
  212. @rt_url, true, @assoc_handle)
  213. assert(r.mode == 'checkid_immediate')
  214. assert(r.immediate)
  215. end
  216. def test_checkid_missing_return_to_and_trust_root
  217. args = {
  218. 'openid.ns' => OPENID2_NS,
  219. 'openid.mode' => 'checkid_setup',
  220. 'openid.identity' => @id_url,
  221. 'openid.claimed_id' => @id_url,
  222. 'openid.assoc_handle' => @assoc_handle,
  223. }
  224. assert_raise(Server::ProtocolError) {
  225. m = Message.from_post_args(args)
  226. Server::CheckIDRequest.from_message(m, @op_endpoint)
  227. }
  228. end
  229. def test_checkid_id_select
  230. args = {
  231. 'openid.ns' => OPENID2_NS,
  232. 'openid.mode' => 'checkid_setup',
  233. 'openid.identity' => IDENTIFIER_SELECT,
  234. 'openid.claimed_id' => IDENTIFIER_SELECT,
  235. 'openid.assoc_handle' => @assoc_handle,
  236. 'openid.return_to' => @rt_url,
  237. 'openid.realm' => @tr_url,
  238. }
  239. m = Message.from_post_args(args)
  240. req = Server::CheckIDRequest.from_message(m, @op_endpoint)
  241. assert(req.id_select)
  242. end
  243. def test_checkid_not_id_select
  244. args = {
  245. 'openid.ns' => OPENID2_NS,
  246. 'openid.mode' => 'checkid_setup',
  247. 'openid.assoc_handle' => @assoc_handle,
  248. 'openid.return_to' => @rt_url,
  249. 'openid.realm' => @tr_url,
  250. }
  251. id_args = [
  252. {'openid.claimed_id' => IDENTIFIER_SELECT,
  253. 'openid.identity' => 'http://bogus.com/'},
  254. {'openid.claimed_id' => 'http://bogus.com/',
  255. 'openid.identity' => 'http://bogus.com/'},
  256. ]
  257. id_args.each { |id|
  258. m = Message.from_post_args(args.merge(id))
  259. req = Server::CheckIDRequest.from_message(m, @op_endpoint)
  260. assert(!req.id_select)
  261. }
  262. end
  263. def test_checkidSetup
  264. args = {
  265. 'openid.mode' => 'checkid_setup',
  266. 'openid.identity' => @id_url,
  267. 'openid.assoc_handle' => @assoc_handle,
  268. 'openid.return_to' => @rt_url,
  269. 'openid.trust_root' => @tr_url,
  270. }
  271. r = @decode.call(args)
  272. assert(r.is_a?(Server::CheckIDRequest))
  273. assert_equal(r.mode, "checkid_setup")
  274. assert_equal(r.immediate, false)
  275. assert_equal(r.identity, @id_url)
  276. assert_equal(r.trust_root, @tr_url)
  277. assert_equal(r.return_to, @rt_url)
  278. end
  279. def test_checkidSetupOpenID2
  280. args = {
  281. 'openid.ns' => OPENID2_NS,
  282. 'openid.mode' => 'checkid_setup',
  283. 'openid.identity' => @id_url,
  284. 'openid.claimed_id' => @claimed_id,
  285. 'openid.assoc_handle' => @assoc_handle,
  286. 'openid.return_to' => @rt_url,
  287. 'openid.realm' => @tr_url,
  288. }
  289. r = @decode.call(args)
  290. assert(r.is_a?(Server::CheckIDRequest))
  291. assert_equal(r.mode, "checkid_setup")
  292. assert_equal(r.immediate, false)
  293. assert_equal(r.identity, @id_url)
  294. assert_equal(r.claimed_id, @claimed_id)
  295. assert_equal(r.trust_root, @tr_url)
  296. assert_equal(r.return_to, @rt_url)
  297. end
  298. def test_checkidSetupNoClaimedIDOpenID2
  299. args = {
  300. 'openid.ns' => OPENID2_NS,
  301. 'openid.mode' => 'checkid_setup',
  302. 'openid.identity' => @id_url,
  303. 'openid.assoc_handle' => @assoc_handle,
  304. 'openid.return_to' => @rt_url,
  305. 'openid.realm' => @tr_url,
  306. }
  307. assert_raise(Server::ProtocolError) {
  308. @decode.call(args)
  309. }
  310. end
  311. def test_checkidSetupNoIdentityOpenID2
  312. args = {
  313. 'openid.ns' => OPENID2_NS,
  314. 'openid.mode' => 'checkid_setup',
  315. 'openid.assoc_handle' => @assoc_handle,
  316. 'openid.return_to' => @rt_url,
  317. 'openid.realm' => @tr_url,
  318. }
  319. r = @decode.call(args)
  320. assert(r.is_a?(Server::CheckIDRequest))
  321. assert_equal(r.mode, "checkid_setup")
  322. assert_equal(r.immediate, false)
  323. assert_equal(r.identity, nil)
  324. assert_equal(r.trust_root, @tr_url)
  325. assert_equal(r.return_to, @rt_url)
  326. end
  327. def test_checkidSetupNoReturnOpenID1
  328. # Make sure an OpenID 1 request cannot be decoded if it lacks a
  329. # return_to.
  330. args = {
  331. 'openid.mode' => 'checkid_setup',
  332. 'openid.identity' => @id_url,
  333. 'openid.assoc_handle' => @assoc_handle,
  334. 'openid.trust_root' => @tr_url,
  335. }
  336. assert_raise(Server::ProtocolError) {
  337. @decode.call(args)
  338. }
  339. end
  340. def test_checkidSetupNoReturnOpenID2
  341. # Make sure an OpenID 2 request with no return_to can be decoded,
  342. # and make sure a response to such a request raises
  343. # NoReturnToError.
  344. args = {
  345. 'openid.ns' => OPENID2_NS,
  346. 'openid.mode' => 'checkid_setup',
  347. 'openid.identity' => @id_url,
  348. 'openid.claimed_id' => @id_url,
  349. 'openid.assoc_handle' => @assoc_handle,
  350. 'openid.realm' => @tr_url,
  351. }
  352. req = @decode.call(args)
  353. assert(req.is_a?(Server::CheckIDRequest))
  354. assert_raise(Server::NoReturnToError) {
  355. req.answer(false)
  356. }
  357. assert_raise(Server::NoReturnToError) {
  358. req.encode_to_url('bogus')
  359. }
  360. assert_raise(Server::NoReturnToError) {
  361. req.cancel_url
  362. }
  363. end
  364. def test_checkidSetupRealmRequiredOpenID2
  365. # Make sure that an OpenID 2 request which lacks return_to cannot
  366. # be decoded if it lacks a realm. Spec => This value
  367. # (openid.realm) MUST be sent if openid.return_to is omitted.
  368. args = {
  369. 'openid.ns' => OPENID2_NS,
  370. 'openid.mode' => 'checkid_setup',
  371. 'openid.identity' => @id_url,
  372. 'openid.assoc_handle' => @assoc_handle,
  373. }
  374. assert_raise(Server::ProtocolError) {
  375. @decode.call(args)
  376. }
  377. end
  378. def test_checkidSetupBadReturn
  379. args = {
  380. 'openid.mode' => 'checkid_setup',
  381. 'openid.identity' => @id_url,
  382. 'openid.assoc_handle' => @assoc_handle,
  383. 'openid.return_to' => 'not a url',
  384. }
  385. begin
  386. result = @decode.call(args)
  387. rescue Server::ProtocolError => err
  388. assert(err.openid_message)
  389. else
  390. flunk("Expected ProtocolError, instead returned with #{result}")
  391. end
  392. end
  393. def test_checkidSetupUntrustedReturn
  394. args = {
  395. 'openid.mode' => 'checkid_setup',
  396. 'openid.identity' => @id_url,
  397. 'openid.assoc_handle' => @assoc_handle,
  398. 'openid.return_to' => @rt_url,
  399. 'openid.trust_root' => 'http://not-the-return-place.unittest/',
  400. }
  401. begin
  402. result = @decode.call(args)
  403. rescue Server::UntrustedReturnURL => err
  404. assert(err.openid_message, err.to_s)
  405. else
  406. flunk("Expected UntrustedReturnURL, instead returned with #{result}")
  407. end
  408. end
  409. def test_checkidSetupUntrustedReturn_Constructor
  410. assert_raise(Server::UntrustedReturnURL) {
  411. Server::CheckIDRequest.new(@id_url, @rt_url, nil,
  412. 'http://not-the-return-place.unittest/',
  413. false, @assoc_handle)
  414. }
  415. end
  416. def test_checkidSetupMalformedReturnURL_Constructor
  417. assert_raise(Server::MalformedReturnURL) {
  418. Server::CheckIDRequest.new(@id_url, 'bogus://return.url', nil,
  419. 'http://trustroot.com/',
  420. false, @assoc_handle)
  421. }
  422. end
  423. def test_checkAuth
  424. args = {
  425. 'openid.mode' => 'check_authentication',
  426. 'openid.assoc_handle' => '{dumb}{handle}',
  427. 'openid.sig' => 'sigblob',
  428. 'openid.signed' => 'identity,return_to,response_nonce,mode',
  429. 'openid.identity' => 'signedval1',
  430. 'openid.return_to' => 'signedval2',
  431. 'openid.response_nonce' => 'signedval3',
  432. 'openid.baz' => 'unsigned',
  433. }
  434. r = @decode.call(args)
  435. assert(r.is_a?(Server::CheckAuthRequest))
  436. assert_equal(r.mode, 'check_authentication')
  437. assert_equal(r.sig, 'sigblob')
  438. end
  439. def test_checkAuthMissingSignature
  440. args = {
  441. 'openid.mode' => 'check_authentication',
  442. 'openid.assoc_handle' => '{dumb}{handle}',
  443. 'openid.signed' => 'foo,bar,mode',
  444. 'openid.foo' => 'signedval1',
  445. 'openid.bar' => 'signedval2',
  446. 'openid.baz' => 'unsigned',
  447. }
  448. assert_raise(Server::ProtocolError) {
  449. @decode.call(args)
  450. }
  451. end
  452. def test_checkAuthAndInvalidate
  453. args = {
  454. 'openid.mode' => 'check_authentication',
  455. 'openid.assoc_handle' => '{dumb}{handle}',
  456. 'openid.invalidate_handle' => '[[SMART_handle]]',
  457. 'openid.sig' => 'sigblob',
  458. 'openid.signed' => 'identity,return_to,response_nonce,mode',
  459. 'openid.identity' => 'signedval1',
  460. 'openid.return_to' => 'signedval2',
  461. 'openid.response_nonce' => 'signedval3',
  462. 'openid.baz' => 'unsigned',
  463. }
  464. r = @decode.call(args)
  465. assert(r.is_a?(Server::CheckAuthRequest))
  466. assert_equal(r.invalidate_handle, '[[SMART_handle]]')
  467. end
  468. def test_associateDH
  469. args = {
  470. 'openid.mode' => 'associate',
  471. 'openid.session_type' => 'DH-SHA1',
  472. 'openid.dh_consumer_public' => "Rzup9265tw==",
  473. }
  474. r = @decode.call(args)
  475. assert(r.is_a?(Server::AssociateRequest))
  476. assert_equal(r.mode, "associate")
  477. assert_equal(r.session.session_type, "DH-SHA1")
  478. assert_equal(r.assoc_type, "HMAC-SHA1")
  479. assert(r.session.consumer_pubkey)
  480. end
  481. def test_associateDHMissingKey
  482. # Trying DH assoc w/o public key
  483. args = {
  484. 'openid.mode' => 'associate',
  485. 'openid.session_type' => 'DH-SHA1',
  486. }
  487. # Using DH-SHA1 without supplying dh_consumer_public is an error.
  488. assert_raise(Server::ProtocolError) {
  489. @decode.call(args)
  490. }
  491. end
  492. def test_associateDHpubKeyNotB64
  493. args = {
  494. 'openid.mode' => 'associate',
  495. 'openid.session_type' => 'DH-SHA1',
  496. 'openid.dh_consumer_public' => "donkeydonkeydonkey",
  497. }
  498. assert_raise(Server::ProtocolError) {
  499. @decode.call(args)
  500. }
  501. end
  502. def test_associateDHModGen
  503. # test dh with non-default but valid values for dh_modulus and
  504. # dh_gen
  505. args = {
  506. 'openid.mode' => 'associate',
  507. 'openid.session_type' => 'DH-SHA1',
  508. 'openid.dh_consumer_public' => "Rzup9265tw==",
  509. 'openid.dh_modulus' => CryptUtil.num_to_base64(ALT_MODULUS),
  510. 'openid.dh_gen' => CryptUtil.num_to_base64(ALT_GEN) ,
  511. }
  512. r = @decode.call(args)
  513. assert(r.is_a?(Server::AssociateRequest))
  514. assert_equal(r.mode, "associate")
  515. assert_equal(r.session.session_type, "DH-SHA1")
  516. assert_equal(r.assoc_type, "HMAC-SHA1")
  517. assert_equal(r.session.dh.modulus, ALT_MODULUS)
  518. assert_equal(r.session.dh.generator, ALT_GEN)
  519. assert(r.session.consumer_pubkey)
  520. end
  521. def test_associateDHCorruptModGen
  522. # test dh with non-default but valid values for dh_modulus and
  523. # dh_gen
  524. args = {
  525. 'openid.mode' => 'associate',
  526. 'openid.session_type' => 'DH-SHA1',
  527. 'openid.dh_consumer_public' => "Rzup9265tw==",
  528. 'openid.dh_modulus' => 'pizza',
  529. 'openid.dh_gen' => 'gnocchi',
  530. }
  531. assert_raise(Server::ProtocolError) {
  532. @decode.call(args)
  533. }
  534. end
  535. def test_associateDHMissingGen
  536. args = {
  537. 'openid.mode' => 'associate',
  538. 'openid.session_type' => 'DH-SHA1',
  539. 'openid.dh_consumer_public' => "Rzup9265tw==",
  540. 'openid.dh_modulus' => 'pizza',
  541. }
  542. assert_raise(Server::ProtocolError) {
  543. @decode.call(args)
  544. }
  545. end
  546. def test_associateDHMissingMod
  547. args = {
  548. 'openid.mode' => 'associate',
  549. 'openid.session_type' => 'DH-SHA1',
  550. 'openid.dh_consumer_public' => "Rzup9265tw==",
  551. 'openid.dh_gen' => 'pizza',
  552. }
  553. assert_raise(Server::ProtocolError) {
  554. @decode.call(args)
  555. }
  556. end
  557. # def test_associateDHInvalidModGen(self):
  558. # # test dh with properly encoded values that are not a valid
  559. # # modulus/generator combination.
  560. # args = {
  561. # 'openid.mode': 'associate',
  562. # 'openid.session_type': 'DH-SHA1',
  563. # 'openid.dh_consumer_public': "Rzup9265tw==",
  564. # 'openid.dh_modulus': cryptutil.longToBase64(9),
  565. # 'openid.dh_gen': cryptutil.longToBase64(27) ,
  566. # }
  567. # self.failUnlessRaises(server.ProtocolError, self.decode, args)
  568. # test_associateDHInvalidModGen.todo = "low-priority feature"
  569. def test_associateWeirdSession
  570. args = {
  571. 'openid.mode' => 'associate',
  572. 'openid.session_type' => 'FLCL6',
  573. 'openid.dh_consumer_public' => "YQ==\n",
  574. }
  575. assert_raise(Server::ProtocolError) {
  576. @decode.call(args)
  577. }
  578. end
  579. def test_associatePlain
  580. args = {
  581. 'openid.mode' => 'associate',
  582. }
  583. r = @decode.call(args)
  584. assert(r.is_a?(Server::AssociateRequest))
  585. assert_equal(r.mode, "associate")
  586. assert_equal(r.session.session_type, "no-encryption")
  587. assert_equal(r.assoc_type, "HMAC-SHA1")
  588. end
  589. def test_nomode
  590. args = {
  591. 'openid.session_type' => 'DH-SHA1',
  592. 'openid.dh_consumer_public' => "my public keeey",
  593. }
  594. assert_raise(Server::ProtocolError) {
  595. @decode.call(args)
  596. }
  597. end
  598. def test_invalidns
  599. args = {'openid.ns' => 'Vegetables',
  600. 'openid.mode' => 'associate'}
  601. begin
  602. r = @decode.call(args)
  603. rescue Server::ProtocolError => err
  604. assert(err.openid_message)
  605. assert(err.to_s.index('Vegetables'))
  606. end
  607. end
  608. end
  609. class BogusEncoder < Server::Encoder
  610. def encode(response)
  611. return "BOGUS"
  612. end
  613. end
  614. class BogusDecoder < Server::Decoder
  615. def decode(query)
  616. return "BOGUS"
  617. end
  618. end
  619. class TestEncode < Test::Unit::TestCase
  620. def setup
  621. @encoder = Server::Encoder.new
  622. @encode = @encoder.method('encode')
  623. @op_endpoint = 'http://endpoint.unittest/encode'
  624. @store = Store::Memory.new
  625. @server = Server::Server.new(@store, @op_endpoint)
  626. end
  627. def test_id_res_OpenID2_GET
  628. # Check that when an OpenID 2 response does not exceed the OpenID
  629. # 1 message size, a GET response (i.e., redirect) is issued.
  630. request = Server::CheckIDRequest.new(
  631. 'http://bombom.unittest/',
  632. 'http://burr.unittest/999',
  633. @server.op_endpoint,
  634. 'http://burr.unittest/',
  635. false,
  636. nil)
  637. request.message = Message.new(OPENID2_NS)
  638. response = Server::OpenIDResponse.new(request)
  639. response.fields = Message.from_openid_args({
  640. 'ns' => OPENID2_NS,
  641. 'mode' => 'id_res',
  642. 'identity' => request.identity,
  643. 'claimed_id' => request.identity,
  644. 'return_to' => request.return_to,
  645. })
  646. assert(!response.render_as_form)
  647. assert(response.which_encoding == Server::ENCODE_URL)
  648. webresponse = @encode.call(response)
  649. assert(webresponse.headers.member?('location'))
  650. end
  651. def test_id_res_OpenID2_POST
  652. # Check that when an OpenID 2 response exceeds the OpenID 1
  653. # message size, a POST response (i.e., an HTML form) is returned.
  654. request = Server::CheckIDRequest.new(
  655. 'http://bombom.unittest/',
  656. 'http://burr.unittest/999',
  657. @server.op_endpoint,
  658. 'http://burr.unittest/',
  659. false,
  660. nil)
  661. request.message = Message.new(OPENID2_NS)
  662. response = Server::OpenIDResponse.new(request)
  663. response.fields = Message.from_openid_args({
  664. 'ns' => OPENID2_NS,
  665. 'mode' => 'id_res',
  666. 'identity' => request.identity,
  667. 'claimed_id' => request.identity,
  668. 'return_to' => 'x' * OPENID1_URL_LIMIT,
  669. })
  670. assert(response.render_as_form)
  671. assert(response.encode_to_url.length > OPENID1_URL_LIMIT)
  672. assert(response.which_encoding == Server::ENCODE_HTML_FORM)
  673. webresponse = @encode.call(response)
  674. assert_equal(webresponse.body, response.to_form_markup)
  675. end
  676. def test_to_form_markup
  677. request = Server::CheckIDRequest.new(
  678. 'http://bombom.unittest/',
  679. 'http://burr.unittest/999',
  680. @server.op_endpoint,
  681. 'http://burr.unittest/',
  682. false,
  683. nil)
  684. request.message = Message.new(OPENID2_NS)
  685. response = Server::OpenIDResponse.new(request)
  686. response.fields = Message.from_openid_args({
  687. 'ns' => OPENID2_NS,
  688. 'mode' => 'id_res',
  689. 'identity' => request.identity,
  690. 'claimed_id' => request.identity,
  691. 'return_to' => 'x' * OPENID1_URL_LIMIT,
  692. })
  693. form_markup = response.to_form_markup({'foo'=>'bar'})
  694. assert(/ foo="bar"/ =~ form_markup, form_markup)
  695. end
  696. def test_to_html
  697. request = Server::CheckIDRequest.new(
  698. 'http://bombom.unittest/',
  699. 'http://burr.unittest/999',
  700. @server.op_endpoint,
  701. 'http://burr.unittest/',
  702. false,
  703. nil)
  704. request.message = Message.new(OPENID2_NS)
  705. response = Server::OpenIDResponse.new(request)
  706. response.fields = Message.from_openid_args({
  707. 'ns' => OPENID2_NS,
  708. 'mode' => 'id_res',
  709. 'identity' => request.identity,
  710. 'claimed_id' => request.identity,
  711. 'return_to' => 'x' * OPENID1_URL_LIMIT,
  712. })
  713. html = response.to_html
  714. assert(html)
  715. end
  716. def test_id_res_OpenID1_exceeds_limit
  717. # Check that when an OpenID 1 response exceeds the OpenID 1
  718. # message size, a GET response is issued. Technically, this
  719. # shouldn't be permitted by the library, but this test is in place
  720. # to preserve the status quo for OpenID 1.
  721. request = Server::CheckIDRequest.new(
  722. 'http://bombom.unittest/',
  723. 'http://burr.unittest/999',
  724. @server.op_endpoint,
  725. 'http://burr.unittest/',
  726. false,
  727. nil)
  728. request.message = Message.new(OPENID1_NS)
  729. response = Server::OpenIDResponse.new(request)
  730. response.fields = Message.from_openid_args({
  731. 'mode' => 'id_res',
  732. 'identity' => request.identity,
  733. 'return_to' => 'x' * OPENID1_URL_LIMIT,
  734. })
  735. assert(!response.render_as_form)
  736. assert(response.encode_to_url.length > OPENID1_URL_LIMIT)
  737. assert(response.which_encoding == Server::ENCODE_URL)
  738. webresponse = @encode.call(response)
  739. assert_equal(webresponse.headers['location'], response.encode_to_url)
  740. end
  741. def test_id_res
  742. request = Server::CheckIDRequest.new(
  743. 'http://bombom.unittest/',
  744. 'http://burr.unittest/999',
  745. @server.op_endpoint,
  746. 'http://burr.unittest/',
  747. false, nil)
  748. request.message = Message.new(OPENID1_NS)
  749. response = Server::OpenIDResponse.new(request)
  750. response.fields = Message.from_openid_args({
  751. 'mode' => 'id_res',
  752. 'identity' => request.identity,
  753. 'return_to' => request.return_to,
  754. })
  755. webresponse = @encode.call(response)
  756. assert_equal(webresponse.code, Server::HTTP_REDIRECT)
  757. assert(webresponse.headers.member?('location'))
  758. location = webresponse.headers['location']
  759. assert(location.starts_with?(request.return_to),
  760. sprintf("%s does not start with %s",
  761. location, request.return_to))
  762. # argh.
  763. q2 = Util.parse_query(URI::parse(location).query)
  764. expected = response.fields.to_post_args
  765. assert_equal(q2, expected)
  766. end
  767. def test_cancel
  768. request = Server::CheckIDRequest.new(
  769. 'http://bombom.unittest/',
  770. 'http://burr.unittest/999',
  771. @server.op_endpoint,
  772. 'http://burr.unittest/',
  773. false, nil)
  774. request.message = Message.new(OPENID2_NS)
  775. response = Server::OpenIDResponse.new(request)
  776. response.fields = Message.from_openid_args({
  777. 'mode' => 'cancel',
  778. })
  779. webresponse = @encode.call(response)
  780. assert_equal(webresponse.code, Server::HTTP_REDIRECT)
  781. assert(webresponse.headers.member?('location'))
  782. end
  783. def test_cancel_to_form
  784. request = Server::CheckIDRequest.new(
  785. 'http://bombom.unittest/',
  786. 'http://burr.unittest/999',
  787. @server.op_endpoint,
  788. 'http://burr.unittest/',
  789. false, nil)
  790. request.message = Message.new(OPENID2_NS)
  791. response = Server::OpenIDResponse.new(request)
  792. response.fields = Message.from_openid_args({
  793. 'mode' => 'cancel',
  794. })
  795. form = response.to_form_markup
  796. assert(form.index(request.return_to))
  797. end
  798. def test_assocReply
  799. msg = Message.new(OPENID2_NS)
  800. msg.set_arg(OPENID2_NS, 'session_type', 'no-encryption')
  801. request = Server::AssociateRequest.from_message(msg)
  802. response = Server::OpenIDResponse.new(request)
  803. response.fields = Message.from_post_args(
  804. {'openid.assoc_handle' => "every-zig"})
  805. webresponse = @encode.call(response)
  806. body = "assoc_handle:every-zig\n"
  807. assert_equal(webresponse.code, Server::HTTP_OK)
  808. assert_equal(webresponse.headers, {})
  809. assert_equal(webresponse.body, body)
  810. end
  811. def test_checkauthReply
  812. request = Server::CheckAuthRequest.new('a_sock_monkey',
  813. 'siggggg',
  814. [])
  815. request.message = Message.new(OPENID2_NS)
  816. response = Server::OpenIDResponse.new(request)
  817. response.fields = Message.from_openid_args({
  818. 'is_valid' => 'true',
  819. 'invalidate_handle' => 'xXxX:xXXx'
  820. })
  821. body = "invalidate_handle:xXxX:xXXx\nis_valid:true\n"
  822. webresponse = @encode.call(response)
  823. assert_equal(webresponse.code, Server::HTTP_OK)
  824. assert_equal(webresponse.headers, {})
  825. assert_equal(webresponse.body, body)
  826. end
  827. def test_unencodableError
  828. args = Message.from_post_args({
  829. 'openid.identity' => 'http://limu.unittest/',
  830. })
  831. e = Server::ProtocolError.new(args, "wet paint")
  832. assert_raise(Server::EncodingError) {
  833. @encode.call(e)
  834. }
  835. end
  836. def test_encodableError
  837. args = Message.from_post_args({
  838. 'openid.mode' => 'associate',
  839. 'openid.identity' => 'http://limu.unittest/',
  840. })
  841. body="error:snoot\nmode:error\n"
  842. webresponse = @encode.call(Server::ProtocolError.new(args, "snoot"))
  843. assert_equal(webresponse.code, Server::HTTP_ERROR)
  844. assert_equal(webresponse.headers, {})
  845. assert_equal(webresponse.body, body)
  846. end
  847. end
  848. class TestSigningEncode < Test::Unit::TestCase
  849. def setup
  850. @_dumb_key = Server::Signatory._dumb_key
  851. @_normal_key = Server::Signatory._normal_key
  852. @store = Store::Memory.new()
  853. @server = Server::Server.new(@store, "http://signing.unittest/enc")
  854. @request = Server::CheckIDRequest.new(
  855. 'http://bombom.unittest/',
  856. 'http://burr.unittest/999',
  857. @server.op_endpoint,
  858. 'http://burr.unittest/',
  859. false, nil)
  860. @request.message = Message.new(OPENID2_NS)
  861. @response = Server::OpenIDResponse.new(@request)
  862. @response.fields = Message.from_openid_args({
  863. 'mode' => 'id_res',
  864. 'identity' => @request.identity,
  865. 'return_to' => @request.return_to,
  866. })
  867. @signatory = Server::Signatory.new(@store)
  868. @encoder = Server::SigningEncoder.new(@signatory)
  869. @encode = @encoder.method('encode')
  870. end
  871. def test_idres
  872. assoc_handle = '{bicycle}{shed}'
  873. @store.store_association(
  874. @_normal_key,
  875. Association.from_expires_in(60, assoc_handle,
  876. 'sekrit', 'HMAC-SHA1'))
  877. @request.assoc_handle = assoc_handle
  878. webresponse = @encode.call(@response)
  879. assert_equal(webresponse.code, Server::HTTP_REDIRECT)
  880. assert(webresponse.headers.member?('location'))
  881. location = webresponse.headers['location']
  882. query = Util.parse_query(URI::parse(location).query)
  883. assert(query.member?('openid.sig'))
  884. assert(query.member?('openid.assoc_handle'))
  885. assert(query.member?('openid.signed'))
  886. end
  887. def test_idresDumb
  888. webresponse = @encode.call(@response)
  889. assert_equal(webresponse.code, Server::HTTP_REDIRECT)
  890. assert(webresponse.headers.has_key?('location'))
  891. location = webresponse.headers['location']
  892. query = Util.parse_query(URI::parse(location).query)
  893. assert(query.member?('openid.sig'))
  894. assert(query.member?('openid.assoc_handle'))
  895. assert(query.member?('openid.signed'))
  896. end
  897. def test_forgotStore
  898. @encoder.signatory = nil
  899. assert_raise(ArgumentError) {
  900. @encode.call(@response)
  901. }
  902. end
  903. def test_cancel
  904. request = Server::CheckIDRequest.new(
  905. 'http://bombom.unittest/',
  906. 'http://burr.unittest/999',
  907. @server.op_endpoint,
  908. 'http://burr.unittest/',
  909. false, nil)
  910. request.message = Message.new(OPENID2_NS)
  911. response = Server::OpenIDResponse.new(request)
  912. response.fields.set_arg(OPENID_NS, 'mode', 'cancel')
  913. webresponse = @encode.call(response)
  914. assert_equal(webresponse.code, Server::HTTP_REDIRECT)
  915. assert(webresponse.headers.has_key?('location'))
  916. location = webresponse.headers['location']
  917. query = Util.parse_query(URI::parse(location).query)
  918. assert(!query.has_key?('openid.sig'), response.fields.to_post_args())
  919. end
  920. def test_assocReply
  921. msg = Message.new(OPENID2_NS)
  922. msg.set_arg(OPENID2_NS, 'session_type', 'no-encryption')
  923. request = Server::AssociateRequest.from_message(msg)
  924. response = Server::OpenIDResponse.new(request)
  925. response.fields = Message.from_openid_args({'assoc_handle' => "every-zig"})
  926. webresponse = @encode.call(response)
  927. body = "assoc_handle:every-zig\n"
  928. assert_equal(webresponse.code, Server::HTTP_OK)
  929. assert_equal(webresponse.headers, {})
  930. assert_equal(webresponse.body, body)
  931. end
  932. def test_alreadySigned
  933. @response.fields.set_arg(OPENID_NS, 'sig', 'priorSig==')
  934. assert_raise(Server::AlreadySigned) {
  935. @encode.call(@response)
  936. }
  937. end
  938. end
  939. class TestCheckID < Test::Unit::TestCase
  940. def setup
  941. @op_endpoint = 'http://endpoint.unittest/'
  942. @store = Store::Memory.new()
  943. @server = Server::Server.new(@store, @op_endpoint)
  944. @request = Server::CheckIDRequest.new(
  945. 'http://bambam.unittest/',
  946. 'http://bar.unittest/999',
  947. @server.op_endpoint,
  948. 'http://bar.unittest/',
  949. false)
  950. @request.message = Message.new(OPENID2_NS)
  951. end
  952. def test_trustRootInvalid
  953. @request.trust_root = "http://foo.unittest/17"
  954. @request.return_to = "http://foo.unittest/39"
  955. assert(!@request.trust_root_valid())
  956. end
  957. def test_trustRootInvalid_modified
  958. @request.trust_root = "does://not.parse/"
  959. @request.message = :sentinel
  960. begin
  961. result = @request.trust_root_valid
  962. rescue Server::MalformedTrustRoot => why
  963. assert_equal(:sentinel, why.openid_message)
  964. else
  965. flunk("Expected MalformedTrustRoot, got #{result.inspect}")
  966. end
  967. end
  968. def test_trustRootvalid_absent_trust_root
  969. @request.trust_root = nil
  970. assert(@request.trust_root_valid())
  971. end
  972. def test_trustRootValid
  973. @request.trust_root = "http://foo.unittest/"
  974. @request.return_to = "http://foo.unittest/39"
  975. assert(@request.trust_root_valid())
  976. end
  977. def test_trustRootValidNoReturnTo
  978. request = Server::CheckIDRequest.new(
  979. 'http://bambam.unittest/',
  980. nil,
  981. @server.op_endpoint,
  982. 'http://bar.unittest/',
  983. false)
  984. assert(request.trust_root_valid())
  985. end
  986. def test_returnToVerified_callsVerify
  987. # Make sure that verifyReturnTo is calling the trustroot
  988. # function verifyReturnTo
  989. # Ensure that exceptions are passed through
  990. sentinel = Exception.new()
  991. __req = @request
  992. tc = self
  993. vrfyExc = Proc.new { |trust_root, return_to|
  994. tc.assert_equal(__req.trust_root, trust_root)
  995. tc.assert_equal(__req.return_to, return_to)
  996. raise sentinel
  997. }
  998. TrustRoot.extend(OverrideMethodMixin)
  999. TrustRoot.with_method_overridden(:verify_return_to, vrfyExc) do
  1000. begin
  1001. @request.return_to_verified()
  1002. flunk("Expected sentinel to be raised, got success")
  1003. rescue Exception => e
  1004. assert(e.equal?(sentinel), [e, sentinel].inspect)
  1005. end
  1006. end
  1007. # Ensure that True and False are passed through unchanged
  1008. constVerify = Proc.new { |val|
  1009. verify = Proc.new { |trust_root, return_to|
  1010. tc.assert_equal(__req.trust_root, trust_root)
  1011. tc.assert_equal(__req.request.return_to, return_to)
  1012. return val
  1013. }
  1014. return verify
  1015. }
  1016. [true, false].each { |val|
  1017. verifier = constVerify.call(val)
  1018. TrustRoot.with_method_overridden(:verify_return_to, verifier) do
  1019. assert_equal(val, @request.return_to_verified())
  1020. end
  1021. }
  1022. end
  1023. def _expectAnswer(answer, identity=nil, claimed_id=nil)
  1024. expected_list = [
  1025. ['mode', 'id_res'],
  1026. ['return_to', @request.return_to],
  1027. ['op_endpoint', @op_endpoint],
  1028. ]
  1029. if identity
  1030. expected_list << ['identity', identity]
  1031. if claimed_id
  1032. expected_list << ['claimed_id', claimed_id]
  1033. else
  1034. expected_list << ['claimed_id', identity]
  1035. end
  1036. end
  1037. expected_list.each { |k, expected|
  1038. actual = answer.fields.get_arg(OPENID_NS, k)
  1039. assert_equal(expected, actual,
  1040. sprintf("%s: expected %s, got %s",
  1041. k, expected, actual))
  1042. }
  1043. assert(answer.fields.has_key?(OPENID_NS, 'response_nonce'))
  1044. assert(answer.fields.get_openid_namespace() == OPENID2_NS)
  1045. # One for nonce, one for ns
  1046. assert_equal(answer.fields.to_post_args.length,
  1047. expected_list.length + 2,
  1048. answer.fields.to_post_args.inspect)
  1049. end
  1050. def test_answerAllow
  1051. # Check the fields specified by "Positive Assertions"
  1052. #
  1053. # including mode=id_res, identity, claimed_id, op_endpoint,
  1054. # return_to
  1055. answer = @request.answer(true)
  1056. assert_equal(answer.request, @request)
  1057. _expectAnswer(answer, @request.identity)
  1058. end
  1059. def test_answerAllowDelegatedIdentity
  1060. @request.claimed_id = 'http://delegating.unittest/'
  1061. answer = @request.answer(true)
  1062. _expectAnswer(answer, @request.identity,
  1063. @request.claimed_id)
  1064. end
  1065. def test_answerAllowWithoutIdentityReally
  1066. @request.identity = nil
  1067. answer = @request.answer(true)
  1068. assert_equal(answer.request, @request)
  1069. _expectAnswer(answer)
  1070. end
  1071. def test_answerAllowAnonymousFail
  1072. @request.identity = nil
  1073. # XXX - Check on this, I think this behavior is legal in OpenID
  1074. # 2.0?
  1075. assert_raise(ArgumentError) {
  1076. @request.answer(true, nil, "=V")
  1077. }
  1078. end
  1079. def test_answerAllowWithIdentity
  1080. @request.identity = IDENTIFIER_SELECT
  1081. selected_id = 'http://anon.unittest/9861'
  1082. answer = @request.answer(true, nil, selected_id)
  1083. _expectAnswer(answer, selected_id)
  1084. end
  1085. def test_answerAllowWithNoIdentity
  1086. @request.identity = IDENTIFIER_SELECT
  1087. selected_id = 'http://anon.unittest/9861'
  1088. assert_raise(ArgumentError) {
  1089. answer = @request.answer(true, nil, nil)
  1090. }
  1091. end
  1092. def test_immediate_openid1_no_identity
  1093. @request.message = Message.new(OPENID1_NS)
  1094. @request.immediate = true
  1095. @request.mode = 'checkid_immediate'
  1096. resp = @request.answer(false)
  1097. assert(resp.fields.get_arg(OPENID_NS, 'mode') == 'id_res')
  1098. end
  1099. def test_checkid_setup_openid1_no_identity
  1100. @request.message = Message.new(OPENID1_NS)
  1101. @request.immediate = false
  1102. @request.mode = 'checkid_setup'
  1103. resp = @request.answer(false)
  1104. assert(resp.fields.get_arg(OPENID_NS, 'mode') == 'cancel')
  1105. end
  1106. def test_immediate_openid1_no_server_url
  1107. @request.message = Message.new(OPENID1_NS)
  1108. @request.immediate = true
  1109. @request.mode = 'checkid_immediate'
  1110. @request.op_endpoint = nil
  1111. assert_raise(ArgumentError) {
  1112. resp = @request.answer(false)
  1113. }
  1114. end
  1115. def test_immediate_encode_to_url
  1116. @request.message = Message.new(OPENID1_NS)
  1117. @request.immediate = true
  1118. @request.mode = 'checkid_immediate'
  1119. @request.trust_root = "BOGUS"
  1120. @request.assoc_handle = "ASSOC"
  1121. server_url = "http://server.com/server"
  1122. url = @request.encode_to_url(server_url)
  1123. assert(url.starts_with?(server_url))
  1124. unused, query = url.split("?", 2)
  1125. args = Util.parse_query(query)
  1126. m = Message.from_post_args(args)
  1127. assert(m.get_arg(OPENID_NS, 'trust_root') == "BOGUS")
  1128. assert(m.get_arg(OPENID_NS, 'assoc_handle') == "ASSOC")
  1129. assert(m.get_arg(OPENID_NS, 'mode'), "checkid_immediate")
  1130. assert(m.get_arg(OPENID_NS, 'identity') == @request.identity)
  1131. assert(m.get_arg(OPENID_NS, 'claimed_id') == @request.claimed_id)
  1132. assert(m.get_arg(OPENID_NS, 'return_to') == @request.return_to)
  1133. end
  1134. def test_answerAllowWithDelegatedIdentityOpenID2
  1135. # Answer an IDENTIFIER_SELECT case with a delegated identifier.
  1136. # claimed_id delegates to selected_id here.
  1137. @request.identity = IDENTIFIER_SELECT
  1138. selected_id = 'http://anon.unittest/9861'
  1139. claimed_id = 'http://monkeyhat.unittest/'
  1140. answer = @request.answer(true, nil, selected_id, claimed_id)
  1141. _expectAnswer(answer, selected_id, claimed_id)
  1142. end
  1143. def test_answerAllowWithDelegatedIdentityOpenID1
  1144. # claimed_id parameter doesn't exist in OpenID 1.
  1145. @request.message = Message.new(OPENID1_NS)
  1146. # claimed_id delegates to selected_id here.
  1147. @request.identity = IDENTIFIER_SELECT
  1148. selected_id = 'http://anon.unittest/9861'
  1149. claimed_id = 'http://monkeyhat.unittest/'
  1150. assert_raise(Server::VersionError) {
  1151. @request.answer(true, nil, selected_id, claimed_id)
  1152. }
  1153. end
  1154. def test_answerAllowWithAnotherIdentity
  1155. # XXX - Check on this, I think this behavior is legal in OpenID
  1156. # 2.0?
  1157. assert_raise(ArgumentError){
  1158. @request.answer(true, nil, "http://pebbles.unittest/")
  1159. }
  1160. end
  1161. def test_answerAllowNoIdentityOpenID1
  1162. @request.message = Message.new(OPENID1_NS)
  1163. @request.identity = nil
  1164. assert_raise(ArgumentError) {
  1165. @request.answer(true, nil, nil)
  1166. }
  1167. end
  1168. def test_answerAllowForgotEndpoint
  1169. @request.op_endpoint = nil
  1170. assert_raise(RuntimeError) {
  1171. @request.answer(true)
  1172. }
  1173. end
  1174. def test_checkIDWithNoIdentityOpenID1
  1175. msg = Message.new(OPENID1_NS)
  1176. msg.set_arg(OPENID_NS, 'return_to', 'bogus')
  1177. msg.set_arg(OPENID_NS, 'trust_root', 'bogus')
  1178. msg.set_arg(OPENID_NS, 'mode', 'checkid_setup')
  1179. msg.set_arg(OPENID_NS, 'assoc_handle', 'bogus')
  1180. assert_raise(Server::ProtocolError) {
  1181. Server::CheckIDRequest.from_message(msg, @server)
  1182. }
  1183. end
  1184. def test_fromMessageClaimedIDWithoutIdentityOpenID2
  1185. msg = Message.new(OPENID2_NS)
  1186. msg.set_arg(OPENID_NS, 'mode', 'checkid_setup')
  1187. msg.set_arg(OPENID_NS, 'return_to', 'http://invalid:8000/rt')
  1188. msg.set_arg(OPENID_NS, 'claimed_id', 'https://example.myopenid.com')
  1189. assert_raise(Server::ProtocolError) {
  1190. Server::CheckIDRequest.from_message(msg, @server)
  1191. }
  1192. end
  1193. def test_fromMessageIdentityWithoutClaimedIDOpenID2
  1194. msg = Message.new(OPENID2_NS)
  1195. msg.set_arg(OPENID_NS, 'mode', 'checkid_setup')
  1196. msg.set_arg(OPENID_NS, 'return_to', 'http://invalid:8000/rt')
  1197. msg.set_arg(OPENID_NS, 'identity', 'https://example.myopenid.com')
  1198. assert_raise(Server::ProtocolError) {
  1199. Server::CheckIDRequest.from_message(msg, @server)
  1200. }
  1201. end
  1202. def test_fromMessageWithEmptyTrustRoot
  1203. return_to = 'http://some.url/foo?bar=baz'
  1204. msg = Message.from_post_args({
  1205. 'openid.assoc_handle' => '{blah}{blah}{OZivdQ==}',
  1206. 'openid.claimed_id' => 'http://delegated.invalid/',
  1207. 'openid.identity' => 'http://op-local.example.com/',
  1208. 'openid.mode' => 'checkid_setup',
  1209. 'openid.ns' => 'http://openid.net/signon/1.0',
  1210. 'openid.return_to' => return_to,
  1211. 'openid.trust_root' => ''
  1212. });
  1213. result = Server::CheckIDRequest.from_message(msg, @server)
  1214. assert_equal(return_to, result.trust_root)
  1215. end
  1216. def test_trustRootOpenID1
  1217. # Ignore openid.realm in OpenID 1
  1218. msg = Message.new(OPENID1_NS)
  1219. msg.set_arg(OPENID_NS, 'mode', 'checkid_setup')
  1220. msg.set_arg(OPENID_NS, 'trust_root', 'http://trustroot.com/')
  1221. msg.set_arg(OPENID_NS, 'realm', 'http://fake_trust_root/')
  1222. msg.set_arg(OPENID_NS, 'return_to', 'http://trustroot.com/foo')
  1223. msg.set_arg(OPENID_NS, 'assoc_handle', 'bogus')
  1224. msg.set_arg(OPENID_NS, 'identity', 'george')
  1225. result = Server::CheckIDRequest.from_message(msg, @server.op_endpoint)
  1226. assert(result.trust_root == 'http://trustroot.com/')
  1227. end
  1228. def test_trustRootOpenID2
  1229. # Ignore openid.trust_root in OpenID 2
  1230. msg = Message.new(OPENID2_NS)
  1231. msg.set_arg(OPENID_NS, 'mode', 'checkid_setup')
  1232. msg.set_arg(OPENID_NS, 'realm', 'http://trustroot.com/')
  1233. msg.set_arg(OPENID_NS, 'trust_root', 'http://fake_trust_root/')
  1234. msg.set_arg(OPENID_NS, 'return_to', 'http://trustroot.com/foo')
  1235. msg.set_arg(OPENID_NS, 'assoc_handle', 'bogus')
  1236. msg.set_arg(OPENID_NS, 'identity', 'george')
  1237. msg.set_arg(OPENID_NS, 'claimed_id', 'george')
  1238. result = Se

Large files files are truncated, but you can click here to view the full file