More git services

This commit is contained in:
Geoffrey Frogeye 2016-12-10 23:18:18 +01:00
parent b7b2ab744a
commit 96fd5c6a7b
2 changed files with 71 additions and 47 deletions

View file

@ -1,11 +1,31 @@
<?php
use OTPHP\TOTP;
$DOMAIN = 'machines.frogeye.fr';
$TOTP = new TOTP(
'newcommer@machines.frogeye.fr', // The label (string)
'EUPJMTU6M7XPHG5P', // The secret encoded in base 32 (string)
'CHANGEMECHANGEME', // The secret encoded in base 32 (string)
10, // The period (int)
'sha512', // The digest algorithm (string)
8 // The number of digits (int)
);
$TOTP->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: ',
),
);
?>

View file

@ -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);