package de.bwlehrpool.bwlp_guac; import java.util.*; import org.apache.guacamole.GuacamoleException; import org.apache.guacamole.net.auth.*; import org.apache.guacamole.net.auth.credentials.GuacamoleCredentialsException; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.map.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletRequest; 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 { Logger LOGGER = LoggerFactory.getLogger(BwlpAuthenticationProvider.class); public String getIdentifier() { return "de.bwlehrpool.bwgpul"; } public Object getResource() throws GuacamoleException { return null; } public AuthenticatedUser authenticateUser(Credentials credentials) throws GuacamoleException { return null; } public AuthenticatedUser updateAuthenticatedUser(AuthenticatedUser authenticatedUser, Credentials credentials) throws GuacamoleException { return null; } public UserContext getUserContext(AuthenticatedUser authenticatedUser) throws GuacamoleException { LOGGER.warn("Ignoring getUserContext for " + authenticatedUser.toString()); return null; } public UserContext updateUserContext(UserContext context, AuthenticatedUser authenticatedUser, Credentials credentials) throws GuacamoleException { LOGGER.warn("Ignoring updateUserContext called with " + context.toString()); return null; } public UserContext decorate(UserContext context, AuthenticatedUser authenticatedUser, Credentials credentials) throws GuacamoleException { return context; } private Map oldMappings = Collections .synchronizedMap(new WeakHashMap()); 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 groupid = requestGroup(credentials); LOGGER.warn("Doing the REdecoration"); user = new BwlpUserContext(authenticatedUser, context, groupid); oldMappings.put(username, user); return user; } private int requestGroup(Credentials credentials) throws GuacamoleException { // Request the user to select a group ConnectionManager.updateList(); HttpServletRequest request = credentials.getRequest(); String groupJson = request.getParameter("group"); if (groupJson == null) { throw new GuacamoleInsufficientCredentialsException( "Select Location", new CredentialsInfo( Collections.singletonList(new GroupField()) )); } ObjectMapper mapper = new ObjectMapper(); String message = "Select a Location"; int selectedId = 0; boolean tryAgain = false; String password = ""; String correctPassword = null; try { JsonNode group = mapper.readTree(groupJson); selectedId = Integer.parseInt(group.get("id").asText()); if (selectedId != 0) { password = group.get("password").asText(); correctPassword = ConnectionManager.getGroupPool().get(selectedId).password; } } catch (Exception e) { LOGGER.info("Error reading group", e); tryAgain = true; } if (selectedId != 0 && correctPassword != null && !password.equals(correctPassword)) { tryAgain = true; message = "Wrong password!"; } if (tryAgain) { throw new GuacamoleCredentialsException( message, new CredentialsInfo( Collections.singletonList(new GroupField()) )); } return selectedId; } public void shutdown() { } }