Compare commits
No commits in common. "8396d597f196ed26c364321d9914f3de2caf3727" and "730b8368cd8e5dd78697c523aec05158dded2a50" have entirely different histories.
8396d597f1
...
730b8368cd
|
@ -27,7 +27,6 @@ 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
|
||||||
|
|
||||||
|
|
|
@ -1,30 +1,24 @@
|
||||||
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",
|
|
||||||
});
|
});
|
|
@ -22,11 +22,9 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/express": "^4.17.21",
|
"@types/express": "^4.17.21",
|
||||||
"@types/express-list-endpoints": "^6.0.3",
|
"@types/express-list-endpoints": "^6.0.3",
|
||||||
"@types/jsonwebtoken": "^9.0.5",
|
|
||||||
"@types/mongoose": "^5.11.97",
|
"@types/mongoose": "^5.11.97",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"express-list-endpoints": "^6.0.0",
|
"express-list-endpoints": "^6.0.0",
|
||||||
"jsonwebtoken": "^9.0.2",
|
|
||||||
"mongoose": "^8.0.3"
|
"mongoose": "^8.0.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,51 +0,0 @@
|
||||||
import jwt from "jsonwebtoken";
|
|
||||||
import AuthService from "../services/AuthService";
|
|
||||||
import { Request, Response, NextFunction } from "express";
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
// Filter app from the apps by app and secret
|
|
||||||
const authenticated = await AuthService.find(app, secret);
|
|
||||||
|
|
||||||
if (authenticated) {
|
|
||||||
console.log("Authenticated app ", authenticated.app);
|
|
||||||
// Generate an access token
|
|
||||||
const accessToken = jwt.sign(
|
|
||||||
{ app: authenticated.app },
|
|
||||||
authTokenSecret,
|
|
||||||
{ expiresIn: "1h" }
|
|
||||||
);
|
|
||||||
|
|
||||||
res.json({
|
|
||||||
token: accessToken,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
res.status(403).send("Credentials incorrect");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
authorize(req: Request, res: Response, next: NextFunction) {
|
|
||||||
const authHeader = req.headers.authorization;
|
|
||||||
if (authHeader) {
|
|
||||||
const token = authHeader.split(" ")[1];
|
|
||||||
|
|
||||||
jwt.verify(token, authTokenSecret, (err, app) => {
|
|
||||||
if (err) {
|
|
||||||
return res.status(403).json("Invalid token provided");
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("Authorization provided for ", next.name, " to app ", app);
|
|
||||||
next();
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
res.status(401).json("No Authorization header provided");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default new AuthControler();
|
|
|
@ -2,7 +2,6 @@ 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();
|
||||||
|
|
||||||
|
@ -15,7 +14,6 @@ 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
|
||||||
|
|
||||||
|
|
|
@ -1,19 +0,0 @@
|
||||||
import mongoose, { Document } from "mongoose";
|
|
||||||
|
|
||||||
export interface Auth extends Document {
|
|
||||||
app: String,
|
|
||||||
secret: String
|
|
||||||
}
|
|
||||||
|
|
||||||
const AuthSchema = new mongoose.Schema({
|
|
||||||
app: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
secret: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
export default mongoose.model("authorizations", AuthSchema);
|
|
|
@ -1,10 +0,0 @@
|
||||||
import AuthModel, { Auth } from "../models/AuthModel";
|
|
||||||
|
|
||||||
class AuthService {
|
|
||||||
async find(app: String, secret: String): Promise<Auth | null> {
|
|
||||||
const auth = await AuthModel.findOne({ app: app, secret: secret }).exec();
|
|
||||||
return auth;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default new AuthService();
|
|
Loading…
Reference in New Issue