1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Oauth_Helper_Data extends Mage_Core_Helper_Abstract
35: {
36: 37: 38:
39: const ENDPOINT_AUTHORIZE_CUSTOMER = 'oauth/authorize';
40: const ENDPOINT_AUTHORIZE_ADMIN = 'adminhtml/oauth_authorize';
41: const ENDPOINT_AUTHORIZE_CUSTOMER_SIMPLE = 'oauth/authorize/simple';
42: const ENDPOINT_AUTHORIZE_ADMIN_SIMPLE = 'adminhtml/oauth_authorize/simple';
43: const ENDPOINT_INITIATE = 'oauth/initiate';
44: const ENDPOINT_TOKEN = 'oauth/token';
45:
46:
47: 48: 49:
50: const XML_PATH_CLEANUP_PROBABILITY = 'oauth/cleanup/cleanup_probability';
51: const XML_PATH_CLEANUP_EXPIRATION_PERIOD = 'oauth/cleanup/expiration_period';
52:
53:
54:
55: const XML_PATH_EMAIL_TEMPLATE = 'oauth/email/template';
56: const XML_PATH_EMAIL_IDENTITY = 'oauth/email/identity';
57:
58:
59: 60: 61:
62: const CLEANUP_EXPIRATION_PERIOD_DEFAULT = 120;
63:
64: 65: 66:
67: const QUERY_PARAM_REJECTED = 'rejected';
68:
69: 70: 71: 72: 73:
74: protected $_endpoints = array(
75: self::ENDPOINT_AUTHORIZE_CUSTOMER,
76: self::ENDPOINT_AUTHORIZE_ADMIN,
77: self::ENDPOINT_AUTHORIZE_CUSTOMER_SIMPLE,
78: self::ENDPOINT_AUTHORIZE_ADMIN_SIMPLE,
79: self::ENDPOINT_INITIATE,
80: self::ENDPOINT_TOKEN
81: );
82:
83: 84: 85: 86: 87: 88:
89: protected function _generateRandomString($length)
90: {
91:
92:
93: $helper = Mage::helper('core');
94:
95: return $helper->getRandomString(
96: $length, Mage_Core_Helper_Data::CHARS_DIGITS . Mage_Core_Helper_Data::CHARS_LOWERS
97: );
98: }
99:
100: 101: 102: 103: 104:
105: public function generateToken()
106: {
107: return $this->_generateRandomString(Mage_Oauth_Model_Token::LENGTH_TOKEN);
108: }
109:
110: 111: 112: 113: 114:
115: public function generateTokenSecret()
116: {
117: return $this->_generateRandomString(Mage_Oauth_Model_Token::LENGTH_SECRET);
118: }
119:
120: 121: 122: 123: 124:
125: public function generateVerifier()
126: {
127: return $this->_generateRandomString(Mage_Oauth_Model_Token::LENGTH_VERIFIER);
128: }
129:
130: 131: 132: 133: 134:
135: public function generateConsumerKey()
136: {
137: return $this->_generateRandomString(Mage_Oauth_Model_Consumer::KEY_LENGTH);
138: }
139:
140: 141: 142: 143: 144:
145: public function generateConsumerSecret()
146: {
147: return $this->_generateRandomString(Mage_Oauth_Model_Consumer::SECRET_LENGTH);
148: }
149:
150: 151: 152: 153: 154: 155: 156:
157: public function getFullCallbackUrl(Mage_Oauth_Model_Token $token, $rejected = false)
158: {
159: $callbackUrl = $token->getCallbackUrl();
160:
161: if (Mage_Oauth_Model_Server::CALLBACK_ESTABLISHED == $callbackUrl) {
162: return false;
163: }
164: if ($rejected) {
165:
166: $consumer = Mage::getModel('oauth/consumer')->load($token->getConsumerId());
167:
168: if ($consumer->getId() && $consumer->getRejectedCallbackUrl()) {
169: $callbackUrl = $consumer->getRejectedCallbackUrl();
170: }
171: } elseif (!$token->getAuthorized()) {
172: Mage::throwException('Token is not authorized');
173: }
174: $callbackUrl .= (false === strpos($callbackUrl, '?') ? '?' : '&');
175: $callbackUrl .= 'oauth_token=' . $token->getToken() . '&';
176: $callbackUrl .= $rejected ? self::QUERY_PARAM_REJECTED . '=1' : 'oauth_verifier=' . $token->getVerifier();
177:
178: return $callbackUrl;
179: }
180:
181: 182: 183: 184: 185: 186: 187:
188: public function getProtocolEndpointUrl($type)
189: {
190: if (!in_array($type, $this->_endpoints)) {
191: throw new Exception('Invalid endpoint type passed.');
192: }
193: return rtrim(Mage::getUrl($type), '/');
194: }
195:
196: 197: 198: 199: 200:
201: public function isCleanupProbability()
202: {
203:
204: $configValue = (int) Mage::getStoreConfig(self::XML_PATH_CLEANUP_PROBABILITY);
205: return $configValue > 0 ? 1 == mt_rand(1, $configValue) : false;
206: }
207:
208: 209: 210: 211: 212:
213: public function getCleanupExpirationPeriod()
214: {
215: $minutes = (int) Mage::getStoreConfig(self::XML_PATH_CLEANUP_EXPIRATION_PERIOD);
216: return $minutes > 0 ? $minutes : self::CLEANUP_EXPIRATION_PERIOD_DEFAULT;
217: }
218:
219: 220: 221: 222: 223: 224: 225: 226:
227: public function sendNotificationOnTokenStatusChange($userEmail, $userName, $applicationName, $status)
228: {
229:
230: $mailTemplate = Mage::getModel('core/email_template');
231:
232: $mailTemplate->sendTransactional(
233: Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
234: Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY),
235: $userEmail,
236: $userName,
237: array(
238: 'name' => $userName,
239: 'email' => $userEmail,
240: 'applicationName' => $applicationName,
241: 'status' => $status,
242:
243: )
244: );
245: }
246:
247: 248: 249: 250: 251:
252: protected function _getIsSimple()
253: {
254: $simple = false;
255: if (stristr($this->_getRequest()->getActionName(), 'simple')
256: || !is_null($this->_getRequest()->getParam('simple', null))
257: ) {
258: $simple = true;
259: }
260:
261: return $simple;
262: }
263:
264: 265: 266: 267: 268: 269:
270: public function getAuthorizeUrl($userType)
271: {
272: $simple = $this->_getIsSimple();
273:
274: if (Mage_Oauth_Model_Token::USER_TYPE_CUSTOMER == $userType) {
275: if ($simple) {
276: $route = self::ENDPOINT_AUTHORIZE_CUSTOMER_SIMPLE;
277: } else {
278: $route = self::ENDPOINT_AUTHORIZE_CUSTOMER;
279: }
280: } elseif (Mage_Oauth_Model_Token::USER_TYPE_ADMIN == $userType) {
281: if ($simple) {
282: $route = self::ENDPOINT_AUTHORIZE_ADMIN_SIMPLE;
283: } else {
284: $route = self::ENDPOINT_AUTHORIZE_ADMIN;
285: }
286: } else {
287: throw new Exception('Invalid user type.');
288: }
289:
290: return $this->_getUrl($route, array('_query' => array('oauth_token' => $this->getOauthToken())));
291: }
292:
293: 294: 295: 296: 297:
298: public function getOauthToken()
299: {
300: return $this->_getRequest()->getParam('oauth_token', null);
301: }
302: }
303: