2020-08-18 03:40:19 -07:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
|
|
|
|
|
|
|
import {
|
2020-10-15 01:41:47 -07:00
|
|
|
ParsedMail,
|
2020-08-18 03:40:19 -07:00
|
|
|
simpleParser,
|
|
|
|
} from 'mailparser';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IBinaryKeyData,
|
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeApiError,
|
|
|
|
NodeOperationError,
|
2020-08-18 03:40:19 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IEmail,
|
|
|
|
} from './Gmail.node';
|
|
|
|
|
2021-01-10 11:49:47 -08:00
|
|
|
import * as moment from 'moment-timezone';
|
|
|
|
|
|
|
|
import * as jwt from 'jsonwebtoken';
|
|
|
|
|
2020-11-02 23:17:54 -08:00
|
|
|
const mailComposer = require('nodemailer/lib/mail-composer');
|
2020-11-02 13:47:47 -08:00
|
|
|
|
2020-08-18 03:40:19 -07:00
|
|
|
export async function googleApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string,
|
|
|
|
endpoint: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
2021-01-10 11:49:47 -08:00
|
|
|
const authenticationMethod = this.getNodeParameter('authentication', 0, 'serviceAccount') as string;
|
2020-08-18 03:40:19 -07:00
|
|
|
let options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
uri: uri || `https://www.googleapis.com${endpoint}`,
|
2021-03-02 03:47:53 -08:00
|
|
|
qsStringifyOptions:{
|
|
|
|
arrayFormat: 'repeat',
|
|
|
|
},
|
2020-08-18 03:40:19 -07:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
options = Object.assign({}, options, option);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
2021-01-10 11:49:47 -08:00
|
|
|
if (authenticationMethod === 'serviceAccount') {
|
|
|
|
const credentials = this.getCredentials('googleApi');
|
|
|
|
|
|
|
|
if (credentials === undefined) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
2021-01-10 11:49:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const { access_token } = await getAccessToken.call(this, credentials as IDataObject);
|
|
|
|
|
|
|
|
options.headers!.Authorization = `Bearer ${access_token}`;
|
|
|
|
//@ts-ignore
|
|
|
|
return await this.helpers.request(options);
|
|
|
|
} else {
|
|
|
|
//@ts-ignore
|
|
|
|
return await this.helpers.requestOAuth2.call(this, 'gmailOAuth2', options);
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
|
|
|
|
} catch (error) {
|
2021-06-22 10:59:27 -07:00
|
|
|
if (error.code === 'ERR_OSSL_PEM_NO_START_LINE') {
|
|
|
|
error.statusCode = '401';
|
|
|
|
}
|
|
|
|
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-15 01:41:47 -07:00
|
|
|
export async function parseRawEmail(this: IExecuteFunctions, messageData: any, dataPropertyNameDownload: string): Promise<INodeExecutionData> { // tslint:disable-line:no-any
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2020-10-15 01:41:47 -07:00
|
|
|
const messageEncoded = Buffer.from(messageData.raw, 'base64').toString('utf8');
|
|
|
|
let responseData = await simpleParser(messageEncoded);
|
2020-08-18 03:40:19 -07:00
|
|
|
|
|
|
|
const headers: IDataObject = {};
|
2020-10-15 01:41:47 -07:00
|
|
|
// @ts-ignore
|
2020-08-18 03:40:19 -07:00
|
|
|
for (const header of responseData.headerLines) {
|
|
|
|
headers[header.key] = header.line;
|
|
|
|
}
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
responseData.headers = headers;
|
|
|
|
// @ts-ignore
|
|
|
|
responseData.headerLines = undefined;
|
|
|
|
|
|
|
|
const binaryData: IBinaryKeyData = {};
|
|
|
|
if (responseData.attachments) {
|
|
|
|
|
|
|
|
for (let i = 0; i < responseData.attachments.length; i++) {
|
|
|
|
const attachment = responseData.attachments[i];
|
|
|
|
binaryData[`${dataPropertyNameDownload}${i}`] = await this.helpers.prepareBinaryData(attachment.content, attachment.filename, attachment.contentType);
|
|
|
|
}
|
|
|
|
// @ts-ignore
|
|
|
|
responseData.attachments = undefined;
|
|
|
|
}
|
|
|
|
|
2020-10-15 01:41:47 -07:00
|
|
|
const mailBaseData: IDataObject = {};
|
|
|
|
|
|
|
|
const resolvedModeAddProperties = [
|
|
|
|
'id',
|
|
|
|
'threadId',
|
|
|
|
'labelIds',
|
|
|
|
'sizeEstimate',
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const key of resolvedModeAddProperties) {
|
|
|
|
// @ts-ignore
|
|
|
|
mailBaseData[key] = messageData[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = Object.assign(mailBaseData, responseData);
|
|
|
|
|
2020-08-18 03:40:19 -07:00
|
|
|
return {
|
|
|
|
json: responseData as unknown as IDataObject,
|
|
|
|
binary: Object.keys(binaryData).length ? binaryData : undefined,
|
|
|
|
} as INodeExecutionData;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// This function converts an email object into a MIME encoded email and then converts that string into base64 encoding
|
|
|
|
// for more info on MIME, https://docs.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/aa494197(v%3Dexchg.140)
|
|
|
|
//------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
2020-11-02 13:47:47 -08:00
|
|
|
export async function encodeEmail(email: IEmail) {
|
|
|
|
let mailBody: Buffer;
|
|
|
|
|
|
|
|
const mailOptions = {
|
2021-07-30 07:26:57 -07:00
|
|
|
from: email.from,
|
2020-11-02 13:47:47 -08:00
|
|
|
to: email.to,
|
2021-01-07 02:58:28 -08:00
|
|
|
cc: email.cc,
|
2020-11-02 13:47:47 -08:00
|
|
|
bcc: email.bcc,
|
|
|
|
replyTo: email.inReplyTo,
|
|
|
|
references: email.reference,
|
|
|
|
subject: email.subject,
|
|
|
|
text: email.body,
|
|
|
|
} as IDataObject;
|
2021-01-07 02:58:28 -08:00
|
|
|
if (email.htmlBody) {
|
|
|
|
mailOptions.html = email.htmlBody;
|
|
|
|
}
|
2020-11-02 13:47:47 -08:00
|
|
|
|
|
|
|
if (email.attachments !== undefined && Array.isArray(email.attachments) && email.attachments.length > 0) {
|
|
|
|
const attachments = email.attachments.map((attachment) => ({
|
|
|
|
filename: attachment.name,
|
|
|
|
content: attachment.content,
|
|
|
|
contentType: attachment.type,
|
2021-01-07 02:58:28 -08:00
|
|
|
encoding: 'base64',
|
2020-11-02 13:47:47 -08:00
|
|
|
}));
|
|
|
|
|
|
|
|
mailOptions.attachments = attachments;
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2020-11-02 23:17:54 -08:00
|
|
|
|
|
|
|
const mail = new mailComposer(mailOptions);
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2020-11-02 13:47:47 -08:00
|
|
|
mailBody = await new Promise((resolve) => {
|
|
|
|
mail.compile().build(async (err: string, result: Buffer) => {
|
|
|
|
resolve(result);
|
2020-08-18 03:40:19 -07:00
|
|
|
});
|
2020-11-02 13:47:47 -08:00
|
|
|
});
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2020-11-02 23:17:54 -08:00
|
|
|
return mailBody.toString('base64').replace(/\+/g, '-').replace(/\//g, '_');
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function googleApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
query.maxResults = 100;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, query);
|
|
|
|
query.pageToken = responseData['nextPageToken'];
|
|
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
|
|
|
} while (
|
|
|
|
responseData['nextPageToken'] !== undefined &&
|
|
|
|
responseData['nextPageToken'] !== ''
|
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function extractEmail(s: string) {
|
|
|
|
const data = s.split('<')[1];
|
|
|
|
return data.substring(0, data.length - 1);
|
|
|
|
}
|
2021-01-10 11:49:47 -08:00
|
|
|
|
|
|
|
function getAccessToken(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, credentials: IDataObject): Promise<IDataObject> {
|
|
|
|
//https://developers.google.com/identity/protocols/oauth2/service-account#httprest
|
|
|
|
|
|
|
|
const scopes = [
|
2021-02-28 08:27:52 -08:00
|
|
|
'https://www.googleapis.com/auth/gmail.labels',
|
|
|
|
'https://www.googleapis.com/auth/gmail.addons.current.action.compose',
|
|
|
|
'https://www.googleapis.com/auth/gmail.addons.current.message.action',
|
|
|
|
'https://mail.google.com/',
|
|
|
|
'https://www.googleapis.com/auth/gmail.modify',
|
|
|
|
'https://www.googleapis.com/auth/gmail.compose',
|
2021-01-10 11:49:47 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
const now = moment().unix();
|
|
|
|
|
|
|
|
const signature = jwt.sign(
|
|
|
|
{
|
|
|
|
'iss': credentials.email as string,
|
|
|
|
'sub': credentials.delegatedEmail || credentials.email as string,
|
|
|
|
'scope': scopes.join(' '),
|
|
|
|
'aud': `https://oauth2.googleapis.com/token`,
|
|
|
|
'iat': now,
|
|
|
|
'exp': now + 3600,
|
|
|
|
},
|
|
|
|
credentials.privateKey as string,
|
|
|
|
{
|
|
|
|
algorithm: 'RS256',
|
|
|
|
header: {
|
|
|
|
'kid': credentials.privateKey as string,
|
|
|
|
'typ': 'JWT',
|
|
|
|
'alg': 'RS256',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
form: {
|
|
|
|
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
|
|
|
|
assertion: signature,
|
|
|
|
},
|
|
|
|
uri: 'https://oauth2.googleapis.com/token',
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
//@ts-ignore
|
|
|
|
return this.helpers.request(options);
|
|
|
|
}
|
|
|
|
|