KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > jaas > sp > jcr > JCRAuthenticationModule


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.jaas.sp.jcr;
14
15 import info.magnolia.cms.beans.config.ContentRepository;
16 import info.magnolia.cms.core.Content;
17 import info.magnolia.cms.core.HierarchyManager;
18 import info.magnolia.cms.security.auth.Entity;
19 import info.magnolia.jaas.principal.EntityImpl;
20 import info.magnolia.jaas.sp.AbstractLoginModule;
21
22 import java.io.IOException JavaDoc;
23
24 import javax.jcr.PathNotFoundException;
25 import javax.jcr.RepositoryException;
26 import javax.security.auth.callback.Callback JavaDoc;
27 import javax.security.auth.callback.NameCallback JavaDoc;
28 import javax.security.auth.callback.PasswordCallback JavaDoc;
29 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
30 import javax.security.auth.login.LoginException JavaDoc;
31 import javax.security.auth.login.FailedLoginException JavaDoc;
32
33 import org.apache.commons.codec.binary.Base64;
34 import org.apache.commons.lang.StringUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38
39 /**
40  * @author Sameer Charles $Id: JCRAuthenticationModule.java 7445 2006-11-14 14:26:15Z scharles $
41  */

42 public class JCRAuthenticationModule extends AbstractLoginModule {
43
44     /**
45      * Logger
46      */

47     private static Logger log = LoggerFactory.getLogger(JCRAuthenticationModule.class);
48
49     protected String JavaDoc name;
50
51     protected char[] pswd;
52
53     protected boolean success;
54
55     protected Content user;
56
57     /**
58      * Authenticate against magnolia/jcr user repository
59      */

60     public boolean login() throws LoginException JavaDoc {
61         if (this.callbackHandler == null) {
62             throw new LoginException JavaDoc("Error: no CallbackHandler available for JCRModule");
63         }
64
65         Callback JavaDoc[] callbacks = new Callback JavaDoc[2];
66         callbacks[0] = new NameCallback JavaDoc("name");
67         callbacks[1] = new PasswordCallback JavaDoc("pswd", false);
68
69         this.success = false;
70         try {
71             this.callbackHandler.handle(callbacks);
72             this.name = ((NameCallback JavaDoc) callbacks[0]).getName();
73             this.pswd = ((PasswordCallback JavaDoc) callbacks[1]).getPassword();
74             this.success = this.isValidUser();
75         }
76         catch (IOException JavaDoc ioe) {
77             if (log.isDebugEnabled()) {
78                 log.debug("Exception caught", ioe);
79             }
80             throw new LoginException JavaDoc(ioe.toString());
81         }
82         catch (UnsupportedCallbackException JavaDoc ce) {
83             if (log.isDebugEnabled()) {
84                 log.debug(ce.getMessage(), ce);
85             }
86             throw new LoginException JavaDoc(ce.getCallback().toString() + " not available");
87         }
88         if (!this.success) {
89             throw new FailedLoginException JavaDoc("failed to authenticate " + this.name);
90         }
91
92         return this.success;
93     }
94
95     /**
96      * Update subject with ACL and other properties
97      */

98     public boolean commit() throws LoginException JavaDoc {
99         if (!this.success) {
100             throw new LoginException JavaDoc("failed to authenticate " + this.name);
101         }
102         this.setEntity();
103         return true;
104     }
105
106     /**
107      * Releases all associated memory
108      */

109     public boolean release() {
110         return true;
111     }
112
113     /**
114      * checks is the credentials exist in the repository
115      * @return boolean
116      */

117     public boolean isValidUser() {
118         HierarchyManager hm = ContentRepository.getHierarchyManager(ContentRepository.USERS);
119         try {
120             this.user = hm.getContent(this.name);
121             String JavaDoc serverPassword = this.user.getNodeData("pswd").getString().trim();
122             // we do not allow users with no password
123
if (StringUtils.isEmpty(serverPassword)) return false;
124             // plain text server password
125
serverPassword = new String JavaDoc(Base64.decodeBase64(serverPassword.getBytes()));
126             return StringUtils.equals(serverPassword, new String JavaDoc(this.pswd));
127         }
128         catch (PathNotFoundException pe) {
129             log.info("Unable to locate user [{}], authentication failed", this.name);
130         }
131         catch (RepositoryException re) {
132             log.error("Unable to locate user ["
133                 + this.name
134                 + "], authentication failed due to a "
135                 + re.getClass().getName(), re);
136         }
137         return false;
138     }
139
140     /**
141      * set user details
142      */

143     public void setEntity() {
144         EntityImpl user = new EntityImpl();
145         String JavaDoc language = this.user.getNodeData("language").getString();
146         user.addProperty(Entity.LANGUAGE, language);
147         user.addProperty(Entity.NAME, this.user.getName());
148         user.addProperty(Entity.FULL_NAME, this.user.getTitle());
149         user.addProperty(Entity.PASSWORD, new String JavaDoc(this.pswd));
150         this.subject.getPrincipals().add(user);
151     }
152
153     /**
154      * set access control list from the user, roles and groups
155      */

156     public void setACL() {
157     }
158
159 }
160
Popular Tags