Add environment example, update .gitignore, and switch license to AGPL v3. Introduce error handling classes, ESLint config, and main modules for database, logging, certificate management, and utility functions. Add authentication routes, schemas, and service layer for a modular REST API. Update README and set up repository structure for further development.
43 lines
1.4 KiB
JavaScript
43 lines
1.4 KiB
JavaScript
const path = require("path")
|
|
const express = require("express")
|
|
const router = express.Router()
|
|
const { YggdrasilError } = require("../../errors/errors")
|
|
const rateLimit = require("express-rate-limit")
|
|
const authService = require("../../services/authService")
|
|
const Logger = require("../../modules/logger")
|
|
const logger = Logger.createLogger(path.join(__dirname, "..", ".."))
|
|
|
|
const limiter = rateLimit({
|
|
windowMs: 15 * 60 * 1000,
|
|
max: 20,
|
|
standardHeaders: true,
|
|
legacyHeaders: false,
|
|
handler: (req, res) => {
|
|
res.status(429).json({
|
|
error: "TooManyRequestsException",
|
|
errorMessage: "Too many login attempts, please try again later."
|
|
})
|
|
}
|
|
})
|
|
|
|
router.post("/", limiter, async (req, res) => {
|
|
const { username, password, clientToken, requestUser } = req.body
|
|
try {
|
|
const result = await authService.authenticate({
|
|
identifier: username,
|
|
password,
|
|
clientToken,
|
|
requireUser: requestUser || false
|
|
})
|
|
|
|
logger.log(`User authenticated: ${username}`, ["AUTH", "green"])
|
|
res.status(200).json(result.response)
|
|
} catch (err) {
|
|
if (err instanceof DefaultError) {
|
|
throw new YggdrasilError( err.code, err.error || "ForbiddenOperationException", err.message, "Invalid credentials")
|
|
}
|
|
throw err
|
|
}
|
|
})
|
|
|
|
module.exports = router |