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.Settings;
24  import davmail.ui.tray.SwtGatewayTray;
25  import org.apache.log4j.Logger;
26  import org.eclipse.swt.SWT;
27  import org.eclipse.swt.browser.Browser;
28  import org.eclipse.swt.browser.LocationEvent;
29  import org.eclipse.swt.browser.LocationListener;
30  import org.eclipse.swt.layout.FillLayout;
31  import org.eclipse.swt.widgets.Display;
32  import org.eclipse.swt.widgets.Shell;
33  
34  import java.util.concurrent.locks.Condition;
35  import java.util.concurrent.locks.ReentrantLock;
36  
37  /**
38   * Alternative embedded browser implementation based on SWT instead of OpenJFX
39   */
40  public class O365InteractiveAuthenticatorSWT implements O365InteractiveAuthenticatorBackend {
41  
42      private static final Logger LOGGER = Logger.getLogger(O365InteractiveAuthenticatorSWT.class);
43  
44      private O365InteractiveAuthenticator authenticator;
45  
46      Shell shell;
47      Browser browser;
48      private final ReentrantLock lock = new ReentrantLock();
49      private final Condition ready = lock.newCondition();
50      private boolean isReady = false;
51      private Error error;
52  
53      public O365InteractiveAuthenticatorSWT() {
54  
55      }
56  
57      public void setO365InteractiveAuthenticator(O365InteractiveAuthenticator authenticator) {
58          this.authenticator = authenticator;
59      }
60  
61      public void authenticate(final String initUrl, final String redirectUri) {
62          // initialize SWT
63          SwtGatewayTray.initDisplay();
64  
65          // allow SSO on Windows
66          System.setProperty("org.eclipse.swt.browser.Edge.allowSingleSignOnUsingOSPrimaryAccount",
67                  Settings.getProperty("davmail.oauth.allowSingleSignOnUsingOSPrimaryAccount", "true"));
68  
69          Display.getDefault().asyncExec(() -> {
70              try {
71                  shell = new Shell(Display.getDefault());
72                  shell.setText(BundleMessage.format("UI_DAVMAIL_GATEWAY"));
73                  shell.setSize(600, 600);
74  
75                  shell.setLayout(new FillLayout());
76  
77                  shell.setImage(SwtGatewayTray.loadSwtImage("tray.png", 32));
78  
79                  shell.addListener(SWT.Close, event -> {
80                      if (!authenticator.isAuthenticated && authenticator.errorCode == null) {
81                          authenticator.errorCode = "user closed authentication window";
82                      }
83                      close();
84                  });
85  
86                  browser = new Browser(shell, SWT.NONE);
87  
88                  browser.setUrl(initUrl);
89                  browser.addTitleListener(titleEvent -> shell.setText("DavMail: " + titleEvent.title));
90                  browser.addLocationListener(new LocationListener() {
91                      @Override
92                      public void changing(LocationEvent locationEvent) {
93                          LOGGER.debug("Navigate to " + locationEvent.toString());
94                          String location = locationEvent.location;
95  
96                          if (location.startsWith(redirectUri)) {
97  
98                              LOGGER.debug("Location starts with redirectUri, check code");
99                              authenticator.handleCode(location);
100 
101                             shell.close();
102                             close();
103                         }
104 
105                     }
106 
107                     @Override
108                     public void changed(LocationEvent locationEvent) {
109                         LOGGER.debug("Page changed: " + locationEvent.toString());
110                     }
111                 });
112 
113                 lock.lock();
114                 try {
115                     isReady = true;
116                     ready.signalAll();
117                 } finally {
118                     lock.unlock();
119                 }
120 
121                 shell.open();
122                 shell.setActive();
123             } catch (Throwable e) {
124                 LOGGER.error("Error in SWT thread", e);
125                 lock.lock();
126                 try {
127                     error = new Error(e);
128                     ready.signalAll();
129                 } finally {
130                     lock.unlock();
131                 }
132             }
133 
134         });
135 
136         lock.lock();
137         try {
138             while (!isReady) {
139                 ready.await();
140                 if (error != null) {
141                     throw error;
142                 }
143             }
144         } catch (InterruptedException e) {
145             Thread.currentThread().interrupt();
146         } finally {
147             lock.unlock();
148         }
149     }
150 
151     public boolean isVisible() {
152         return true;
153     }
154 
155     public void close() {
156         Display.getDefault().asyncExec(() -> {
157             if (browser != null) {
158                 browser.dispose();
159             }
160             if (shell != null) {
161                 shell.dispose();
162             }
163         });
164     }
165 }