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_Api2
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: * API Auth Adapter class
29: *
30: * @category Mage
31: * @package Mage_Api2
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Api2_Model_Auth_Adapter
35: {
36: /**
37: * Adapter models
38: *
39: * @var array
40: */
41: protected $_adapters = array();
42:
43: /**
44: * Load adapters configuration and create adapters models
45: *
46: * @return Mage_Api2_Model_Auth_Adapter
47: * @throws Exception
48: */
49: protected function _initAdapters()
50: {
51: /** @var $helper Mage_Api2_Helper_Data */
52: $helper = Mage::helper('api2');
53:
54: foreach ($helper->getAuthAdapters(true) as $adapterKey => $adapterParams) {
55: $adapterModel = Mage::getModel($adapterParams['model']);
56:
57: if (!$adapterModel instanceof Mage_Api2_Model_Auth_Adapter_Abstract) {
58: throw new Exception('Authentication adapter must to extend Mage_Api2_Model_Auth_Adapter_Abstract');
59: }
60: $this->_adapters[$adapterKey] = $adapterModel;
61: }
62: if (!$this->_adapters) {
63: throw new Exception('No active authentication adapters found');
64: }
65: return $this;
66: }
67:
68: /**
69: * Process request and figure out an API user type and its identifier
70: *
71: * Returns stdClass object with two properties: type and id
72: *
73: * @param Mage_Api2_Model_Request $request
74: * @return stdClass
75: */
76: public function getUserParams(Mage_Api2_Model_Request $request)
77: {
78: $this->_initAdapters();
79:
80: foreach ($this->_adapters as $adapterModel) {
81: /** @var $adapterModel Mage_Api2_Model_Auth_Adapter_Abstract */
82: if ($adapterModel->isApplicableToRequest($request)) {
83: $userParams = $adapterModel->getUserParams($request);
84:
85: if (null !== $userParams->type) {
86: return $userParams;
87: }
88: throw new Mage_Api2_Exception('Can not determine user type', Mage_Api2_Model_Server::HTTP_UNAUTHORIZED);
89: }
90: }
91: return (object) array('type' => Mage_Api2_Model_Auth::DEFAULT_USER_TYPE, 'id' => null);
92: }
93: }
94: