summaryrefslogblamecommitdiffstats
path: root/src/main/java/de/bwlehrpool/bwlp_guac/BwlpAuthenticationProvider.java
blob: 304333934dd8fe6d721d047ecaa0365a78ef37b2 (plain) (tree)
1
2
3
4
5
6
7
8
9

                                



                                  
                   
                                               
                                       
                                                                               
                                     
                                             

                               
                                             
                                       
                                                                                           
                                                                 


                                                                           




















                                                                                                        





                                                               
                                      


                                                                                                      













                                                                                                                      
                                                                                                                                     





                                                                                                                      
                               






                                                                                                                      
 

                                                                    
                                                                      



                                                                              
 

                                                                 
 

                                                              




                                                                                             
                                                                          
                                                              
                                                                                                                      
                 
                                                
 


                            






                                                                                              
 
                                                     

                                                                      
                                                                 
 
                                        


                                                                                                              
                                                                                          




                                                         
                                                         
 



                                              
                                                                    



                                                                                      
                                                                          
                                                                                                        

                                       
                                                                                              


                                        
                                                                                                            
                                        
                                                                   


                               


                                                                              
                                                                                          


                           
                                

         



                                
package de.bwlehrpool.bwlp_guac;

import java.net.URL;
import java.net.URLClassLoader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.net.auth.*;
import org.apache.guacamole.language.TranslatableGuacamoleCredentialsException;
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.language.TranslatableGuacamoleInsufficientCredentialsException;
import org.apache.guacamole.net.auth.credentials.CredentialsInfo;

public class BwlpAuthenticationProvider implements AuthenticationProvider {

	static Logger LOGGER = LoggerFactory.getLogger(BwlpAuthenticationProvider.class);

	static Properties MANIFEST = new Properties();

	static {
		URLClassLoader cl = (URLClassLoader) BwlpAuthenticationProvider.class.getClassLoader();
		URL url = cl.findResource("META-INF/MANIFEST.MF");
		try {
			MANIFEST.load(url.openStream());
			LOGGER.info("Plugin build revision: " + MANIFEST.getProperty("Build-Revision"));

			String timestamp = MANIFEST.getProperty("Build-Revision-Timestamp");
			DateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH:mm");
			long milliSeconds= Long.parseLong(timestamp);
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeInMillis(milliSeconds);
			LOGGER.info("Plugin build timestamp: " + formatter.format(calendar.getTime()));
		} catch (Exception e) {
			System.out.println("unable to read manifest " + e.getMessage());
		}
	}

	public String getIdentifier() {
		return "de.bwlehrpool.bwgpul";
	}

	public Object getResource() throws GuacamoleException {
		return new BwlpREST();
	}

	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<String, BwlpUserContext> oldMappings = Collections
			.synchronizedMap(new WeakHashMap<String, BwlpUserContext>());

	public UserContext redecorate(UserContext decorated, UserContext context, AuthenticatedUser authenticatedUser,
			Credentials credentials) throws GuacamoleException {

		LOGGER.info(MANIFEST.getProperty("Build-Revision"));

		String username = Util.getUsername(authenticatedUser);
		if (username == null) {
			LOGGER.warn("redecorate: Ignoring user without name");
			return context;
		}

		LOGGER.warn("REdecorate called for " + username);
		BwlpUserContext user = oldMappings.get(username);

		if (user != null && user.hasValidConnection())
			return user;
		
		WrappedConnection exConn = ConnectionManager.getExistingConnection(username);
		if (exConn != null) {
			user = new BwlpUserContext(authenticatedUser, context, exConn);
		} else {
			UserResponse response = requestGroup(credentials);
			LOGGER.warn("Doing the REdecoration");
			user = new BwlpUserContext(authenticatedUser, context, response.groupid, response.resolution);
		}
		oldMappings.put(username, user);

		return user;
	}

	final class UserResponse {
		public int groupid = 0;
		public String resolution = "1920x1080";
	}

	private UserResponse requestGroup(Credentials credentials) throws GuacamoleException {
		UserResponse response = new UserResponse();

		// Request the user to select a group
		ConnectionManager.updateList();
		HttpServletRequest request = credentials.getRequest();
		String groupJson = request.getParameter("group");

		if (groupJson == null) {
			LOGGER.warn("test 1");
			throw new TranslatableGuacamoleInsufficientCredentialsException(
					"GROUP_SELECTION.TITLE", "GROUP_SELECTION.TITLE", new CredentialsInfo(
					Collections.<Field>singletonList(new GroupField())
			));
		}

		ObjectMapper mapper = new ObjectMapper();

		String message = "GROUP_SELECTION.TITLE";

		boolean tryAgain = false;
		String password = "";
		String correctPassword = null;
		try {
			JsonNode group = mapper.readTree(groupJson);
			response.resolution = group.get("resolution").asText();

			response.groupid = Integer.parseInt(group.get("id").asText());
			if (response.groupid != 0) {
				password = group.get("password").asText();
				correctPassword = ConnectionManager.getGroup(response.groupid).password;
			}
		} catch (Exception e) {
			LOGGER.info("Error reading group choice by user, asking again...", e);
			tryAgain = true;
		}

		if (response.groupid != 0 && correctPassword != null && !password.equals(correctPassword)) {
			tryAgain = true;
			message = "GROUP_SELECTION.PASSWORD_ERROR";
		}

		if (tryAgain) {
			LOGGER.warn("test 2");
			throw new TranslatableGuacamoleCredentialsException(
					message, message, new CredentialsInfo(
					Collections.<Field>singletonList(new GroupField())
			));
		}

		return response;
	}

	public void shutdown() {
	}

}