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: * oAuth Authentication adapter
29: *
30: * @category Mage
31: * @package Mage_Api2
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Api2_Model_Auth_Adapter_Oauth extends Mage_Api2_Model_Auth_Adapter_Abstract
35: {
36: /**
37: * Process request and figure out an API user type and its identifier
38: *
39: * Returns stdClass object with two properties: type and id
40: *
41: * @param Mage_Api2_Model_Request $request
42: * @return stdClass
43: */
44: public function getUserParams(Mage_Api2_Model_Request $request)
45: {
46: /** @var $oauthServer Mage_Oauth_Model_Server */
47: $oauthServer = Mage::getModel('oauth/server', $request);
48: $userParamsObj = (object) array('type' => null, 'id' => null);
49:
50: try {
51: $token = $oauthServer->checkAccessRequest();
52: $userType = $token->getUserType();
53:
54: if (Mage_Oauth_Model_Token::USER_TYPE_ADMIN == $userType) {
55: $userParamsObj->id = $token->getAdminId();
56: } else {
57: $userParamsObj->id = $token->getCustomerId();
58: }
59: $userParamsObj->type = $userType;
60: } catch (Exception $e) {
61: throw new Mage_Api2_Exception($oauthServer->reportProblem($e), Mage_Api2_Model_Server::HTTP_UNAUTHORIZED);
62: }
63: return $userParamsObj;
64: }
65:
66: /**
67: * Check if request contains authentication info for adapter
68: *
69: * @param Mage_Api2_Model_Request $request
70: * @return boolean
71: */
72: public function isApplicableToRequest(Mage_Api2_Model_Request $request)
73: {
74: $headerValue = $request->getHeader('Authorization');
75:
76: return $headerValue && 'oauth' === strtolower(substr($headerValue, 0, 5));
77: }
78: }
79: