u2f-server.phps 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/php
  2. <?php
  3. /* Copyright (c) 2015 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 basic example of a u2f-server command line that can be used
  32. * with the u2f-host binary to perform regitrations and authentications.
  33. */
  34. require_once('../../src/u2flib_server/U2F.php');
  35. $options = getopt("rao:R:");
  36. $mode;
  37. $challenge;
  38. $response;
  39. $result;
  40. $regs;
  41. if(array_key_exists('r', $options)) {
  42. $mode = "register";
  43. } elseif(array_key_exists('a', $options)) {
  44. if(!array_key_exists('R', $options)) {
  45. print "a registration must be supplied with -R";
  46. exit(1);
  47. }
  48. $regs = json_decode('[' . $options['R'] . ']');
  49. $mode = "authenticate";
  50. } else {
  51. print "-r or -a must be used\n";
  52. exit(1);
  53. }
  54. if(!array_key_exists('o', $options)) {
  55. print "origin must be supplied with -o\n";
  56. exit(1);
  57. }
  58. $u2f = new u2flib_server\U2F($options['o']);
  59. if($mode === "register") {
  60. $challenge = $u2f->getRegisterData();
  61. } elseif($mode === "authenticate") {
  62. $challenge = $u2f->getAuthenticateData($regs);
  63. }
  64. print json_encode($challenge[0]) . "\n";
  65. $response = fgets(STDIN);
  66. if($mode === "register") {
  67. $result = $u2f->doRegister($challenge[0], json_decode($response));
  68. } elseif($mode === "authenticate") {
  69. $result = $u2f->doAuthenticate($challenge, $regs, json_decode($response));
  70. }
  71. print json_encode($result) . "\n";
  72. ?>