Advertisement
Guest User

Untitled

a guest
May 20th, 2025
46
0
156 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // onAuth function passed to smtp-server via options:
  2.     onAuth(auth, session, callback) {
  3.         // Supported authentication types
  4.         if ( auth.method != 'LOGIN' && auth.method != 'PLAIN' ) {
  5.             return callback(new Error('Invalid authentication type.'));
  6.         }
  7.  
  8.         if ( auth.username == null || auth.username == undefined ) {
  9.             err = new Error("Must provide a ussername");
  10.             callback(err);
  11.         }  
  12.         if ( auth.password == null || auth.password == undefined ) {
  13.             err = new Error("Must provide a password");
  14.             callback(err);
  15.         }
  16.  
  17.         console.log('onAuth(): Using AUTH ' + auth.method.trim().toUpperCase() + ' to authenticate ' + auth.username.trim());
  18.         log.info('onAuth(): Using AUTH ' + auth.method.trim().toUpperCase() + ' to authenticate ' + auth.username.trim());
  19.  
  20.         if ( authenticateUser(auth, callback) != true ) {
  21.             return callback(new Error("Invalid username or password."));
  22.         }  
  23.  
  24.         return callback(null, {'username': auth.username.trim(), relay: true, ts: Date.now() });
  25.     },
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33. // authenticateUser(auth, callback)
  34. //
  35. // Helper functions for the SMTPServiceOptions object.
  36. //
  37. async function authenticateUser(auth,callback) {
  38.     try {
  39.         const username = auth.username.toString().trim();
  40.         const password = auth.password.toString().trim();
  41.  
  42.         if ( !username || !password ) {
  43.             return callback(new Error("Missing username or password"));
  44.         }
  45.  
  46.         // FILTER:
  47.         FILTER = { 'username': username };
  48.  
  49.         await mailboxes.findOne( FILTER ).exec()
  50.             .then( async (doc) => {
  51.                 const match = bcrypt.compareSync( password, doc.password );
  52.                 console.log('then(): match? ' + match);
  53.                     if ( match != true ) {
  54.                         return callback(new Error('Invalid username or password.'));
  55.                     }
  56.        
  57.                     // Since from this point on, we can assume that 'match' == true,
  58.                     //  and that the user is authenticated.
  59.        
  60.                     if ( doc.enabled != true ) {
  61.                         return callback(new Error('User access is disabled.'));
  62.                     }
  63.  
  64.                     if ( doc.expires != null && doc.expires < Date.now() ) {
  65.                         return callback(new Error('User access has expired.'));
  66.                     }
  67.  
  68.                     doc.lastLogin = Date.now();
  69.                     await doc.save();
  70.  
  71.                     console.log('authenticateUser(): ' + username + ' authenticated successfully.');
  72.  
  73.                     return callback(null, { username: username, relay: true });
  74.         });
  75.  
  76.     } catch ( error ) {
  77.         console.error(error);
  78.     }
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement