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,52 @@ 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
*
72
+ * TokenUserContext constructor.
53
73
* @param Request $request
54
74
* @param TokenFactory $tokenFactory
55
75
* @param IntegrationServiceInterface $integrationService
76
+ * @param DateTime|null $dateTime
77
+ * @param Date|null $date
78
+ * @param OauthHelper|null $oauthHelper
56
79
*/
57
80
public function __construct (
58
81
Request $ request ,
59
82
TokenFactory $ tokenFactory ,
60
- IntegrationServiceInterface $ integrationService
83
+ IntegrationServiceInterface $ integrationService ,
84
+ DateTime $ dateTime = null ,
85
+ Date $ date = null ,
86
+ OauthHelper $ oauthHelper = null
61
87
) {
62
88
$ this ->request = $ request ;
63
89
$ this ->tokenFactory = $ tokenFactory ;
64
90
$ this ->integrationService = $ integrationService ;
91
+ $ this ->dateTime = $ dateTime ?: ObjectManager::getInstance ()->get (
92
+ DateTime::class
93
+ );
94
+ $ this ->date = $ date ?: ObjectManager::getInstance ()->get (
95
+ Date::class
96
+ );
97
+ $ this ->oauthHelper = $ oauthHelper ?: ObjectManager::getInstance ()->get (
98
+ OauthHelper::class
99
+ );
65
100
}
66
101
67
102
/**
@@ -82,6 +117,28 @@ public function getUserType()
82
117
return $ this ->userType ;
83
118
}
84
119
120
+ /**
121
+ * Check if token is expired.
122
+ *
123
+ * @param Token $token
124
+ * @return bool
125
+ */
126
+ private function isTokenExpired (Token $ token ): bool
127
+ {
128
+ if ($ token ->getUserType () == UserContextInterface::USER_TYPE_ADMIN ) {
129
+ $ tokenTtl = $ this ->oauthHelper ->getAdminTokenLifetime ();
130
+ } elseif ($ token ->getUserType () == UserContextInterface::USER_TYPE_CUSTOMER ) {
131
+ $ tokenTtl = $ this ->oauthHelper ->getCustomerTokenLifetime ();
132
+ } else {
133
+ // other user-type tokens are considered always valid
134
+ return false ;
135
+ }
136
+ if ($ this ->dateTime ->strToTime ($ token ->getCreatedAt ()) < ($ this ->date ->gmtTimestamp () - $ tokenTtl * 3600 )) {
137
+ return true ;
138
+ }
139
+ return false ;
140
+ }
141
+
85
142
/**
86
143
* Finds the bearer token and looks up the value.
87
144
*
@@ -114,7 +171,7 @@ 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 ;
119
176
return ;
120
177
}
0 commit comments