AuthenticationContext.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Copyright 2014 Fabian Grutschus. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. * The views and conclusions contained in the software and documentation are those
  27. * of the authors and should not be interpreted as representing official policies,
  28. * either expressed or implied, of the copyright holders.
  29. *
  30. * @author Fabian Grutschus <f.grutschus@lubyte.de>
  31. * @copyright 2014 Fabian Grutschus. All rights reserved.
  32. * @license BSD
  33. * @link http://github.com/fabiang/xmpp
  34. */
  35. namespace Fabiang\Xmpp\Integration;
  36. use Behat\Behat\Context\BehatContext;
  37. use Behat\Behat\Exception\PendingException;
  38. use Fabiang\Xmpp\Util\XML;
  39. class AuthenticationContext extends BehatContext
  40. {
  41. /**
  42. * @Given /^Test response data for plain$/
  43. */
  44. public function testResponseDataForPlain()
  45. {
  46. $this->getConnection()->setData(array(
  47. "<?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' "
  48. . "id='1234567890' from='localhost' version='1.0' xml:lang='en'><stream:features>"
  49. . "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>PLAIN</mechanism></mechanisms>"
  50. . "</stream:features>",
  51. "<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>",
  52. "<?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' "
  53. . "id='1234567890' from='localhost' version='1.0' xml:lang='en'><stream:features></stream:features>"
  54. ));
  55. }
  56. /**
  57. * @Given /^Test response data for authentication failure$/
  58. */
  59. public function testResponseDataForAuthenticationFailure()
  60. {
  61. $this->getConnection()->setData(array(
  62. "<?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' "
  63. . "id='1234567890' from='localhost' version='1.0' xml:lang='en'><stream:features>"
  64. . "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>PLAIN</mechanism></mechanisms>"
  65. . "</stream:features>",
  66. "<failure xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><not-authorized/></failure>"
  67. ));
  68. }
  69. /**
  70. * @Given /^Test response data for digest-md5 auth$/
  71. */
  72. public function testResponseDataForDigestMdAuth()
  73. {
  74. $this->getConnection()->setData(array(
  75. "<?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' "
  76. . "id='1234567890' from='localhost' version='1.0' xml:lang='en'>",
  77. "<stream:features>"
  78. . "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>DIGEST-MD5</mechanism></mechanisms>"
  79. . "</stream:features>",
  80. '<challenge xmlns="urn:ietf:params:xml:ns:xmpp-sasl">'
  81. . XML::base64Encode(
  82. 'realm="localhost",nonce="abcdefghijklmnopqrstuvw",'
  83. . 'qop="auth",charset=utf-8,algorithm=md5-sess'
  84. )
  85. . '</challenge>',
  86. '<challenge xmlns="urn:ietf:params:xml:ns:xmpp-sasl">'
  87. . XML::base64Encode('rspauth=1234567890') . '</challenge>',
  88. '<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl">'
  89. . XML::base64Encode('rspauth=7fb0ac7ac1ff501a330a76e89a0f1633')
  90. . '</success>',
  91. "<?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' "
  92. . "id='1234567890' from='localhost' version='1.0' xml:lang='en'><stream:features></stream:features>"
  93. ));
  94. }
  95. /**
  96. * @Then /^plain authentication element should be send$/
  97. */
  98. public function plainAuthenticationElementShouldBeSend()
  99. {
  100. assertContains(
  101. '<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="PLAIN">AGFhYQBiYmI=</auth>',
  102. $this->getConnection()->getBuffer()
  103. );
  104. }
  105. /**
  106. * @Then /^digest-md5 authentication element should be send$/
  107. */
  108. public function digestMdAuthenticationElementShouldBeSend()
  109. {
  110. $buffer = $this->getConnection()->getBuffer();
  111. assertContains('<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="DIGEST-MD5"/>', $buffer[1]);
  112. }
  113. /**
  114. * @Given /^should be authenticated$/
  115. */
  116. public function shouldBeAuthenticated()
  117. {
  118. assertTrue($this->getConnection()->getOptions()->isAuthenticated());
  119. }
  120. /**
  121. * @Then /^a authorization exception should be catched$/
  122. */
  123. public function aAuthorizationExceptionShouldBeCatched()
  124. {
  125. /* @var $exception \Exception */
  126. $exception = $this->getMainContext()->exception;
  127. assertInstanceOf('\Fabiang\Xmpp\Exception\Stream\AuthenticationErrorException', $exception);
  128. assertSame('Stream Error: "not-authorized"', $exception->getMessage());
  129. }
  130. /**
  131. * @Then /^digest-md5 response send$/
  132. */
  133. public function digestMdResponseSend()
  134. {
  135. $buffer = $this->getConnection()->getBuffer();
  136. assertRegExp('#^<response xmlns="urn:ietf:params:xml:ns:xmpp-sasl">[\w=]+</response>$#', $buffer[2]);
  137. }
  138. /**
  139. * @Then /^empty digest-md5 response send$/
  140. */
  141. public function emptyDigestMdResponseSend()
  142. {
  143. $buffer = $this->getConnection()->getBuffer();
  144. assertRegExp('<response xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>', $buffer[3]);
  145. }
  146. /**
  147. *
  148. * @return \Fabiang\Xmpp\Connection\Test
  149. */
  150. public function getConnection()
  151. {
  152. return $this->getMainContext()->getConnection();
  153. }
  154. }