-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework templates to allow writing emails
add rate-limiting to protect public sendmail route move route /stats to /admin/stats
- Loading branch information
Showing
21 changed files
with
619 additions
and
467 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Request, Response } from "express"; | ||
import sendmail from "../../../../utils/mails/send.js"; | ||
import { getUser } from "../../../../utils/locals.js"; | ||
import config from "../../../../utils/config.js"; | ||
import { BadRequestError } from "../../../../utils/errors.js"; | ||
|
||
/** | ||
* Send a test email | ||
* Exposes all possible logs from the emailer | ||
* This is a protected route and requires admin privileges | ||
*/ | ||
export default async function handleMailtest(req :Request, res :Response){ | ||
const {username :requester, email:to} = getUser(req); | ||
if(!to){ | ||
throw new BadRequestError("No email address found for user "+ requester); | ||
} | ||
let out = await sendmail({ | ||
to, | ||
subject: config.brand+" test email", | ||
html: [ | ||
`\t<h1>${config.brand} test email</h1>`, | ||
`\t<p>This is a test email sent from the admin panel of ${config.hostname}</p>`, | ||
].join("\n")}); | ||
|
||
res.status(200).send(out); | ||
} |
2 changes: 1 addition & 1 deletion
2
source/server/routes/api/v1/stats/index.ts → ...server/routes/api/v1/admin/stats/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
<h1>eCorpus - connection link</h1> | ||
<p>Hi <b>{{name}}</b>,</p> | ||
<p>Click the link below to connect automatically:</p> | ||
<p><a href="{{url}}">{{url}}</a></p> | ||
<p>This link stays valid for 30 days</p> | ||
<p>You might want to change your password in user settings to be able to reconnect in the future</p> | ||
<p>Have a great day!</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
<h1>eCorpus - lien de connexion</h1> | ||
<p>Bonjour <b>{{name}}</b>,</p> | ||
<p> | ||
Nous venons de recevoir une demande de lien de connexion pour un compte lié à votre adresse.<br> | ||
Si vous n'êtes pas à l'origine de cette demande, vous pouvez ignorer cet email. | ||
</p> | ||
<p> | ||
Sinon, vous pouvez vous connecter en cliquant sur le bouton ci-dessous. | ||
</p> | ||
<p align="center"> | ||
<a class="btn" href="{{url}}">Connectez-vous</a> | ||
</p> | ||
<p> | ||
Pour des raisons de sécurité, ce lien expirera dans 30 jours. | ||
</p> | ||
<p> | ||
Une fois connecté, n'oubliez pas de réinitialiser votre mot de passe! | ||
</p> | ||
<p> | ||
Pour rapporter toute erreur, vous pouvez nous contacter sur la <a href="https://github.com/Holusion/e-thesaurus">page du projet eThesaurus</a> | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<html lang="{{lang}}"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover"> | ||
|
||
<style> | ||
h1{ | ||
font-size:1.2rem; | ||
font-weight: bold; | ||
color: #00517d; | ||
} | ||
.btn { | ||
cursor: pointer; | ||
color: white !important; | ||
background-color: #0089bf; | ||
text-decoration: none; | ||
padding: 0.5rem 1rem; | ||
margin: 2rem 0.5rem; | ||
border-radius: 4px; | ||
} | ||
</style> | ||
|
||
</head> | ||
<body> | ||
{{{body}}} | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import express, { Express, NextFunction, Request, RequestHandler, Response } from "express"; | ||
import request from "supertest"; | ||
import { InternalError, UnauthorizedError } from "./errors.js"; | ||
import { either } from "./locals.js"; | ||
|
||
//Dummy middlewares | ||
function pass(req :Request, res :Response, next :NextFunction){ | ||
next(); | ||
} | ||
|
||
function fail(req :Request, res :Response, next :NextFunction){ | ||
next(new UnauthorizedError()); | ||
} | ||
|
||
function err(req :Request, res :Response, next :NextFunction){ | ||
next(new InternalError()); | ||
} | ||
|
||
function h(req:Request, res:Response){ | ||
res.status(204).send(); | ||
} | ||
|
||
describe("either() middleware", function(){ | ||
let app :Express; | ||
let handler :RequestHandler; | ||
this.beforeEach(function(){ | ||
app = express(); | ||
//small trick to allow error handling : | ||
process.nextTick(()=>{ | ||
app.use((err:Error, req :Request, res:Response, next :NextFunction)=>{ | ||
res.status((err as any).code ?? 500).send(err.message); | ||
}); | ||
}); | ||
}); | ||
|
||
it("checks each middleware for a pass", async function(){ | ||
app.get("/", either(fail, fail, pass), h); | ||
await request(app).get("/").expect(204); | ||
}); | ||
|
||
it("uses first middleware to pass", async function(){ | ||
app.get("/", either(pass, fail), h); | ||
await request(app).get("/").expect(204); | ||
}); | ||
|
||
it("doesn't allow errors other than UnauthoriezError", async function(){ | ||
app.get("/", either(fail, err), h); | ||
await request(app).get("/").expect(500); | ||
}); | ||
|
||
it("throws if no middleware passed", async function(){ | ||
app.get("/", either(fail, fail), h); | ||
await request(app).get("/").expect(401); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.