PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/web/auth/openid.php

https://bitbucket.org/yoander/mtrack
PHP | 147 lines | 123 code | 20 blank | 4 comment | 40 complexity | 7b3e693c28ab06a1a63893c6e375e133 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. <?php # vim:ts=2:sw=2:et:
  2. /* For licensing and copyright terms, see the file named LICENSE */
  3. include '../../inc/common.php';
  4. require_once 'Auth/OpenID/Consumer.php';
  5. require_once 'Auth/OpenID/FileStore.php';
  6. require_once 'Auth/OpenID/SReg.php';
  7. require_once 'Auth/OpenID/PAPE.php';
  8. if (!MTrackAuth::getMech('MTrackAuth_OpenID')) {
  9. header("Location: $ABSWEB");
  10. exit;
  11. }
  12. $store_location = MTrackConfig::get('openid', 'store_dir');
  13. if (!$store_location) {
  14. $store_location = MTrackConfig::get('core', 'vardir') . '/openid';
  15. }
  16. if (!is_dir($store_location)) {
  17. mkdir($store_location);
  18. }
  19. $store = new Auth_OpenID_FileStore($store_location);
  20. $consumer = new Auth_OpenID_Consumer($store);
  21. $message = null;
  22. $pi = mtrack_get_pathinfo();
  23. if ($_SERVER['REQUEST_METHOD'] == 'POST' && $pi != 'register') {
  24. $req = null;
  25. if (!isset($_POST['openid_identifier']) ||
  26. !strlen($_POST['openid_identifier'])) {
  27. $message = "you must fill in your OpenID";
  28. } else {
  29. $id = $_POST['openid_identifier'];
  30. if (!preg_match('/^https?:\/\//', $id)) {
  31. $id = "http://$id";
  32. }
  33. $req = $consumer->begin($id);
  34. if (!$req) {
  35. $message = "not a valid OpenID";
  36. }
  37. }
  38. if ($req) {
  39. $sreg = Auth_OpenID_SRegRequest::build(
  40. array('nickname', 'fullname', 'email')
  41. );
  42. $req->addExtension($sreg);
  43. if ($req->shouldSendRedirect()) {
  44. $rurl = $req->redirectURL(
  45. $ABSWEB, $ABSWEB . 'auth/openid.php/callback');
  46. if (Auth_OpenID::isFailure($rurl)) {
  47. $message = "Unable to redirect to server: " . $rurl->message;
  48. } else {
  49. header("Location: $rurl");
  50. exit;
  51. }
  52. } else {
  53. $html = $req->htmlMarkup($ABSWEB, $ABSWEB . 'auth/openid.php/callback',
  54. false, array('id' => 'openid_message'));
  55. if (Auth_OpenID::isFailure($html)) {
  56. $message = "Unable to redirect to server: " . $html->message;
  57. } else {
  58. echo $html;
  59. }
  60. }
  61. }
  62. } else if ($pi == 'callback') {
  63. $res = $consumer->complete($ABSWEB . 'auth/openid.php/callback');
  64. if ($res->status == Auth_OpenID_CANCEL) {
  65. $message = 'Verification cancelled';
  66. } else if ($res->status == Auth_OpenID_FAILURE) {
  67. $message = 'OpenID authentication failed: ' . $res->message;
  68. } else if ($res->status == Auth_OpenID_SUCCESS) {
  69. $id = $res->getDisplayIdentifier();
  70. $sreg = Auth_OpenID_SRegResponse::fromSuccessResponse($res)->contents();
  71. if (!empty($sreg['nickname'])) {
  72. $name = $sreg['nickname'];
  73. } else if (!empty($sreg['fullname'])) {
  74. $name = $sreg['fullname'];
  75. } else {
  76. $name = $id;
  77. }
  78. $message = 'Authenticated as ' . $name;
  79. $_SESSION['openid.id'] = $id;
  80. unset($_SESSION['openid.userid']);
  81. $_SESSION['openid.name'] = $name;
  82. if (!empty($sreg['email'])) {
  83. $_SESSION['openid.email'] = $sreg['email'];
  84. }
  85. /* See if we can find a canonical identity for the user */
  86. $user = MTrackUser::loadUser($id, true);
  87. if ($user) {
  88. $_SESSION['openid.userid'] = $user->userid;
  89. header("Location: " . $ABSWEB);
  90. exit;
  91. }
  92. /* prompt the user to fill out some basic details so that we can create
  93. * a local identity and associate their OpenID with it */
  94. $_SESSION['mtrack.auth.register'] = array(
  95. 'mech' => 'MTrackAuth_OpenID',
  96. 'login' => $name,
  97. 'email' => $sreg['email'],
  98. 'name' => $sreg['fullname'],
  99. 'alias' => $id,
  100. 'sreg' => $sreg,
  101. );
  102. header("Location: {$ABSWEB}auth/register.php");
  103. exit;
  104. } else {
  105. $message = 'An error occurred while talking to your OpenID provider';
  106. }
  107. } else if ($pi == 'signout') {
  108. session_destroy();
  109. header('Location: ' . $ABSWEB);
  110. exit;
  111. }
  112. mtrack_head('Authentication Required');
  113. echo "<h1>Please sign in with your <a id='openidlink' href='http://openid.net'><img src='{$ABSWEB}images/logo_openid.png' alt='OpenID' border='0'></a></h1>\n";
  114. echo "<form method='post' action='{$ABSWEB}auth/openid.php'>";
  115. echo "<input type='text' name='openid_identifier' id='openid_identifier'>";
  116. echo " <button type='submit' id='openid-sign-in'>Sign In</button>";
  117. if ($message) {
  118. $message = htmlentities($message, ENT_QUOTES, 'utf-8');
  119. echo <<<HTML
  120. <div class='ui-state-highlight ui-corner-all'>
  121. <span class='ui-icon ui-icon-info'></span>
  122. $message
  123. </div>
  124. HTML;
  125. }
  126. echo "</form>";
  127. mtrack_foot();