Skip to content

Commit 7836c00

Browse files
committed
Add support for admin WebAPI token refresh
1 parent b2a23dc commit 7836c00

File tree

1 file changed

+14
-1
lines changed
  • src/Magento/FunctionalTestingFramework/DataTransport/Auth

1 file changed

+14
-1
lines changed

src/Magento/FunctionalTestingFramework/DataTransport/Auth/WebApiAuth.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ class WebApiAuth
3636
*/
3737
private static $adminAuthTokens = [];
3838

39+
/**
40+
* Timestamps of when admin user tokens were created. They need to be refreshed every ~4 hours
41+
*
42+
* @var int[]
43+
*/
44+
private static $adminAuthTokenTimestamps = [];
45+
3946
/**
4047
* Return the API token for an admin user
4148
* Use MAGENTO_ADMIN_USERNAME and MAGENTO_ADMIN_PASSWORD when $username and/or $password is/are omitted
@@ -63,7 +70,12 @@ public static function getAdminToken($username = null, $password = null)
6370
}
6471

6572
if (isset(self::$adminAuthTokens[$login])) {
66-
return self::$adminAuthTokens[$login];
73+
$threeHours = 60 * 60 * 3;
74+
$isTokenAboutToExpire = time() - self::$adminAuthTokenTimestamps[$login] > $threeHours;
75+
76+
if (!$isTokenAboutToExpire) {
77+
return self::$adminAuthTokens[$login];
78+
}
6779
}
6880

6981
try {
@@ -97,6 +109,7 @@ public static function getAdminToken($username = null, $password = null)
97109
$token = json_decode($response);
98110
if ($token !== null) {
99111
self::$adminAuthTokens[$login] = $token;
112+
self::$adminAuthTokenTimestamps[$login] = time();
100113
return $token;
101114
}
102115
$errMessage = "Invalid response: {$response}";

0 commit comments

Comments
 (0)