1
0
mirror of https://github.com/0rangebananaspy/authelia.git synced 2024-09-14 22:47:21 +07:00

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.
This commit is contained in:
Clement Michaud 2017-10-10 00:07:12 +02:00
parent d5035b8704
commit 1ab09b71d4
18 changed files with 60 additions and 52 deletions

View File

@ -204,6 +204,7 @@ notifier:
# gmail: # gmail:
# username: user@example.com # username: user@example.com
# password: yourpassword # password: yourpassword
# sender: admin@example.com
# Use a SMTP server for sending notifications # Use a SMTP server for sending notifications
smtp: smtp:
@ -212,3 +213,4 @@ notifier:
secure: false secure: false
host: 'smtp' host: 'smtp'
port: 1025 port: 1025
sender: admin@example.com

View File

@ -166,6 +166,7 @@ notifier:
# gmail: # gmail:
# username: user@example.com # username: user@example.com
# password: yourpassword # password: yourpassword
# sender: admin@example.com
# Use a SMTP server for sending notifications # Use a SMTP server for sending notifications
smtp: smtp:
@ -174,4 +175,4 @@ notifier:
secure: false secure: false
host: 'smtp' host: 'smtp'
port: 1025 port: 1025
sender: admin@example.com

View File

@ -121,7 +121,7 @@ export function get_start_validation(handler: IdentityValidable, postValidationE
const host = req.get("Host"); const host = req.get("Host");
const link_url = util.format("https://%s%s?identity_token=%s", host, postValidationEndpoint, token); const link_url = util.format("https://%s%s?identity_token=%s", host, postValidationEndpoint, token);
logger.info(req, "Notification sent to user \"%s\"", identity.userid); 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 () { .then(function () {
handler.preValidationResponse(req, res); handler.preValidationResponse(req, res);

View File

@ -69,6 +69,7 @@ interface SessionCookieConfiguration {
export interface GmailNotifierConfiguration { export interface GmailNotifierConfiguration {
username: string; username: string;
password: string; password: string;
sender: string;
} }
export interface SmtpNotifierConfiguration { export interface SmtpNotifierConfiguration {
@ -77,6 +78,7 @@ export interface SmtpNotifierConfiguration {
host: string; host: string;
port: number; port: number;
secure: boolean; secure: boolean;
sender: string;
} }
export interface FileSystemNotifierConfiguration { export interface FileSystemNotifierConfiguration {

View File

@ -10,15 +10,14 @@ import BluebirdPromise = require("bluebird");
const email_template = Fs.readFileSync(Path.join(__dirname, "../../resources/email-template.ejs"), "UTF-8"); const email_template = Fs.readFileSync(Path.join(__dirname, "../../resources/email-template.ejs"), "UTF-8");
export abstract class AbstractEmailNotifier implements INotifier { export abstract class AbstractEmailNotifier implements INotifier {
notify(to: string, subject: string, link: string): BluebirdPromise<void> {
notify(identity: Identity, subject: string, link: string): BluebirdPromise<void> {
const d = { const d = {
url: link, url: link,
button_title: "Continue", button_title: "Continue",
title: subject 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<void>; abstract sendEmail(to: string, subject: string, content: string): BluebirdPromise<void>;
} }

View File

@ -13,9 +13,9 @@ export class FileSystemNotifier implements INotifier {
this.filename = options.filename; this.filename = options.filename;
} }
notify(identity: Identity, subject: string, link: string): BluebirdPromise<void> { notify(to: string, subject: string, link: string): BluebirdPromise<void> {
const content = util.format("Date: %s\nUser: %s\nSubject: %s\nLink: %s", new Date().toString(), identity.userid, const content = util.format("Date: %s\nEmail: %s\nSubject: %s\nLink: %s",
subject, link); new Date().toString(), to, subject, link);
const writeFilePromised: any = BluebirdPromise.promisify(Fs.writeFile); const writeFilePromised: any = BluebirdPromise.promisify(Fs.writeFile);
return writeFilePromised(this.filename, content); return writeFilePromised(this.filename, content);
} }

View File

@ -7,16 +7,18 @@ import { IMailSender } from "./IMailSender";
export class GMailNotifier extends AbstractEmailNotifier { export class GMailNotifier extends AbstractEmailNotifier {
private mailSender: IMailSender; private mailSender: IMailSender;
private sender: string;
constructor(options: GmailNotifierConfiguration, mailSender: IMailSender) { constructor(options: GmailNotifierConfiguration, mailSender: IMailSender) {
super(); super();
this.mailSender = mailSender; this.mailSender = mailSender;
this.sender = options.sender;
} }
sendEmail(email: string, subject: string, content: string) { sendEmail(to: string, subject: string, content: string) {
const mailOptions = { const mailOptions = {
from: "authelia@authelia.com", from: this.sender,
to: email, to: to,
subject: subject, subject: subject,
html: content html: content
}; };

View File

@ -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<void>;
}

View File

@ -0,0 +1,5 @@
import * as BluebirdPromise from "bluebird";
export interface INotifier {
notify(to: string, subject: string, link: string): BluebirdPromise<void>;
}

View File

@ -8,17 +8,19 @@ import { SmtpNotifierConfiguration } from "../configuration/Configuration";
export class SmtpNotifier extends AbstractEmailNotifier { export class SmtpNotifier extends AbstractEmailNotifier {
private mailSender: IMailSender; private mailSender: IMailSender;
private sender: string;
constructor(options: SmtpNotifierConfiguration, constructor(options: SmtpNotifierConfiguration,
mailSender: IMailSender) { mailSender: IMailSender) {
super(); super();
this.mailSender = mailSender; this.mailSender = mailSender;
this.sender = options.sender;
} }
sendEmail(email: string, subject: string, content: string) { sendEmail(to: string, subject: string, content: string) {
const mailOptions = { const mailOptions = {
from: "authelia@authelia.com", from: this.sender,
to: email, to: to,
subject: subject, subject: subject,
html: content html: content
}; };

View File

@ -55,7 +55,8 @@ describe("test server configuration", function () {
notifier: { notifier: {
gmail: { gmail: {
username: "user@example.com", username: "user@example.com",
password: "password" password: "password",
sender: "test@authelia.com"
} }
}, },
regulation: { regulation: {

View File

@ -36,7 +36,8 @@ describe("test config adapter", function () {
notifier: { notifier: {
gmail: { gmail: {
username: "user", username: "user",
password: "password" password: "password",
sender: "admin@example.com"
} }
} }
}; };
@ -84,14 +85,16 @@ describe("test config adapter", function () {
yaml_config.notifier = { yaml_config.notifier = {
gmail: { gmail: {
username: "user", username: "user",
password: "pass" password: "pass",
sender: "admin@example.com"
} }
}; };
const config = ConfigurationAdapter.adapt(yaml_config); const config = ConfigurationAdapter.adapt(yaml_config);
Assert.deepEqual(config.notifier, { Assert.deepEqual(config.notifier, {
gmail: { gmail: {
username: "user", username: "user",
password: "pass" password: "pass",
sender: "admin@example.com"
} }
}); });
}); });

View File

@ -33,7 +33,8 @@ describe("test ldap configuration adaptation", function () {
notifier: { notifier: {
gmail: { gmail: {
username: "user", username: "user",
password: "password" password: "password",
sender: "admin@example.com"
} }
} }
}; };

View File

@ -1,5 +1,5 @@
import * as sinon from "sinon"; import * as sinon from "sinon";
import * as assert from "assert"; import * as Assert from "assert";
import BluebirdPromise = require("bluebird"); import BluebirdPromise = require("bluebird");
import { MailSenderStub } from "../mocks/notifiers/MailSenderStub"; import { MailSenderStub } from "../mocks/notifiers/MailSenderStub";
@ -11,24 +11,19 @@ describe("test gmail notifier", function () {
const mailSender = new MailSenderStub(); const mailSender = new MailSenderStub();
const options = { const options = {
username: "user_gmail", username: "user_gmail",
password: "pass_gmail" password: "pass_gmail",
sender: "admin@example.com"
}; };
mailSender.sendStub.returns(BluebirdPromise.resolve()); mailSender.sendStub.returns(BluebirdPromise.resolve());
const sender = new GMailNotifier.GMailNotifier(options, mailSender); const sender = new GMailNotifier.GMailNotifier(options, mailSender);
const subject = "subject"; const subject = "subject";
const identity = {
userid: "user",
email: "user@example.com"
};
const url = "http://test.com"; const url = "http://test.com";
return sender.notify(identity, subject, url) return sender.notify("user@example.com", subject, url)
.then(function () { .then(function () {
assert.equal(mailSender.sendStub.getCall(0).args[0].to, "user@example.com"); 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].subject, "subject");
return BluebirdPromise.resolve(); return BluebirdPromise.resolve();
}); });
}); });
@ -37,24 +32,20 @@ describe("test gmail notifier", function () {
const mailSender = new MailSenderStub(); const mailSender = new MailSenderStub();
const options = { const options = {
username: "user_gmail", username: "user_gmail",
password: "pass_gmail" password: "pass_gmail",
sender: "admin@example.com"
}; };
mailSender.sendStub.returns(BluebirdPromise.reject(new Error("Failed to send mail"))); mailSender.sendStub.returns(BluebirdPromise.reject(new Error("Failed to send mail")));
const sender = new GMailNotifier.GMailNotifier(options, mailSender); const sender = new GMailNotifier.GMailNotifier(options, mailSender);
const subject = "subject"; const subject = "subject";
const identity = {
userid: "user",
email: "user@example.com"
};
const url = "http://test.com"; const url = "http://test.com";
return sender.notify(identity, subject, url) return sender.notify("user@example.com", subject, url)
.then(function () { .then(function () {
return BluebirdPromise.reject(new Error()); return BluebirdPromise.reject(new Error());
}, function() { }, function() {
Assert.equal(mailSender.sendStub.getCall(0).args[0].from, "admin@example.com");
return BluebirdPromise.resolve(); return BluebirdPromise.resolve();
}); });
}); });

View File

@ -18,7 +18,8 @@ describe("test MailSenderBuilder", function() {
const mailSenderBuilder = new MailSenderBuilder(Nodemailer); const mailSenderBuilder = new MailSenderBuilder(Nodemailer);
mailSenderBuilder.buildGmail({ mailSenderBuilder.buildGmail({
username: "user_gmail", 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.user, "user_gmail");
Assert.equal(createTransportStub.getCall(0).args[0].auth.pass, "pass_gmail"); Assert.equal(createTransportStub.getCall(0).args[0].auth.pass, "pass_gmail");
@ -31,7 +32,8 @@ describe("test MailSenderBuilder", function() {
password: "password", password: "password",
port: 25, port: 25,
secure: true, secure: true,
username: "user" username: "user",
sender: "admin@example.com"
}); });
Assert.deepStrictEqual(createTransportStub.getCall(0).args[0], { Assert.deepStrictEqual(createTransportStub.getCall(0).args[0], {
host: "mail.example.com", host: "mail.example.com",

View File

@ -15,7 +15,8 @@ describe("test notifier factory", function () {
const options = { const options = {
gmail: { gmail: {
username: "abc", username: "abc",
password: "password" password: "password",
sender: "admin@example.com"
} }
}; };
mailSenderBuilderStub = new MailSenderBuilderStub(); mailSenderBuilderStub = new MailSenderBuilderStub();
@ -29,7 +30,8 @@ describe("test notifier factory", function () {
password: "pass", password: "pass",
secure: true, secure: true,
host: "localhost", host: "localhost",
port: 25 port: 25,
sender: "admin@example.com"
} }
}; };

View File

@ -54,7 +54,8 @@ describe("Private pages of the server must not be accessible without session", f
notifier: { notifier: {
gmail: { gmail: {
username: "user@example.com", username: "user@example.com",
password: "password" password: "password",
sender: "admin@example.com"
} }
} }
}; };

View File

@ -54,7 +54,8 @@ describe("Public pages of the server must be accessible without session", functi
notifier: { notifier: {
gmail: { gmail: {
username: "user@example.com", username: "user@example.com",
password: "password" password: "password",
sender: "admin@example.com"
} }
} }
}; };