View Javadoc
1   /**
2    * This file Copyright (c) 2017 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.mail.smtp.authentication;
35  
36  import info.magnolia.keystore.registry.PasswordRegistry;
37  
38  import java.io.IOException;
39  
40  import javax.inject.Inject;
41  import javax.mail.Authenticator;
42  import javax.mail.PasswordAuthentication;
43  
44  import org.slf4j.Logger;
45  import org.slf4j.LoggerFactory;
46  
47  import com.google.api.client.auth.oauth2.TokenErrorResponse;
48  import com.google.api.client.auth.oauth2.TokenResponse;
49  import com.google.api.client.auth.oauth2.TokenResponseException;
50  import com.google.api.client.googleapis.auth.oauth2.GoogleRefreshTokenRequest;
51  import com.google.api.client.http.javanet.NetHttpTransport;
52  import com.google.api.client.json.jackson2.JacksonFactory;
53  
54  /**
55   * Oauth2 SMTP authentication.
56   */
57  public class GoogleOauthSmtpAuthentication implements SmtpAuthentication {
58  
59      private static final Logger log = LoggerFactory.getLogger(GoogleOauthSmtpAuthentication.class);
60  
61      private String user;
62      private String clientId;
63      private String refreshTokenKeyStoreId;
64      private String clientSecretKeyStoreId;
65  
66      private final PasswordRegistry passwordRegistry;
67  
68  
69      @Inject
70      public GoogleOauthSmtpAuthentication(PasswordRegistry passwordRegistry) {
71          this.passwordRegistry = passwordRegistry;
72      }
73  
74      public Authenticator getAuthenticator() {
75          return new Authenticator() {
76              @Override
77              protected PasswordAuthentication getPasswordAuthentication() {
78                  return new PasswordAuthentication(getUser(), getAccessToken());
79              }
80          };
81      }
82  
83      /**
84       * Obtain new access token from the refresh token & client credentials.
85       */
86      private String getAccessToken() {
87          try {
88              final TokenResponse response = new GoogleRefreshTokenRequest(new NetHttpTransport(), new JacksonFactory(), passwordRegistry.getPassword(getRefreshTokenKeyStoreId()).getDecryptedValue(), getClientId(), passwordRegistry.getPassword(getClientSecretKeyStoreId()).getDecryptedValue()).execute();
89              return response.getAccessToken();
90          } catch (IOException e) {
91              log.debug("Error while requesting the access token.", e);
92              if (e instanceof TokenResponseException) {
93                  TokenErrorResponse details = ((TokenResponseException) e).getDetails();
94                  if (details != null) {
95                      log.debug(details.getError());
96                  }
97              }
98              throw new RuntimeException(e);
99          }
100     }
101 
102     @Override
103     public String getUser() {
104         return user;
105     }
106 
107     public void setUser(String user) {
108         this.user = user;
109     }
110 
111     public String getClientId() {
112         return clientId;
113     }
114 
115     public void setClientId(String clientId) {
116         this.clientId = clientId;
117     }
118 
119     public String getRefreshTokenKeyStoreId() {
120         return refreshTokenKeyStoreId;
121     }
122 
123     public void setRefreshTokenKeyStoreId(String refreshTokenKeyStoreId) {
124         this.refreshTokenKeyStoreId = refreshTokenKeyStoreId;
125     }
126 
127     public String getClientSecretKeyStoreId() {
128         return clientSecretKeyStoreId;
129     }
130 
131     public void setClientSecretKeyStoreId(String clientSecretKeyStoreId) {
132         this.clientSecretKeyStoreId = clientSecretKeyStoreId;
133     }
134 }