1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
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 }