Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add discord oath2 #66

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added public/assets/img/discord-mark-blue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions public/assets/img/discord-mark-white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@
});

$router->add('/auth/link/discord', function () {
require("../include/main.php");
require("../view/auth/link/discord.php");
});

$router->add('/auth/discord', function () {
require("../include/main.php");
require("../view/auth/discord.php");
});
Expand Down
86 changes: 77 additions & 9 deletions view/auth/discord.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php
if ($settings['enable_discord_link'] == "true") {
if (isset($_GET['code'])) {
$tokenUrl = 'https://discord.com/api/oauth2/token';
Expand All @@ -7,7 +7,7 @@
'client_secret' => $settings["discord_clientsecret"],
'grant_type' => 'authorization_code',
'code' => $_GET['code'],
'redirect_uri' => $appURL . '/auth/link/discord',
'redirect_uri' => $appURL . '/auth/discord',
'scope' => 'identify guilds email guilds.join'
);
$options = array(
Expand All @@ -19,11 +19,11 @@
);
$context = stream_context_create($options);
$result = file_get_contents($tokenUrl, false, $context);

$accessToken = json_decode($result, true)['access_token'];

$userUrl = 'https://discord.com/api/users/@me';

$options = array(
'http' => array(
'header' => "Authorization: Bearer $accessToken\r\n",
Expand All @@ -32,13 +32,81 @@
);
$context = stream_context_create($options);
$result = file_get_contents($userUrl, false, $context);

$userInfo = json_decode($result, true);
echo $result;
if (isset($userInfo)) {

if (isset($userInfo)) {
$discord_email = $userInfo['email'];
$discord_id = $userInfo['id'];
$query = "SELECT * FROM mythicaldash_users WHERE discord_id = '$discord_id'";
$result = mysqli_query($conn, $query);
if ($result) {
if (mysqli_num_rows($result) == 1) {
$row = mysqli_fetch_assoc($result);
$token = $row['api_key'];
$email = $row['email'];
$banned = $row['banned'];
if (!$banned == "") {
header('location: /auth/login?e=We are sorry but you are banned from using our system!');
die();
} else {
$usr_id = $row['api_key'];
if ($ip_address == "127.0.0.1") {
$ip_address = "12.34.56.78";
}
$url = "http://ipinfo.io/$ip_address/json";
$data = json_decode(file_get_contents($url), true);

if (isset($data['error']) || $data['org'] == "AS1221 Telstra Pty Ltd") {
header('location: /auth/login?e=Hmmm it looks like you are trying to abuse. You are trying to use a VPN, which is not allowed.');
die();
}
$userids = array();
$loginlogs = mysqli_query($conn, "SELECT * FROM mythicaldash_login_logs WHERE userkey = '$usr_id'");
foreach ($loginlogs as $login) {
$ip = $login['ipaddr'];
if ($ip == "12.34.56.78") {
continue;
}
$saio = mysqli_query($conn, "SELECT * FROM mythicaldash_login_logs WHERE ipaddr = '" . $ip . "'");
foreach ($saio as $hello) {
if (in_array($hello['userkey'], $userids)) {
continue;
}
if ($hello['userkey'] == $usr_id) {
continue;
}
array_push($userids, $hello['userkey']);
}
}
if (count($userids) !== 0) {
header('location: /auth/login?e=Using multiple accounts is really sad when using free services!');
die();
}
$conn->query("INSERT INTO mythicaldash_login_logs (ipaddr, userkey) VALUES ('" . $ip_address . "', '$usr_id')");

$cookie_name = 'token';
$cookie_value = $token;
setcookie($cookie_name, $cookie_value, time() + (10 * 365 * 24 * 60 * 60), '/');
$conn->query("UPDATE `mythicaldash_users` SET `last_ip` = '" . $ip_address . "' WHERE `mythicaldash_users`.`api_key` = '" . $usr_id . "';");
header('location: /dashboard');
}
} else {
header('location: /auth/login?e=No accounts were found under this discord account.');
$conn->close();
die();
}
} else {
header('location: /auth/login?e=No accounts were found under this discord account.');
$conn->close();
die();
}
$conn->close();

} else {
header('location: /auth/discord');
}
} else {
$authorizeUrl = 'https://discord.com/api/oauth2/authorize?client_id=' . $settings["discord_clientid"] . '&redirect_uri=' . urlencode($appURL. '/auth/link/discord') . '&response_type=code&scope=' . urlencode('identify guilds email guilds.join');
$authorizeUrl = 'https://discord.com/api/oauth2/authorize?client_id=' . $settings["discord_clientid"] . '&redirect_uri=' . urlencode($appURL . '/auth/discord') . '&response_type=code&scope=' . urlencode('identify guilds email guilds.join');
header('Location: ' . $authorizeUrl);
}
} else {
Expand Down
59 changes: 59 additions & 0 deletions view/auth/link/discord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
include(__DIR__ . '/../../requirements/page.php');
if ($settings['enable_discord_link'] == "true") {
if (isset($_GET['code'])) {
$tokenUrl = 'https://discord.com/api/oauth2/token';
$data = array(
'client_id' => $settings["discord_clientid"],
'client_secret' => $settings["discord_clientsecret"],
'grant_type' => 'authorization_code',
'code' => $_GET['code'],
'redirect_uri' => $appURL . '/auth/link/discord',
'scope' => 'identify guilds email guilds.join'
);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($tokenUrl, false, $context);

$accessToken = json_decode($result, true)['access_token'];

$userUrl = 'https://discord.com/api/users/@me';

$options = array(
'http' => array(
'header' => "Authorization: Bearer $accessToken\r\n",
'method' => 'GET',
),
);
$context = stream_context_create($options);
$result = file_get_contents($userUrl, false, $context);

$userInfo = json_decode($result, true);
echo $result;
if (isset($userInfo)) {
$conn->query("UPDATE `mythicaldash_users` SET `discord_linked` = 'true' WHERE `mythicaldash_users`.`api_key` = '" . mysqli_real_escape_string($conn, $_COOKIE['token']) . "';");
$conn->query("UPDATE `mythicaldash_users` SET `discord_id` = '" . $userInfo['id'] . "' WHERE `mythicaldash_users`.`api_key` = '" . mysqli_real_escape_string($conn, $_COOKIE['token']) . "';");
$conn->query("UPDATE `mythicaldash_users` SET `discord_username` = '" . $userInfo['username'] . "' WHERE `mythicaldash_users`.`api_key` = '" . mysqli_real_escape_string($conn, $_COOKIE['token']) . "';");
$conn->query("UPDATE `mythicaldash_users` SET `discord_global_username` = '" . $userInfo['global_name'] . "' WHERE `mythicaldash_users`.`api_key` = '" . mysqli_real_escape_string($conn, $_COOKIE['token']) . "';");
$conn->query("UPDATE `mythicaldash_users` SET `discord_email` = '" . $userInfo['email'] . "' WHERE `mythicaldash_users`.`api_key` = '" . mysqli_real_escape_string($conn, $_COOKIE['token']) . "';");
$conn->close();
header("location: /user/connections");
} else {
header('location: /auth/link/discord');
}
} else {
$authorizeUrl = 'https://discord.com/api/oauth2/authorize?client_id=' . $settings["discord_clientid"] . '&redirect_uri=' . urlencode($appURL . '/auth/link/discord') . '&response_type=code&scope=' . urlencode('identify guilds email guilds.join');
header('Location: ' . $authorizeUrl);
}
} else {
header("location: /dashboard?e=We are sorry but we don't provide support for discord link right now");
die();
}

?>
42 changes: 29 additions & 13 deletions view/auth/login.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
include(__DIR__.'/../../include/php-csrf.php');
include(__DIR__.'/../../functions/telemetry.php');
include(__DIR__.'/../../functions/report.php');
include(__DIR__ . '/../../include/php-csrf.php');
include(__DIR__ . '/../../functions/telemetry.php');
include(__DIR__ . '/../../functions/report.php');
session_start();
$csrf = new CSRF();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
Expand Down Expand Up @@ -69,7 +69,7 @@
$cookie_name = 'token';
$cookie_value = $token;
setcookie($cookie_name, $cookie_value, time() + (10 * 365 * 24 * 60 * 60), '/');
$conn->query("UPDATE `mythicaldash_users` SET `last_ip` = '".$ip_address."' WHERE `mythicaldash_users`.`api_key` = '".$usr_id."';");
$conn->query("UPDATE `mythicaldash_users` SET `last_ip` = '" . $ip_address . "' WHERE `mythicaldash_users`.`api_key` = '" . $usr_id . "';");
if (isset($_GET['r'])) {
header('location: ' . $_GET['r']);
} else {
Expand Down Expand Up @@ -145,6 +145,17 @@ class="platform-bg" data-app-light-img="illustrations/bg-shape-image-light.png"
<?= $settings['name'] ?>!
</h3>
<p class="mb-4">Please sign-in to your account and start the adventure</p>
<?php
if (isset($_GET['e'])) {
?>
<div class="text-center alert alert-danger" role="alert">
<?= $_GET['e'] ?>
</div>
<?php
} else {

}
?>
<form method="POST">
<div class="mb-3">
<label for="email" class="form-label">Email</label>
Expand Down Expand Up @@ -191,17 +202,22 @@ class="platform-bg" data-app-light-img="illustrations/bg-shape-image-light.png"
<span>Create an account</span>
</a>
</p>
<?php
if (isset($_GET['e'])) {
?>
<div class="text-center alert alert-danger" role="alert">
<?= $_GET['e'] ?>
</div>
<div class="divider my-2">
<div class="divider-text"> or </div>
</div>
<div class="auth-footer-btn d-flex justify-content-center">
<?php
} else {
if (!$settings['discord_clientid'] == "" && !$settings['discord_clientsecret'] == "") {
?>
<a href="/auth/discord" target="_self" class="btn btn-primary">
<img width="18px" height="18px" src="/assets/img/discord-mark-white.svg" alt="Discord Logo">
</a>
<?php
}
?>

}
?>
</div>
<br>
</p>
</div>
</div>
Expand Down
5 changes: 3 additions & 2 deletions view/requirements/head.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ class="template-customizer-theme-css" />
<link rel="stylesheet" href="<?= $appURL ?>/assets/vendor/libs/select2/select2.css" />
<script src="<?= $appURL ?>/assets/vendor/js/helpers.js"></script>
<script src="<?= $appURL ?>/assets/vendor/js/template-customizer.js"></script>


<script src="<?= $appURL ?>/assets/js/config.js"></script>
<link rel="stylesheet" href="<?= $appURL ?>/assets/css/preloader.css" />
<style>
Expand All @@ -64,6 +62,9 @@ class="template-customizer-theme-css" />
if ($settings['customcss_enabled'] == 'true') {
?>
<style type="text/css">
#template-customizer .template-customizer-open-btn {
visibility: hidden;
}
<?= $settings['customcss_code'] ?>
</style>
<?php
Expand Down
Loading