669c7de43cb82c377ec1b5b5b1abe94088237ddb
[guacamole.git] / src / main / java / net / sourceforge / guacamole / net / basic / BasicLogin.java
1 package net.sourceforge.guacamole.net.basic;
2
3 /*
4  *  Guacamole - Clientless Remote Desktop
5  *  Copyright (C) 2010  Michael Jumper
6  *
7  *  This program is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU Affero General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU Affero General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Affero General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 import net.sourceforge.guacamole.net.auth.UserConfiguration;
22 import net.sourceforge.guacamole.net.auth.AuthenticationProvider;
23 import java.io.IOException;
24 import javax.servlet.ServletException;
25 import javax.servlet.http.HttpServlet;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.servlet.http.HttpSession;
29 import net.sourceforge.guacamole.GuacamoleException;
30 import net.sourceforge.guacamole.properties.GuacamoleProperties;
31 import net.sourceforge.guacamole.net.basic.properties.BasicGuacamoleProperties;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class BasicLogin extends HttpServlet {
36
37     private Logger logger = LoggerFactory.getLogger(BasicLogin.class);
38     
39     private AuthenticationProvider authProvider;
40
41     @Override
42     public void init() throws ServletException {
43
44         // Get auth provider instance
45         try {
46             authProvider = GuacamoleProperties.getProperty(BasicGuacamoleProperties.AUTH_PROVIDER);
47         }
48         catch (GuacamoleException e) {
49             logger.error("Error getting authentication provider from properties.", e);
50             throw new ServletException(e);
51         }
52
53     }
54
55     @Override
56     protected void service(HttpServletRequest request, HttpServletResponse response)
57     throws IOException {
58
59         HttpSession httpSession = request.getSession(true);
60
61         // Retrieve username and password from parms
62         String username = request.getParameter("username");
63         String password = request.getParameter("password");
64
65         // Get authorized configs
66         UserConfiguration config;
67         try {
68             config = authProvider.getUserConfiguration(username, password);
69         }
70         catch (GuacamoleException e) {
71             logger.error("Error retrieving configuration for user {}.", username);
72             response.sendError(HttpServletResponse.SC_FORBIDDEN);
73             return;
74         }
75         
76         if (config == null) {
77             logger.warn("Failed login from {} for user \"{}\".", request.getRemoteAddr(), username);
78             response.sendError(HttpServletResponse.SC_FORBIDDEN);
79             return;
80         }
81
82         logger.info("Successful login from {} for user \"{}\".", request.getRemoteAddr(), username);
83
84         // Associate configs with session
85         httpSession.setAttribute("GUAC_USER_CONFIG", config);
86
87     }
88
89 }
90