Skip to content

Commit 4f76bda

Browse files
authored
Merge pull request #30 from magento-commerce/imported-magento-magento2-functional-testing-framework-817
[Imported] Add support for admin WebAPI token refresh
2 parents 59d131f + f11ef1d commit 4f76bda

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

docs/configuration.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,16 @@ Example:
408408
REMOTE_STORAGE_AWSS3_PREFIX=local
409409
```
410410

411+
### MAGENTO_ADMIN_WEBAPI_TOKEN_LIFETIME
412+
413+
The lifetime (in seconds) of Magento Admin WebAPI token; if token is older than this value a refresh attempt will be made just before the next WebAPI call.
414+
415+
Example:
416+
417+
```conf
418+
MAGENTO_ADMIN_WEBAPI_TOKEN_LIFETIME=10800
419+
```
420+
411421
<!-- Link definitions -->
412422

413423
[`MAGENTO_CLI_COMMAND_PATH`]: #magento_cli_command_path

etc/config/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,8 @@ BROWSER_LOG_BLOCKLIST=other
7272

7373
#*** Elastic Search version used for test ***#
7474
ELASTICSEARCH_VERSION=7
75+
76+
#*** Lifetime (in seconds) of Magento Admin WebAPI Token; if token is older than this value a refresh attempt will be made just before the next WebAPI call ***#
77+
#MAGENTO_ADMIN_WEBAPI_TOKEN_LIFETIME=10800
78+
7579
#*** End of .env ***#

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

Lines changed: 28 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
@@ -62,7 +69,7 @@ public static function getAdminToken($username = null, $password = null)
6269
throw new FastFailException($message, $context);
6370
}
6471

65-
if (isset(self::$adminAuthTokens[$login])) {
72+
if (self::hasExistingToken($login)) {
6673
return self::$adminAuthTokens[$login];
6774
}
6875

@@ -97,6 +104,7 @@ public static function getAdminToken($username = null, $password = null)
97104
$token = json_decode($response);
98105
if ($token !== null) {
99106
self::$adminAuthTokens[$login] = $token;
107+
self::$adminAuthTokenTimestamps[$login] = time();
100108
return $token;
101109
}
102110
$errMessage = "Invalid response: {$response}";
@@ -117,4 +125,23 @@ public static function getAdminToken($username = null, $password = null)
117125
$context = ['url' => $authUrl];
118126
throw new FastFailException($message, $context);
119127
}
128+
129+
/**
130+
* Is there an existing WebAPI admin token for this login?
131+
*
132+
* @param string $login
133+
* @return boolean
134+
*/
135+
private static function hasExistingToken(string $login)
136+
{
137+
if (!isset(self::$adminAuthTokens[$login])) {
138+
return false;
139+
}
140+
141+
$tokenLifetime = getenv('MAGENTO_ADMIN_WEBAPI_TOKEN_LIFETIME');
142+
143+
$isTokenExpired = $tokenLifetime && time() - self::$adminAuthTokenTimestamps[$login] > $tokenLifetime;
144+
145+
return !$isTokenExpired;
146+
}
120147
}

0 commit comments

Comments
 (0)