Working login + connection list UI stub.
[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 java.io.IOException;
22 import javax.servlet.ServletException;
23 import javax.servlet.http.HttpServlet;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26 import javax.servlet.http.HttpSession;
27 import net.sourceforge.guacamole.GuacamoleException;
28 import net.sourceforge.guacamole.protocol.GuacamoleConfiguration;
29 import net.sourceforge.guacamole.properties.GuacamoleProperties;
30 import net.sourceforge.guacamole.net.basic.properties.BasicGuacamoleProperties;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class BasicLogin extends HttpServlet {
35
36     private Logger logger = LoggerFactory.getLogger(BasicLogin.class);
37     
38     private AuthenticationProvider authProvider;
39
40     @Override
41     public void init() throws ServletException {
42
43         // Get auth provider instance
44         try {
45             authProvider = GuacamoleProperties.getProperty(BasicGuacamoleProperties.AUTH_PROVIDER);
46         }
47         catch (GuacamoleException e) {
48             logger.error("Error getting authentication provider from properties.", e);
49             throw new ServletException(e);
50         }
51
52     }
53
54     @Override
55     protected void service(HttpServletRequest request, HttpServletResponse response)
56     throws IOException {
57
58         HttpSession httpSession = request.getSession(true);
59
60         // Retrieve username and password from parms
61         String username = request.getParameter("username");
62         String password = request.getParameter("password");
63
64         // Get authorized config
65         GuacamoleConfiguration config;
66         try {
67             config = authProvider.getAuthorizedConfiguration(username, password);
68         }
69         catch (GuacamoleException e) {
70             logger.error("Error retrieving authorized configuration for user {}.", username);
71             response.sendError(HttpServletResponse.SC_FORBIDDEN);
72             return;
73         }
74         
75         if (config == null) {
76             logger.warn("Failed login from {} for user \"{}\".", request.getRemoteAddr(), username);
77             response.sendError(HttpServletResponse.SC_FORBIDDEN);
78             return;
79         }
80
81         logger.info("Successful login from {} for user \"{}\".", request.getRemoteAddr(), username);
82
83         httpSession.setAttribute("GUAC_AUTH_CONFIGS", new Integer(0));
84
85     }
86
87 }
88