PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/oauth_callback.php

https://github.com/whale2/users
PHP | 127 lines | 106 code | 18 blank | 3 comment | 14 complexity | 2826186278cb2581882cdb920aa341cb MD5 | raw file
  1. <?php
  2. require_once(dirname(__FILE__).'/config.php');
  3. require_once(dirname(__FILE__).'/User.php');
  4. $current_user = User::get();
  5. $oauth_user_id = null;
  6. try
  7. {
  8. if (!array_key_exists('module', $_GET)) {
  9. throw new Exception('module not specified');
  10. }
  11. if (!array_key_exists('oauth_token', $_GET) || !array_key_exists('oauth_verifier', $_GET)) {
  12. throw new Exception('oauth_token & oauth_varifier required');
  13. }
  14. $module = AuthenticationModule::get($_GET['module']);
  15. $storage = new MrClay_CookieStorage(array(
  16. 'secret' => UserConfig::$SESSION_SECRET,
  17. 'mode' => MrClay_CookieStorage::MODE_ENCRYPT,
  18. 'path' => UserConfig::$SITEROOTURL,
  19. 'httponly' => true
  20. ));
  21. $oauth_user_id = $storage->fetch(UserConfig::$oauth_user_id_key);
  22. $storage->delete(UserConfig::$oauth_user_id_key);
  23. if (is_null($oauth_user_id)) {
  24. throw new Exception("can't determine OAuth User ID");
  25. }
  26. try
  27. {
  28. $module->getAccessToken($oauth_user_id);
  29. }
  30. catch (OAuthException2 $e)
  31. {
  32. throw new Exception('problem getting access token: '.$e->getMessage());
  33. }
  34. try
  35. {
  36. $identity = $module->getIdentity($oauth_user_id);
  37. }
  38. catch (OAuthException2 $e)
  39. {
  40. throw new Exception('problem getting user identity: '.$e->getMessage());
  41. }
  42. if (is_null($identity)) {
  43. throw new Exception('no identity returned');
  44. }
  45. #error_log(
  46. # '$identity = '.var_export($identity, true).
  47. # '$oauth_user_id = '.$oauth_user_id
  48. #);
  49. $user = $module->getUserByOAuthIdentity($identity, $oauth_user_id);
  50. if (is_null($current_user)) {
  51. // if user is not logged in yet, it means we're logging them in
  52. if (is_null($user)) {
  53. // This user doesn't exist yet, registering them
  54. $new_user = User::createNewWithoutCredentials(
  55. $identity['name'],
  56. array_key_exists('email', $identity) ? $identity['email'] : null
  57. );
  58. $module->addUserOAuthIdentity($new_user, $identity, $oauth_user_id);
  59. $new_user->setRegistrationModule($module);
  60. $new_user->setSession(true);
  61. $module->recordRegistrationActivity($new_user);
  62. } else {
  63. $user->setSession(true);
  64. $module->recordLoginActivity($user);
  65. }
  66. } else {
  67. // otherwise, we're adding their credential to an existing user
  68. if (!is_null($user)) {
  69. throw new Exception('another user is already connected with this account');
  70. }
  71. $module->addUserOAuthIdentity($current_user, $identity, $oauth_user_id);
  72. $module->recordAddActivity($current_user);
  73. }
  74. } catch (Exception $e) {
  75. error_log($e->getMessage());
  76. // we should delete temporary OAuth User ID
  77. if (!is_null($oauth_user_id)) {
  78. $module->deleteOAuthUser($oauth_user_id);
  79. }
  80. if (is_null($current_user)) {
  81. header('Location: '.UserConfig::$USERSROOTURL.'/login.php?'.
  82. (array_key_exists('module', $_GET) ? 'module='.$_GET['module'].'&' : '').
  83. 'error=failed');
  84. } else {
  85. header('Location: '.UserConfig::$USERSROOTURL.'/edit.php?'.
  86. (array_key_exists('module', $_GET) ? 'module='.$_GET['module'].'&' : '').
  87. 'error=failed');
  88. }
  89. exit;
  90. }
  91. $return = User::getReturn();
  92. User::clearReturn();
  93. if (is_null($return) && !is_null($current_user)) {
  94. $return = UserConfig::$USERSROOTURL.'/edit.php';
  95. }
  96. if (!is_null($return))
  97. {
  98. header('Location: '.$return);
  99. }
  100. else
  101. {
  102. header('Location: '.UserConfig::$DEFAULTLOGINRETURN);
  103. }