static-php-cli/src/SPC/store/CurlHook.php

40 lines
1.1 KiB
PHP
Raw Normal View History

2023-03-15 20:54:33 +08:00
<?php
declare(strict_types=1);
namespace SPC\store;
class CurlHook
{
/**
* 执行 GitHub Token Curl 头添加
*
* @param string $method 修改的 method
* @param string $url 修改的链接
* @param array $headers 修改的 headers
*/
public static function setupGithubToken(string $method, string $url, array &$headers): void
2023-03-15 20:54:33 +08:00
{
$token = getenv('GITHUB_TOKEN');
if (!$token) {
logger()->debug('no github token found, skip');
2023-03-15 20:54:33 +08:00
return;
}
if (getenv('GITHUB_USER')) {
$auth = base64_encode(getenv('GITHUB_USER') . ':' . $token);
2024-10-04 17:19:38 +08:00
$he = "Authorization: Basic {$auth}";
if (!in_array($he, $headers)) {
$headers[] = $he;
}
2023-03-15 20:54:33 +08:00
logger()->info("using basic github token for {$method} {$url}");
} else {
$auth = $token;
2024-10-04 17:19:38 +08:00
$he = "Authorization: Bearer {$auth}";
if (!in_array($he, $headers)) {
$headers[] = $he;
}
2023-03-15 20:54:33 +08:00
logger()->info("using bearer github token for {$method} {$url}");
}
}
}