Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. <?php
  2. // $callbackUrl is a path to your file with OAuth authentication example for the Customer user
  3. $callbackUrl = "http://www.seocompanyinpune.co.in/demo/ecom2/oauth_customer.php";
  4. $temporaryCredentialsRequestUrl = "http://www.seocompanyinpune.co.in/demo/ecom2/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
  5. $customerAuthorizationUrl = 'http://www.seocompanyinpune.co.in/demo/ecom2/oauth/authorize/apiLogin';
  6. $accessTokenRequestUrl = 'http://www.seocompanyinpune.co.in/demo/ecom2/oauth/token';
  7. $apiUrl = 'http://www.seocompanyinpune.co.in/demo/ecom2/api/rest';
  8. $consumerKey = 'd19df3aa6784569c19b51d38c8aaf620';
  9. $consumerSecret = '17c26fc92a71004c546b61a9d46d7fe8';
  10. session_start();
  11. $postdata = file_get_contents("php://input");
  12. $request = json_decode($postdata);
  13. $username = $request->email;
  14. $password = $request->pass;
  15.  
  16. if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
  17. $_SESSION['state'] = 0;
  18. }
  19. try {
  20. $authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
  21. $oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
  22. $oauthClient->enableDebug();
  23.  
  24. if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
  25. $requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
  26. $_SESSION['secret'] = $requestToken['oauth_token_secret'];
  27. $_SESSION['state'] = 1;
  28.  
  29. header('Location: ' . $customerAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token'] . '&username=' . $username . '&password=' . $password );
  30. exit;
  31. } else if ($_SESSION['state'] == 1) {
  32.  
  33. $oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
  34. $accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
  35. $_SESSION['state'] = 2;
  36. $_SESSION['token'] = $accessToken['oauth_token'];
  37. $_SESSION['secret'] = $accessToken['oauth_token_secret'];
  38.  
  39. header('Location: ' . $callbackUrl);
  40. exit;
  41. } else {
  42.  
  43. $oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
  44.  
  45. $resourceUrl = "$apiUrl/customers";
  46.  
  47. $oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/json', 'Accept' => 'application/json'));
  48. $res['secret'] = $_SESSION['secret'];
  49. $res['token'] = $_SESSION['token'];
  50. $res['message'] = 'Login successfull';
  51. $res['customer'] = json_decode($oauthClient->getLastResponse());
  52. echo json_encode($res);
  53. include('logout.php');
  54. session_destroy();
  55. }
  56. } catch (OAuthException $e) {
  57. print_r($e->getMessage());
  58. echo "<br/>";
  59. print_r($e->lastResponse);
  60. }
  61.  
  62. public function apiLoginAction($simple = false)
  63. {
  64. /** @var $server Mage_Oauth_Model_Server */
  65. $server = Mage::getModel('oauth/server');
  66. /** @var $session Mage_Customer_Model_Session */
  67. $session = Mage::getSingleton($this->_sessionName);
  68.  
  69. $isException = false;
  70. try {
  71. $server->checkAuthorizeRequest();
  72. } catch (Mage_Core_Exception $e) {
  73. $session->addError($e->getMessage());
  74. } catch (Mage_Oauth_Exception $e) {
  75. $isException = true;
  76. $session->addException($e, $this->__('An error occurred. Your authorization request is invalid.'));
  77. } catch (Exception $e) {
  78. $isException = true;
  79. $session->addException($e, $this->__('An error occurred.'));
  80. }
  81.  
  82. $this->loadLayout();
  83. $layout = $this->getLayout();
  84. $logged = $session->isLoggedIn();
  85.  
  86. $contentBlock = $layout->getBlock('content');
  87. $form_key = "lAv9vvP9dCvSVBYl";
  88. $oauth_token = $this->getRequest()->getQuery('oauth_token');
  89.  
  90. $username = $_GET['username'];
  91. $password = $_GET['password'];
  92.  
  93. unset($_SESSION['username']);
  94. unset($_SESSION['password']);
  95.  
  96. $url = "http://www.seocompanyinpune.co.in/demo/ecom2/customer/account/loginGet?form_key=$form_key&login[username]=$username&login[password]=$password&oauth_token=$oauth_token&send=";
  97. header("Location: $url");
  98. die();
  99.  
  100. /** @var $helper Mage_Core_Helper_Url */
  101. /*$helper = Mage::helper('core/url');
  102. $session->setAfterAuthUrl(Mage::getUrl('customer/account/login', array('_nosid' => true)))
  103. ->setBeforeAuthUrl($helper->getCurrentUrl());
  104.  
  105. $block->setIsSimple($simple)->setToken($this->getRequest()->getQuery('oauth_token'))
  106. ->setHasException($isException);
  107. return $this;*/
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement