Skip to content

Commit acaa153

Browse files
author
ogorkun
committed
MC-32830: Do not store admin and customer tokens in DB
1 parent 181d3da commit acaa153

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Webapi\Model\Authorization;
8+
9+
use Magento\Authorization\Model\UserContextInterface;
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Integration\Model\Oauth\Token;
12+
use Magento\Integration\Model\Oauth\TokenFactory;
13+
use Magento\Integration\Api\IntegrationServiceInterface;
14+
use Magento\Framework\Webapi\Request;
15+
use Magento\Framework\Stdlib\DateTime\DateTime as Date;
16+
use Magento\Framework\Stdlib\DateTime;
17+
use Magento\Integration\Helper\Oauth\Data as OauthHelper;
18+
19+
/**
20+
* SOAP specific user context based on opaque tokens.
21+
*/
22+
class SoapUserContext implements UserContextInterface
23+
{
24+
/**
25+
* @var Request
26+
*/
27+
protected $request;
28+
29+
/**
30+
* @var Token
31+
*/
32+
protected $tokenFactory;
33+
34+
/**
35+
* @var int
36+
*/
37+
protected $userId;
38+
39+
/**
40+
* @var string
41+
*/
42+
protected $userType;
43+
44+
/**
45+
* @var bool
46+
*/
47+
protected $isRequestProcessed;
48+
49+
/**
50+
* @var IntegrationServiceInterface
51+
*/
52+
protected $integrationService;
53+
54+
/**
55+
* @var DateTime
56+
*/
57+
private $dateTime;
58+
59+
/**
60+
* @var Date
61+
*/
62+
private $date;
63+
64+
/**
65+
* @var OauthHelper
66+
*/
67+
private $oauthHelper;
68+
69+
/**
70+
* Initialize dependencies.
71+
*
72+
* @param Request $request
73+
* @param TokenFactory $tokenFactory
74+
* @param IntegrationServiceInterface $integrationService
75+
* @param DateTime|null $dateTime
76+
* @param Date|null $date
77+
* @param OauthHelper|null $oauthHelper
78+
*/
79+
public function __construct(
80+
Request $request,
81+
TokenFactory $tokenFactory,
82+
IntegrationServiceInterface $integrationService,
83+
DateTime $dateTime = null,
84+
Date $date = null,
85+
OauthHelper $oauthHelper = null
86+
) {
87+
$this->request = $request;
88+
$this->tokenFactory = $tokenFactory;
89+
$this->integrationService = $integrationService;
90+
$this->dateTime = $dateTime ?: ObjectManager::getInstance()->get(
91+
DateTime::class
92+
);
93+
$this->date = $date ?: ObjectManager::getInstance()->get(
94+
Date::class
95+
);
96+
$this->oauthHelper = $oauthHelper ?: ObjectManager::getInstance()->get(
97+
OauthHelper::class
98+
);
99+
}
100+
101+
/**
102+
* @inheritdoc
103+
*/
104+
public function getUserId()
105+
{
106+
$this->processRequest();
107+
return $this->userId;
108+
}
109+
110+
/**
111+
* @inheritdoc
112+
*/
113+
public function getUserType()
114+
{
115+
$this->processRequest();
116+
return $this->userType;
117+
}
118+
119+
/**
120+
* Finds the bearer token and looks up the value.
121+
*
122+
* @return void
123+
*/
124+
protected function processRequest()
125+
{
126+
if ($this->isRequestProcessed) {
127+
return;
128+
}
129+
130+
$authorizationHeaderValue = $this->request->getHeader('Authorization');
131+
if (!$authorizationHeaderValue) {
132+
$this->isRequestProcessed = true;
133+
return;
134+
}
135+
136+
$headerPieces = explode(" ", $authorizationHeaderValue);
137+
if (count($headerPieces) !== 2) {
138+
$this->isRequestProcessed = true;
139+
return;
140+
}
141+
142+
$tokenType = strtolower($headerPieces[0]);
143+
if ($tokenType !== 'bearer') {
144+
$this->isRequestProcessed = true;
145+
return;
146+
}
147+
148+
$bearerToken = $headerPieces[1];
149+
/** @var Token $token */
150+
$token = $this->tokenFactory->create()->load($bearerToken, 'token');
151+
152+
if (!$token->getId() || $token->getRevoked()) {
153+
$this->isRequestProcessed = true;
154+
155+
return;
156+
}
157+
if (((int) $token->getUserType()) === UserContextInterface::USER_TYPE_INTEGRATION) {
158+
$this->userId = $this->integrationService->findByConsumerId($token->getConsumerId())->getId();
159+
$this->userType = UserContextInterface::USER_TYPE_INTEGRATION;
160+
}
161+
162+
$this->isRequestProcessed = true;
163+
}
164+
}

app/code/Magento/Webapi/etc/webapi_soap/di.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
<type name="Magento\Authorization\Model\CompositeUserContext">
1515
<arguments>
1616
<argument name="userContexts" xsi:type="array">
17+
<item name="soapUserContext" xsi:type="array">
18+
<item name="type" xsi:type="object">Magento\Webapi\Model\Authorization\SoapUserContext</item>
19+
<item name="sortOrder" xsi:type="string">9</item>
20+
</item>
1721
<item name="tokenUserContext" xsi:type="array">
1822
<item name="type" xsi:type="object">Magento\Webapi\Model\Authorization\TokenUserContext</item>
1923
<item name="sortOrder" xsi:type="string">10</item>

0 commit comments

Comments
 (0)