Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/VCS/Adapter/Git/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ class GitHub extends Git

public const CONTENTS_FILE = 'file';

/**
* GitHub App JWT expiry in seconds. GitHub allows a maximum of 10 minutes;
* we use 9 minutes to leave a 1-minute safety margin for clock drift.
*
* @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
* "The time must be no more than 10 minutes into the future."
*/
public const GITHUB_APP_JWT_EXPIRY = 60 * 9;

protected string $endpoint = 'https://api.github.com';

protected string $accessToken;
Expand Down Expand Up @@ -60,7 +69,8 @@ public function initializeVariables(string $installationId, string $privateKey,
{
$this->installationId = $installationId;

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

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

$iat = time();
$exp = $iat + 10 * 60;
$exp = $iat + self::GITHUB_APP_JWT_EXPIRY;
$payload = [
'iat' => $iat,
'exp' => $exp,
Expand Down
Loading