auth #6

Merged
bizcochito merged 6 commits from auth into develop 2023-12-27 19:34:32 +00:00
6 changed files with 35 additions and 25 deletions
Showing only changes of commit 8396d597f1 - Show all commits

View File

@ -27,6 +27,7 @@ services:
MONGODB_URI: "mongodb://mongodb:27017/bot" MONGODB_URI: "mongodb://mongodb:27017/bot"
MONGODB_USER: "root" MONGODB_USER: "root"
MONGODB_PASS: "password" MONGODB_PASS: "password"
JWTSECRET: "cooljwtsecret"
volumes: volumes:
- ./:/usr/src/app:ro - ./:/usr/src/app:ro

View File

@ -1,24 +1,30 @@
db.createUser({ db.createUser({
user: 'root', user: "root",
pwd: 'password', pwd: "password",
roles: [ roles: [
{ {
role: 'readWrite', role: "readWrite",
db: 'admin', db: "admin",
}, },
{ {
role: 'readWrite', role: "readWrite",
db: 'bot', db: "bot",
}, },
], ],
}); });
db = new Mongo().getDB("bot"); db = new Mongo().getDB("bot");
db.images.createIndex({ "status": 1 }); db.images.createIndex({ status: 1 });
db.images.createIndex({ "url": 1 }, { "unique": true }); db.images.createIndex({ url: 1 }, { unique: true });
db.images.insert({ db.images.insert({
url: "https://example.com", url: "https://example.com",
status: "consumed", status: "consumed",
tags: ["2girls", "sleeping"] tags: ["2girls", "sleeping"],
}); });
db.authorizations.createIndex({ app: 1 });
db.authorizations.insert({
app: "tester",
secret: "test",
});

View File

@ -2,9 +2,9 @@ import jwt from "jsonwebtoken";
import AuthService from "../services/AuthService"; import AuthService from "../services/AuthService";
import { Request, Response, NextFunction } from "express"; import { Request, Response, NextFunction } from "express";
class AuthControler { const authTokenSecret = process.env.JWTSECRET || "badsecret";
authTokenSecret = process.env.JWTSECRET || "badsecret";
class AuthControler {
async login(req: Request, res: Response) { async login(req: Request, res: Response) {
// Read app and secret from request body // Read app and secret from request body
const { app, secret } = req.body; const { app, secret } = req.body;
@ -17,12 +17,12 @@ class AuthControler {
// Generate an access token // Generate an access token
const accessToken = jwt.sign( const accessToken = jwt.sign(
{ app: authenticated.app }, { app: authenticated.app },
this.authTokenSecret, authTokenSecret,
{ expiresIn: "1h" } { expiresIn: "1h" }
); );
res.json({ res.json({
accessToken, token: accessToken,
}); });
} else { } else {
res.status(403).send("Credentials incorrect"); res.status(403).send("Credentials incorrect");
@ -34,7 +34,7 @@ class AuthControler {
if (authHeader) { if (authHeader) {
const token = authHeader.split(" ")[1]; const token = authHeader.split(" ")[1];
jwt.verify(token, this.authTokenSecret, (err, app) => { jwt.verify(token, authTokenSecret, (err, app) => {
if (err) { if (err) {
return res.status(403).json("Invalid token provided"); return res.status(403).json("Invalid token provided");
} }

View File

@ -2,6 +2,7 @@ import express from "express";
import mongoose from "mongoose"; import mongoose from "mongoose";
import listEndpoints from "express-list-endpoints"; import listEndpoints from "express-list-endpoints";
import imageController from "./controllers/ImageController"; import imageController from "./controllers/ImageController";
import authControler from "./controllers/AuthControler";
export const app = express(); export const app = express();
@ -14,6 +15,7 @@ app.get("/", (_, res) => {
app.get("/images", imageController.getAllImages); app.get("/images", imageController.getAllImages);
app.post("/images", imageController.addImage); app.post("/images", imageController.addImage);
app.post("/login", authControler.login)
// Set the default port to 8080, or use the PORT environment variable // Set the default port to 8080, or use the PORT environment variable

View File

@ -16,4 +16,4 @@ const AuthSchema = new mongoose.Schema({
}, },
}); });
export default mongoose.model("auth", AuthSchema); export default mongoose.model("authorizations", AuthSchema);

View File

@ -2,7 +2,8 @@ import AuthModel, { Auth } from "../models/AuthModel";
class AuthService { class AuthService {
async find(app: String, secret: String): Promise<Auth | null> { async find(app: String, secret: String): Promise<Auth | null> {
return await AuthModel.findOne(app, secret); const auth = await AuthModel.findOne({ app: app, secret: secret }).exec();
return auth;
} }
} }