Skip to content

Commit 28f17ff

Browse files
committed
move github to its own class
1 parent 1c77335 commit 28f17ff

File tree

8 files changed

+75
-50
lines changed

8 files changed

+75
-50
lines changed

resources/autoload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
require_once __DIR__ . "/lib/UnityConfig.php";
2020
require_once __DIR__ . "/lib/UnityWebhook.php";
2121
require_once __DIR__ . "/lib/UnityRedis.php";
22+
require_once __DIR__ . "/lib/UnityGithub.php";
2223

2324
// run init script
2425
require __DIR__ . "/init.php";

resources/init.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use UnityWebPortal\lib\UnityUser;
1313
use UnityWebPortal\lib\UnityRedis;
1414
use UnityWebPortal\lib\UnityWebhook;
15+
use UnityWebPortal\lib\UnityGithub;
1516

1617
//
1718
// Initialize Session
@@ -84,6 +85,8 @@
8485
$CONFIG["site"]["url"] . $CONFIG["site"]["prefix"]
8586
);
8687

88+
$GITHUB = new UnityGithub();
89+
8790
//
8891
// SSO Init
8992
//

resources/lib/UnityGithub.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace UnityWebPortal\lib;
4+
5+
class UnityGithub
6+
{
7+
public function getSshPublicKeys($username)
8+
{
9+
$url = "https://api.github.com/users/$username/keys";
10+
$headers = array(
11+
"User-Agent: Unity Cluster User Portal"
12+
);
13+
14+
$curl = curl_init();
15+
curl_setopt($curl, CURLOPT_URL, $url);
16+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
17+
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
18+
$keys = json_decode(curl_exec($curl), false);
19+
curl_close($curl);
20+
21+
// normally returns array of objects each with a ->key attribute
22+
// if bad URL or no such user, returns status=404 object
23+
// if no keys, returns []
24+
if ((!is_array($keys)) || (count($keys) == 0)) {
25+
return [];
26+
}
27+
// phpcs:disable
28+
return array_map(function($x){return $x->key;}, $keys);
29+
// phpcs:enable
30+
}
31+
}

resources/lib/UnitySite.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,6 @@ public static function removeTrailingWhitespace($arr)
2525
return $out;
2626
}
2727

28-
public static function getGithubKeys($username)
29-
{
30-
$url = "https://api.github.com/users/$username/keys";
31-
$headers = array(
32-
"User-Agent: Unity Cluster User Portal"
33-
);
34-
35-
$curl = curl_init();
36-
curl_setopt($curl, CURLOPT_URL, $url);
37-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
38-
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
39-
$keys = json_decode(curl_exec($curl), false);
40-
curl_close($curl);
41-
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 [];
47-
}
48-
// phpcs:disable
49-
return array_map(function($x){return $x->key;}, $keys);
50-
// phpcs:enable
51-
}
52-
5328
public static function testValidSSHKey($key_str)
5429
{
5530
// key loader still throws, these just mute warnings for phpunit

test/phpunit-bootstrap.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
function switchUser(string $eppn, string $given_name, string $sn, string $mail): void
4545
{
46-
global $CONFIG, $REDIS, $LDAP, $SQL, $MAILER, $WEBHOOK, $SITE, $SSO, $OPERATOR, $USER, $SEND_PIMESG_TO_ADMINS, $LOC_HEADER, $LOC_FOOTER;
46+
global $CONFIG, $REDIS, $LDAP, $SQL, $MAILER, $WEBHOOK, $GITHUB, $SITE, $SSO, $OPERATOR, $USER, $SEND_PIMESG_TO_ADMINS, $LOC_HEADER, $LOC_FOOTER;
4747
session_write_close();
4848
session_id(str_replace(["_", "@", "."], "-", $eppn));
4949
// session_start will be called on the first post()
@@ -58,7 +58,7 @@ function switchUser(string $eppn, string $given_name, string $sn, string $mail):
5858

5959
function post(string $phpfile, array $post_data): void
6060
{
61-
global $CONFIG, $REDIS, $LDAP, $SQL, $MAILER, $WEBHOOK, $SITE, $SSO, $OPERATOR, $USER, $SEND_PIMESG_TO_ADMINS, $LOC_HEADER, $LOC_FOOTER;
61+
global $CONFIG, $REDIS, $LDAP, $SQL, $MAILER, $WEBHOOK, $GITHUB, $SITE, $SSO, $OPERATOR, $USER, $SEND_PIMESG_TO_ADMINS, $LOC_HEADER, $LOC_FOOTER;
6262
$_SERVER["REQUEST_METHOD"] = "POST";
6363
$_POST = $post_data;
6464
ob_start();
@@ -88,3 +88,8 @@ function getUserHasNotRequestedAccountDeletionHasNoGroups()
8888
{
8989
return ["[email protected]", "foo", "bar", "[email protected]"];
9090
}
91+
92+
function getUserHasNoSshKeys()
93+
{
94+
return ["[email protected]", "foo", "bar", "[email protected]"];
95+
}

test/unit/UnityGithubTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace UnityWebPortal\lib;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use PHPUnit\Framework\Attributes\DataProvider;
7+
8+
class UnitySiteTest extends TestCase
9+
{
10+
public static function providerTestGetGithubKeys()
11+
{
12+
return [
13+
# empty
14+
["", []],
15+
# nonexistent user
16+
["asdfkljhasdflkjashdflkjashdflkasjd", []],
17+
# user with no keys
18+
["sheldor1510", []],
19+
# user with 1 key
20+
//phpcs:disable
21+
["simonLeary42", ["ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDGRl6JWPj+Gq2Lz9GjYdUl4/unLoFOyfgeiII1CxutpabPByRJbeuonR0zTpn51tZeYuAUOJBOeKt+Lj4i4UDGl6igpdXXSwkBXl7jxfRPwJ6WuTkDx7Z8ynwnqlDV2q089q4OX/b/uuHgsIhIBwrouKsRQaZIqTbwNMfiqQ2zl14V0KMrTPzOiwR6Q+hqSaR5Z29WKE7ff/OWzSC3/0T6avCmcCbQaRPJdVM+QC17B0vl8FzPwRjorMngwZ0cImdQ/0Ww1d12YAL7UWp1c2egfnthKP3MuQZnNF8ixsAk1eIIwTRdiI87BOoorW8NXhxXmhyheRCsFwyP4LJBqyUVoZJ0UYyk0AO4G9EStnfpiz8YXGK+M1G4tUrWgzs1cdjlHtgCWUmITtgabnYCC4141m7n4GZTk2H/lSrJcvAs3JEiwLTj1lzeGgzeSsz/XKsnOJyzjEVr2Jp3iT+J9PbQpfS0SxTCIGgxMqllovv79pfsF/zc+vaxqSShyHW7oyn7hLMHM60LO/IIX1RWGL3rD9ecXx2pXXQ1RhIkVteIi13XkFt+KW00cstFlAd3EHCoY/XorShd2jeID7tpnYlmNfotYUs6IKefvpNC0PWkh5UXFEv3SUfw4Wd8O0DiHfhkrhxn1W/GajqSIlZ5DKgPzFg8EHexv8lSa7WJg0H3YQ=="]]
22+
//phpcs:enable
23+
];
24+
}
25+
26+
#[DataProvider("providerTestGetGithubKeys")]
27+
public function testGetGithubKeys(string $username, array $expected)
28+
{
29+
$GITHUB = new UnityGithub();
30+
$this->assertEquals($expected, $GITHUB->getSshPublicKeys($username));
31+
}
32+
}

test/unit/UnitySiteTest.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,4 @@ public function testTestValidSSHKey(bool $expected, string $key)
3030
$this->assertEquals($expected, $SITE->testValidSSHKey($key));
3131
}
3232

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-
}
5533
}

webroot/panel/account.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
break;
3939
case "github":
4040
$gh_user = $_POST["gh_user"];
41-
$keys = UnitySite::getGithubKeys($gh_user);
41+
$keys = $GITHUB->getSshPublicKeys($gh_user);
4242
foreach ($keys as $key) {
4343
if (UnitySite::testValidSSHKey($key)) {
4444
array_push($added_keys, $key);

0 commit comments

Comments
 (0)