package gui.intro; import java.awt.Color; import java.awt.Component; import java.awt.Image; import java.awt.SystemColor; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.List; import java.util.concurrent.ExecutionException; import javax.swing.ButtonGroup; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JRadioButton; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.SwingWorker; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.border.EmptyBorder; import javax.swing.border.TitledBorder; import models.SessionData; import models.person; import org.apache.log4j.Logger; import org.apache.thrift.TException; import org.openslx.imagemaster.thrift.iface.ImageServer.Client; import org.openslx.imagemaster.thrift.iface.UserInfo; //import server.generated.Server.Client; import thrift.MasterThriftConnection; import thrift.ThriftConnection; import util.GuiOrganizer; import util.ResourceLoader; import util.ShibbolethECP; import config.Config; @SuppressWarnings("serial") public class Login_GUI extends JFrame { /** * Logger instance for this class. */ private final static Logger LOGGER = Logger.getLogger(Login_GUI.class); // String constants representing the different login methods private final static String LOGIN_TYPE_BWIDM = "bwidm"; private final static String LOGIN_TYPE_BWLEHRPOOL = "bwlp"; private final static String LOGIN_TYPE_SATELLITE = "sat"; private JPanel contentPane; private JTextField lblusername; private JPasswordField lblpass; JLabel lblConnectionInfo = new JLabel(""); boolean isClientReturnedCorrectly = false; JCheckBox chckbxBenutzernameSpeichern; private static final String HELP_MESSAGE = ""; Component c = null; String[] args = null; private JPanel panel; private JPanel panel_1; private JLabel LabelUser; private JLabel LabelPass; // radio button and group for login types. private ButtonGroup bgLoginType; private JRadioButton rdbtnBwIDM; private JRadioButton rdbtnDirekteVerbindung; private JRadioButton rdbtnMasterserver; // its action listener private ActionListener loginTypeActionListener; // bwIDM label and dropdown private JComboBox choice; private JLabel lblIdp; // Satellite IP label and text field private JLabel lblSatellitenIp; private JTextField lblSatIP; /** * Create the frame. */ public Login_GUI(String[] args) { addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); this.args = args; // Fenster darf nicht vergroessert werden setResizable(false); try { // Setzt das Look and Feel auf System UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { e.printStackTrace(); } // Titel des Fensters setzen setTitle("bwLehrpool Suite"); // Aktion die beim Schliessen durchgefuehrt werden soll setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Größe setzen und mittig ausrichten setBounds(0, 0, 785, 430); // Erzeugen eines Panels contentPane = new JPanel(); // Hintergrund Farbe des Panels setzen contentPane.setBackground(SystemColor.menu); // Rahmen des Fensters setzen contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); // Layout setzen contentPane.setLayout(null); // Label fuer das Logo erzeugen JLabel imgLabel = new JLabel(); // Groesse und Position des Logos festelegen imgLabel.setBounds(231, 11, 350, 142); // Pfadangabe des Logos ImageIcon icon = ResourceLoader.getIcon("/img/Logo_bwLehrpool.png", "Logo"); // Skalierung des Logos Image scaled = icon.getImage().getScaledInstance(270, 64, 0); imgLabel.setIcon(new ImageIcon(scaled)); // Hinzufuegen des Logos in das Fenster contentPane.add(imgLabel); lblConnectionInfo.setHorizontalAlignment(SwingConstants.LEFT); lblConnectionInfo.setBounds(10, 359, 449, 20); lblConnectionInfo.setEnabled(true); lblConnectionInfo.setText("Info: Bereit für Login"); contentPane.add(lblConnectionInfo); // Erzeugen, Hinzufuegen und definierung der Aktion des Buttons JButton BtnLogin = new JButton("Login"); BtnLogin.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { performLogin(bgLoginType.getSelection().getActionCommand()); }// end actionPerformed }); BtnLogin.setBounds(488, 358, 239, 23); contentPane.add(BtnLogin); // Start of the actual content of the right panel panel = new JPanel(); panel.setLayout(null); panel.setBorder(new TitledBorder(null, "Zugangsdaten", TitledBorder.LEADING, TitledBorder.TOP, null, null)); panel.setBackground(SystemColor.menu); panel.setBounds(354, 143, 400, 181); contentPane.add(panel); chckbxBenutzernameSpeichern = new JCheckBox("Benutzername speichern"); chckbxBenutzernameSpeichern.setBounds(135, 137, 237, 23); chckbxBenutzernameSpeichern.setSelected(Config.getSaveUsername()); panel.add(chckbxBenutzernameSpeichern); // Erzeugen und Hinzufuegen des Labels LabelUser = new JLabel("Benutzername:"); LabelUser.setBounds(25, 60, 82, 20); panel.add(LabelUser); // Erzeugen und Hinzufuegen des Textfeldes lblusername = new JTextField(); lblusername.setBounds(135, 60, 237, 20); lblusername.setToolTipText("Bitte geben Sie Ihren bwIDM-Benutzernamen ein."); lblusername.setColumns(10); // Lese Benutzername aus der Konfigurationsdatei lblusername.setText(Config.getUsername()); lblusername.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { performLogin(bgLoginType.getSelection().getActionCommand()); } }); panel.add(lblusername); // Erzeugen und Hinzufuegen des Labels LabelPass = new JLabel("Passwort:"); LabelPass.setBounds(25, 85, 82, 20); panel.add(LabelPass); // Erzeugen und Hinzufuegen des Passwortfeldes lblpass = new JPasswordField(); lblpass.setBounds(135, 85, 237, 20); lblpass.setToolTipText("Bitte geben Sie Ihr bwIDM-Passwort ein."); lblpass.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { performLogin(bgLoginType.getSelection().getActionCommand()); } }); panel.add(lblpass); lblSatellitenIp = new JLabel("Satelliten IP:"); lblSatellitenIp.setBounds(25, 105, 82, 20); //panel.add(lblSatellitenIp); lblSatIP = new JTextField(); lblSatIP.setBounds(135, 110, 237, 20); lblSatIP.setToolTipText("Bitte geben Sie die IP-Adresse des Satelliten ein."); //panel.add(lblSatIP); String organisations[] = { "https://testidp3-dev.aai.dfn.de/idp/profile/SAML2/SOAP/ECP" }; choice = new JComboBox(organisations); choice.setBounds(135, 35, 237, 20); //panel.add(choice); lblIdp = new JLabel("IDP:"); lblIdp.setBounds(25, 35, 82, 20); //panel.add(lblIdp); // action listener for the radio buttons to // toggle the gui elements depending on the login type loginTypeActionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { toggleLoginType(e.getActionCommand()); } }; panel_1 = new JPanel(); panel_1.setLayout(null); panel_1.setBorder(new TitledBorder( UIManager.getBorder("TitledBorder.border"), "Zugangsart", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0))); panel_1.setBackground(SystemColor.menu); panel_1.setBounds(10, 143, 334, 181); contentPane.add(panel_1); // group radio button for the login type together bgLoginType = new ButtonGroup(); rdbtnBwIDM = new JRadioButton("Zugang über bwIDM"); rdbtnBwIDM.setBounds(32, 35, 244, 23); rdbtnBwIDM.setActionCommand(LOGIN_TYPE_BWIDM); rdbtnBwIDM.addActionListener(loginTypeActionListener); bgLoginType.add(rdbtnBwIDM); panel_1.add(rdbtnBwIDM); rdbtnMasterserver = new JRadioButton("Test-Zugang mit festem Benutzer"); rdbtnMasterserver.setSelected(true); rdbtnMasterserver.setActionCommand(LOGIN_TYPE_BWLEHRPOOL); rdbtnMasterserver.addActionListener(loginTypeActionListener); rdbtnMasterserver.setBounds(32, 60, 244, 23); bgLoginType.add(rdbtnMasterserver); panel_1.add(rdbtnMasterserver); rdbtnDirekteVerbindung = new JRadioButton("Direkte Verbindung zum Satelliten"); rdbtnDirekteVerbindung.setBounds(32, 85, 244, 23); rdbtnDirekteVerbindung.setActionCommand(LOGIN_TYPE_SATELLITE); rdbtnDirekteVerbindung.addActionListener(loginTypeActionListener); bgLoginType.add(rdbtnDirekteVerbindung); panel_1.add(rdbtnDirekteVerbindung); panel.setVisible(true); panel_1.setVisible(true); contentPane.setVisible(true); GuiOrganizer.centerGUI(this); c = this; }// end LoginGUI() // Versuche den Benutzer einzuloggen public void performLogin(final String loginType) { LOGGER.info("Trying login of type: " + loginType); if (loginType == LOGIN_TYPE_BWLEHRPOOL) { // start the authentication process against the masterserver org.openslx.imagemaster.thrift.iface.SessionData result = null; // Connect to Master MasterThriftConnection thrift = new MasterThriftConnection(); Client client = thrift.getMasterThriftConnection(); if (client == null) { // Thrift connection to master could not be established setStatus(Color.red, "Fehler: Masterserver nicht erreichbar.", null); LOGGER.info("Thrift client could not be initialized, is the masterserver up and running?"); return; } String passText = new String(lblpass.getPassword()); UserInfo user = null; // Log user in try { setStatus(Color.black, "Authentifiziere - warte auf Antwort von Server...", null); result = client.authenticate(lblusername.getText(), passText); // Sanity checks if (result.authToken == null || result.sessionId == null) { setStatus(Color.red, "Antwort des Masterservers korrupt.", null); LOGGER.info("Antwort des Masterservers korrupt"); return; } // User-Info LOGGER.info("Authentifizierung von '" + lblusername.getText() + "' erfolgreich."); user = client.getUserFromToken(result.authToken); } catch (org.openslx.imagemaster.thrift.iface.AuthenticationException e) { setStatus( Color.red, "Fehler: Zugangsdaten nicht akzeptiert.", "Benutzeranmeldung fehlgeschlagen: Bitte Benutzername und Passwort überprüfen.\n" + e.message); e.printStackTrace(); return; } catch (TException e) { e.printStackTrace(); setStatus(Color.red, "Kommunikation mit Masterserver fehlgeschlagen.", null); LOGGER.error("Thrift-communication to the masterserver failed, see trace: ", e); return; } // set Information SessionData.session.setAuthToken(result.authToken); SessionData.session.setSessionID(result.sessionId); SessionData.session.setServerAdress(result.serverAddress); // Satellite-Connection setStatus(Color.black, "Hole Verbindungsdaten von Server...", null); // set connection to bwLehrpoolSuiteServer ThriftConnection bwthrift = new ThriftConnection(); server.generated.Server.Client bwClient = bwthrift.getThriftConnection(); models.Client.clientcon.setClient(bwClient); // User-Info //LOGGER.debug("Session-ID: " + result.sessionId); //LOGGER.debug("Token: " + result.authToken); setStatus(Color.green, "Daten erhalten.", null); String hochschule = user.eMail.substring(user.eMail.indexOf("@") + 1); person.verantwortlicher.setUsername(lblusername.getText().trim()); person.verantwortlicher.setUserID(lblusername.getText().trim()); person.verantwortlicher.setName(user.lastName); person.verantwortlicher.setVorname(user.firstName); person.verantwortlicher.setEMail(user.eMail); person.verantwortlicher.setHochschule(hochschule); // Spaeter ueber result.getRole zum Beispiel die Rolle holen if(lblusername.getText().trim().startsWith("admin@")){ person.verantwortlicher.setRole("Admin"); } else { person.verantwortlicher.setRole("Dozent"); //person.verantwortlicher.setRole("Student"); } showMainMenu(); //} } // end bwLehrpool-Login if (loginType == LOGIN_TYPE_BWIDM) { // try the shibbo login in its own SwingWorker to avoid GUI lockups SwingWorker worker = new SwingWorker(){ @Override protected Boolean doInBackground() throws Exception { publish("Info: Initialisiere Shibboleth-Client ..."); ShibbolethECP.init(choice.getSelectedItem().toString()); publish("Info: Überprüfe Zugangdaten über bwIDM ..."); return ShibbolethECP.doLogin(lblusername.getText().trim(), String.valueOf(lblpass.getPassword())); } protected void done() { try { if (get()) { setStatus(Color.green, "Info: bwIDM-Login erfolgreich.", null); // TODO: call showNextStep() //showMainMenu(); JOptionPane.showMessageDialog(c, "Der Shibboleth-Login war erfolgreich! Der Rest ist noch in Entwicklung :)", "bwIDM-Login erfolgreich", JOptionPane.PLAIN_MESSAGE); // all done, show main menu showMainMenu(); } else { setStatus(Color.red, "Fehler: bwIDM-Login fehlgeschlagen!", null); } } catch (InterruptedException e) { LOGGER.error("SwingWorker for ShibbolethECP got interrupted, see trace: ", e); } catch (ExecutionException e) { LOGGER.error("Execution of SwingWorker for ShibbolethECP failed, see trace: ", e); } } @Override protected void process(List msgs) { String currentMessage = msgs.get(msgs.size()-1); setStatus(Color.black, currentMessage, null); } }; worker.execute(); // now we fake the user information for testing purposes. // TODO parse the response from the SP and set the correct user info person.verantwortlicher.setUsername(lblusername.getText().trim()); person.verantwortlicher.setUserID(lblusername.getText().trim()); person.verantwortlicher.setName("Joe"); person.verantwortlicher.setVorname("B"); person.verantwortlicher.setEMail("spam@aol.com"); person.verantwortlicher.setHochschule("fr-test.de"); person.verantwortlicher.setRole("Dozent"); SessionData.session.setServerAdress("132.230.8.199"); } // always save to user's config file. if (chckbxBenutzernameSpeichern.isSelected()) { Config.setUsername(lblusername.getText()); Config.setSaveUsername(true); } else { Config.setUsername(""); Config.setSaveUsername(false); } // save it to local disk Config.store(); }// end performLogin /** * Shows the next GUI's til the main menu. * It checks if the user has already seen the bill of rights * and/or the vmware license windows and shows them before * continuing to the main menu. * * This function should be called on a successful login. */ private void showMainMenu() { /* * TODO check if user has already been regisistered. if not, do so * * */ //import server.generated.Server.Client; server.generated.Server.Client client_2 = models.Client.clientcon.getClient(); try { String pk_institution = client_2.setInstitution(person.verantwortlicher.getHochschule()); //LOGGER.info("Institution of user is: "+pk_institution); client_2.setPerson(person.verantwortlicher.getUserID(), person.verantwortlicher.getName(), person.verantwortlicher.getVorname(), person.verantwortlicher.getEMail(), pk_institution); //LOGGER.info("user data, ID="+person.verantwortlicher.getUserID()+" , Name="+person.verantwortlicher.getName()+" , Vorname="+ person.verantwortlicher.getVorname()+" , E-Mail="+ person.verantwortlicher.getEMail()+" , Institution="+ pk_institution); } catch (TException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Pruefe ob Bills Of Rights schon akzeptiert wurden, // wenn nicht zeige diese an if (!Config.getBillOfRights()) { // Erstellen einer Instanz der Aktionsauswahl BillOfRights_GUI re = new BillOfRights_GUI(); re.setVisible(true); // Schliessen des Fensters nach erfolgreichen Login setVisible(false); } else { // Pruefe ob die Links zu vmware angezeigt werden // sollen if (!Config.getVmwareLicense()) { VmWareLink_GUI vm = new VmWareLink_GUI(); vm.setVisible(true); setVisible(false); } else { // oeffne das Hauptmenue MainMenue_GUI main = new MainMenue_GUI(c); main.setVisible(true); setVisible(false); } } } /** * Set connection status message, optionally show message box. * * @param color * Color to display status message in. * @param shortMessage * The message to display. * @param longMessage * Optional message to show in a message box. null means don't * show. */ private void setStatus(Color color, String shortMessage, String longMessage) { LOGGER.info(shortMessage); lblConnectionInfo.setForeground(color); lblConnectionInfo.setText(shortMessage); if (longMessage != null) JOptionPane.showMessageDialog(c, longMessage, "Anmeldung fehlgeschlagen", JOptionPane.ERROR_MESSAGE); } /** * Toggle the elements needed for the different login types. * * @param type * Type of the login to toggle. Valid are: * - LOGIN_TYPE_BWIDM * - LOGIN_TYPE_BWLEHRPOOL * - LOGIN_TYPE_SATELLITE */ private void toggleLoginType(String type) { LOGGER.info("Toggling to login method to: " + type); panel.setVisible(false); switch(type) { case LOGIN_TYPE_BWIDM: panel.remove(lblSatIP); panel.remove(lblSatellitenIp); panel.add(lblIdp); panel.add(choice); panel.add(LabelPass); lblpass.setText(""); lblpass.setToolTipText("Bitte geben Sie Ihr bwIDM-Passwort ein."); panel.add(lblpass); lblusername.setToolTipText("Bitte geben Sie Ihr bwIDM-Benutzername ein."); panel.revalidate(); panel.repaint(); panel.setVisible(true); break; case LOGIN_TYPE_BWLEHRPOOL: panel.remove(choice); panel.remove(lblIdp); panel.remove(lblSatIP); panel.remove(lblSatellitenIp); panel.add(LabelPass); lblpass.setToolTipText("Bitte geben Sie Ihr bwLehrpool-Passwort ein."); panel.add(lblpass); lblusername.setToolTipText("Bitte geben Sie Ihr bwLehrpool-Benutzername ein."); panel.revalidate(); panel.repaint(); panel.setVisible(true); break; case LOGIN_TYPE_SATELLITE: panel.remove(choice); panel.remove(lblIdp); panel.remove(LabelPass); panel.remove(lblpass); lblSatIP.setBounds(135, 85, 237, 20); lblSatIP.setText(""); panel.add(lblSatIP); lblSatellitenIp.setBounds(25, 85, 82, 20); panel.add(lblSatellitenIp); panel.revalidate(); panel.repaint(); panel.setVisible(true); break; default: LOGGER.error("Unknown action '" + type + "'. Ignoring."); panel.setVisible(true); return; } } }// end class