PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/application/modules/twitter/controllers/callback.php

http://github.com/AntonShevchuk/Bluz
PHP | 140 lines | 78 code | 24 blank | 38 comment | 7 complexity | 8fc5cc0a22f4f48cea2a3ac294a62954 MD5 | raw file
  1. <?php
  2. /**
  3. * Twitter Auth controller
  4. *
  5. * @author Anton Shevchuk
  6. * @created 23.10.12 18:10
  7. */
  8. namespace Application;
  9. use Bluz;
  10. use Application\Users;
  11. return
  12. /**
  13. * @return \closure
  14. */
  15. function() {
  16. /**
  17. * @var Bluz\Application $this
  18. */
  19. // process "denied" response
  20. if ($this->getRequest()->getParam('denied')) {
  21. $this->redirectTo('index', 'index');
  22. }
  23. $options = $this->getConfigData('auth', 'twitter');
  24. // random string
  25. $oauth_nonce = md5(uniqid(rand(), true));
  26. // timestamp
  27. $oauth_timestamp = time(); // 1310727371
  28. $oauth_token = $this->getRequest()->getParam('oauth_token');
  29. $oauth_verifier = $this->getRequest()->getParam('oauth_verifier');
  30. $oauthTokenSecret = $this->getSession()->oauthTokenSecret;
  31. // build base text
  32. $oauth_base_text = "GET&"
  33. . urlencode('https://api.twitter.com/oauth/access_token')."&"
  34. . urlencode("oauth_consumer_key=".$options['consumerKey']."&")
  35. . urlencode("oauth_nonce=".$oauth_nonce."&")
  36. . urlencode("oauth_signature_method=HMAC-SHA1&")
  37. . urlencode("oauth_token=".$oauth_token."&")
  38. . urlencode("oauth_timestamp=".$oauth_timestamp."&")
  39. . urlencode("oauth_verifier=".$oauth_verifier."&")
  40. . urlencode("oauth_version=1.0");
  41. // create key (Consumer secret + '&' + oauth_token_secret)
  42. $key = $options['consumerSecret']."&".$oauthTokenSecret;
  43. // build auth_signature
  44. $signature = base64_encode(hash_hmac("sha1", $oauth_base_text, $key, true));
  45. // build URL
  46. $url = 'https://api.twitter.com/oauth/access_token'
  47. . '?oauth_nonce='.$oauth_nonce
  48. . '&oauth_signature_method=HMAC-SHA1'
  49. . '&oauth_timestamp='.$oauth_timestamp
  50. . '&oauth_consumer_key='.$options['consumerKey']
  51. . '&oauth_token='.urlencode($oauth_token)
  52. . '&oauth_verifier='.urlencode($oauth_verifier)
  53. . '&oauth_signature='.urlencode($signature)
  54. . '&oauth_version=1.0';
  55. // send request
  56. if (!$response = @file_get_contents($url)) {
  57. throw new Exception("Invalid Twitter token", 401);
  58. }
  59. // parse result
  60. parse_str($response, $result);
  61. /*
  62. array (size=4)
  63. 'oauth_token' => string '****' (length=49)
  64. 'oauth_token_secret' => string '****' (length=42)
  65. 'user_id' => string '********' (length=8)
  66. 'screen_name' => string '*****' (length=13)
  67. */
  68. $authTable = Auth\Table::getInstance();
  69. // try to load previous information
  70. /* @var /Application/Auth/Row $row */
  71. $row = $authTable->getAuthRow(Auth\Row::PROVIDER_TWITTER, $result['user_id']);
  72. if ($row) {
  73. if ($row->status != Users\Row::STATUS_ACTIVE) {
  74. $this->getMessages()->addError('User is not active');
  75. $this->redirectTo('index', 'index');
  76. }
  77. // update tokens
  78. $row->token = $result['oauth_token'];
  79. $row->tokenSecret = $result['oauth_token_secret'];
  80. $row->tokenType = Auth\Row::TYPE_ACCESS;
  81. $row->save();
  82. // try to sign in
  83. $usersTable = Users\Table::getInstance();
  84. $user = $usersTable -> findRow($row->userId);
  85. // sign in
  86. $user->login();
  87. } else {
  88. // if user already signed - link new auth provider to account
  89. // another - create new user
  90. if (!$user = $this->getAuth()->getIdentity()) {
  91. // create new user
  92. $user = new Users\Row();
  93. $user->login = $result['screen_name'];
  94. $user->status = Users\Row::STATUS_ACTIVE;
  95. $user->save();
  96. // set default role
  97. $user2role = new UsersToRoles\Row();
  98. $user2role -> userId = $user->id;
  99. $user2role -> roleId = 2;
  100. $user2role -> save();
  101. // sign in
  102. $user->login();
  103. }
  104. $row = new Auth\Row();
  105. $row->userId = $user->id;
  106. $row->provider = 'twitter';
  107. $row->foreignKey = $result['user_id'];
  108. $row->token = $result['oauth_token'];
  109. $row->tokenSecret = $result['oauth_token_secret'];
  110. $row->tokenType = Auth\Row::TYPE_ACCESS;
  111. $row->save();
  112. }
  113. $this->getMessages()->addNotice('You are signed');
  114. $this->redirectTo('index', 'index');
  115. return false;
  116. };