|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Validate Docker Hub credentials against the API path that |
| 5 | + * peter-evans/dockerhub-description uses (PATCH /v2/repositories/<repo>/). |
| 6 | + * |
| 7 | + * Distinguishes "bad credential" from "credential lacks scope on this repo" — |
| 8 | + * a token can pass docker login (registry auth) and still 403 on the API |
| 9 | + * path, which is exactly the failure mode openemr/openemr-devops#714 had to |
| 10 | + * recover from. |
| 11 | + * |
| 12 | + * @package openemr-devops |
| 13 | + * @link https://www.open-emr.org |
| 14 | + * @author Michael A. Smith <michael@opencoreemr.com> |
| 15 | + * @copyright Copyright (c) 2026 OpenCoreEMR Inc. |
| 16 | + * @license https://github.com/openemr/openemr-devops/blob/master/LICENSE GNU General Public License 3 |
| 17 | + */ |
| 18 | + |
| 19 | +declare(strict_types=1); |
| 20 | + |
| 21 | +namespace OpenEMR\Release; |
| 22 | + |
| 23 | +final readonly class DockerHubCredentialChecker |
| 24 | +{ |
| 25 | + private const DEFAULT_API_BASE = 'https://hub.docker.com/v2'; |
| 26 | + |
| 27 | + public function __construct( |
| 28 | + private string $apiBase = self::DEFAULT_API_BASE, |
| 29 | + ) { |
| 30 | + } |
| 31 | + |
| 32 | + public function check(string $username, string $token, string $repository): DockerHubCredentialCheckResult |
| 33 | + { |
| 34 | + $jwt = $this->mintJwt($username, $token); |
| 35 | + $repoStatus = $jwt !== null |
| 36 | + ? $this->probeRepository($jwt, $repository) |
| 37 | + : null; |
| 38 | + return DockerHubCredentialCheckResult::interpret($repository, $jwt, $repoStatus); |
| 39 | + } |
| 40 | + |
| 41 | + private function mintJwt(string $username, string $token): ?string |
| 42 | + { |
| 43 | + $body = json_encode(['username' => $username, 'password' => $token], JSON_THROW_ON_ERROR); |
| 44 | + [$status, $responseBody] = $this->httpRequest('POST', $this->apiBase . '/users/login/', [ |
| 45 | + 'Content-Type: application/json', |
| 46 | + ], $body); |
| 47 | + |
| 48 | + if ($status !== 200) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + $decoded = json_decode($responseBody, true, flags: JSON_THROW_ON_ERROR); |
| 52 | + if (!is_array($decoded) || !isset($decoded['token']) || !is_string($decoded['token'])) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + return $decoded['token']; |
| 56 | + } |
| 57 | + |
| 58 | + private function probeRepository(string $jwt, string $repository): int |
| 59 | + { |
| 60 | + [$status] = $this->httpRequest('GET', $this->apiBase . '/repositories/' . $repository . '/', [ |
| 61 | + 'Authorization: JWT ' . $jwt, |
| 62 | + ]); |
| 63 | + return $status; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @param non-empty-string $method |
| 68 | + * @param list<string> $headers |
| 69 | + * @return array{int, string} |
| 70 | + */ |
| 71 | + private function httpRequest(string $method, string $url, array $headers, ?string $body = null): array |
| 72 | + { |
| 73 | + $ch = curl_init($url); |
| 74 | + if ($ch === false) { |
| 75 | + throw new \RuntimeException('curl_init failed'); |
| 76 | + } |
| 77 | + curl_setopt_array($ch, [ |
| 78 | + CURLOPT_CUSTOMREQUEST => $method, |
| 79 | + CURLOPT_RETURNTRANSFER => true, |
| 80 | + CURLOPT_HTTPHEADER => $headers, |
| 81 | + CURLOPT_TIMEOUT => 10, |
| 82 | + ]); |
| 83 | + if ($body !== null) { |
| 84 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
| 85 | + } |
| 86 | + $response = curl_exec($ch); |
| 87 | + if ($response === false) { |
| 88 | + $error = curl_error($ch); |
| 89 | + throw new \RuntimeException("curl error for {$method} {$url}: {$error}"); |
| 90 | + } |
| 91 | + /** @var int $status */ |
| 92 | + $status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); |
| 93 | + return [$status, is_string($response) ? $response : '']; |
| 94 | + } |
| 95 | +} |
0 commit comments