Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // onAuth function passed to smtp-server via options:
- onAuth(auth, session, callback) {
- // Supported authentication types
- if ( auth.method != 'LOGIN' && auth.method != 'PLAIN' ) {
- return callback(new Error('Invalid authentication type.'));
- }
- if ( auth.username == null || auth.username == undefined ) {
- err = new Error("Must provide a ussername");
- callback(err);
- }
- if ( auth.password == null || auth.password == undefined ) {
- err = new Error("Must provide a password");
- callback(err);
- }
- console.log('onAuth(): Using AUTH ' + auth.method.trim().toUpperCase() + ' to authenticate ' + auth.username.trim());
- log.info('onAuth(): Using AUTH ' + auth.method.trim().toUpperCase() + ' to authenticate ' + auth.username.trim());
- if ( authenticateUser(auth, callback) != true ) {
- return callback(new Error("Invalid username or password."));
- }
- return callback(null, {'username': auth.username.trim(), relay: true, ts: Date.now() });
- },
- // authenticateUser(auth, callback)
- //
- // Helper functions for the SMTPServiceOptions object.
- //
- async function authenticateUser(auth,callback) {
- try {
- const username = auth.username.toString().trim();
- const password = auth.password.toString().trim();
- if ( !username || !password ) {
- return callback(new Error("Missing username or password"));
- }
- // FILTER:
- FILTER = { 'username': username };
- await mailboxes.findOne( FILTER ).exec()
- .then( async (doc) => {
- const match = bcrypt.compareSync( password, doc.password );
- console.log('then(): match? ' + match);
- if ( match != true ) {
- return callback(new Error('Invalid username or password.'));
- }
- // Since from this point on, we can assume that 'match' == true,
- // and that the user is authenticated.
- if ( doc.enabled != true ) {
- return callback(new Error('User access is disabled.'));
- }
- if ( doc.expires != null && doc.expires < Date.now() ) {
- return callback(new Error('User access has expired.'));
- }
- doc.lastLogin = Date.now();
- await doc.save();
- console.log('authenticateUser(): ' + username + ' authenticated successfully.');
- return callback(null, { username: username, relay: true });
- });
- } catch ( error ) {
- console.error(error);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement