root / src / org / gss_project / gss / server / webdav / milton / GssSecurityManager.java @ 1206:292dec4eae08
History | View | Annotate | Download (6.2 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2011 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 org.gss_project.gss.server.webdav.milton; |
20 |
|
21 |
import static org.gss_project.gss.server.configuration.GSSConfigurationFactory.getConfiguration; |
22 |
import org.gss_project.gss.common.exceptions.RpcException; |
23 |
import org.gss_project.gss.server.domain.User; |
24 |
import org.gss_project.gss.server.ejb.ExternalAPI; |
25 |
import org.gss_project.gss.server.ejb.TransactionHelper; |
26 |
|
27 |
import java.io.UnsupportedEncodingException; |
28 |
import java.util.concurrent.Callable; |
29 |
|
30 |
import javax.naming.Context; |
31 |
import javax.naming.InitialContext; |
32 |
import javax.naming.NamingException; |
33 |
import javax.rmi.PortableRemoteObject; |
34 |
import javax.security.auth.login.FailedLoginException; |
35 |
|
36 |
import org.apache.commons.codec.binary.Base64; |
37 |
import org.slf4j.Logger; |
38 |
import org.slf4j.LoggerFactory; |
39 |
|
40 |
import com.bradmcevoy.http.Auth; |
41 |
import com.bradmcevoy.http.Request; |
42 |
import com.bradmcevoy.http.Resource; |
43 |
import com.bradmcevoy.http.Request.Method; |
44 |
import com.bradmcevoy.http.http11.auth.DigestGenerator; |
45 |
import com.bradmcevoy.http.http11.auth.DigestResponse; |
46 |
import com.ettrema.http.fs.SimpleSecurityManager; |
47 |
|
48 |
|
49 |
/**
|
50 |
* @author kman
|
51 |
*
|
52 |
*/
|
53 |
public class GssSecurityManager implements com.bradmcevoy.http.SecurityManager{ |
54 |
|
55 |
private static final Logger log = LoggerFactory.getLogger(SimpleSecurityManager.class); |
56 |
|
57 |
private String realm; |
58 |
private DigestGenerator digestGenerator;
|
59 |
|
60 |
public GssSecurityManager() {
|
61 |
digestGenerator = new DigestGenerator();
|
62 |
} |
63 |
|
64 |
public GssSecurityManager( DigestGenerator digestGenerator ) {
|
65 |
this.digestGenerator = digestGenerator;
|
66 |
} |
67 |
|
68 |
|
69 |
public GssSecurityManager( String realm) { |
70 |
this.realm = realm;
|
71 |
this.digestGenerator = new DigestGenerator(); |
72 |
} |
73 |
/*
|
74 |
public Object getUserByName( String name ) {
|
75 |
String actualPassword = nameAndPasswords.get( name );
|
76 |
if( actualPassword != null ) return name;
|
77 |
return null;
|
78 |
}*/
|
79 |
|
80 |
|
81 |
|
82 |
public Object authenticate( String user, String password ) { |
83 |
//log.info( "authenticate: " + user + " - " + password);
|
84 |
// user name will include domain when coming form ftp. we just strip it off
|
85 |
if( user.contains( "@")) { |
86 |
user = user.substring( 0, user.indexOf( "@")); |
87 |
} |
88 |
String actualPassword=null; |
89 |
try {
|
90 |
actualPassword = getUsersPassword( user ); |
91 |
} catch (Exception e) { |
92 |
// TODO Auto-generated catch block
|
93 |
e.printStackTrace(); |
94 |
return null; |
95 |
} |
96 |
if( actualPassword == null ) { |
97 |
log.debug( "user not found: " + user);
|
98 |
return null; |
99 |
} else {
|
100 |
boolean ok;
|
101 |
if( actualPassword == null ) { |
102 |
ok = password == null || password.length()==0; |
103 |
} else {
|
104 |
ok = actualPassword.equals( password); |
105 |
} |
106 |
return ok ? user : null; |
107 |
} |
108 |
} |
109 |
|
110 |
public Object authenticate( DigestResponse digestRequest ) { |
111 |
//log.info( "DIGEST authenticate: " + digestRequest);
|
112 |
String actualPassword=null; |
113 |
try {
|
114 |
actualPassword = getUsersPassword( digestRequest.getUser() ); |
115 |
} catch (Exception e) { |
116 |
// TODO Auto-generated catch block
|
117 |
return null; |
118 |
} |
119 |
|
120 |
String serverResponse = digestGenerator.generateDigest( digestRequest, actualPassword );
|
121 |
String clientResponse = digestRequest.getResponseDigest();
|
122 |
|
123 |
if( serverResponse.equals( clientResponse ) ) {
|
124 |
try {
|
125 |
return getService().getUserByUserName(digestRequest.getUser());
|
126 |
} catch (RpcException e) {
|
127 |
// TODO Auto-generated catch block
|
128 |
return null; |
129 |
} |
130 |
} else {
|
131 |
return null; |
132 |
} |
133 |
} |
134 |
|
135 |
|
136 |
|
137 |
public boolean authorise( Request request, Method method, Auth auth, Resource resource ) { |
138 |
return auth != null && auth.getTag() != null; |
139 |
} |
140 |
|
141 |
public String getRealm(String host) { |
142 |
return realm;
|
143 |
} |
144 |
|
145 |
/**
|
146 |
* @param realm the realm to set
|
147 |
*/
|
148 |
public void setRealm( String realm ) { |
149 |
this.realm = realm;
|
150 |
} |
151 |
|
152 |
private ExternalAPI getService() throws RpcException { |
153 |
try {
|
154 |
final Context ctx = new InitialContext(); |
155 |
final Object ref = ctx.lookup(getConfiguration().getString("externalApiPath")); |
156 |
return (ExternalAPI) PortableRemoteObject.narrow(ref, ExternalAPI.class); |
157 |
} catch (final NamingException e) { |
158 |
log.error("Unable to retrieve the ExternalAPI EJB", e);
|
159 |
throw new RpcException("An error occurred while contacting the naming service"); |
160 |
} |
161 |
} |
162 |
|
163 |
|
164 |
protected String getUsersPassword(String username) throws Exception { |
165 |
|
166 |
try {
|
167 |
final User user = getService().findUser(username);
|
168 |
if (user == null) throw new FailedLoginException("User '" + username + "' not found."); |
169 |
if (!user.isActive()) throw new FailedLoginException("User '" + username + "' is disabled."); |
170 |
if (user.getWebDAVPassword() != null && user.getWebDAVPassword().length() > 0) |
171 |
return user.getWebDAVPassword();
|
172 |
// If no password has ever been generated, use token instead
|
173 |
String tokenEncoded = new String(Base64.encodeBase64(user.getAuthToken()), "US-ASCII"); |
174 |
user.setWebDAVPassword(tokenEncoded); |
175 |
new TransactionHelper<Void>().tryExecute(new Callable<Void>() { |
176 |
@Override
|
177 |
public Void call() throws Exception { |
178 |
getService().updateUser(user); |
179 |
return null; |
180 |
} |
181 |
}); |
182 |
return tokenEncoded;
|
183 |
} catch (RpcException e) {
|
184 |
String error = "An error occurred while communicating with the service"; |
185 |
log.error(error, e); |
186 |
throw new Exception(e.getMessage()); |
187 |
} catch (UnsupportedEncodingException e) { |
188 |
log.error("", e);
|
189 |
throw new Exception(e.getMessage()); |
190 |
} catch (Exception e) { |
191 |
log.error("", e);
|
192 |
throw new Exception(e.getMessage()); |
193 |
} |
194 |
} |
195 |
|
196 |
|
197 |
|
198 |
|
199 |
} |