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.Settings;
23  import davmail.http.HttpClientAdapter;
24  import org.apache.log4j.Logger;
25  
26  import java.io.IOException;
27  import java.net.URI;
28  
29  /**
30   * Experimental: load Oauth2 token from settings
31   */
32  @SuppressWarnings("unused")
33  public class O365StoredTokenAuthenticator implements ExchangeAuthenticator {
34      private static final Logger LOGGER = Logger.getLogger(O365StoredTokenAuthenticator.class);
35  
36      URI ewsUrl = URI.create(Settings.getO365Url());
37  
38      private String username;
39      private String password;
40      private O365Token token;
41  
42      @Override
43      public void setUsername(String username) {
44          this.username = username;
45      }
46  
47      @Override
48      public void setPassword(String password) {
49          this.password = password;
50      }
51  
52      /**
53       * Return a pool enabled HttpClientAdapter instance to access O365
54       * @return HttpClientAdapter instance
55       */
56      @Override
57      public HttpClientAdapter getHttpClientAdapter() {
58          return new HttpClientAdapter(getExchangeUri(), username, password, true);
59      }
60  
61      @Override
62      public void authenticate() throws IOException {
63          // common DavMail client id
64          final String clientId = Settings.getProperty("davmail.oauth.clientId", "facd6cff-a294-4415-b59f-c5b01937d7bd");
65          // standard native app redirectUri
66          final String redirectUri = Settings.getProperty("davmail.oauth.redirectUri", Settings.getO365LoginUrl()+"/common/oauth2/nativeclient");
67          // company tenantId or common
68          String tenantId = Settings.getProperty("davmail.oauth.tenantId", "common");
69  
70          String refreshToken = Settings.getProperty("davmail.oauth."+username.toLowerCase()+".refreshToken");
71          if (refreshToken == null) {
72              // single user mode
73              refreshToken = Settings.getProperty("davmail.oauth.refreshToken");
74          }
75          String accessToken = Settings.getProperty("davmail.oauth.accessToken");
76          if (refreshToken == null && accessToken == null) {
77              LOGGER.warn("No stored Oauth refresh token found for "+username);
78              throw new IOException("No stored Oauth refresh token found for "+username);
79          }
80  
81          token = new O365Token(tenantId, clientId, redirectUri, password);
82          if (accessToken != null) {
83              // for tests only: load access token, will expire in at most one hour
84              token.setAccessToken(accessToken);
85          } else {
86              token.setRefreshToken(refreshToken);
87              token.refreshToken();
88          }
89      }
90  
91      @Override
92      public O365Token getToken() {
93          return token;
94      }
95  
96      @Override
97      public URI getExchangeUri() {
98          return ewsUrl;
99      }
100 }