KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > authentication > HibernateAuthenticationManager


1 /*
2  * Shark Hibernate authentification layer - Open Wide
3  */

4
5 package org.enhydra.shark.authentication;
6
7 import net.sf.hibernate.Session;
8
9 import org.enhydra.shark.api.RootException;
10 import org.enhydra.shark.api.UserTransaction;
11 import org.enhydra.shark.api.internal.authentication.AuthenticationManager;
12 import org.enhydra.shark.api.internal.working.CallbackUtilities;
13 import org.enhydra.shark.usergroup.HibernateUser;
14 import org.enhydra.shark.usertransaction.SharkHibernateUserTransaction;
15 import org.enhydra.shark.utilities.hibernate.HibernateUtilities;
16
17 /**
18  * Authenticates userId or password for some userId.
19  * @author Vladislav Pernin
20  */

21 public class HibernateAuthenticationManager implements AuthenticationManager {
22
23    private static final String JavaDoc LDB_PARAM_NAME = "HibernateAuthenticationManager.DatabaseName";
24    private CallbackUtilities callback;
25
26    public void configure (CallbackUtilities cus) throws RootException {
27       if (null == cus)
28          throw new RootException("Cannot configure without call back impl.");
29       callback = cus;
30       HibernateUtilities.init(callback.getProperties());
31       callback.debug("HibernateUserGroupManager configured");
32    }
33
34    public boolean validateUser(UserTransaction t,
35                                String JavaDoc username,
36                                String JavaDoc password) throws RootException {
37       boolean ret = false;
38       try {
39         Session session = ((SharkHibernateUserTransaction)t).getSession();
40                  
41         HibernateUser user = (HibernateUser) session.get(HibernateUser.class,username);
42         
43          ret = (null != user)
44             ? passwordDigest(password).equals(user.getPasswd())
45             : false;
46
47          endTransaction(t, false);
48       } catch (Exception JavaDoc e) {
49          if (e instanceof RootException) {
50             throw (RootException)e;
51          }
52          throw new RootException(e);
53       }
54       return ret;
55    }
56
57    private void endTransaction(UserTransaction t,
58                                boolean commitToo) throws RootException {
59          try {
60             if (commitToo) {
61                t.commit();
62             }
63          } catch (Exception JavaDoc e) {
64             throw new RootException(e);
65          } finally {
66             t.release();
67          }
68    }
69
70    private static String JavaDoc passwordDigest(String JavaDoc password) {
71       // TODO: should we store plain text passwords, or ... ?
72
return password;
73    }
74 }
75
76
Popular Tags