-
Notifications
You must be signed in to change notification settings - Fork 988
/
GoogleOAuthSMTP.php
236 lines (208 loc) · 7.27 KB
/
GoogleOAuthSMTP.php
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
use League\OAuth2\Client\Provider\Google;
use LimeSurvey\PluginManager\SmtpOAuthPluginBase;
use PHPMailer\PHPMailer\PHPMailer;
class GoogleOAuthSMTP extends SmtpOAuthPluginBase
{
protected $storage = 'DbStorage';
protected static $description = 'Core: Adds Google OAuth support for email sending';
protected static $name = 'GoogleOAuthSMTP';
/** @inheritdoc, this plugin doesn't have any public method */
public $allowedPublicMethods = [];
/** @inheritdoc */
protected $credentialAttributes = ['clientId', 'clientSecret'];
protected $settings = [
'help' => [
'type' => 'info',
'content' => '',
],
'clientId' => [
'type' => 'string',
'label' => 'Client ID',
],
'clientSecret' => [
'type' => 'string',
'label' => 'Client Secret',
],
];
public function init()
{
$this->subscribe('listEmailPlugins');
$this->subscribe('afterSelectEmailPlugin');
$this->subscribe('MailerConstruct'); // Handler defined in SmtpOAuthPluginBase
$this->subscribe('beforePrepareRedirectToAuthPage');
$this->subscribe('beforeRedirectToAuthPage'); // Handler defined in SmtpOAuthPluginBase
$this->subscribe('afterReceiveOAuthResponse'); // Handler defined in SmtpOAuthPluginBase
$this->subscribe('beforeEmailDispatch');
}
/**
* @inheritdoc
* Update the information content
*/
public function getPluginSettings($getValues = true)
{
$settings = parent::getPluginSettings($getValues);
$settings['help']['content'] = $this->getHelpContent();
$settings['clientId']['label'] = gT("Client ID");
$settings['clientSecret']['label'] = gT("Client Secret");
return $settings;
}
private function getHelpContent()
{
$this->subscribe('getPluginTwigPath');
$data = [
'redirectUri' => $this->getRedirectUri(),
'isHttps' => (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on'),
];
return Yii::app()->twigRenderer->renderPartial('/Help.twig', $data);
// Translations here just so the translations bot can pick them up.
$lang = [
gT("Help"),
gT("Prerequisites:"),
gT("Access LimeSurvey over HTTPS."),
gT("Currently not served over HTTPS"),
gT("Instructions:"),
gT("Setup the OAuth 2.0 Web Application in %s."),
gT("Google Cloud Platform Console"),
gT("Redirect URI:"),
gT("You can find more details %shere%s."),
gT("Activate the plugin."),
gT("Set the 'Client ID' and 'Client Secret' below and save the settings."),
gT("Click the 'Get Token' button to open Google's consent screen in a new window."),
gT("Follow the steps in the consent screen and check the requested permissions."),
gT("Switch the 'Enabled' setting to 'On' and save."),
];
}
/**
* @inheritdoc
*/
protected function getProvider($credentials)
{
$redirectUri = $this->getRedirectUri();
$params = [
'clientId' => $credentials['clientId'],
'clientSecret' => $credentials['clientSecret'],
'redirectUri' => $redirectUri,
'accessType' => 'offline',
'prompt' => 'consent',
];
return new Google($params);
}
/**
* Adds view's path to twig system
*/
public function getPluginTwigPath()
{
$viewPath = dirname(__FILE__) . "/views";
$this->getEvent()->append('add', array($viewPath));
}
/**
* Adds the plugin to the list of SMTP OAuth plugins
*/
public function listEmailPlugins()
{
$event = $this->getEvent();
$event->append('plugins', [
'google' => $this->getEmailPluginInfo()
]);
}
/**
* @inheritdoc
*/
public function getOAuthConfigForMailer()
{
try {
$credentials = $this->getCredentials();
} catch (Exception $ex) {
$this->log("GoogleOAuthSMTP is enabled but credentials are incomplete.", CLogger::LEVEL_WARNING);
return;
}
$refreshToken = $this->get("refreshToken");
if (empty($refreshToken)) {
$this->log("GoogleOAuthSMTP is enabled but there is no refresh token stored.", CLogger::LEVEL_WARNING);
return;
}
$emailAddress = $this->get("email");
if (empty($emailAddress)) {
$this->log("GoogleOAuthSMTP is enabled but there is no email address. Please generate a new token.", CLogger::LEVEL_WARNING);
return;
}
$provider = $this->getProvider($credentials);
$config = [
'provider' => $provider,
'clientId' => $credentials['clientId'],
'clientSecret' => $credentials['clientSecret'],
'refreshToken' => $refreshToken,
'userName' => $emailAddress,
];
return $config;
}
/**
* @inheritdoc
* Add Google specific settings to the mailer
*/
protected function setupMailer($mailer)
{
parent::setupMailer($mailer);
$mailer->Host = 'smtp.gmail.com';
$mailer->Port = 465;
$mailer->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
}
/**
* Handles the afterSelectEmailPlugin event, triggered when the plugin
* is selected as the SMTP OAuth plugin in Global Settings
*/
public function afterSelectEmailPlugin()
{
$setupStatus = $this->getSetupStatus();
if ($setupStatus !== self::SETUP_STATUS_VALID_REFRESH_TOKEN) {
$event = $this->getEvent();
$event->set('warning', sprintf(gT("The %s plugin is not configured correctly. Please check the plugin settings."), self::getName()));
}
}
/**
* @inheritdoc
*/
protected function getAuthorizationOptions()
{
return [
'scope' => [
'https://mail.google.com/'
]
];
}
/**
* Handles the beforeEmailDispatch event, triggered right before an email is sent.
* This is used to set the "Reply To" header, because Google automatically overrides
* the sender with the logged user.
*/
public function beforeEmailDispatch()
{
// Don't do anything if the current plugin is not the one selected.
if (!$this->isCurrentEmailPlugin()) {
return;
}
$event = $this->getEvent();
/** @var LimeMailer */
$limeMailer = $event->get('mailer');
// Set "Reply To" because Gmail overrides the From/Sender with the logged user.
$limeMailer->AddReplyTo($limeMailer->From, $limeMailer->FromName);
}
/**
* @inheritdoc
*/
protected function getDisplayName()
{
return 'Google';
}
public function beforePrepareRedirectToAuthPage()
{
$event = $this->getEvent();
$event->set('width', 600);
$event->set('height', 700);
$event->set('providerName', $this->getDisplayName());
$setupStatus = $this->getSetupStatus();
$description = $this->getSetupStatusDescription($setupStatus);
$event->setContent($this, $description);
}
}