Skip to content

Commit ed055d8

Browse files
authored
Merge branch 'utopia-php:main' into feat/gitlab-adapter
2 parents f120a94 + 44a84ab commit ed055d8

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/VCS/Adapter/Git/GitHub.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ class GitHub extends Git
2121

2222
public const CONTENTS_FILE = 'file';
2323

24+
/**
25+
* GitHub App JWT expiry in seconds. GitHub allows a maximum of 10 minutes;
26+
* we use 9 minutes to leave a 1-minute safety margin for clock drift.
27+
*
28+
* @see https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-json-web-token-jwt-for-a-github-app
29+
* "The time must be no more than 10 minutes into the future."
30+
*/
31+
public const GITHUB_APP_JWT_EXPIRY = 60 * 9;
32+
2433
protected string $endpoint = 'https://api.github.com';
2534

2635
protected string $accessToken;
@@ -60,7 +69,8 @@ public function initializeVariables(string $installationId, string $privateKey,
6069
{
6170
$this->installationId = $installationId;
6271

63-
$response = $this->cache->load($installationId, 60 * 9); // 10 minutes, but 1 minute earlier to be safe
72+
// Cache for 1 minute less than the JWT expiry so we refresh before the token actually expires.
73+
$response = $this->cache->load($installationId, self::GITHUB_APP_JWT_EXPIRY - 60);
6474
if ($response == false) {
6575
$this->generateAccessToken($privateKey, $appId);
6676

@@ -600,7 +610,7 @@ protected function generateAccessToken(string $privateKey, ?string $appId): void
600610
$appIdentifier = $appId;
601611

602612
$iat = time();
603-
$exp = $iat + 10 * 60;
613+
$exp = $iat + self::GITHUB_APP_JWT_EXPIRY;
604614
$payload = [
605615
'iat' => $iat,
606616
'exp' => $exp,

0 commit comments

Comments
 (0)