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          // first try to load stored token
71          token = O365Token.load(tenantId, clientId, redirectUri, username, password);
72          if (token == null) {
73              throw new IOException("No valid refresh token found for "+username);
74          }
75      }
76  
77      @Override
78      public O365Token getToken() {
79          return token;
80      }
81  
82      @Override
83      public URI getExchangeUri() {
84          return ewsUrl;
85      }
86  }