View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2010  Mickael Guessant
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18   */
19  
20  package davmail.exchange.auth;
21  
22  import davmail.BundleMessage;
23  import davmail.ui.browser.DesktopBrowser;
24  import davmail.ui.tray.DavGatewayTray;
25  
26  import javax.swing.*;
27  import javax.swing.event.HyperlinkEvent;
28  import javax.swing.text.html.HTMLEditorKit;
29  import javax.swing.text.html.StyleSheet;
30  import java.awt.*;
31  import java.net.URISyntaxException;
32  import java.awt.datatransfer.Clipboard;
33  import java.awt.datatransfer.StringSelection;
34  
35  public class O365ManualAuthenticatorDialog extends JDialog {
36      final JTextField codeField = new JTextField(30);
37      protected String code;
38  
39      /**
40       * Get Oauth authentication code.
41       *
42       * @return authentication code
43       */
44      public String getCode() {
45          if (code != null && code.contains("code=") && code.contains("&session_state=")) {
46              code = code.substring(code.indexOf("code=")+5, code.indexOf("&session_state="));
47          }
48          return code;
49      }
50  
51      /**
52       * Get credentials.
53       *
54       * @param initUrl Kerberos prompt from callback handler
55       */
56      public O365ManualAuthenticatorDialog(String initUrl) {
57          setAlwaysOnTop(true);
58  
59          setTitle(BundleMessage.format("UI_O365_MANUAL_PROMPT"));
60  
61          try {
62              setIconImages(DavGatewayTray.getFrameIcons());
63          } catch (NoSuchMethodError error) {
64              DavGatewayTray.debug(new BundleMessage("LOG_UNABLE_TO_SET_ICON_IMAGE"));
65          }
66  
67          JPanel messagePanel = new JPanel();
68          messagePanel.setLayout(new BoxLayout(messagePanel, BoxLayout.X_AXIS));
69  
70          JLabel imageLabel = new JLabel();
71          imageLabel.setIcon(UIManager.getIcon("OptionPane.questionIcon"));
72          messagePanel.add(imageLabel);
73  
74          messagePanel.add(getEditorPane(BundleMessage.format("UI_0365_AUTHENTICATION_PROMPT", initUrl)));
75  
76  
77          JPanel credentialPanel = new JPanel();
78          credentialPanel.setLayout(new BoxLayout(credentialPanel, BoxLayout.X_AXIS));
79  
80          JLabel promptLabel = new JLabel(BundleMessage.format("UI_0365_AUTHENTICATION_CODE"));
81          promptLabel.setHorizontalAlignment(SwingConstants.RIGHT);
82          promptLabel.setVerticalAlignment(SwingConstants.CENTER);
83  
84          credentialPanel.add(promptLabel);
85  
86          codeField.setMaximumSize(codeField.getPreferredSize());
87          codeField.addActionListener(evt -> {
88              code = codeField.getText();
89              setVisible(false);
90          });
91          credentialPanel.add(codeField);
92  
93          JPanel centerPanel = new JPanel();
94          centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
95          centerPanel.add(messagePanel);
96          centerPanel.add(getOpenButtonPanel(initUrl));
97          centerPanel.add(getEditorPane(BundleMessage.format("UI_0365_AUTHENTICATION_CODE_PROMPT")));
98          centerPanel.add(credentialPanel);
99          centerPanel.add(Box.createVerticalGlue());
100 
101         //add(messagePanel, BorderLayout.NORTH);
102         add(centerPanel, BorderLayout.CENTER);
103         add(getSendButtonPanel(), BorderLayout.SOUTH);
104         setModal(true);
105 
106         pack();
107         // center frame
108         setLocation(getToolkit().getScreenSize().width / 2 -
109                         getSize().width / 2,
110                 getToolkit().getScreenSize().height / 2 -
111                         getSize().height / 2);
112         setAlwaysOnTop(true);
113         setVisible(true);
114     }
115 
116     private JEditorPane getEditorPane(String text) {
117         JEditorPane jEditorPane = new JEditorPane();
118         HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
119         StyleSheet stylesheet = htmlEditorKit.getStyleSheet();
120         Font font = jEditorPane.getFont();
121         stylesheet.addRule("body { font-size:small;font-family: " + ((font==null)?"Arial":font.getFamily()) + '}');
122         jEditorPane.setEditorKit(htmlEditorKit);
123         jEditorPane.setContentType("text/html");
124         jEditorPane.setText(text);
125 
126         jEditorPane.setEditable(false);
127         jEditorPane.setOpaque(false);
128         jEditorPane.addHyperlinkListener(hle -> {
129             if (HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType())) {
130                 try {
131                     DesktopBrowser.browse(hle.getURL().toURI());
132                 } catch (URISyntaxException e) {
133                     DavGatewayTray.error(new BundleMessage("LOG_UNABLE_TO_OPEN_LINK"), e);
134                 }
135             }
136         });
137         return jEditorPane;
138     }
139 
140     protected JPanel getOpenButtonPanel(final String initUrl) {
141         JPanel buttonPanel = new JPanel();
142         JButton openButton = new JButton(BundleMessage.format("UI_BUTTON_OPEN"));
143         JButton copyButton = new JButton(BundleMessage.format("UI_BUTTON_COPY"));
144         openButton.addActionListener(evt -> DesktopBrowser.browse(initUrl));
145         copyButton.addActionListener(evt -> {
146             Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
147             clipboard.setContents(new StringSelection(initUrl), null);
148         });
149 
150         buttonPanel.add(openButton);
151         buttonPanel.add(copyButton);
152         return buttonPanel;
153     }
154 
155     protected JPanel getSendButtonPanel() {
156         JPanel buttonPanel = new JPanel();
157         JButton sendButton = new JButton(BundleMessage.format("UI_BUTTON_SEND"));
158         JButton cancelButton = new JButton(BundleMessage.format("UI_BUTTON_CANCEL"));
159         sendButton.addActionListener(evt -> {
160             code = codeField.getText();
161             setVisible(false);
162         });
163         cancelButton.addActionListener(evt -> {
164             code = null;
165             setVisible(false);
166         });
167 
168         buttonPanel.add(sendButton);
169         buttonPanel.add(cancelButton);
170         return buttonPanel;
171     }
172 }