From b0273d54aa92e259d02f8e0ad4175452d4050f56 Mon Sep 17 00:00:00 2001 From: shibdib Date: Thu, 17 May 2018 18:46:44 -0400 Subject: [PATCH] 1.2 --- app.php | 175 ++++++++++++++++++------ config/config.new.php | 14 ++ libraries/Db.php | 5 + libraries/discord.php | 3 - modules/announcements/announcements.php | 7 + public/index.php | 2 +- view/authed.twig | 3 + view/dashboard.twig | 132 ++++++++++++++++++ view/index.twig | 2 +- view/ping.twig | 133 ++++++++++++++++++ 10 files changed, 430 insertions(+), 46 deletions(-) create mode 100644 modules/announcements/announcements.php create mode 100644 view/dashboard.twig create mode 100644 view/ping.twig diff --git a/app.php b/app.php index 1bda057..9cb4c02 100644 --- a/app.php +++ b/app.php @@ -55,20 +55,15 @@ $app->get('/', function () use ($app, $config) { //Clear out session just incase $_SESSION = []; - if (isset($_COOKIE[session_name()])) { setcookie(session_name(), '', time() - 42000, '/'); } - - //Check if keepstar is linked to firetail - if ($config['firetail']['active'] === true) { - $scopes = str_replace(' ', '%20', $config['firetail']['scopes']); - $url = 'https://login.eveonline.com/oauth/authorize?response_type=code&scope=' . $scopes . '&redirect_uri=' . $config['sso']['callbackURL'] . '&client_id=' . $config['sso']['clientID']; - } else { - $url = 'https://login.eveonline.com/oauth/authorize?response_type=code&redirect_uri=' . $config['sso']['callbackURL'] . '&client_id=' . $config['sso']['clientID']; + if (!isset($config['auth']['title'])) { + $config['auth']['title'] = 'Keepstar Auth'; } - + $url = 'https://login.eveonline.com/oauth/authorize?response_type=code&redirect_uri=' . $config['sso']['callbackURL'] . '&client_id=' . $config['sso']['clientID']; $app->render('index.twig', [ + 'config' => $config, 'crestURL' => $url ]); }); @@ -77,13 +72,88 @@ if (isset($_GET['code']) && !isset($_SESSION['eveCode'])) { $_SESSION['eveCode'] = $_GET['code']; $url = $config['sso']['callbackURL']; - echo ""; - return; + } else if (isset($_GET['code'])) { + $_SESSION['discordCode'] = $_GET['code']; + echo ""; + return; + } + if (!isset($_SESSION['eveData'])) { + $code = $_SESSION['eveCode']; + $tokenURL = 'https://login.eveonline.com/oauth/token'; + $base64 = base64_encode($config['sso']['clientID'] . ':' . $config['sso']['secretKey']); + $data = json_decode(sendData($tokenURL, [ + 'grant_type' => 'authorization_code', + 'code' => $code + ], [ + "Authorization: Basic {$base64}" + ])); + $accessToken = $data->access_token; + // Verify Token + $verifyURL = 'https://login.eveonline.com/oauth/verify'; + $_SESSION['eveData'] = json_decode(sendData($verifyURL, [], ["Authorization: Bearer {$accessToken}"])); + } + $data = $_SESSION['eveData']; + $characterID = $data->CharacterID; + $_SESSION['characterID'] = $characterID; + $characterData = characterDetails($characterID); + $corporationID = $characterData['corporation_id']; + $_SESSION['corporationID'] = $corporationID; + $corporationData = corporationDetails($corporationID); + $corporationName = $corporationData['name']; + $eveName = trim($characterData['name']); + $allianceName = null; + if (!isset($characterData['alliance_id'])) { + $allianceID = 1; + $allianceTicker = null; + } else { + $allianceID = $characterData['alliance_id']; + $allianceData = allianceDetails($allianceID); + $allianceName = $allianceData['name']; } + $_SESSION['allianceID'] = $allianceID; + $imageURL = 'https://image.eveonline.com/Character/' . $characterID . '_256.jpg'; + // Check if user can ping + $canPing = null; + if (isset($config['pings']['enabled']) && $config['pings']['enabled'] === true) { + $authInfo = getUserWithEve($characterID); + if (isset($authInfo[0]['discordID'])) { + $restcord = new DiscordClient([ + 'token' => $config['discord']['botToken'] + ]); + $memberInfo = $restcord->guild->getGuildMember([ + 'guild.id' => (int)$config['discord']['guildId'], + 'user.id' => (int)$authInfo[0]['discordID'] + ]); + $memberRoles = $memberInfo->roles; + $roles = $restcord->guild->getGuildRoles([ + 'guild.id' => $config['discord']['guildId'] + ]); + foreach ($roles as $role) { + if ($role->name == $config['pings']['pingRole']) { + if (in_array((int)$role->id, $memberRoles)) { + $canPing = true; + break; + } else { + break; + } + } + } + } + } + $app->render('dashboard.twig', [ + 'image' => $imageURL, + 'name' => $eveName, + 'corp' => $corporationName, + 'alliance' => $allianceName, + 'canPing' => $canPing, + 'config' => $config + ]); +}); - if (!isset($_GET['code'])) { +$app->get('/discord/', function () use ($app, $config, $log) { + if (!isset($_SESSION['discordCode'])) { // If we don't have a code yet, we need to make the link $scopes = 'identify%20guilds.join'; $discordLink = url($config['discord']['clientId'], $config['discord']['redirectUri'], $scopes); @@ -93,8 +163,7 @@ 'discordLink' => $discordLink ]); } else { - // If we do have a code, use it to get a token - $code = $_GET['code']; + $code = $_SESSION['discordCode']; init($code, $config['discord']['redirectUri'], $config['discord']['clientId'], $config['discord']['clientSecret']); get_user(); @@ -124,8 +193,6 @@ } } - $code = $_SESSION['eveCode']; - //Make sure bots nick is set if (isset($config['discord']['botNick'])) { /** @@ -145,36 +212,17 @@ } } - $tokenURL = 'https://login.eveonline.com/oauth/token'; - $base64 = base64_encode($config['sso']['clientID'] . ':' . $config['sso']['secretKey']); - - $data = json_decode(sendData($tokenURL, [ - 'grant_type' => 'authorization_code', - 'code' => $code - ], [ - "Authorization: Basic {$base64}" - ])); - - $accessToken = $data->access_token; - - // Verify Token - $verifyURL = 'https://login.eveonline.com/oauth/verify'; - $data = json_decode(sendData($verifyURL, [], ["Authorization: Bearer {$accessToken}"])); - - $characterID = $data->CharacterID; + $characterID = $_SESSION['characterID']; $characterData = characterDetails($characterID); $corporationID = $characterData['corporation_id']; $corporationData = corporationDetails($corporationID); $eveName = trim($characterData['name']); + $_SESSION['characterName'] = $eveName; - if (!isset($characterData['alliance_id'])) { - $allianceID = 1; - $allianceTicker = null; - } else { - $allianceID = $characterData['alliance_id']; - $allianceData = allianceDetails($allianceID); + if ($_SESSION['allianceID'] !== 1) { + $allianceData = allianceDetails($_SESSION['allianceID']); $allianceTicker = $allianceData['ticker']; } @@ -311,7 +359,7 @@ } // Autnetification by allianceID - if (in_array($allianceID, $id)) { + if (in_array($_SESSION['allianceID'], $id)) { foreach ($roles as $role) { if ($role->name == $authGroup['role']) { break; @@ -371,7 +419,7 @@ // If firetail link is active, insert into firetail db if ($config['firetail']['active'] === true) { - $refreshToken = $data->refresh_token; + $refreshToken = $_SESSION['auth_token']; firetailEntry($characterID, (int)$_SESSION['user_id'], $refreshToken, $config['firetail']['path']); } @@ -379,6 +427,7 @@ if (count($access) > 0) { //if (isset($eveName)) {$log->notice("$eveName has been added to the role $role->name.");} else {$log->notice("$discordId has been added to the role $role->name.");} + $_SESSION['discordCode'] = null; $app->render('authed.twig'); } else { //if (isset($eveName)) {$log->notice("Auth Failed - $eveName attempted to auth but no roles were found.");} else {$log->notice("Auth Failed - $discordId attempted to auth but no roles were found.");} @@ -387,6 +436,50 @@ } }); +$app->get('/ping/', function () use ($app, $config, $log) { + if (isset($_GET['message'])) { + $restcord = new DiscordClient([ + 'token' => $config['discord']['botToken'] + ]); + $data = $_SESSION['eveData']; + $characterID = $data->CharacterID; + $characterData = characterDetails($characterID); + $characterName = $characterData['name']; + $content = ''; + if (isset($_GET['everyone'])) { + $content = '@everyone'; + } + $restcord->channel->createMessage([ + 'channel.id' => (int)$_GET['channel'], + 'content' => $content, + 'embed' => [ + "title" => 'Incoming Ping', + "description" => 'Ping From: ' . $characterName, + "color" => 14290439, + "footer" => [ + "icon_url" => "https://webimg.ccpgamescdn.com/kvd74o0q2fjg/1M08UMgc7y8u6sQcikSuqk/6ef1923a91e38e800fb3bfca575a23c0/UPDATES_PALATINE.png_w=1280&fm=jpg", + "text" => $config['pings']['append'] + ], + "thumbnail" => [ + "url" => 'https://image.eveonline.com/Character/' . $characterID . '_32.jpg' + ], + "fields" => [ + [ + "name" => "-", + "value" => $_GET['message'] + ] + ] + ] + ]); + echo ""; + return; + } else { + $app->render('ping.twig', [ + 'config' => $config + ]); + } +}); + $app->run(); /** diff --git a/config/config.new.php b/config/config.new.php index 4e3d6f1..3b65b16 100644 --- a/config/config.new.php +++ b/config/config.new.php @@ -2,6 +2,12 @@ $config = []; +// Auth +$config['auth'] = [ + 'title' => 'EVE Online', // Alliance/Corp/Group Name + 'admin' => 0 // The In-Game characterID of the admin +]; + // CREST $config['sso'] = [ 'clientID' => '', // https://developers.eveonline.com/ @@ -36,6 +42,14 @@ ], ]; +// Additional Modules +$config['pings'] = [ // Send announcements to various discord channels + 'enable' => false, + 'pingChannel' => 0, // Channel ID that pings default to + 'pingRole' => '', // Discord role that can send pings + 'append' => '=== Ping Sent Via Keepstar Auth ===', // All pings will have this line added to the footer +]; + // Site IGNORE EVERYTHING BELOW THIS LINE $config['site'] = [ 'debug' => true, diff --git a/libraries/Db.php b/libraries/Db.php index 8220055..0048941 100644 --- a/libraries/Db.php +++ b/libraries/Db.php @@ -16,6 +16,11 @@ function getUser($discordId) return dbQuery('SELECT * FROM authed WHERE `discordID` = :discordID', array(':discordID' => $discordId)); } +function getUserWithEve($characterID) +{ + return dbQuery('SELECT * FROM authed WHERE `characterID` = :characterID', array(':characterID' => $characterID)); +} + function deleteUser($id) { dbQueryRow('DELETE from authed WHERE `id` = :id', array(':id' => $id)); diff --git a/libraries/discord.php b/libraries/discord.php index 35dbeb8..d670c8c 100644 --- a/libraries/discord.php +++ b/libraries/discord.php @@ -25,9 +25,6 @@ function url($clientid, $redirect, $scope) // Get code and initialize the variables function init($code, $redirect, $clientid, $clientsecretid) { -$code = $_GET['code']; -$data = "grant_type=authorization_code&code=$code&redirect_uri=$redirect&client_id=$clientid&client_secret=$clientsecretid"; - // Get authorization code by posting to discord's API $response = $GLOBALS['http']->request('POST', '/api/oauth2/token', [ 'form_params' => [ diff --git a/modules/announcements/announcements.php b/modules/announcements/announcements.php new file mode 100644 index 0000000..8a1b961 --- /dev/null +++ b/modules/announcements/announcements.php @@ -0,0 +1,7 @@ + diff --git a/view/authed.twig b/view/authed.twig index 81c8c86..7d45cb2 100644 --- a/view/authed.twig +++ b/view/authed.twig @@ -90,6 +90,9 @@

Auth Successful

Roles Added

You should now have roles on the server!

+

+ Return to Dashboard +

diff --git a/view/dashboard.twig b/view/dashboard.twig new file mode 100644 index 0000000..5cf62fd --- /dev/null +++ b/view/dashboard.twig @@ -0,0 +1,132 @@ + + + + + + Discord Authenticator + + + + + + + + + +
+
+ +
+
+ +

{{ name }}

+

{{ corp }}

+

Choose your desired action below.

+

+ Get Discord Roles +

+ + +
+
+ + + + \ No newline at end of file diff --git a/view/index.twig b/view/index.twig index 29a1727..efaccfd 100644 --- a/view/index.twig +++ b/view/index.twig @@ -107,7 +107,7 @@
-

EVE Online

+

{{ config['auth']['title'] }}

Click the button below to securely login with your EVE Online account.

diff --git a/view/ping.twig b/view/ping.twig new file mode 100644 index 0000000..fa1e8d4 --- /dev/null +++ b/view/ping.twig @@ -0,0 +1,133 @@ + + + + + + Discord Authenticator + + + + + + + + + +

+
+ +
+
+

Send a Discord Ping

+
+
+ + +
+
+ + +
+
+ + +
+ +
+
+
+ + + + \ No newline at end of file