1 | /**
|
2 | * vim:set sw=2 ts=2 sts=2 ft=javascript expandtab:
|
3 | *
|
4 | * # Permission Module
|
5 | *
|
6 | * ## License
|
7 | *
|
8 | * Licensed to the Apache Software Foundation (ASF) under one
|
9 | * or more contributor license agreements. See the NOTICE file
|
10 | * distributed with this work for additional information
|
11 | * regarding copyright ownership. The ASF licenses this file
|
12 | * to you under the Apache License, Version 2.0 (the
|
13 | * "License"); you may not use this file except in compliance
|
14 | * with the License. You may obtain a copy of the License at
|
15 | *
|
16 | * http://www.apache.org/licenses/LICENSE-2.0
|
17 | *
|
18 | * Unless required by applicable law or agreed to in writing,
|
19 | * software distributed under the License is distributed on an
|
20 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
21 | * KIND, either express or implied. See the License for the
|
22 | * specific language governing permissions and limitations
|
23 | * under the License.
|
24 | *
|
25 | * ## Description
|
26 | *
|
27 | * This module contains all functions about permission for groups and pads,
|
28 | * according to authenticated user.
|
29 | */
|
30 |
|
31 | // External dependencies
|
32 | var ld = require('lodash');
|
33 | var cuid = require('cuid');
|
34 | var emailjs = require('emailjs');
|
35 | // Local dependencies
|
36 | require('./helpers.js'); // Helpers auto-init
|
37 | var conf = require('./configuration.js');
|
38 |
|
39 | module.exports = (function () {
|
40 | ;
|
41 |
|
42 | var mail = {
|
43 | tokens: {},
|
44 | ends: {},
|
45 | connection: undefined
|
46 | };
|
47 |
|
48 | /**
|
49 | * ## genToken
|
50 | *
|
51 | * `genToken` is a function that creates a token using *cuid* and populates
|
52 | * local in memory store with necessary information. It also fixes maximum
|
53 | * time validity for the current token, according to configuration.
|
54 | *
|
55 | * It takes a mandatory `value` and returns the generated token.
|
56 | */
|
57 |
|
58 | mail.genToken = function (value) {
|
59 | if (ld.isUndefined(value)) {
|
60 | throw new TypeError('BACKEND.ERROR.TYPE.PARAMS_REQUIRED');
|
61 | }
|
62 | var duration = parseFloat(conf.get('tokenDuration')) || 60;
|
63 | var ts = Date.now();
|
64 | var end = ts + duration * 60 * 1000;
|
65 | var token = cuid();
|
66 | mail.tokens[token] = value;
|
67 | mail.ends[token] = end;
|
68 | return token;
|
69 | };
|
70 |
|
71 | /**
|
72 | * ## isValidToken
|
73 | *
|
74 | * This function checks if, for a given token, expiration has not been
|
75 | * reached.
|
76 | */
|
77 |
|
78 | mail.isValidToken = function (token) {
|
79 | return (mail.tokens[token] && (Date.now() < mail.ends[token]));
|
80 | };
|
81 |
|
82 | /**
|
83 | * ## connect
|
84 | *
|
85 | * `connect` uses SMTP configuration options to connect to the remote SMTP
|
86 | * server. It takes a `callback` function on which it returns *true* for
|
87 | * success or *error* on failure. If a connection has already been setup, it
|
88 | * quits before recreating one. If user and pass are provided, it uses them to
|
89 | * login after connection.
|
90 | */
|
91 |
|
92 | mail.connect = function () {
|
93 | var opts = {
|
94 | port: parseInt(conf.get('SMTPPort'), 10),
|
95 | host: conf.get('SMTPHost'),
|
96 | ssl: conf.get('SMTPSSL'),
|
97 | tls: conf.get('SMTPTLS'),
|
98 | user: conf.get('SMTPUser'),
|
99 | password: conf.get('SMTPPass')
|
100 | };
|
101 | if (!ld.isNumber(opts.port) || !ld.isString(opts.host) ||
|
102 | !ld.isBoolean(opts.ssl) || !ld.isBoolean(opts.tls)) {
|
103 | throw new TypeError('BACKEND.ERROR.TYPE.SMTP_CONFIG');
|
104 | }
|
105 | try {
|
106 | mail.server = emailjs.server.connect(opts);
|
107 | }
|
108 | catch (e) { console.error(e); }
|
109 | };
|
110 |
|
111 | /**
|
112 | * ## send
|
113 | *
|
114 | * `send` is an asynchronous function that sends an email. It takes mandatory
|
115 | *
|
116 | * - `to`, an email string
|
117 | * - `message`, a string containing the text message
|
118 | * - a `callback` function
|
119 | *
|
120 | * It returns to the callback function *error* if there is, *null* otherwise
|
121 | * and an information object. See smtp-connection for more details.
|
122 | */
|
123 |
|
124 | mail.send = function (to, subject, message, callback) {
|
125 | var err;
|
126 | if (!ld.isEmail(to)) {
|
127 | throw new TypeError('BACKEND.ERROR.TYPE.TO_MAIL');
|
128 | }
|
129 | if (!ld.isString(subject)) {
|
130 | throw new TypeError('BACKEND.ERROR.TYPE.SUBJECT_STR');
|
131 | }
|
132 | if (!ld.isString(message)) {
|
133 | throw new TypeError('BACKEND.ERROR.TYPE.MSG_STR');
|
134 | }
|
135 | if (!ld.isFunction(callback)) {
|
136 | throw new TypeError('BACKEND.ERROR.TYPE.CALLBACK_FN');
|
137 | }
|
138 | var emailFrom = conf.get('SMTPEmailFrom');
|
139 | if (!mail.server || !emailFrom) {
|
140 | err = 'BACKEND.ERROR.CONFIGURATION.MAIL_NOT_CONFIGURED';
|
141 | return callback(err);
|
142 | }
|
143 | var envelope = {
|
144 | from: emailFrom,
|
145 | to: to,
|
146 | subject: subject,
|
147 | text: message
|
148 | };
|
149 | mail.server.send(envelope, callback);
|
150 | };
|
151 |
|
152 | /**
|
153 | * ## init
|
154 | *
|
155 | * This function gets configuration option to initialize SMTP connection if
|
156 | * `SMTPHost` is defined.
|
157 | */
|
158 |
|
159 | mail.init = function () {
|
160 | if (conf.get('SMTPHost')) {
|
161 | mail.connect();
|
162 | }
|
163 | };
|
164 |
|
165 | return mail;
|
166 |
|
167 | }).call(this);
|