Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

Commit

Permalink
1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
shibdib committed May 17, 2018
1 parent 53bb54f commit b0273d5
Show file tree
Hide file tree
Showing 10 changed files with 430 additions and 46 deletions.
175 changes: 134 additions & 41 deletions app.php
Expand Up @@ -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
]);
});
Expand All @@ -77,13 +72,88 @@
if (isset($_GET['code']) && !isset($_SESSION['eveCode'])) {
$_SESSION['eveCode'] = $_GET['code'];
$url = $config['sso']['callbackURL'];

echo "<head><meta http-equiv='refresh' content='0; url=$url' /></head>";

return;
} else if (isset($_GET['code'])) {
$_SESSION['discordCode'] = $_GET['code'];
echo "<head><meta http-equiv='refresh' content='0; url=/discord/' /></head>";
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);
Expand All @@ -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();
Expand Down Expand Up @@ -124,8 +193,6 @@
}
}

$code = $_SESSION['eveCode'];

//Make sure bots nick is set
if (isset($config['discord']['botNick'])) {
/**
Expand All @@ -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'];
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -371,14 +419,15 @@

// 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']);
}


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.");}
Expand All @@ -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 "<head><meta http-equiv='refresh' content='0; url=/auth/' /></head>";
return;
} else {
$app->render('ping.twig', [
'config' => $config
]);
}
});

$app->run();

/**
Expand Down
14 changes: 14 additions & 0 deletions config/config.new.php
Expand Up @@ -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/
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions libraries/Db.php
Expand Up @@ -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));
Expand Down
3 changes: 0 additions & 3 deletions libraries/discord.php
Expand Up @@ -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' => [
Expand Down
7 changes: 7 additions & 0 deletions modules/announcements/announcements.php
@@ -0,0 +1,7 @@
<?php
/**
* Created by PhpStorm.
* User: rober
* Date: 5/17/2018
* Time: 1:25 PM
*/
2 changes: 1 addition & 1 deletion public/index.php
Expand Up @@ -4,7 +4,7 @@
echo "<footer>
<!--Copyright-->
<div class='footer-copyright py-3 text-center'>
<i><a href='https://github.com/shibdib/Keepstar'> Keepstar Discord Auth V. 1.1</a></i>
<i><a href='https://github.com/shibdib/Keepstar'> Keepstar Auth V. 1.2</a></i>
</div>
<!--/.Copyright-->
</footer>
Expand Down
3 changes: 3 additions & 0 deletions view/authed.twig
Expand Up @@ -90,6 +90,9 @@
<h1>Auth Successful</h1>
<p class="lead">Roles Added</p>
<p>You should now have roles on the server!</p>
<p>
<a class="btn btn-outline-success" href="/auth/" role="button">Return to Dashboard</a>
</p>
</div>
</div>
<!-- /container -->
Expand Down

0 comments on commit b0273d5

Please sign in to comment.