Add admin page for showing access tokens

This commit is contained in:
Aelita4 2023-11-05 21:23:46 +01:00
parent 0acc5d797c
commit 4feba517aa
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
3 changed files with 65 additions and 1 deletions

View File

@ -23,4 +23,21 @@ export const getAccessToken = async (accessToken: string) => {
{ username },
{ entropy }
] }) as Promise<AccessToken | null>;
}
export const getAllAccessTokens = async () => {
const master: AccessToken = {
type: "X",
username: "0",
entropy: "b70972be3921e04a8d0c442f64c2c19a4ac01e886e8e5649916f9b88870fa6dd",
createdAt: new Date(1698851542427),
expiresAt: null,
createdFrom: null
}
const accessTokens = await AccessTokens();
const arrayOfTokens = await accessTokens.find({}).toArray() as AccessToken[];
let arr = [master].concat(arrayOfTokens);
// return accessTokens.find({}).toArray() as Promise<AccessToken[]>;
return arr;
}

View File

@ -0,0 +1,47 @@
---
import Layout from "../../layouts/Layout.astro";
import { getAllAccessTokens } from "../../lib/accessTokens";
const tokens = await getAllAccessTokens();
const type = {
"A": "Authorization",
"X": "Master Key"
}
---
<Layout title="Access tokens">
<h1>Access tokens</h1>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>Type</th>
<th>Username</th>
<th>Entropy hash</th>
<th>Created</th>
<th>Expires</th>
<th>Created from</th>
<th>Validity</th>
</tr>
</thead>
<tbody>
{tokens.map(token => <tr>
<td>{token._id}</td>
<td>{type[token.type] ?? `Other`}</td>
<td>{token.username}</td>
<td>{token.entropy}</td>
<td>{token.createdAt.getTime()}</td>
<td>{token.expiresAt?.getTime() ?? "null"}</td>
<td>{token.createdFrom}</td>
<td>{token.expiresAt !== null && Date.now() > token.expiresAt.getTime() ? <span style="color: red;">Expired {Math.floor((Date.now() - token.expiresAt.getTime()) / 1000 / 60)} minutes ago</span> : Date.now() < token.createdAt.getTime() ? <span style="color: cyan;">TimeTravelException (check in {Math.floor((token.createdAt.getTime() - Date.now()) / 1000 / 60)} minutes)</span> : <span style="color: lime;">Valid for{token.expiresAt === null ? "ever" : ` ${Math.floor((token.expiresAt.getTime() - Date.now()) / 1000 / 60)} minutes`}</span>}</td>
</tr>)}
</tbody>
</table>
</Layout>
<style>
* {
color: white;
}
</style>

View File

@ -6,6 +6,6 @@ export default interface AccessToken {
username: string;
entropy: string;
createdAt: Date;
expiresAt: Date;
expiresAt: Date | null;
createdFrom: null | 'loginForm';
}