Skip to content

Commit 89150e1

Browse files
authored
Merge pull request #162 from UnityHPC/patch-14
handle github keys api properly
2 parents 0935dbc + 2cdb0ad commit 89150e1

File tree

5 files changed

+39
-10
lines changed

5 files changed

+39
-10
lines changed

Diff for: .pre-commit-config.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,9 @@ repos:
4646
language: system
4747
files: \.php$
4848
args: [--standard=PSR2, --colors]
49+
- id: php-l
50+
name: php -l
51+
entry: php
52+
language: system
53+
files: \.php$
54+
args: [-l]

Diff for: composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"require": {
33
"psr/log": "1.1.4",
4-
"phpseclib/phpseclib": "3.0.16",
4+
"phpseclib/phpseclib": "3.0.43",
55
"phpmailer/phpmailer": "6.6.4",
66
"hakasapl/phpopenldaper": "1.0.5"
77
}

Diff for: resources/lib/UnitySite.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,18 @@ public static function getGithubKeys($username)
3636
curl_setopt($curl, CURLOPT_URL, $url);
3737
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
3838
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
39-
$output = json_decode(curl_exec($curl), true);
39+
$keys = json_decode(curl_exec($curl), false);
4040
curl_close($curl);
4141

42-
$out = array();
43-
foreach ($output as $value) {
44-
array_push($out, $value["key"]);
42+
// normally returns array of objects each with a ->key attribute
43+
// if bad URL or no such user, returns status=404 object
44+
// if no keys, returns []
45+
if ((!is_array($keys)) || (count($keys) == 0)) {
46+
return [];
4547
}
46-
47-
return $out;
48+
// phpcs:disable
49+
return array_map(function($x){return $x->key;}, $keys);
50+
// phpcs:enable
4851
}
4952

5053
public static function testValidSSHKey($key_str)

Diff for: test/unit/UnitySiteTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,27 @@ public function testTestValidSSHKey(bool $expected, string $key)
2929
$SITE = new UnitySite();
3030
$this->assertEquals($expected, $SITE->testValidSSHKey($key));
3131
}
32+
33+
public static function providerTestGetGithubKeys()
34+
{
35+
return [
36+
# empty
37+
["", []],
38+
# nonexistent user
39+
["asdfkljhasdflkjashdflkjashdflkasjd", []],
40+
# user with no keys
41+
["sheldor1510", []],
42+
# user with 1 key
43+
//phpcs:disable
44+
["simonLeary42", ["ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDGRl6JWPj+Gq2Lz9GjYdUl4/unLoFOyfgeiII1CxutpabPByRJbeuonR0zTpn51tZeYuAUOJBOeKt+Lj4i4UDGl6igpdXXSwkBXl7jxfRPwJ6WuTkDx7Z8ynwnqlDV2q089q4OX/b/uuHgsIhIBwrouKsRQaZIqTbwNMfiqQ2zl14V0KMrTPzOiwR6Q+hqSaR5Z29WKE7ff/OWzSC3/0T6avCmcCbQaRPJdVM+QC17B0vl8FzPwRjorMngwZ0cImdQ/0Ww1d12YAL7UWp1c2egfnthKP3MuQZnNF8ixsAk1eIIwTRdiI87BOoorW8NXhxXmhyheRCsFwyP4LJBqyUVoZJ0UYyk0AO4G9EStnfpiz8YXGK+M1G4tUrWgzs1cdjlHtgCWUmITtgabnYCC4141m7n4GZTk2H/lSrJcvAs3JEiwLTj1lzeGgzeSsz/XKsnOJyzjEVr2Jp3iT+J9PbQpfS0SxTCIGgxMqllovv79pfsF/zc+vaxqSShyHW7oyn7hLMHM60LO/IIX1RWGL3rD9ecXx2pXXQ1RhIkVteIi13XkFt+KW00cstFlAd3EHCoY/XorShd2jeID7tpnYlmNfotYUs6IKefvpNC0PWkh5UXFEv3SUfw4Wd8O0DiHfhkrhxn1W/GajqSIlZ5DKgPzFg8EHexv8lSa7WJg0H3YQ=="]]
45+
//phpcs:enable
46+
];
47+
}
48+
49+
#[DataProvider("providerTestGetGithubKeys")]
50+
public function testGetGithubKeys(string $username, array $expected)
51+
{
52+
$SITE = new UnitySite();
53+
$this->assertEquals($expected, $SITE->getGithubKeys($username));
54+
}
3255
}

Diff for: webroot/panel/account.php

-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@
3838
break;
3939
case "github":
4040
$gh_user = $_POST["gh_user"];
41-
if (empty($gh_user)) {
42-
break;
43-
}
4441
$keys = UnitySite::getGithubKeys($gh_user);
4542
foreach ($keys as $key) {
4643
if (UnitySite::testValidSSHKey($key)) {

0 commit comments

Comments
 (0)