7
7
namespace Magento \Webapi \Model \Authorization ;
8
8
9
9
use Magento \Authorization \Model \UserContextInterface ;
10
+ use Magento \Framework \App \ObjectManager ;
10
11
use Magento \Integration \Model \Oauth \Token ;
11
12
use Magento \Integration \Model \Oauth \TokenFactory ;
12
13
use Magento \Integration \Api \IntegrationServiceInterface ;
13
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 ;
14
18
15
19
/**
16
20
* A user context determined by tokens in a HTTP request Authorization header.
@@ -47,21 +51,51 @@ class TokenUserContext implements UserContextInterface
47
51
*/
48
52
protected $ integrationService ;
49
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
+
50
69
/**
51
70
* Initialize dependencies.
52
71
*
53
72
* @param Request $request
54
73
* @param TokenFactory $tokenFactory
55
74
* @param IntegrationServiceInterface $integrationService
75
+ * @param DateTime|null $dateTime
76
+ * @param Date|null $date
77
+ * @param OauthHelper|null $oauthHelper
56
78
*/
57
79
public function __construct (
58
80
Request $ request ,
59
81
TokenFactory $ tokenFactory ,
60
- IntegrationServiceInterface $ integrationService
82
+ IntegrationServiceInterface $ integrationService ,
83
+ DateTime $ dateTime = null ,
84
+ Date $ date = null ,
85
+ OauthHelper $ oauthHelper = null
61
86
) {
62
87
$ this ->request = $ request ;
63
88
$ this ->tokenFactory = $ tokenFactory ;
64
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
+ );
65
99
}
66
100
67
101
/**
@@ -82,6 +116,29 @@ public function getUserType()
82
116
return $ this ->userType ;
83
117
}
84
118
119
+ /**
120
+ * Check if token is expired.
121
+ *
122
+ * @param Token $token
123
+ * @return bool
124
+ */
125
+ private function isTokenExpired (Token $ token ): bool
126
+ {
127
+ if ($ token ->getUserType () == UserContextInterface::USER_TYPE_ADMIN ) {
128
+ $ tokenTtl = $ this ->oauthHelper ->getAdminTokenLifetime ();
129
+ } elseif ($ token ->getUserType () == UserContextInterface::USER_TYPE_CUSTOMER ) {
130
+ $ tokenTtl = $ this ->oauthHelper ->getCustomerTokenLifetime ();
131
+ } else {
132
+ // other user-type tokens are considered always valid
133
+ return false ;
134
+ }
135
+ if ($ this ->dateTime ->strToTime ($ token ->getCreatedAt ()) < ($ this ->date ->gmtTimestamp () - $ tokenTtl * 3600 )) {
136
+ return true ;
137
+ }
138
+
139
+ return false ;
140
+ }
141
+
85
142
/**
86
143
* Finds the bearer token and looks up the value.
87
144
*
@@ -114,8 +171,9 @@ protected function processRequest()
114
171
$ bearerToken = $ headerPieces [1 ];
115
172
$ token = $ this ->tokenFactory ->create ()->loadByToken ($ bearerToken );
116
173
117
- if (!$ token ->getId () || $ token ->getRevoked ()) {
174
+ if (!$ token ->getId () || $ token ->getRevoked () || $ this -> isTokenExpired ( $ token ) ) {
118
175
$ this ->isRequestProcessed = true ;
176
+
119
177
return ;
120
178
}
121
179
0 commit comments