1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Oauth
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Application model
29: *
30: * @category Mage
31: * @package Mage_Oauth
32: * @author Magento Core Team <core@magentocommerce.com>
33: * @method Mage_Oauth_Model_Resource_Consumer _getResource()
34: * @method Mage_Oauth_Model_Resource_Consumer getResource()
35: * @method Mage_Oauth_Model_Resource_Consumer_Collection getCollection()
36: * @method Mage_Oauth_Model_Resource_Consumer_Collection getResourceCollection()
37: * @method string getName()
38: * @method Mage_Oauth_Model_Consumer setName() setName(string $name)
39: * @method string getKey()
40: * @method Mage_Oauth_Model_Consumer setKey() setKey(string $key)
41: * @method string getSecret()
42: * @method Mage_Oauth_Model_Consumer setSecret() setSecret(string $secret)
43: * @method string getCallbackUrl()
44: * @method Mage_Oauth_Model_Consumer setCallbackUrl() setCallbackUrl(string $url)
45: * @method string getCreatedAt()
46: * @method Mage_Oauth_Model_Consumer setCreatedAt() setCreatedAt(string $date)
47: * @method string getUpdatedAt()
48: * @method Mage_Oauth_Model_Consumer setUpdatedAt() setUpdatedAt(string $date)
49: * @method string getRejectedCallbackUrl()
50: * @method Mage_Oauth_Model_Consumer setRejectedCallbackUrl() setRejectedCallbackUrl(string $rejectedCallbackUrl)
51: */
52: class Mage_Oauth_Model_Consumer extends Mage_Core_Model_Abstract
53: {
54: /**
55: * Key hash length
56: */
57: const KEY_LENGTH = 32;
58:
59: /**
60: * Secret hash length
61: */
62: const SECRET_LENGTH = 32;
63:
64: /**
65: * Initialize resource model
66: *
67: * @return void
68: */
69: protected function _construct()
70: {
71: $this->_init('oauth/consumer');
72: }
73:
74: /**
75: * BeforeSave actions
76: *
77: * @return Mage_Oauth_Model_Consumer
78: */
79: protected function _beforeSave()
80: {
81: if (!$this->getId()) {
82: $this->setUpdatedAt(time());
83: }
84: $this->validate();
85: parent::_beforeSave();
86: return $this;
87: }
88:
89: /**
90: * Validate data
91: *
92: * @return array|bool
93: * @throw Mage_Core_Exception|Exception Throw exception on fail validation
94: */
95: public function validate()
96: {
97: if ($this->getCallbackUrl() || $this->getRejectedCallbackUrl()) {
98: $this->setCallbackUrl(trim($this->getCallbackUrl()));
99: $this->setRejectedCallbackUrl(trim($this->getRejectedCallbackUrl()));
100:
101: /** @var $validatorUrl Mage_Core_Model_Url_Validator */
102: $validatorUrl = Mage::getSingleton('core/url_validator');
103:
104: if ($this->getCallbackUrl() && !$validatorUrl->isValid($this->getCallbackUrl())) {
105: Mage::throwException(Mage::helper('oauth')->__('Invalid Callback URL'));
106: }
107: if ($this->getRejectedCallbackUrl() && !$validatorUrl->isValid($this->getRejectedCallbackUrl())) {
108: Mage::throwException(Mage::helper('oauth')->__('Invalid Rejected Callback URL'));
109: }
110: }
111:
112: /** @var $validatorLength Mage_Oauth_Model_Consumer_Validator_KeyLength */
113: $validatorLength = Mage::getModel('oauth/consumer_validator_keyLength', array('length' => self::KEY_LENGTH));
114:
115: $validatorLength->setName('Consumer Key');
116: if (!$validatorLength->isValid($this->getKey())) {
117: $messages = $validatorLength->getMessages();
118: Mage::throwException(array_shift($messages));
119: }
120:
121: $validatorLength->setLength(self::SECRET_LENGTH);
122: $validatorLength->setName('Consumer Secret');
123: if (!$validatorLength->isValid($this->getSecret())) {
124: $messages = $validatorLength->getMessages();
125: Mage::throwException(array_shift($messages));
126: }
127: return true;
128: }
129: }
130: