43e8721d93d6ef47c01b3ba8cacbd5d4a553cf4d
[guacamole.git] / web / guacamole-default-webapp / src / main / java / net / sourceforge / guacamole / net / authentication / basic / BasicLogin.java
1
2 package net.sourceforge.guacamole.net.authentication.basic;
3
4 /*
5  *  Guacamole - Clientless Remote Desktop
6  *  Copyright (C) 2010  Michael Jumper
7  *
8  *  This program is free software: you can redistribute it and/or modify
9  *  it under the terms of the GNU Affero General Public License as published by
10  *  the Free Software Foundation, either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU Affero General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Affero General Public License
19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 import java.io.IOException;
23 import java.lang.reflect.InvocationTargetException;
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.net.Configuration;
31
32 public class BasicLogin extends HttpServlet {
33
34     private Config config;
35
36     @Override
37     public void init() throws ServletException {
38         try {
39             config = new Config();
40         }
41         catch (GuacamoleException e) {
42             throw new ServletException(e);
43         }
44     }
45
46
47     private class Config extends Configuration {
48
49         private AuthenticationProvider authProvider;
50
51         public Config() throws GuacamoleException {
52
53             // Get auth provider instance
54             try {
55                 String authProviderClassName = readParameter("auth-provider");
56                 Object obj = Class.forName(authProviderClassName).getConstructor().newInstance();
57                 if (!(obj instanceof AuthenticationProvider))
58                     throw new GuacamoleException("Specified session provider class is not a GuacamoleSessionProvider");
59
60                 authProvider = (AuthenticationProvider) obj;
61             }
62             catch (ClassNotFoundException e) {
63                 throw new GuacamoleException("Session provider class not found", e);
64             }
65             catch (NoSuchMethodException e) {
66                 throw new GuacamoleException("Default constructor for session provider not present", e);
67             }
68             catch (SecurityException e) {
69                 throw new GuacamoleException("Creation of session provider disallowed; check your security settings", e);
70             }
71             catch (InstantiationException e) {
72                 throw new GuacamoleException("Unable to instantiate session provider", e);
73             }
74             catch (IllegalAccessException e) {
75                 throw new GuacamoleException("Unable to access default constructor of session provider", e);
76             }
77             catch (InvocationTargetException e) {
78                 throw new GuacamoleException("Internal error in constructor of session provider", e.getTargetException());
79             }
80
81         }
82
83         public AuthenticationProvider getAuthenticationProvider() {
84             return authProvider;
85         }
86
87     }
88
89     public static interface AuthenticationProvider {
90         public AuthorizedConfiguration getAuthorizedConfiguration(String username, String password) throws GuacamoleException;
91     }
92
93     // Added to session when session validated
94     public static class AuthorizedConfiguration {
95
96         private String protocol;
97         private String hostname;
98         private int port;
99         private String password;
100
101         public AuthorizedConfiguration(String protocol, String hostname, int port, String password) {
102             this.protocol = protocol;
103             this.hostname = hostname;
104             this.port = port;
105             this.password = password;
106         }
107
108         public String getHostname() {
109             return hostname;
110         }
111
112         public String getPassword() {
113             return password;
114         }
115
116         public int getPort() {
117             return port;
118         }
119
120         public String getProtocol() {
121             return protocol;
122         }
123
124     }
125
126     @Override
127     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
128
129         // Retrieve username and password from parms
130         String username = req.getParameter("username");
131         String password = req.getParameter("password");
132
133         // Validate username and password
134         try {
135
136             AuthorizedConfiguration info = config.getAuthenticationProvider().getAuthorizedConfiguration(username, password);
137             if (info != null) {
138
139                 // Store authorized configuration
140                 HttpSession session = req.getSession(true);
141                 session.setAttribute(
142                     "BASIC-LOGIN-AUTH",
143                     info
144                 );
145
146                 // Success
147                 return;
148
149             }
150
151             // Report "forbidden" on any failure
152             resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Login invalid");
153         }
154         catch (GuacamoleException e) {
155             throw new ServletException("Error validating credentials", e);
156         }
157
158     }
159
160
161 }