Add system-generated password for WebDAV. (Bug #705)
[pithos] / src / gr / ebs / gss / server / webdav / login / GssWebDAVLoginModule.java
1 /*\r
2  * Copyright 2005, 2008, 2009 Electronic Business Systems Ltd.\r
3  *\r
4  * This file is part of GSS.\r
5  *\r
6  * GSS is free software: you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation, either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * GSS is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.\r
18  */\r
19 package gr.ebs.gss.server.webdav.login;\r
20 \r
21 import static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration;\r
22 import gr.ebs.gss.client.exceptions.RpcException;\r
23 import gr.ebs.gss.server.domain.User;\r
24 import gr.ebs.gss.server.ejb.ExternalAPI;\r
25 \r
26 import java.security.Principal;\r
27 import java.security.acl.Group;\r
28 import java.util.HashSet;\r
29 \r
30 import javax.naming.Context;\r
31 import javax.naming.InitialContext;\r
32 import javax.naming.NamingException;\r
33 import javax.rmi.PortableRemoteObject;\r
34 import javax.security.auth.login.FailedLoginException;\r
35 import javax.security.auth.login.LoginException;\r
36 \r
37 import org.apache.commons.logging.Log;\r
38 import org.apache.commons.logging.LogFactory;\r
39 import org.jboss.security.auth.spi.UsernamePasswordLoginModule;\r
40 \r
41 \r
42 /**\r
43  * The custom login module for the GSS WebDAV implementation.\r
44  */\r
45 public class GssWebDAVLoginModule extends UsernamePasswordLoginModule {\r
46 \r
47         /**\r
48          * Logger for this class\r
49          */\r
50         private static final Log logger = LogFactory.getLog(GssWebDAVLoginModule.class);\r
51 \r
52         /**\r
53          * A helper method that retrieves a reference to the ExternalAPI bean and\r
54          * stores it for future use.\r
55          *\r
56          * @return an ExternalAPI instance\r
57          * @throws RpcException in case an error occurs\r
58          */\r
59         private ExternalAPI getService() throws RpcException {\r
60                 try {\r
61                         final Context ctx = new InitialContext();\r
62                         final Object ref = ctx.lookup(getConfiguration().getString("externalApiPath"));\r
63                         return (ExternalAPI) PortableRemoteObject.narrow(ref, ExternalAPI.class);\r
64                 } catch (final NamingException e) {\r
65                         logger.error("Unable to retrieve the ExternalAPI EJB", e);\r
66                         throw new RpcException("An error occurred while contacting the naming service");\r
67                 }\r
68         }\r
69 \r
70         @Override\r
71         protected String getUsersPassword() throws LoginException {\r
72                 String username = getUsername();\r
73                 try {\r
74                         User user = getService().findUser(username);\r
75                         if (user==null) throw new FailedLoginException("User '"+username+"' not found.");\r
76                         return user.getWebDAVPassword();\r
77                 } catch (RpcException e) {\r
78                         String error = "An error occurred while communicating with the service";\r
79                         logger.error(error, e);\r
80                         throw new LoginException(e.getMessage());\r
81                 }\r
82         }\r
83 \r
84         /**\r
85          * Overrides parent's implementation by returning only the simpleUser\r
86          * role for any successful login.\r
87          *\r
88          * @return Group[] that contains only the authenticatedUser group (role)\r
89          * @throws LoginException\r
90          * @see org.jboss.security.auth.spi.AbstractServerLoginModule#getRoleSets()\r
91          */\r
92         @Override\r
93         protected Group[] getRoleSets() throws LoginException {\r
94                 Principal principal;\r
95                 try {\r
96                         principal = createIdentity("simpleUser");\r
97                 } catch (Exception e) {\r
98                         logger.error("", e);\r
99                         throw new LoginException(e.getMessage());\r
100                 }\r
101                 Group rolesGroup = null;\r
102                 rolesGroup = createGroup("Roles", new HashSet());\r
103                 rolesGroup.addMember(principal);\r
104                 Group[] roles = new Group[1];\r
105                 roles[0] = rolesGroup;\r
106                 return roles;\r
107         }\r
108 \r
109 }\r