diff options
Diffstat (limited to 'src/main/java/de/bwlehrpool/bwlp_guac/BwlpAuthenticationProvider.java')
-rw-r--r-- | src/main/java/de/bwlehrpool/bwlp_guac/BwlpAuthenticationProvider.java | 92 |
1 files changed, 82 insertions, 10 deletions
diff --git a/src/main/java/de/bwlehrpool/bwlp_guac/BwlpAuthenticationProvider.java b/src/main/java/de/bwlehrpool/bwlp_guac/BwlpAuthenticationProvider.java index 4ec4f48..b902621 100644 --- a/src/main/java/de/bwlehrpool/bwlp_guac/BwlpAuthenticationProvider.java +++ b/src/main/java/de/bwlehrpool/bwlp_guac/BwlpAuthenticationProvider.java @@ -1,16 +1,25 @@ package de.bwlehrpool.bwlp_guac; -import java.util.Collections; -import java.util.Map; -import java.util.WeakHashMap; +import java.io.IOException; +import java.util.*; import org.apache.guacamole.GuacamoleException; -import org.apache.guacamole.net.auth.AuthenticatedUser; -import org.apache.guacamole.net.auth.AuthenticationProvider; -import org.apache.guacamole.net.auth.Credentials; -import org.apache.guacamole.net.auth.UserContext; +import org.apache.guacamole.form.TextField; +import org.apache.guacamole.net.auth.*; +import org.apache.guacamole.net.auth.credentials.GuacamoleCredentialsException; +import org.apache.guacamole.net.auth.credentials.GuacamoleInvalidCredentialsException; +import org.codehaus.jackson.JsonGenerationException; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.map.JsonMappingException; +import org.codehaus.jackson.map.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.servlet.http.HttpServletRequest; +import javax.xml.soap.Text; + +import org.apache.guacamole.form.Field; +import org.apache.guacamole.net.auth.credentials.GuacamoleInsufficientCredentialsException; +import org.apache.guacamole.net.auth.credentials.CredentialsInfo; public class BwlpAuthenticationProvider implements AuthenticationProvider { @@ -44,7 +53,7 @@ public class BwlpAuthenticationProvider implements AuthenticationProvider { } public UserContext updateUserContext(UserContext context, AuthenticatedUser authenticatedUser, - Credentials credentials) throws GuacamoleException { + Credentials credentials) throws GuacamoleException { LOGGER.warn("Ignoring updateUserContext called with " + context.toString()); return null; } @@ -52,13 +61,19 @@ public class BwlpAuthenticationProvider implements AuthenticationProvider { public UserContext decorate(UserContext context, AuthenticatedUser authenticatedUser, Credentials credentials) throws GuacamoleException { String username = authenticatedUser.getCredentials().getUsername(); + LOGGER.warn("decorate called for " + username); BwlpUserContext user = oldMappings.get(username); + if (user != null) return user; + + int locationid = requestLocation(credentials); + LOGGER.warn("Doing the decoration"); - user = new BwlpUserContext(authenticatedUser, context); + user = new BwlpUserContext(authenticatedUser, context, locationid); oldMappings.put(username, user); + return user; } @@ -67,17 +82,74 @@ public class BwlpAuthenticationProvider implements AuthenticationProvider { public UserContext redecorate(UserContext decorated, UserContext context, AuthenticatedUser authenticatedUser, Credentials credentials) throws GuacamoleException { + String username = authenticatedUser.getCredentials().getUsername(); + LOGGER.warn("REdecorate called for " + username); BwlpUserContext user = oldMappings.get(username); + if (user != null && user.hasValidConnection()) return user; + + int locationid = requestLocation(credentials); + LOGGER.warn("Doing the REdecoration"); - user = new BwlpUserContext(authenticatedUser, context); + user = new BwlpUserContext(authenticatedUser, context, locationid); oldMappings.put(username, user); + return user; } + + private int requestLocation(Credentials credentials) throws GuacamoleException { + // Request the user to select a location + ConnectionManager.updateList(); + HttpServletRequest request = credentials.getRequest(); + String locationJson = request.getParameter("location"); + + if (locationJson == null) { + throw new GuacamoleInsufficientCredentialsException( + "Select Location", new CredentialsInfo( + Collections.<Field>singletonList(new LocationField()) + )); + } + + ObjectMapper mapper = new ObjectMapper(); + + String message = "Select a Location"; + + int selectedId = 0; + boolean tryAgain = false; + String password = ""; + String correctPassword = null; + try { + JsonNode location = mapper.readTree(locationJson); + selectedId = Integer.parseInt(location.get("id").asText()); + if (selectedId != 0) { + password = location.get("password").asText(); + correctPassword = ConnectionManager.getLocationPool().get(selectedId).password; + } + } catch (Exception e) { + LOGGER.info("Error reading location"); + LOGGER.info(e.toString()); + tryAgain = true; + } + + if (selectedId != 0 && correctPassword != null && !password.equals(correctPassword)) { + tryAgain = true; + message = "Wrong password!"; + } + + if (tryAgain) { + throw new GuacamoleCredentialsException( + message, new CredentialsInfo( + Collections.<Field>singletonList(new LocationField()) + )); + } + + return selectedId; + } + public void shutdown() { } |