feat: Simple security centre

This commit is contained in:
Pazaz committed 2025-02-01 07:25:36 -05:00
1 parent 4dcf07585d
commit 2368605ca0
12 files changed
+304 -227

No files matched your search

+1
View File
@@ -6,6 +6,7 @@ tsconfig.tsbuildinfo
node_modules/
data/config/worlds.json
data/players/
# coverage reports
coverage/
+116
View File
@@ -0,0 +1,116 @@
import bcrypt from 'bcrypt';
import fs from 'fs';
import { db } from '#/db/query.js';
import { profiles, resolveSelectedProfile } from '#/util/Profile.js';
export default function (f: any, opts: any, next: any) {
f.get('/', async (req: any, res: any) => {
// todo: validation handler instead of in route
if (!req.session.account) {
return res.redirect('/account/login', 302);
}
const profile = resolveSelectedProfile(req);
const { success, error} = req.session;
delete req.session.success;
delete req.session.error;
return res.view('account/index', {
success,
error,
profile,
profiles
});
});
f.get('/login', async (req: any, res: any) => {
const { success, error} = req.session;
delete req.session.success;
delete req.session.error;
return res.view('account/login', {
success,
error
});
});
f.get('/logout', async (req: any, res: any) => {
req.session.account = null;
return res.redirect('/account/login', 302);
});
f.get('/forgot', async (req: any, res: any) => {
const { success, error} = req.session;
delete req.session.success;
delete req.session.error;
return res.view('account/forgot', {
success,
error
});
});
f.post('/login', async (req: any, res: any) => {
const { username, password } = req.body;
const account = await db.selectFrom('account').where('username', '=', username).selectAll().executeTakeFirst();
if (!account || !(await bcrypt.compare(password.toLowerCase(), account.password))) {
req.session.error = 'Invalid username or password.';
return res.redirect('/account/login', 302);
}
req.session.account = {
id: account.id,
username: account.username
};
return res.redirect('/account', 302);
});
f.post('/password', async (req: any, res: any) => {
if (!req.session.account) {
return res.redirect('/account/login', 302);
}
const { password, password2 } = req.body;
if (!password || password.length < 5 || password.length > 20) {
req.session.error = 'Your password must be between 5 and 20 characters long.';
return res.redirect('/account', 302);
}
if (password !== password2) {
req.session.error = 'Your passwords do not match.';
return res.redirect('/account', 302);
}
const hash = await bcrypt.hash(password.toLowerCase(), 10);
await db.updateTable('account').set({ password: hash }).where('id', '=', req.session.account.id).execute();
req.session.success = 'Your password has been updated.';
return res.redirect('/account', 302);
});
f.post('/forgot', async (req: any, res: any) => {
return res.redirect('/account/forgot', 302);
});
f.post('/export', async (req: any, res: any) => {
// todo: move to input validation
if (!req.session.account) {
return res.redirect('/account', 302);
}
const profile = resolveSelectedProfile(req);
if (!fs.existsSync(`data/players/${profile.id}/${req.session.account.username}.sav`)) {
return res.redirect('/account', 302);
}
return fs.readFileSync(`data/players/${profile.id}/${req.session.account.username}.sav`);
});
next();
}
+1 -22
View File
@@ -1,5 +1,6 @@
import { toDisplayName, toSafeName } from '#/jstring/JString.js';
import { db } from '#/db/query.js';
import { profiles, resolveSelectedProfile } from '#/util/Profile.js';
const categories = [
{ id: 0, name: 'Overall', large: true, level: true },
@@ -24,14 +25,6 @@ const categories = [
{ id: 21, name: 'Runecrafting' }
];
// todo: move to db table
const profiles = [
{ id: 'alpha3', name: 'Alpha 3.0' }, // todo: primary/fallback should be configurable
{ id: 'alpha1', name: 'Alpha 1.0' },
{ id: 'alpha2', name: 'Alpha 2.0' },
{ id: 'beta', name: 'Beta' },
];
const levelExperience = new Int32Array(99);
let acc = 0;
@@ -56,20 +49,6 @@ function numberWithCommas(x: number) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
function resolveSelectedProfile(req: any): { id: string } {
let profile = profiles.find((p) => p.id === req.query.profile);
if (!profile && req.session.selectedProfile) {
profile = profiles.find((p) => p.id === req.session.selectedProfile);
}
if (!profile) {
profile = profiles[0];
}
req.session.selectedProfile = profile.id;
return profile;
}
export default function (f: any, opts: any, next: any) {
f.get('/', async (req: any, res: any) => {
const profile = resolveSelectedProfile(req);
+4
View File
@@ -92,4 +92,8 @@ export default async function (app: FastifyInstance) {
app.get('/worldmap', async (req, reply) => {
return reply.view('worldmap/applet');
});
app.get('/guide', async (req, reply) => {
return reply.view('guide');
});
}
+21
View File
@@ -0,0 +1,21 @@
// todo: move to db table
export const profiles = [
{ id: 'beta', name: 'Beta' },
{ id: 'alpha1', name: 'Alpha 1.0' },
{ id: 'alpha2', name: 'Alpha 2.0' },
{ id: 'alpha3', name: 'Alpha 3.0' },
];
export function resolveSelectedProfile(req: any): { id: string } {
let profile = profiles.find((p) => p.id === req.query.profile);
if (!profile && req.session.selectedProfile) {
profile = profiles.find((p) => p.id === req.session.selectedProfile);
}
if (!profile) {
profile = profiles[0];
}
req.session.selectedProfile = profile.id;
return profile;
}
+23
View File
@@ -0,0 +1,23 @@
<%- include('../_partial/header.ejs', { title: 'Forgot Password - Security Centre' }); %>
<%- include('../_components/frame-top.ejs'); %>
<%- include('../_components/content-start.ejs'); %>
<%- include('../_components/title-box.ejs', { title: 'Forgot Password - Security Centre' }); %>
<%- include('../_components/content-body-start.ejs'); %>
<% if (success) { %>
<p class="green"><%= success %></p>
<% } else if (error) { %>
<p class="red"><%= error %></p>
<% } %>
<img src="https://i.imgur.com/XPTGP7i.jpeg"><br>
Gotcha! Get a better memory next time.<br><br>
Only kidding - Reach out on Discord for help until this automated system is complete :)
<%- include('../_components/content-body-end.ejs'); %>
<br>
<%- include('../_components/content-end.ejs', { nobr: true }); %>
<%- include('../_partial/footer.ejs'); %>
+47
View File
@@ -0,0 +1,47 @@
<%- include('../_partial/header.ejs', { title: 'Security Centre' }); %>
<%- include('../_components/frame-top.ejs'); %>
<%- include('../_components/content-start.ejs'); %>
<%- include('../_components/title-box.ejs', { title: 'Security Centre', links: [ { text: 'Logout', href: '/account/logout' } ] }); %>
<%- include('../_components/content-body-start.ejs'); %>
<% if (success) { %>
<p class="green"><%= success %></p>
<% } else if (error) { %>
<p class="red"><%= error %></p>
<% } else { %>
<p>Change your password:</p>
<% } %>
<form method="post" action="/account/password">
<label for="password">Desired Password:</label>
<input type="password" name="password">
<br>
<label for="password">Confirm Password:</label>
<input type="password" name="password2">
<br><br>
<input type="submit" value="Update">
</form>
<%- include('../_components/content-body-end.ejs'); %>
<br>
<%- include('../_components/content-body-start.ejs'); %>
<br>
Export your player save:<br><br>
<%- include('../hiscores/partials/profile.ejs', { profile, profiles }) %>
<form method="post" action="/account/export">
<input type="submit" value="Download">
</form>
<%- include('../_components/content-body-end.ejs'); %>
<br>
<%- include('../_components/content-end.ejs', { nobr: true }); %>
<%- include('../_partial/footer.ejs'); %>
+51
View File
@@ -0,0 +1,51 @@
<%- include('../_partial/header.ejs', { title: 'Security Centre Login' }); %>
<%- include('../_components/frame-top.ejs'); %>
<%- include('../_components/content-start.ejs'); %>
<%- include('../_components/title-box.ejs', { title: 'Security Centre Login' }); %>
<%- include('../_components/content-body-start.ejs'); %>
<% if (success) { %>
<p class="green"><%= success %></p>
<% } else if (error) { %>
<p class="red"><%= error %></p>
<% } else { %>
<p>Enter your username & password.</p>
<% } %>
<form method="post">
<label for="username">Username:</label>
<input type="text" name="username">
<br>
<label for="password">Password:</label>
<input type="password" name="password">
<br><br>
<input type="submit" value="Login">
</form>
<%- include('../_components/content-body-end.ejs'); %>
<br>
<%- include('../_components/content-body-start.ejs'); %>
<p>Or have you forgotten your password?</p>
<form method="post" action="/account/forgot">
<label for="username">Username:</label>
<input type="text" name="username">
<br>Or<br>
<label for="password">Email:</label>
<input type="password" name="password">
<br><br>
<input type="submit" value="Send Password Reset">
</form>
<%- include('../_components/content-body-end.ejs'); %>
<br>
<%- include('../_components/content-end.ejs', { nobr: true }); %>
<%- include('../_partial/footer.ejs'); %>
+1 -1
View File
@@ -57,7 +57,7 @@
<td><input type="password" name="password2" value="" minlength="5" maxlength="20" size="20"></td>
</tr>
<tr>
<td align=right>Recovery email (optional):</td>
<td align=right>Recovery Email (optional):</td>
<td><input type="email" name="email" value=""></td>
</tr>
<tr>
+12
View File
@@ -0,0 +1,12 @@
<%- include('./_partial/header.ejs', { title: 'Guide' }); %>
<%- include('./_components/frame-top.ejs'); %>
<%- include('./_components/content-start.ejs'); %>
<%- include('./_components/title-box.ejs', { title: 'Guide' }); %>
<%- include('./_components/content-body-start.ejs'); %>
<p>This area is under construction - Coming Soon!</p>
<%- include('./_components/content-body-end.ejs'); %>
<br>
<%- include('./_components/content-end.ejs', { nobr: true }); %>
<%- include('./_partial/footer.ejs'); %>
-203
View File
@@ -1,203 +0,0 @@
<html><head>
<META HTTP-EQUIV="Expires" CONTENT="0">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<meta name="MSSmartTagsPreventParsing" content="TRUE">
<title>RuneScape - the massive online adventure game by Jagex Ltd</title>
<STYLE>
BODY, TD, P
{
font-family:Arial,Helvetica,sans-serif;
font-size:13px;
}
.b {border-style: outset; border-width:3pt; border-color:#373737}
.e {border:2px solid #382418}
.c {text-decoration:none}
A.c:hover {text-decoration:underline}
</STYLE>
</head>
<body bgcolor=black text="white" link=#90c040 alink=#90c040 vlink=#90c040 topmargin=0 leftmargin=0><table width=100% height=100% cellpadding=0 cellspacing=0><tr><td valign=middle><center>
<!--border top-->
<table cellpadding=0 cellspacing=0><tr>
<td valign=top><img src=/img/edge_a.jpg width=100 height=43 hspace=0 vspace=0></td>
<td valign=top><img src=/img/edge_c.jpg width=400 height=42 hspace=0 vspace=0></td>
<td valign=top><img src=/img/edge_d.jpg width=100 height=43 hspace=0 vspace=0></td>
</tr></table>
<!--end border top-->
<!-- content table-->
<table width=600 cellpadding=0 cellspacing=0 border=0 background=/img/background2.jpg><tr><td valign=bottom>
<center>
<br>
<table width=300 bgcolor=black cellpadding=4>
<tr><td class=e><center> <b>How To Play RuneScape</b><br> <a href="/title" class=c>Main menu</a> </center></td></tr>
</table>
<br>
<table>
<tr valign=top>
<td>
<table width=240 bgcolor=black cellpadding=4>
<tr><td class=e><center>
<table width=150 bgcolor=black cellpadding=0>
<tr><td>
<DIV STYLE="LEFT: 0px; TOP: 0px; POSITION: relative;">
<DIV STYLE="LEFT: 0px; TOP: 0px; POSITION: absolute;">
<a href='/about/getstart'><img src=/img/title/blank.gif width=150 height=35 border=0></a>
</div>
</div>
<table width=150 bgcolor=black cellpadding=2>
<tr><td class=b bgcolor=#474747 background=/img/stoneback.gif align=center>Getting Started</td></tr>
</table>
</td></tr></table>
<table>
<tr><td><a href=/about/getstart class=c><img src=/img/about/gettingstarted.jpg width=95 height=65 border=0></a></td>
<td>How to take the first step to playing RuneScape! <br><a href=/about/getstart class=c>Click here</a></td>
</tr>
</table>
</td></tr>
</table>
</td>
<td width=10></td>
<td>
<table width=240 bgcolor=black cellpadding=4>
<tr><td class=e><center>
<table width=150 bgcolor=black cellpadding=0>
<tr><td>
<DIV STYLE="LEFT: 0px; TOP: 0px; POSITION: relative;">
<DIV STYLE="LEFT: 0px; TOP: 0px; POSITION: absolute;">
<a href='/about'><img src=/img/title/blank.gif width=150 height=35 border=0></a>
</div>
</div>
<table width=150 bgcolor=black cellpadding=2>
<tr><td class=b bgcolor=#474747 background=/img/stoneback.gif align=center>About RuneScape</td></tr>
</table>
</td></tr></table>
<table>
<tr><td><a href=/about class=c><img src=/img/about/whatisrs.jpg width=95 height=65 border=0></a></td>
<td>What is RuneScape?<br>&nbsp;<br><a href=/about class=c>Click here</a></td>
</tr>
</table>
</td></tr>
</table>
</td>
</tr>
<tr><td colspan=3>&nbsp;</td></tr>
<tr valign=top><td>
<table width=240 bgcolor=black cellpadding=4>
<tr><td class=e><center>
<table width=150 bgcolor=black cellpadding=0>
<tr><td>
<DIV STYLE="LEFT: 0px; TOP: 0px; POSITION: relative;">
<DIV STYLE="LEFT: 0px; TOP: 0px; POSITION: absolute;">
<a href='/rs2/skills'><img src=/img/title/blank.gif width=150 height=35 border=0></a>
</div>
</div>
<table width=150 bgcolor=black cellpadding=2>
<tr><td class=b bgcolor=#474747 background=/img/stoneback.gif align=center>Skills</td></tr>
</table>
</td></tr></table>
<table>
<tr><td><a href=/rs2/skills class=c><img src=/img/manual/skills.jpg width=95 height=65 border=0></a></td>
<td>See what skills you can learn in the game.<br><a href=/rs2/skills class=c>Click here</a></td>
</tr>
</table>
</td></tr>
</table>
</td>
<td width=10></td><td>
<table width=240 bgcolor=black cellpadding=4>
<tr><td class=e><center>
<table width=150 bgcolor=black cellpadding=0>
<tr><td>
<DIV STYLE="LEFT: 0px; TOP: 0px; POSITION: relative;">
<DIV STYLE="LEFT: 0px; TOP: 0px; POSITION: absolute;">
<a href='/rs2/controls'><img src=/img/title/blank.gif width=150 height=35 border=0></a>
</div>
</div>
<table width=150 bgcolor=black cellpadding=2>
<tr><td class=b bgcolor=#474747 background=/img/stoneback.gif align=center>Controls</td></tr>
</table>
</td></tr></table>
<table>
<tr><td><a href=/rs2/controls class=c><img src=/img/manual/controls.jpg width=95 height=65 border=0></a></td>
<td>Learn how to move around the world.<br><a href=/rs2/controls class=c>Click here</a></td>
</tr>
</table>
</td></tr>
</table>
</td>
</tr>
<tr><td colspan=3>&nbsp;</td></tr>
<tr valign=top><td>
<table width=240 bgcolor=black cellpadding=4>
<tr><td class=e><center>
<table width=150 bgcolor=black cellpadding=0>
<tr><td>
<DIV STYLE="LEFT: 0px; TOP: 0px; POSITION: relative;">
<DIV STYLE="LEFT: 0px; TOP: 0px; POSITION: absolute;">
<a href='/worldmap'><img src=/img/title/blank.gif width=150 height=35 border=0></a>
</div>
</div>
<table width=150 bgcolor=black cellpadding=2>
<tr><td class=b bgcolor=#474747 background=/img/stoneback.gif align=center>World Map</td></tr>
</table>
</td></tr></table>
<table>
<tr><td><a href=/worldmap class=c><img src=/img/manual/worldmap.jpg width=95 height=65 border=0></a></td>
<td>A full map of the RuneScape World!<br><a href=/worldmap class=c>Click here</a></td>
</tr>
</table>
</td></tr>
</table>
</td>
<td width=10></td>
<td>
<table width=240 bgcolor=black cellpadding=4>
<tr><td class=e><center>
<table width=150 bgcolor=black cellpadding=0>
<tr><td>
<DIV STYLE="LEFT: 0px; TOP: 0px; POSITION: relative;">
<DIV STYLE="LEFT: 0px; TOP: 0px; POSITION: absolute;">
<a href='/rs2/quests'><img src=/img/title/blank.gif width=150 height=35 border=0></a>
</div>
</div>
<table width=150 bgcolor=black cellpadding=2>
<tr><td class=b bgcolor=#474747 background=/img/stoneback.gif align=center>Quests</td></tr>
</table>
</td></tr></table>
<table>
<tr><td><a href=/rs2/quests class=c><img src=/img/manual/quests.jpg width=95 height=65 border=0></a></td>
<td>View all the Quests in RuneScape!<br>&nbsp;<br><a href=/rs2/quests class=c>Click here</a></td>
</tr>
</table>
</td></tr>
</table>
</td>
</tr>
</table>
<!-- border footer-->
<table cellpadding=0 cellspacing=0><tr>
<td valign=bottom><img src=/img/edge_g2.jpg width=100 height=82 hspace=0 vspace=0></td>
<td valign=bottom>
<div align=center style="font-family:Arial,Helvetica,sans-serif; font-size:11px;">
This webpage and its contents is copyright 2004 Jagex Ltd<br>
To use our service you must agree to our
<a href="/terms" class=c>Terms+Conditions</a>
+
<a href="/privacy" class=c>Privacy policy</a>
</div>
<img src=/img/edge_c.jpg width=400 height=42 hspace=0 vspace=0></td>
<td valign=bottom><img src=/img/edge_h2.jpg width=100 height=82 hspace=0 vspace=0></td>
</tr></table>
<!-- border footer-->
</td></tr></table>
</center></td></tr></table></body></html>
+27 -1
View File
@@ -86,7 +86,7 @@ To view a full list of news and updates, click <a href="/news" class=c>here</a>.
</div>
</div>
<table width=100 height=45 bgcolor=black cellpadding=2><tr><td class=b bgcolor=#474747 background=/img/stoneback.gif><center><b>Hiscores Table</b></center></td></tr></table>
Is your character in the top 500,000?<br>
Is your character in the top 250,000?<br>
<a href='/hiscores' class=c>Click Here</a>
</td>
@@ -111,6 +111,32 @@ To view a full list of news and updates, click <a href="/news" class=c>here</a>.
<%- include('./_components/stones-border-top.ejs'); %>
<%- include('./_components/stones-content-start.ejs'); %>
<%- include('./_components/options-start.ejs'); %>
<td width=100 align=center><a href='/account'><div width=77 height=120 border=0></div></a></td>
<td width=120>
<DIV STYLE="LEFT: 0px; TOP: 0px; POSITION: relative;">
<DIV STYLE="LEFT: 0px; TOP: 0px; POSITION: absolute;">
<a href='/account'><img src=/img/title/blank.gif width=100 height=45 border=0></a>
</div>
</div>
<table width=100 height=45 bgcolor=black cellpadding=2><tr><td class=b bgcolor=#474747 background=/img/stoneback.gif><center><b>Security Centre</b></center></td></tr></table>
Manage your account security.<br>
<a href='/account' class=c>Click Here</a>
</td>
<td></td>
<td width=100 align=center><a href='/guide'><div width=77 height=120 border=0></div></a></td>
<td width=120>
<DIV STYLE="LEFT: 0px; TOP: 0px; POSITION: relative;">
<DIV STYLE="LEFT: 0px; TOP: 0px; POSITION: absolute;">
<a href='/guide'><img src=/img/title/blank.gif width=100 height=45 border=0></a>
</div>
</div>
<table width=100 height=45 bgcolor=black cellpadding=2><tr><td class=b bgcolor=#474747 background=/img/stoneback.gif><center><b>Guide</b></center></td></tr></table>
Answers to all your questions.<br>
<a href='/guide' class=c>Click Here</a>
</td>
<%- include('./_components/options-continue.ejs'); %>
<td width=100 align=center><a href='https://discord.lostcity.rs'><img src='/img/title/mm_discord.jpg' width=77 height=120 border=0></a></td>
<td width=120>
<DIV STYLE="LEFT: 0px; TOP: 0px; POSITION: relative;">