Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support to use OAuth2 as the authentication method #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions Mail.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 31 additions & 7 deletions Mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import { info, debug, error } from "./Log";

export interface ISmtpConfig {
smtp: {
type?: string,
host?: string;
port?: number;
user?: string;
password?: string;
clientId?: string,
clientSecret?: string,
accessToken?: string,
refreshToken?: string,
secure?: boolean;
from?: string;
disabled: boolean;
Expand All @@ -30,19 +35,28 @@ export interface IMessage {

export class Mail {
private _template = "<p><!-- body --></p><p><!-- timeStamp --></p>";
private _authTypes = ['plain', 'oauth2'];

constructor(private _config: ISmtpConfig) {
if (!this._config.smtp)
const _smtp = this._config.smtp;
if (!_smtp)
this._config.smtp = { disabled: true };

if (this._config.smtp.disabled === true)
if (_smtp.disabled === true)
return; // don't analyze config if disabled

if (!this._config.smtp)
throw new Error(`[smtp] not set`);
if (!this._config.smtp.host)
if (!_smtp.type || _smtp.type === 'plain|oauth2') {
_smtp.type = "plain";
}

if (!this._authTypes.includes(_smtp.type.trim().toLowerCase()))
throw new Error(`[smtp.type] Authentication type not found ${_smtp.type.trim().toLowerCase()}`);

_smtp.type = _smtp.type.trim().toLowerCase();

if (!_smtp.host)
throw new Error(`[smtp.host] not set`);
if (!this._config.smtp)
if (!_smtp.port)
throw new Error(`[smtp.port] not set`);

try {
Expand Down Expand Up @@ -73,12 +87,22 @@ export class Mail {
name: typeof this._config.smtp.clientHostName == "string" && this._config.smtp.clientHostName ? this._config.smtp.clientHostName : null
};

if (this._config.smtp.user)
if (this._config.smtp.type === 'plain' && this._config.smtp.user)
temp.auth = {
user: this._config.smtp.user,
pass: this._config.smtp.password
};

if (this._config.smtp.type === 'oauth2')
temp.auth = {
type: 'OAuth2',
user: this._config.smtp.user,
clientId: this._config.smtp.clientId,
clientSecret: this._config.smtp.clientSecret,
accessToken: this._config.smtp.accessToken,
refreshToken: this._config.smtp.refreshToken
}

const
transport = Mailer.createTransport(temp),
headers = {};
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ After installation run `pm2 conf` to configure module. Alternatively edit `modul
```json
"pm2-health": {
"smtp": {
"type": "plain|oauth2", // optional, if not set plain is the default
"host": "your-smtp-host",
"port": 587,
"from": "your-from-mail", // if not set, user will be used
"user": "your-smtp-user", // auth
"password": "your-smtp-password", // auth
"clientId": "client-id", //OAuth2 clientId
"clientSecret": "client-secret", //OAuth2 clientSecret
"accessToken": "access-token", //OAuth2 accessToken
"refreshToken": "refresh-token", //OAuth2 refreshToken
"secure": false,
"disabled": false,
"clientHostName": "your-machine.com", // optional, will force the client host-name FQDN used in SMTP HELLO. If not set, NodeMailer will ask host name to the OS, and use 127.0.0.1 if it's not a FQDN.
Expand All @@ -38,6 +43,8 @@ After installation run `pm2 conf` to configure module. Alternatively edit `modul

* `smtp` - SMTP server configuration. If your SMTP doesn't require auth, leave `smtp.user` empty

* `type` - Use Plain or OAuth2 as per your need

* `mailTo` - comma separated list of notification receipients

* `replyTo` - reply to address (optional)
Expand Down
Loading