KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > plugins > smtpExtension > BasicSmtpAuth


1 /*
2  *
3  * Jsmtpd, Java SMTP daemon
4  * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */

21 package org.jsmtpd.plugins.smtpExtension;
22
23 import java.security.MessageDigest JavaDoc;
24 import java.security.NoSuchAlgorithmException JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.jsmtpd.core.common.PluginInitException;
29 import org.jsmtpd.tools.Base64Helper;
30 import org.jsmtpd.tools.ByteArrayTool;
31
32 /**
33  * @author Jean-Francois POUX
34  */

35 public class BasicSmtpAuth extends SmtpAuthenticator {
36
37     private Map JavaDoc<String JavaDoc,byte[]> users = new HashMap JavaDoc<String JavaDoc,byte[]>();
38     private MessageDigest JavaDoc md;
39     protected boolean performAuth(String JavaDoc login, byte[] password) {
40         if (!users.containsKey(login))
41             return false;
42
43         byte[] hash = (byte[]) users.get(login);
44         
45         return ByteArrayTool.compare(hash, md.digest(password));
46     }
47
48     private void addPlainUser(String JavaDoc user, String JavaDoc password) throws Exception JavaDoc {
49         MessageDigest JavaDoc md5 = MessageDigest.getInstance("MD5");
50         users.put(user, md5.digest(password.getBytes()));
51     }
52
53     private void addMD5User(String JavaDoc user, String JavaDoc md5base64password) {
54         users.put(user, Base64Helper.decode(md5base64password));
55     }
56
57     public void setPlainUser(String JavaDoc in) throws Exception JavaDoc {
58         String JavaDoc[] tmp = in.split(",");
59         addPlainUser(tmp[0], tmp[1]);
60     }
61
62     public void setMD5User(String JavaDoc in) {
63         String JavaDoc[] tmp = in.split(",");
64         addMD5User(tmp[0], tmp[1]);
65     }
66
67     @Override JavaDoc
68     public void initPlugin() throws PluginInitException {
69         try {
70             md=MessageDigest.getInstance("md5");
71         } catch (NoSuchAlgorithmException JavaDoc e) {
72             throw new PluginInitException("md5 not available");
73         }
74         super.initPlugin();
75     }
76 }
Popular Tags