Skip to content
This repository was archived by the owner on Feb 20, 2022. It is now read-only.

Commit 843e4fe

Browse files
authored
Merge pull request #297 from oof2win2/feature/jwt-apikey
2 parents 25fec61 + 051e70e commit 843e4fe

File tree

15 files changed

+618
-122
lines changed

15 files changed

+618
-122
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
MONGOURI=MongoDB URI
22
DISCORD_BOTTOKEN=Discord bot token
3+
CLIENTID=Discord bot Client ID (User ID of the bot)
34
SENTRY_LINK=Sentry.io link
45
API_PORT=Port to run the API at
56
PROMETHEUS_PORT=Port to run the Prometheus server at
7+
JWT_SECRET=Your JWT secret

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
config.ts
44
dist/
55
*.code-workspace
6+
masterapikey.txt
67

78
# Logs
89
logs

package-lock.json

Lines changed: 67 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"discord.js": "^13.6.0",
2929
"dotenv": "^14.1.0",
3030
"envalid": "^7.2.2",
31-
"fagc-api-types": "^1.6.1",
31+
"fagc-api-types": "^1.7.0",
3232
"fastify": "^3.24.0",
3333
"fastify-autoload": "^3.10.0",
3434
"fastify-cors": "^6.0.2",
@@ -40,6 +40,7 @@
4040
"fastify-request-context": "^2.2.0",
4141
"fastify-swagger": "^4.13.1",
4242
"fastify-websocket": "^4.0.0",
43+
"jose": "^4.3.8",
4344
"mongoose": "^6.1.6",
4445
"mongoose-to-swagger": "^1.3.0",
4546
"openapi3-ts": "^2.0.1",
@@ -61,6 +62,7 @@
6162
"devDependencies": {
6263
"@types/debug": "^4.1.7",
6364
"@types/eslint": "^8.2.2",
65+
"@types/faker": "^5.5.9",
6466
"@types/fastify-rate-limit": "^2.1.0",
6567
"@types/jest": "^27.4.0",
6668
"@types/mongoose": "^5.11.97",
@@ -71,10 +73,12 @@
7173
"@types/ws": "^8.2.2",
7274
"@typescript-eslint/eslint-plugin": "^5.9.1",
7375
"@typescript-eslint/parser": "^5.9.1",
76+
"faker": "^5.5.3",
7477
"inversify": "^6.0.1",
7578
"jest": "^27.4.7",
7679
"jest-mock-extended": "^2.0.4",
7780
"lint-staged": "^12.1.7",
81+
"mockingoose": "^2.15.2",
7882
"nodemon": "^2.0.12",
7983
"ts-jest": "^27.1.3",
8084
"ts-node": "^10.4.0",

src/database/authentication.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/database/community.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export class CommunityClass {
2727

2828
@prop({ type: [ String ] })
2929
guildIds!: string[]
30+
31+
@prop({ type: Date, default: new Date(0) })
32+
tokenInvalidBefore!: Date
3033
}
3134

3235
const CommunityModel = getModelForClass(CommunityClass)

src/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import ENV from "./utils/env"
22
import mongoose from "mongoose"
33
import fastify from "./app"
44
import { client } from "./utils/discord"
5+
import CommunityModel from "./database/community"
6+
import { createApikey } from "./utils/authentication"
7+
import fs from "fs/promises"
58

69
mongoose.connect(ENV.MONGOURI, {
710
ignoreUndefined: true,
@@ -17,6 +20,22 @@ const start = async () => {
1720
console.log(`Server listening on :${port}`)
1821

1922
client.login(ENV.DISCORD_BOTTOKEN)
23+
24+
const communityCount = await CommunityModel.countDocuments()
25+
if (communityCount == 0) {
26+
// need to create the first community as none exist, so that the api can be used
27+
const community = await CommunityModel.create({
28+
name: "FAGC Master Community",
29+
contact: client.user?.id
30+
})
31+
const apikey = await createApikey(community, "master")
32+
try {
33+
await fs.writeFile("./masterapikey.txt", apikey, { flag: "w", mode: process.platform === "win32" ? undefined : 0o600 })
34+
console.log(`Created first community ${community.id} with apikey written to ./masterapikey.txt, which has master API access. Be careful with sharing it.`)
35+
} catch (e) {
36+
console.log(`Failed to write master apikey for community ${community.id} to ./masterapikey.txt. It is: ||${apikey}|| (remove ||). Be careful with sharing it.`)
37+
}
38+
}
2039
} catch (err) {
2140
console.error(err)
2241
process.exit(1)

0 commit comments

Comments
 (0)