2 * Copyright 2011 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.server.webdav.milton;
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.ejb.ExternalAPI;
25 import gr.ebs.gss.server.ejb.TransactionHelper;
27 import java.io.UnsupportedEncodingException;
29 import java.util.concurrent.Callable;
31 import javax.naming.Context;
32 import javax.naming.InitialContext;
33 import javax.naming.NamingException;
34 import javax.rmi.PortableRemoteObject;
35 import javax.security.auth.login.FailedLoginException;
36 import javax.security.auth.login.LoginException;
38 import org.apache.commons.codec.binary.Base64;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import com.bradmcevoy.http.Auth;
43 import com.bradmcevoy.http.Request;
44 import com.bradmcevoy.http.Resource;
45 import com.bradmcevoy.http.Request.Method;
46 import com.bradmcevoy.http.http11.auth.DigestGenerator;
47 import com.bradmcevoy.http.http11.auth.DigestResponse;
48 import com.ettrema.http.fs.SimpleSecurityManager;
55 public class GssSecurityManager implements com.bradmcevoy.http.SecurityManager{
57 private static final Logger log = LoggerFactory.getLogger(SimpleSecurityManager.class);
60 private DigestGenerator digestGenerator;
62 public GssSecurityManager() {
63 digestGenerator = new DigestGenerator();
66 public GssSecurityManager( DigestGenerator digestGenerator ) {
67 this.digestGenerator = digestGenerator;
71 public GssSecurityManager( String realm) {
76 public Object getUserByName( String name ) {
77 String actualPassword = nameAndPasswords.get( name );
78 if( actualPassword != null ) return name;
84 public Object authenticate( String user, String password ) {
85 log.debug( "authenticate: " + user + " - " + password);
86 // user name will include domain when coming form ftp. we just strip it off
87 if( user.contains( "@")) {
88 user = user.substring( 0, user.indexOf( "@"));
90 String actualPassword=null;
92 actualPassword = getUsersPassword( user );
93 } catch (Exception e) {
94 // TODO Auto-generated catch block
98 if( actualPassword == null ) {
99 log.debug( "user not found: " + user);
103 if( actualPassword == null ) {
104 ok = password == null || password.length()==0;
106 ok = actualPassword.equals( password);
108 return ok ? user : null;
112 public Object authenticate( DigestResponse digestRequest ) {
113 String actualPassword=null;
115 actualPassword = getUsersPassword( digestRequest.getUser() );
116 } catch (Exception e) {
117 // TODO Auto-generated catch block
122 String serverResponse = digestGenerator.generateDigest( digestRequest, actualPassword );
123 String clientResponse = digestRequest.getResponseDigest();
125 if( serverResponse.equals( clientResponse ) ) {
127 return getService().getUserByUserName(digestRequest.getUser());
128 } catch (RpcException e) {
129 // TODO Auto-generated catch block
140 public boolean authorise( Request request, Method method, Auth auth, Resource resource ) {
141 return auth != null && auth.getTag() != null;
144 public String getRealm(String host) {
149 * @param realm the realm to set
151 public void setRealm( String realm ) {
155 private ExternalAPI getService() throws RpcException {
157 final Context ctx = new InitialContext();
158 final Object ref = ctx.lookup(getConfiguration().getString("externalApiPath"));
159 return (ExternalAPI) PortableRemoteObject.narrow(ref, ExternalAPI.class);
160 } catch (final NamingException e) {
161 log.error("Unable to retrieve the ExternalAPI EJB", e);
162 throw new RpcException("An error occurred while contacting the naming service");
167 protected String getUsersPassword(String username) throws Exception {
170 final User user = getService().findUser(username);
171 if (user == null) throw new FailedLoginException("User '" + username + "' not found.");
172 if (!user.isActive()) throw new FailedLoginException("User '" + username + "' is disabled.");
173 if (user.getWebDAVPassword() != null && user.getWebDAVPassword().length() > 0)
174 return user.getWebDAVPassword();
175 // If no password has ever been generated, use token instead
176 String tokenEncoded = new String(Base64.encodeBase64(user.getAuthToken()), "US-ASCII");
177 user.setWebDAVPassword(tokenEncoded);
178 new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
180 public Void call() throws Exception {
181 getService().updateUser(user);
186 } catch (RpcException e) {
187 String error = "An error occurred while communicating with the service";
189 throw new Exception(e.getMessage());
190 } catch (UnsupportedEncodingException e) {
192 throw new Exception(e.getMessage());
193 } catch (Exception e) {
195 throw new Exception(e.getMessage());