index.phps 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Yubico AB
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * This is a minimal example of U2F registration and authentication.
  32. * The data that has to be stored between registration and authentication
  33. * is stored in browser localStorage, so there's nothing real-world
  34. * about this.
  35. */
  36. require_once('../../src/u2flib_server/U2F.php');
  37. $scheme = isset($_SERVER['HTTPS']) ? "https://" : "http://";
  38. $u2f = new u2flib_server\U2F($scheme . $_SERVER['HTTP_HOST']);
  39. ?>
  40. <html>
  41. <head>
  42. <title>PHP U2F Demo</title>
  43. <script src="../assets/u2f-api.js"></script>
  44. <script>
  45. function addRegistration(reg) {
  46. var existing = localStorage.getItem('u2fregistration');
  47. var regobj = JSON.parse(reg);
  48. var data = null;
  49. if(existing) {
  50. data = JSON.parse(existing);
  51. if(Array.isArray(data)) {
  52. for (var i = 0; i < data.length; i++) {
  53. if(data[i].keyHandle === regobj.keyHandle) {
  54. data.splice(i,1);
  55. break;
  56. }
  57. }
  58. data.push(regobj);
  59. } else {
  60. data = null;
  61. }
  62. }
  63. if(data == null) {
  64. data = [regobj];
  65. }
  66. localStorage.setItem('u2fregistration', JSON.stringify(data));
  67. }
  68. <?php
  69. function fixupArray($data) {
  70. $ret = array();
  71. $decoded = json_decode($data);
  72. foreach ($decoded as $d) {
  73. $ret[] = json_encode($d);
  74. }
  75. return $ret;
  76. }
  77. if($_SERVER['REQUEST_METHOD'] === 'POST') {
  78. if(isset($_POST['startRegister'])) {
  79. $regs = json_decode($_POST['registrations']) ? : array();
  80. list($data, $reqs) = $u2f->getRegisterData($regs);
  81. echo "var request = " . json_encode($data) . ";\n";
  82. echo "var signs = " . json_encode($reqs) . ";\n";
  83. ?>
  84. setTimeout(function() {
  85. console.log("Register: ", request);
  86. u2f.register([request], signs, function(data) {
  87. var form = document.getElementById('form');
  88. var reg = document.getElementById('doRegister');
  89. var req = document.getElementById('request');
  90. console.log("Register callback", data);
  91. if(data.errorCode && data.errorCode != 0) {
  92. alert("registration failed with errror: " + data.errorCode);
  93. return;
  94. }
  95. reg.value=JSON.stringify(data);
  96. req.value=JSON.stringify(request);
  97. form.submit();
  98. });
  99. }, 1000);
  100. <?php
  101. } else if($_POST['doRegister']) {
  102. try {
  103. $data = $u2f->doRegister(json_decode($_POST['request']), json_decode($_POST['doRegister']));
  104. echo "var registration = '" . json_encode($data) . "';\n";
  105. ?>
  106. addRegistration(registration);
  107. alert("registration successful!");
  108. <?php
  109. } catch(u2flib_server\Error $e) {
  110. echo "alert('error:" . $e->getMessage() . "');\n";
  111. }
  112. } else if(isset($_POST['startAuthenticate'])) {
  113. $regs = json_decode($_POST['registrations']);
  114. $data = $u2f->getAuthenticateData($regs);
  115. echo "var registrations = " . $_POST['registrations'] . ";\n";
  116. echo "var request = " . json_encode($data) . ";\n";
  117. ?>
  118. setTimeout(function() {
  119. console.log("sign: ", request);
  120. u2f.sign(request, function(data) {
  121. var form = document.getElementById('form');
  122. var reg = document.getElementById('doAuthenticate');
  123. var req = document.getElementById('request');
  124. var regs = document.getElementById('registrations');
  125. console.log("Authenticate callback", data);
  126. reg.value=JSON.stringify(data);
  127. req.value=JSON.stringify(request);
  128. regs.value=JSON.stringify(registrations);
  129. form.submit();
  130. });
  131. }, 1000);
  132. <?php
  133. } else if($_POST['doAuthenticate']) {
  134. $reqs = json_decode($_POST['request']);
  135. $regs = json_decode($_POST['registrations']);
  136. try {
  137. $data = $u2f->doAuthenticate($reqs, $regs, json_decode($_POST['doAuthenticate']));
  138. echo "var registration = '" . json_encode($data) . "';\n";
  139. echo "addRegistration(registration);\n";
  140. echo "alert('Authentication successful, counter:" . $data->counter . "');\n";
  141. } catch(u2flib_server\Error $e) {
  142. echo "alert('error:" . $e->getMessage() . "');\n";
  143. }
  144. }
  145. }
  146. ?>
  147. </script>
  148. </head>
  149. <body>
  150. <form method="POST" id="form">
  151. <button name="startRegister" type="submit">Register</button>
  152. <input type="hidden" name="doRegister" id="doRegister"/>
  153. <button name="startAuthenticate" type="submit" id="startAuthenticate">Authenticate</button>
  154. <input type="hidden" name="doAuthenticate" id="doAuthenticate"/>
  155. <input type="hidden" name="request" id="request"/>
  156. <input type="hidden" name="registrations" id="registrations"/>
  157. </form>
  158. <p>
  159. <span id="registered">0</span> Authenticators currently registered.
  160. </p>
  161. <script>
  162. var reg = localStorage.getItem('u2fregistration');
  163. var auth = document.getElementById('startAuthenticate');
  164. if(reg == null) {
  165. auth.disabled = true;
  166. } else {
  167. var regs = document.getElementById('registrations');
  168. decoded = JSON.parse(reg);
  169. if(!Array.isArray(decoded)) {
  170. auth.disabled = true;
  171. } else {
  172. regs.value = reg;
  173. console.log("set the registrations to : ", reg);
  174. var regged = document.getElementById('registered');
  175. regged.innerHTML = decoded.length;
  176. }
  177. }
  178. </script>
  179. </body>
  180. </html>