diff --git a/compose.yaml b/compose.yaml index 68d1912..ecec3f3 100644 --- a/compose.yaml +++ b/compose.yaml @@ -27,6 +27,7 @@ services: MONGODB_URI: "mongodb://mongodb:27017/bot" MONGODB_USER: "root" MONGODB_PASS: "password" + JWTSECRET: "cooljwtsecret" volumes: - ./:/usr/src/app:ro diff --git a/mongo-init.js b/mongo-init.js index a7b191b..67b53c9 100644 --- a/mongo-init.js +++ b/mongo-init.js @@ -1,24 +1,30 @@ db.createUser({ - user: 'root', - pwd: 'password', - roles: [ - { - role: 'readWrite', - db: 'admin', - }, - { - role: 'readWrite', - db: 'bot', - }, - ], + user: "root", + pwd: "password", + roles: [ + { + role: "readWrite", + db: "admin", + }, + { + role: "readWrite", + db: "bot", + }, + ], }); db = new Mongo().getDB("bot"); -db.images.createIndex({ "status": 1 }); -db.images.createIndex({ "url": 1 }, { "unique": true }); +db.images.createIndex({ status: 1 }); +db.images.createIndex({ url: 1 }, { unique: true }); db.images.insert({ - url: "https://example.com", - status: "consumed", - tags: ["2girls", "sleeping"] -}); \ No newline at end of file + url: "https://example.com", + status: "consumed", + tags: ["2girls", "sleeping"], +}); + +db.authorizations.createIndex({ app: 1 }); +db.authorizations.insert({ + app: "tester", + secret: "test", +}); diff --git a/src/controllers/AuthControler.ts b/src/controllers/AuthControler.ts index e983020..072638e 100644 --- a/src/controllers/AuthControler.ts +++ b/src/controllers/AuthControler.ts @@ -2,9 +2,9 @@ import jwt from "jsonwebtoken"; import AuthService from "../services/AuthService"; import { Request, Response, NextFunction } from "express"; -class AuthControler { - authTokenSecret = process.env.JWTSECRET || "badsecret"; +const authTokenSecret = process.env.JWTSECRET || "badsecret"; +class AuthControler { async login(req: Request, res: Response) { // Read app and secret from request body const { app, secret } = req.body; @@ -17,12 +17,12 @@ class AuthControler { // Generate an access token const accessToken = jwt.sign( { app: authenticated.app }, - this.authTokenSecret, + authTokenSecret, { expiresIn: "1h" } ); res.json({ - accessToken, + token: accessToken, }); } else { res.status(403).send("Credentials incorrect"); @@ -34,7 +34,7 @@ class AuthControler { if (authHeader) { const token = authHeader.split(" ")[1]; - jwt.verify(token, this.authTokenSecret, (err, app) => { + jwt.verify(token, authTokenSecret, (err, app) => { if (err) { return res.status(403).json("Invalid token provided"); } diff --git a/src/index.ts b/src/index.ts index 9dff064..d48f3fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ import express from "express"; import mongoose from "mongoose"; import listEndpoints from "express-list-endpoints"; import imageController from "./controllers/ImageController"; +import authControler from "./controllers/AuthControler"; export const app = express(); @@ -14,6 +15,7 @@ app.get("/", (_, res) => { app.get("/images", imageController.getAllImages); app.post("/images", imageController.addImage); +app.post("/login", authControler.login) // Set the default port to 8080, or use the PORT environment variable diff --git a/src/models/AuthModel.ts b/src/models/AuthModel.ts index 52dc172..394a64b 100644 --- a/src/models/AuthModel.ts +++ b/src/models/AuthModel.ts @@ -16,4 +16,4 @@ const AuthSchema = new mongoose.Schema({ }, }); -export default mongoose.model("auth", AuthSchema); +export default mongoose.model("authorizations", AuthSchema); diff --git a/src/services/AuthService.ts b/src/services/AuthService.ts index 1b4bb62..aed8d12 100644 --- a/src/services/AuthService.ts +++ b/src/services/AuthService.ts @@ -2,7 +2,8 @@ import AuthModel, { Auth } from "../models/AuthModel"; class AuthService { async find(app: String, secret: String): Promise { - return await AuthModel.findOne(app, secret); + const auth = await AuthModel.findOne({ app: app, secret: secret }).exec(); + return auth; } }