root / src / gr / ebs / gss / server / webdav / login / GssWebDAVLoginModule.java @ 33d60990
History | View | Annotate | Download (5.3 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2005, 2008, 2009 Electronic Business Systems Ltd.
|
3 |
*
|
4 |
* This file is part of GSS.
|
5 |
*
|
6 |
* GSS is free software: you can redistribute it and/or modify
|
7 |
* it under the terms of the GNU General Public License as published by
|
8 |
* the Free Software Foundation, either version 3 of the License, or
|
9 |
* (at your option) any later version.
|
10 |
*
|
11 |
* GSS 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 GSS. If not, see <http://www.gnu.org/licenses/>.
|
18 |
*/
|
19 |
package gr.ebs.gss.server.webdav.login; |
20 |
|
21 |
import static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration; |
22 |
import gr.ebs.gss.client.exceptions.RpcException; |
23 |
import gr.ebs.gss.server.domain.User; |
24 |
import gr.ebs.gss.server.domain.UserLogin; |
25 |
import gr.ebs.gss.server.ejb.ExternalAPI; |
26 |
import gr.ebs.gss.server.ejb.TransactionHelper; |
27 |
|
28 |
import java.io.UnsupportedEncodingException; |
29 |
import java.security.Principal; |
30 |
import java.security.acl.Group; |
31 |
import java.util.Date; |
32 |
import java.util.HashSet; |
33 |
import java.util.concurrent.Callable; |
34 |
|
35 |
import javax.naming.Context; |
36 |
import javax.naming.InitialContext; |
37 |
import javax.naming.NamingException; |
38 |
import javax.rmi.PortableRemoteObject; |
39 |
import javax.security.auth.login.FailedLoginException; |
40 |
import javax.security.auth.login.LoginException; |
41 |
|
42 |
import org.apache.commons.codec.binary.Base64; |
43 |
import org.apache.commons.logging.Log; |
44 |
import org.apache.commons.logging.LogFactory; |
45 |
import org.jboss.security.auth.spi.UsernamePasswordLoginModule; |
46 |
|
47 |
|
48 |
/**
|
49 |
* The custom login module for the GSS WebDAV implementation.
|
50 |
*/
|
51 |
public class GssWebDAVLoginModule extends UsernamePasswordLoginModule { |
52 |
|
53 |
/**
|
54 |
* Logger for this class
|
55 |
*/
|
56 |
private static final Log logger = LogFactory.getLog(GssWebDAVLoginModule.class); |
57 |
|
58 |
/**
|
59 |
* A helper method that retrieves a reference to the ExternalAPI bean and
|
60 |
* stores it for future use.
|
61 |
*
|
62 |
* @return an ExternalAPI instance
|
63 |
* @throws RpcException in case an error occurs
|
64 |
*/
|
65 |
private ExternalAPI getService() throws RpcException { |
66 |
try {
|
67 |
final Context ctx = new InitialContext(); |
68 |
final Object ref = ctx.lookup(getConfiguration().getString("externalApiPath")); |
69 |
return (ExternalAPI) PortableRemoteObject.narrow(ref, ExternalAPI.class); |
70 |
} catch (final NamingException e) { |
71 |
logger.error("Unable to retrieve the ExternalAPI EJB", e);
|
72 |
throw new RpcException("An error occurred while contacting the naming service"); |
73 |
} |
74 |
} |
75 |
|
76 |
@Override
|
77 |
protected String getUsersPassword() throws LoginException { |
78 |
String username = getUsername();
|
79 |
try {
|
80 |
final User user = getService().findUser(username);
|
81 |
if (user == null) throw new FailedLoginException("User '" + username + "' not found."); |
82 |
if (!user.isActive()) throw new FailedLoginException("User '" + username + "' is disabled."); |
83 |
if (user.getWebDAVPassword() != null && user.getWebDAVPassword().length() > 0) |
84 |
return user.getWebDAVPassword();
|
85 |
// If no password has ever been generated, use token instead
|
86 |
String tokenEncoded = new String(Base64.encodeBase64(user.getAuthToken()), "US-ASCII"); |
87 |
user.setWebDAVPassword(tokenEncoded); |
88 |
new TransactionHelper<Void>().tryExecute(new Callable<Void>() { |
89 |
@Override
|
90 |
public Void call() throws Exception { |
91 |
getService().updateUser(user); |
92 |
return null; |
93 |
} |
94 |
}); |
95 |
return tokenEncoded;
|
96 |
} catch (RpcException e) {
|
97 |
String error = "An error occurred while communicating with the service"; |
98 |
logger.error(error, e); |
99 |
throw new LoginException(e.getMessage()); |
100 |
} catch (UnsupportedEncodingException e) { |
101 |
logger.error("", e);
|
102 |
throw new LoginException(e.getMessage()); |
103 |
} catch (Exception e) { |
104 |
logger.error("", e);
|
105 |
throw new LoginException(e.getMessage()); |
106 |
} |
107 |
} |
108 |
|
109 |
/**
|
110 |
* Overrides parent's implementation by returning only the simpleUser
|
111 |
* role for any successful login.
|
112 |
*
|
113 |
* @return Group[] that contains only the authenticatedUser group (role)
|
114 |
* @throws LoginException
|
115 |
* @see org.jboss.security.auth.spi.AbstractServerLoginModule#getRoleSets()
|
116 |
*/
|
117 |
@Override
|
118 |
protected Group[] getRoleSets() throws LoginException { |
119 |
Principal principal;
|
120 |
try {
|
121 |
principal = createIdentity("simpleUser");
|
122 |
} catch (Exception e) { |
123 |
logger.error("", e);
|
124 |
throw new LoginException(e.getMessage()); |
125 |
} |
126 |
Group rolesGroup = null; |
127 |
rolesGroup = createGroup("Roles", new HashSet()); |
128 |
rolesGroup.addMember(principal); |
129 |
Group[] roles = new Group[1]; |
130 |
roles[0] = rolesGroup;
|
131 |
// Update the last login.
|
132 |
//TODO: Handle the userlogins via WebDAV
|
133 |
// try {
|
134 |
// new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
|
135 |
// @Override
|
136 |
// public Void call() throws Exception {
|
137 |
// User user = getService().findUser(getUsername());
|
138 |
// UserLogin userLogin = new UserLogin();
|
139 |
// userLogin.setLoginDate(new Date());
|
140 |
// getService().addUserLogin(userLogin);
|
141 |
// getService().updateUser(user);
|
142 |
// return null;
|
143 |
// }
|
144 |
// });
|
145 |
// } catch (Exception e) {
|
146 |
// logger.error("", e);
|
147 |
// throw new LoginException(e.getMessage());
|
148 |
// }
|
149 |
return roles;
|
150 |
} |
151 |
|
152 |
} |