0
0
mirror of https://github.com/PHPMailer/PHPMailer.git synced 2024-09-19 17:42:14 +02:00
PHPMailer/get_oauth_token.php

183 lines
6.1 KiB
PHP
Raw Normal View History

<?php
/**
2016-04-07 11:33:03 +02:00
* PHPMailer - PHP email creation and transport class.
* PHP Version 5.5
* @package PHPMailer
* @see https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
2016-04-07 11:33:03 +02:00
* @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
* @author Jim Jagielski (jimjag) <jimjag@gmail.com>
* @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
* @author Brent R. Matzelle (original founder)
2020-09-02 08:41:14 +02:00
* @copyright 2012 - 2020 Marcus Bointon
2016-04-07 11:33:03 +02:00
* @copyright 2010 - 2012 Jim Jagielski
* @copyright 2004 - 2009 Andy Prevost
* @license https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html GNU Lesser General Public License
2016-04-07 11:33:03 +02:00
* @note This program is distributed in the hope that it will be useful - WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*/
2016-04-07 11:33:03 +02:00
/**
* Get an OAuth2 token from an OAuth2 provider.
* * Install this script on your server so that it's accessible
* as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
2016-04-07 11:33:03 +02:00
* e.g.: http://localhost/phpmailer/get_oauth_token.php
* * Ensure dependencies are installed with 'composer install'
2016-04-07 11:33:03 +02:00
* * Set up an app in your Google/Yahoo/Microsoft account
* * Set the script address as the app's redirect URL
2016-04-07 11:33:03 +02:00
* If no refresh token is obtained when running this file,
* revoke access to your app and run the script again.
*/
2016-04-07 11:33:03 +02:00
namespace PHPMailer\PHPMailer;
2016-04-21 12:51:31 +02:00
/**
* Aliases for League Provider Classes
* Make sure you have added these to your composer.json and run `composer install`
* Plenty to choose from here:
* @see https://oauth2-client.thephpleague.com/providers/thirdparty/
2016-04-21 12:51:31 +02:00
*/
2021-02-19 13:42:01 +01:00
//@see https://github.com/thephpleague/oauth2-google
2016-04-07 11:33:03 +02:00
use League\OAuth2\Client\Provider\Google;
2021-02-19 13:42:01 +01:00
//@see https://packagist.org/packages/hayageek/oauth2-yahoo
2016-04-07 11:33:03 +02:00
use Hayageek\OAuth2\Client\Provider\Yahoo;
2021-02-19 13:42:01 +01:00
//@see https://github.com/stevenmaguire/oauth2-microsoft
2016-04-07 11:33:03 +02:00
use Stevenmaguire\OAuth2\Client\Provider\Microsoft;
2022-10-07 22:34:23 +02:00
//@see https://github.com/greew/oauth2-azure-provider
use Greew\OAuth2\Client\Provider\Azure;
2016-04-07 11:33:03 +02:00
if (!isset($_GET['code']) && !isset($_POST['provider'])) {
2022-06-17 00:17:12 +02:00
?>
2016-04-07 11:33:03 +02:00
<html>
<body>
<form method="post">
<h1>Select Provider</h1>
2022-06-17 00:11:49 +02:00
<input type="radio" name="provider" value="Google" id="providerGoogle">
<label for="providerGoogle">Google</label><br>
<input type="radio" name="provider" value="Yahoo" id="providerYahoo">
<label for="providerYahoo">Yahoo</label><br>
<input type="radio" name="provider" value="Microsoft" id="providerMicrosoft">
<label for="providerMicrosoft">Microsoft</label><br>
2022-10-07 22:34:23 +02:00
<input type="radio" name="provider" value="Azure" id="providerAzure">
<label for="providerAzure">Azure</label><br>
2022-06-17 00:11:49 +02:00
<h1>Enter id and secret</h1>
<p>These details are obtained by setting up an app in your provider's developer console.
</p>
<p>ClientId: <input type="text" name="clientId"><p>
<p>ClientSecret: <input type="text" name="clientSecret"></p>
2022-10-07 22:34:23 +02:00
<p>TenantID (only relevant for Azure): <input type="text" name="tenantId"></p>
<input type="submit" value="Continue">
</form>
2016-04-07 11:33:03 +02:00
</body>
2016-08-31 10:26:06 +02:00
</html>
<?php
exit;
2016-04-07 11:33:03 +02:00
}
require 'vendor/autoload.php';
session_start();
2016-04-07 11:33:03 +02:00
$providerName = '';
$clientId = '';
$clientSecret = '';
2022-10-07 22:34:23 +02:00
$tenantId = '';
2016-04-07 11:33:03 +02:00
if (array_key_exists('provider', $_POST)) {
$providerName = $_POST['provider'];
$clientId = $_POST['clientId'];
$clientSecret = $_POST['clientSecret'];
2022-10-07 22:34:23 +02:00
$tenantId = $_POST['tenantId'];
2016-04-07 11:33:03 +02:00
$_SESSION['provider'] = $providerName;
$_SESSION['clientId'] = $clientId;
$_SESSION['clientSecret'] = $clientSecret;
2022-10-07 22:34:23 +02:00
$_SESSION['tenantId'] = $tenantId;
2016-04-07 11:33:03 +02:00
} elseif (array_key_exists('provider', $_SESSION)) {
$providerName = $_SESSION['provider'];
$clientId = $_SESSION['clientId'];
$clientSecret = $_SESSION['clientSecret'];
2022-10-07 22:34:23 +02:00
$tenantId = $_SESSION['tenantId'];
2016-04-07 11:33:03 +02:00
}
2022-06-17 00:11:49 +02:00
//If you don't want to use the built-in form, set your client id and secret here
//$clientId = 'RANDOMCHARS-----duv1n2.apps.googleusercontent.com';
//$clientSecret = 'RANDOMCHARS-----lGyjPcRtvP';
2016-08-31 10:26:06 +02:00
//If this automatic URL doesn't work, set it yourself manually to the URL of this script
2016-04-08 01:41:46 +02:00
$redirectUri = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
2016-04-07 11:33:03 +02:00
//$redirectUri = 'http://localhost/PHPMailer/redirect';
$params = [
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'redirectUri' => $redirectUri,
'accessType' => 'offline'
];
$options = [];
2016-08-31 10:26:06 +02:00
$provider = null;
2016-04-07 11:33:03 +02:00
switch ($providerName) {
case 'Google':
$provider = new Google($params);
$options = [
'scope' => [
'https://mail.google.com/'
]
];
2016-04-07 11:33:03 +02:00
break;
case 'Yahoo':
$provider = new Yahoo($params);
break;
case 'Microsoft':
$provider = new Microsoft($params);
2016-04-07 11:35:44 +02:00
$options = [
'scope' => [
'wl.imap',
'wl.offline_access'
]
2022-10-07 22:34:23 +02:00
];
break;
case 'Azure':
$params['tenantId'] = $tenantId;
$provider = new Azure($params);
$options = [
'scope' => [
'https://outlook.office.com/SMTP.Send',
'offline_access'
]
2016-04-07 11:35:44 +02:00
];
2016-04-07 11:33:03 +02:00
break;
}
2017-09-07 10:17:38 +02:00
if (null === $provider) {
2017-07-28 10:48:35 +02:00
exit('Provider missing');
2016-08-31 10:26:06 +02:00
}
if (!isset($_GET['code'])) {
2021-02-19 13:42:01 +01:00
//If we don't have an authorization code then get one
2016-04-07 11:33:03 +02:00
$authUrl = $provider->getAuthorizationUrl($options);
2015-10-23 13:32:43 +02:00
$_SESSION['oauth2state'] = $provider->getState();
header('Location: ' . $authUrl);
exit;
2021-02-19 13:42:01 +01:00
//Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
2016-04-07 11:33:03 +02:00
unset($_SESSION['provider']);
exit('Invalid state');
} else {
2016-04-07 11:33:03 +02:00
unset($_SESSION['provider']);
2021-02-19 13:42:01 +01:00
//Try to get an access token (using the authorization code grant)
$token = $provider->getAccessToken(
'authorization_code',
2015-11-10 13:45:13 +01:00
[
'code' => $_GET['code']
2015-11-10 13:45:13 +01:00
]
);
2021-02-19 13:42:01 +01:00
//Use this to interact with an API on the users behalf
//Use this to get a new access token if the old one expires
2024-01-31 23:05:36 +01:00
echo 'Refresh Token: ', htmlspecialchars($token->getRefreshToken());
}