From 1ab09b71d423a6a4067b436f32bd590ab773784a Mon Sep 17 00:00:00 2001 From: Clement Michaud Date: Tue, 10 Oct 2017 00:07:12 +0200 Subject: [PATCH] Specify the sender email in Gmail and Smtp notifier configuration Sender email address can now be specified in configuration and applies to GMail notifier and SMTP notifier. --- config.template.yml | 2 ++ config.test.yml | 3 +- server/src/lib/IdentityCheckMiddleware.ts | 2 +- .../src/lib/configuration/Configuration.d.ts | 2 ++ .../lib/notifiers/AbstractEmailNotifier.ts | 7 ++--- .../src/lib/notifiers/FileSystemNotifier.ts | 6 ++-- server/src/lib/notifiers/GMailNotifier.ts | 8 +++-- server/src/lib/notifiers/INotifier.d.ts | 7 ----- server/src/lib/notifiers/INotifier.ts | 5 ++++ server/src/lib/notifiers/SmtpNotifier.ts | 8 +++-- server/test/ServerConfiguration.test.ts | 3 +- .../ConfigurationAdapter.test.ts | 9 ++++-- .../LdapConfigurationAdaptation.test.ts | 3 +- server/test/notifiers/GMailNotifier.test.ts | 29 +++++++------------ .../test/notifiers/MailSenderBuilder.test.ts | 6 ++-- server/test/notifiers/NotifierFactory.test.ts | 6 ++-- server/test/server/PrivatePages.ts | 3 +- server/test/server/PublicPages.ts | 3 +- 18 files changed, 60 insertions(+), 52 deletions(-) delete mode 100644 server/src/lib/notifiers/INotifier.d.ts create mode 100644 server/src/lib/notifiers/INotifier.ts diff --git a/config.template.yml b/config.template.yml index 810f0133a..8bef04b69 100644 --- a/config.template.yml +++ b/config.template.yml @@ -204,6 +204,7 @@ notifier: # gmail: # username: user@example.com # password: yourpassword + # sender: admin@example.com # Use a SMTP server for sending notifications smtp: @@ -212,3 +213,4 @@ notifier: secure: false host: 'smtp' port: 1025 + sender: admin@example.com \ No newline at end of file diff --git a/config.test.yml b/config.test.yml index 7b9d1e1ff..034d12ad0 100644 --- a/config.test.yml +++ b/config.test.yml @@ -166,6 +166,7 @@ notifier: # gmail: # username: user@example.com # password: yourpassword + # sender: admin@example.com # Use a SMTP server for sending notifications smtp: @@ -174,4 +175,4 @@ notifier: secure: false host: 'smtp' port: 1025 - + sender: admin@example.com diff --git a/server/src/lib/IdentityCheckMiddleware.ts b/server/src/lib/IdentityCheckMiddleware.ts index ffe9292f5..0625c13df 100644 --- a/server/src/lib/IdentityCheckMiddleware.ts +++ b/server/src/lib/IdentityCheckMiddleware.ts @@ -121,7 +121,7 @@ export function get_start_validation(handler: IdentityValidable, postValidationE const host = req.get("Host"); const link_url = util.format("https://%s%s?identity_token=%s", host, postValidationEndpoint, token); logger.info(req, "Notification sent to user \"%s\"", identity.userid); - return notifier.notify(identity, handler.mailSubject(), link_url); + return notifier.notify(identity.email, handler.mailSubject(), link_url); }) .then(function () { handler.preValidationResponse(req, res); diff --git a/server/src/lib/configuration/Configuration.d.ts b/server/src/lib/configuration/Configuration.d.ts index 8faa487d5..b06aecd1f 100644 --- a/server/src/lib/configuration/Configuration.d.ts +++ b/server/src/lib/configuration/Configuration.d.ts @@ -69,6 +69,7 @@ interface SessionCookieConfiguration { export interface GmailNotifierConfiguration { username: string; password: string; + sender: string; } export interface SmtpNotifierConfiguration { @@ -77,6 +78,7 @@ export interface SmtpNotifierConfiguration { host: string; port: number; secure: boolean; + sender: string; } export interface FileSystemNotifierConfiguration { diff --git a/server/src/lib/notifiers/AbstractEmailNotifier.ts b/server/src/lib/notifiers/AbstractEmailNotifier.ts index 2a925fd88..198e4e5db 100644 --- a/server/src/lib/notifiers/AbstractEmailNotifier.ts +++ b/server/src/lib/notifiers/AbstractEmailNotifier.ts @@ -10,15 +10,14 @@ import BluebirdPromise = require("bluebird"); const email_template = Fs.readFileSync(Path.join(__dirname, "../../resources/email-template.ejs"), "UTF-8"); export abstract class AbstractEmailNotifier implements INotifier { - - notify(identity: Identity, subject: string, link: string): BluebirdPromise { + notify(to: string, subject: string, link: string): BluebirdPromise { const d = { url: link, button_title: "Continue", title: subject }; - return this.sendEmail(identity.email, subject, Ejs.render(email_template, d)); + return this.sendEmail(to, subject, Ejs.render(email_template, d)); } - abstract sendEmail(email: string, subject: string, content: string): BluebirdPromise; + abstract sendEmail(to: string, subject: string, content: string): BluebirdPromise; } \ No newline at end of file diff --git a/server/src/lib/notifiers/FileSystemNotifier.ts b/server/src/lib/notifiers/FileSystemNotifier.ts index efa9c6229..c5a9f30cd 100644 --- a/server/src/lib/notifiers/FileSystemNotifier.ts +++ b/server/src/lib/notifiers/FileSystemNotifier.ts @@ -13,9 +13,9 @@ export class FileSystemNotifier implements INotifier { this.filename = options.filename; } - notify(identity: Identity, subject: string, link: string): BluebirdPromise { - const content = util.format("Date: %s\nUser: %s\nSubject: %s\nLink: %s", new Date().toString(), identity.userid, - subject, link); + notify(to: string, subject: string, link: string): BluebirdPromise { + const content = util.format("Date: %s\nEmail: %s\nSubject: %s\nLink: %s", + new Date().toString(), to, subject, link); const writeFilePromised: any = BluebirdPromise.promisify(Fs.writeFile); return writeFilePromised(this.filename, content); } diff --git a/server/src/lib/notifiers/GMailNotifier.ts b/server/src/lib/notifiers/GMailNotifier.ts index 16aeb4171..caa81581c 100644 --- a/server/src/lib/notifiers/GMailNotifier.ts +++ b/server/src/lib/notifiers/GMailNotifier.ts @@ -7,16 +7,18 @@ import { IMailSender } from "./IMailSender"; export class GMailNotifier extends AbstractEmailNotifier { private mailSender: IMailSender; + private sender: string; constructor(options: GmailNotifierConfiguration, mailSender: IMailSender) { super(); this.mailSender = mailSender; + this.sender = options.sender; } - sendEmail(email: string, subject: string, content: string) { + sendEmail(to: string, subject: string, content: string) { const mailOptions = { - from: "authelia@authelia.com", - to: email, + from: this.sender, + to: to, subject: subject, html: content }; diff --git a/server/src/lib/notifiers/INotifier.d.ts b/server/src/lib/notifiers/INotifier.d.ts deleted file mode 100644 index 63f26097d..000000000 --- a/server/src/lib/notifiers/INotifier.d.ts +++ /dev/null @@ -1,7 +0,0 @@ - -import * as BluebirdPromise from "bluebird"; -import { Identity } from "../../../types/Identity"; - -export interface INotifier { - notify(identity: Identity, subject: string, link: string): BluebirdPromise; -} \ No newline at end of file diff --git a/server/src/lib/notifiers/INotifier.ts b/server/src/lib/notifiers/INotifier.ts new file mode 100644 index 000000000..b9a6b138c --- /dev/null +++ b/server/src/lib/notifiers/INotifier.ts @@ -0,0 +1,5 @@ +import * as BluebirdPromise from "bluebird"; + +export interface INotifier { + notify(to: string, subject: string, link: string): BluebirdPromise; +} \ No newline at end of file diff --git a/server/src/lib/notifiers/SmtpNotifier.ts b/server/src/lib/notifiers/SmtpNotifier.ts index 5ed86621d..cda174c98 100644 --- a/server/src/lib/notifiers/SmtpNotifier.ts +++ b/server/src/lib/notifiers/SmtpNotifier.ts @@ -8,17 +8,19 @@ import { SmtpNotifierConfiguration } from "../configuration/Configuration"; export class SmtpNotifier extends AbstractEmailNotifier { private mailSender: IMailSender; + private sender: string; constructor(options: SmtpNotifierConfiguration, mailSender: IMailSender) { super(); this.mailSender = mailSender; + this.sender = options.sender; } - sendEmail(email: string, subject: string, content: string) { + sendEmail(to: string, subject: string, content: string) { const mailOptions = { - from: "authelia@authelia.com", - to: email, + from: this.sender, + to: to, subject: subject, html: content }; diff --git a/server/test/ServerConfiguration.test.ts b/server/test/ServerConfiguration.test.ts index 3f3002732..e33277655 100644 --- a/server/test/ServerConfiguration.test.ts +++ b/server/test/ServerConfiguration.test.ts @@ -55,7 +55,8 @@ describe("test server configuration", function () { notifier: { gmail: { username: "user@example.com", - password: "password" + password: "password", + sender: "test@authelia.com" } }, regulation: { diff --git a/server/test/configuration/ConfigurationAdapter.test.ts b/server/test/configuration/ConfigurationAdapter.test.ts index 62f6f6e39..5a2f5299a 100644 --- a/server/test/configuration/ConfigurationAdapter.test.ts +++ b/server/test/configuration/ConfigurationAdapter.test.ts @@ -36,7 +36,8 @@ describe("test config adapter", function () { notifier: { gmail: { username: "user", - password: "password" + password: "password", + sender: "admin@example.com" } } }; @@ -84,14 +85,16 @@ describe("test config adapter", function () { yaml_config.notifier = { gmail: { username: "user", - password: "pass" + password: "pass", + sender: "admin@example.com" } }; const config = ConfigurationAdapter.adapt(yaml_config); Assert.deepEqual(config.notifier, { gmail: { username: "user", - password: "pass" + password: "pass", + sender: "admin@example.com" } }); }); diff --git a/server/test/configuration/LdapConfigurationAdaptation.test.ts b/server/test/configuration/LdapConfigurationAdaptation.test.ts index 63c0a8bdd..79d9ae24e 100644 --- a/server/test/configuration/LdapConfigurationAdaptation.test.ts +++ b/server/test/configuration/LdapConfigurationAdaptation.test.ts @@ -33,7 +33,8 @@ describe("test ldap configuration adaptation", function () { notifier: { gmail: { username: "user", - password: "password" + password: "password", + sender: "admin@example.com" } } }; diff --git a/server/test/notifiers/GMailNotifier.test.ts b/server/test/notifiers/GMailNotifier.test.ts index 7efba4446..8d598d5da 100644 --- a/server/test/notifiers/GMailNotifier.test.ts +++ b/server/test/notifiers/GMailNotifier.test.ts @@ -1,5 +1,5 @@ import * as sinon from "sinon"; -import * as assert from "assert"; +import * as Assert from "assert"; import BluebirdPromise = require("bluebird"); import { MailSenderStub } from "../mocks/notifiers/MailSenderStub"; @@ -11,24 +11,19 @@ describe("test gmail notifier", function () { const mailSender = new MailSenderStub(); const options = { username: "user_gmail", - password: "pass_gmail" + password: "pass_gmail", + sender: "admin@example.com" }; mailSender.sendStub.returns(BluebirdPromise.resolve()); const sender = new GMailNotifier.GMailNotifier(options, mailSender); const subject = "subject"; - - const identity = { - userid: "user", - email: "user@example.com" - }; - const url = "http://test.com"; - return sender.notify(identity, subject, url) + return sender.notify("user@example.com", subject, url) .then(function () { - assert.equal(mailSender.sendStub.getCall(0).args[0].to, "user@example.com"); - assert.equal(mailSender.sendStub.getCall(0).args[0].subject, "subject"); + Assert.equal(mailSender.sendStub.getCall(0).args[0].to, "user@example.com"); + Assert.equal(mailSender.sendStub.getCall(0).args[0].subject, "subject"); return BluebirdPromise.resolve(); }); }); @@ -37,24 +32,20 @@ describe("test gmail notifier", function () { const mailSender = new MailSenderStub(); const options = { username: "user_gmail", - password: "pass_gmail" + password: "pass_gmail", + sender: "admin@example.com" }; mailSender.sendStub.returns(BluebirdPromise.reject(new Error("Failed to send mail"))); const sender = new GMailNotifier.GMailNotifier(options, mailSender); const subject = "subject"; - - const identity = { - userid: "user", - email: "user@example.com" - }; - const url = "http://test.com"; - return sender.notify(identity, subject, url) + return sender.notify("user@example.com", subject, url) .then(function () { return BluebirdPromise.reject(new Error()); }, function() { + Assert.equal(mailSender.sendStub.getCall(0).args[0].from, "admin@example.com"); return BluebirdPromise.resolve(); }); }); diff --git a/server/test/notifiers/MailSenderBuilder.test.ts b/server/test/notifiers/MailSenderBuilder.test.ts index 9cb088804..f0d769116 100644 --- a/server/test/notifiers/MailSenderBuilder.test.ts +++ b/server/test/notifiers/MailSenderBuilder.test.ts @@ -18,7 +18,8 @@ describe("test MailSenderBuilder", function() { const mailSenderBuilder = new MailSenderBuilder(Nodemailer); mailSenderBuilder.buildGmail({ username: "user_gmail", - password: "pass_gmail" + password: "pass_gmail", + sender: "admin@example.com" }); Assert.equal(createTransportStub.getCall(0).args[0].auth.user, "user_gmail"); Assert.equal(createTransportStub.getCall(0).args[0].auth.pass, "pass_gmail"); @@ -31,7 +32,8 @@ describe("test MailSenderBuilder", function() { password: "password", port: 25, secure: true, - username: "user" + username: "user", + sender: "admin@example.com" }); Assert.deepStrictEqual(createTransportStub.getCall(0).args[0], { host: "mail.example.com", diff --git a/server/test/notifiers/NotifierFactory.test.ts b/server/test/notifiers/NotifierFactory.test.ts index 5763f7038..6dd25d4ac 100644 --- a/server/test/notifiers/NotifierFactory.test.ts +++ b/server/test/notifiers/NotifierFactory.test.ts @@ -15,7 +15,8 @@ describe("test notifier factory", function () { const options = { gmail: { username: "abc", - password: "password" + password: "password", + sender: "admin@example.com" } }; mailSenderBuilderStub = new MailSenderBuilderStub(); @@ -29,7 +30,8 @@ describe("test notifier factory", function () { password: "pass", secure: true, host: "localhost", - port: 25 + port: 25, + sender: "admin@example.com" } }; diff --git a/server/test/server/PrivatePages.ts b/server/test/server/PrivatePages.ts index 09bdb383b..dee301190 100644 --- a/server/test/server/PrivatePages.ts +++ b/server/test/server/PrivatePages.ts @@ -54,7 +54,8 @@ describe("Private pages of the server must not be accessible without session", f notifier: { gmail: { username: "user@example.com", - password: "password" + password: "password", + sender: "admin@example.com" } } }; diff --git a/server/test/server/PublicPages.ts b/server/test/server/PublicPages.ts index aef0bf6b0..21f099176 100644 --- a/server/test/server/PublicPages.ts +++ b/server/test/server/PublicPages.ts @@ -54,7 +54,8 @@ describe("Public pages of the server must be accessible without session", functi notifier: { gmail: { username: "user@example.com", - password: "password" + password: "password", + sender: "admin@example.com" } } };