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 <?php
use OTPHP\TOTP;
$DOMAIN = 'machines.frogeye.fr'; $DOMAIN = 'machines.frogeye.fr';
$TOTP = new TOTP( $TOTP = new TOTP(
'newcommer@machines.frogeye.fr', // The label (string) '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) 10, // The period (int)
'sha512', // The digest algorithm (string) 'sha512', // The digest algorithm (string)
8 // The number of digits (int) 8 // The number of digits (int)
); );
$TOTP->setIssuer('Machines Frogeye'); $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,49 +313,54 @@ function argAssert($arg, $data, $args) {
// Hooks // Hooks
// //
function gogsRequest($route, $meth = 'GET', $data = null) {
global $GOGS_API; function updateGitKeys($api, $keys) {
global $GOGS_TOKEN; function apiRequest($api, $route, $meth = 'GET', $data = null) {
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $GOGS_API.'/'.$route); curl_setopt($ch, CURLOPT_URL, $api['api'].'/'.$route);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: token '.$GOGS_TOKEN));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $meth); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $meth);
curl_setopt($ch, CURLOPT_USERAGENT, 'Machines Frogeye');
if ($data) { if ($data) {
$textdata = ''; $dataStr = json_encode($data);
foreach ($data as $key => $value) { curl_setopt($ch, CURLOPT_POSTFIELDS, $dataStr);
$textdata .= '&'.$key.'='.urlencode($value); } 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); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$raw = curl_exec($ch); $raw = curl_exec($ch);
curl_close($ch); curl_close($ch);
return json_decode($raw); return json_decode($raw);
} }
function updateGogsKeys($keys) {
global $GOGS_API;
global $GOGS_TOKEN;
global $SSH_KEY_REGEX; global $SSH_KEY_REGEX;
if (isset($GOGS_API) && isset($GOGS_TOKEN)) { $existing = apiRequest($api, 'user/keys');
$existing = gogsRequest('user/keys'); if ($existing === null) {
return 1;
}
$toDelete = []; $toDelete = [];
foreach ($existing as $ekey) { foreach ($existing as $ekey) {
$toDelete[$ekey->id] = $ekey->key; $toDelete[$ekey->id] = $ekey->key;
} }
foreach (explode("\n", $keys) as $key) { foreach (explode("\n", $keys) as $key) {
if ($key == '') {
continue;
}
$found = false; $found = false;
foreach ($toDelete as $id => $ekey) { foreach ($toDelete as $id => $ekey) {
if ($key == $ekey) { if (strpos($key, $ekey) !== false) {
unset($toDelete[$id]); unset($toDelete[$id]);
$found = true; $found = true;
break; break;
} }
} }
if (!$found) { if (!$found) {
gogsRequest('user/keys', 'POST', array( apiRequest($api, 'user/keys', 'POST', array(
"title" => ltrim(preg_replace($SSH_KEY_REGEX, '', $key)), "title" => ltrim(preg_replace($SSH_KEY_REGEX, '', $key)),
"key" => $key "key" => $key
)); ));
@ -363,8 +368,7 @@ function updateGogsKeys($keys) {
} }
foreach ($toDelete as $id => $ekey) { foreach ($toDelete as $id => $ekey) {
gogsRequest('user/keys/'.$id, 'DELETE'); 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', getKeys($networkName ? $network : null));
file_put_contents('akey/'.$networkName.'.authorized_keys.sha256', $sign); file_put_contents('akey/'.$networkName.'.authorized_keys.sha256', $sign);
if ($networkName == 'gogs') { if (array_key_exists($networkName, $GIT_APIS)) {
updateGogsKeys(getKeys($network)); updateGitKeys($GIT_APIS[$networkName], getKeys($network));
} }
http_response_code(201); http_response_code(201);