diff --git a/config.inc.php.example b/config.inc.php.example index e49b591..338a205 100644 --- a/config.inc.php.example +++ b/config.inc.php.example @@ -1,11 +1,31 @@ setIssuer('Machines Frogeye'); + +$GIT_APIS = array( + 'github' => array( + 'api' => 'https://api.github.com', + 'token' => 'CHANGEME' + ), + 'gogs' => array( + 'api' => 'https://try.gogs.io/api/v1', + 'token' => 'CHANGEME' + ), + 'gitlab' => array( + 'api' => 'https://gitlab.com/api/v3', + 'token' => 'CHANGEME', + 'authHeader' => 'PRIVATE-TOKEN: ', + ), +); + ?> diff --git a/index.php b/index.php index b472244..aabd3d0 100644 --- a/index.php +++ b/index.php @@ -313,58 +313,62 @@ function argAssert($arg, $data, $args) { // Hooks // -function gogsRequest($route, $meth = 'GET', $data = null) { - global $GOGS_API; - global $GOGS_TOKEN; - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $GOGS_API.'/'.$route); - curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: token '.$GOGS_TOKEN)); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $meth); - if ($data) { - $textdata = ''; - foreach ($data as $key => $value) { - $textdata .= '&'.$key.'='.urlencode($value); + +function updateGitKeys($api, $keys) { + function apiRequest($api, $route, $meth = 'GET', $data = null) { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $api['api'].'/'.$route); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $meth); + curl_setopt($ch, CURLOPT_USERAGENT, 'Machines Frogeye'); + if ($data) { + $dataStr = json_encode($data); + curl_setopt($ch, CURLOPT_POSTFIELDS, $dataStr); + } else { + $dataStr = ''; } - curl_setopt($ch, CURLOPT_POST, count($data)); - curl_setopt($ch, CURLOPT_POSTFIELDS, ltrim($textdata, '&')); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + (isset($api['authHeader']) ? $api['authHeader'] : 'Authorization: token').' '.$api['token'], + 'Content-Type: application/json', + )); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $raw = curl_exec($ch); + curl_close($ch); + return json_decode($raw); } - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - $raw = curl_exec($ch); - curl_close($ch); - return json_decode($raw); -} -function updateGogsKeys($keys) { - global $GOGS_API; - global $GOGS_TOKEN; global $SSH_KEY_REGEX; - if (isset($GOGS_API) && isset($GOGS_TOKEN)) { - $existing = gogsRequest('user/keys'); - $toDelete = []; - foreach ($existing as $ekey) { - $toDelete[$ekey->id] = $ekey->key; - } - - foreach (explode("\n", $keys) as $key) { - $found = false; - foreach ($toDelete as $id => $ekey) { - if ($key == $ekey) { - unset($toDelete[$id]); - $found = true; - break; - } - } - if (!$found) { - gogsRequest('user/keys', 'POST', array( - "title" => ltrim(preg_replace($SSH_KEY_REGEX, '', $key)), - "key" => $key - )); - } + $existing = apiRequest($api, 'user/keys'); + if ($existing === null) { + return 1; + } + $toDelete = []; + foreach ($existing as $ekey) { + $toDelete[$ekey->id] = $ekey->key; + } + + foreach (explode("\n", $keys) as $key) { + if ($key == '') { + continue; } + $found = false; foreach ($toDelete as $id => $ekey) { - gogsRequest('user/keys/'.$id, 'DELETE'); + if (strpos($key, $ekey) !== false) { + unset($toDelete[$id]); + $found = true; + break; + } } + if (!$found) { + apiRequest($api, 'user/keys', 'POST', array( + "title" => ltrim(preg_replace($SSH_KEY_REGEX, '', $key)), + "key" => $key + )); + } + } + + foreach ($toDelete as $id => $ekey) { + apiRequest($api, 'user/keys/'.$id, 'DELETE'); } } @@ -570,8 +574,8 @@ case 'akey': file_put_contents('akey/'.$networkName.'.authorized_keys', getKeys($networkName ? $network : null)); file_put_contents('akey/'.$networkName.'.authorized_keys.sha256', $sign); - if ($networkName == 'gogs') { - updateGogsKeys(getKeys($network)); + if (array_key_exists($networkName, $GIT_APIS)) { + updateGitKeys($GIT_APIS[$networkName], getKeys($network)); } http_response_code(201);