fresh/src/Credentials/BasicCredentialsLoader.php

31 lines
735 B
PHP

<?php
namespace NoccyLabs\FreshDocker\Credentials;
class BasicCredentialsLoader implements CredentialsLoaderInterface
{
private array $auth = [];
public function __construct()
{
$this->load();
}
private function load()
{
$config = getenv("HOME")."/.docker/config.json";
if (!file_exists($config)) return;
$conf = json_decode(file_get_contents($config));
$this->auth = isset($conf->auths) ? (array)$conf->auths : [];
}
public function getCredentials(string $repo): ?array
{
if (!array_key_exists($repo, $this->auth))
return null;
$auth = $this->auth[$repo]->auth;
return explode(":", base64_decode($auth), 2);
}
}