superflux/phpmailer/get_oauth_token.php

147 lines
4.8 KiB
PHP
Raw Normal View History

2020-01-18 23:03:01 +01:00
<?php
2021-04-05 16:29:11 +02:00
2020-01-18 23:03:01 +01:00
/**
* PHPMailer - PHP email creation and transport class.
* PHP Version 5.5
* @package PHPMailer
* @see https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
* @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)
2021-04-05 16:29:11 +02:00
* @copyright 2012 - 2020 Marcus Bointon
2020-01-18 23:03:01 +01:00
* @copyright 2010 - 2012 Jim Jagielski
* @copyright 2004 - 2009 Andy Prevost
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
* @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.
*/
2021-04-05 16:29:11 +02:00
2020-01-18 23:03:01 +01: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
* e.g.: http://localhost/phpmailer/get_oauth_token.php
* * Ensure dependencies are installed with 'composer install'
* * Set up an app in your Google/Yahoo/Microsoft account
* * Set the script address as the app's redirect URL
* If no refresh token is obtained when running this file,
* revoke access to your app and run the script again.
*/
namespace PHPMailer\PHPMailer;
/**
* 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 http://oauth2-client.thephpleague.com/providers/thirdparty/
*/
2021-04-05 16:29:11 +02:00
//@see https://github.com/thephpleague/oauth2-google
2020-01-18 23:03:01 +01:00
use League\OAuth2\Client\Provider\Google;
2021-04-05 16:29:11 +02:00
//@see https://packagist.org/packages/hayageek/oauth2-yahoo
2020-01-18 23:03:01 +01:00
use Hayageek\OAuth2\Client\Provider\Yahoo;
2021-04-05 16:29:11 +02:00
//@see https://github.com/stevenmaguire/oauth2-microsoft
2020-01-18 23:03:01 +01:00
use Stevenmaguire\OAuth2\Client\Provider\Microsoft;
if (!isset($_GET['code']) && !isset($_GET['provider'])) {
2021-04-05 16:29:11 +02:00
?>
2020-01-18 23:03:01 +01:00
<html>
2021-04-05 16:29:11 +02:00
<body>Select Provider:<br>
<a href='?provider=Google'>Google</a><br>
<a href='?provider=Yahoo'>Yahoo</a><br>
<a href='?provider=Microsoft'>Microsoft/Outlook/Hotmail/Live/Office365</a><br>
2020-01-18 23:03:01 +01:00
</body>
</html>
2021-04-05 16:29:11 +02:00
<?php
exit;
2020-01-18 23:03:01 +01:00
}
require 'vendor/autoload.php';
session_start();
$providerName = '';
if (array_key_exists('provider', $_GET)) {
$providerName = $_GET['provider'];
$_SESSION['provider'] = $providerName;
} elseif (array_key_exists('provider', $_SESSION)) {
$providerName = $_SESSION['provider'];
}
if (!in_array($providerName, ['Google', 'Microsoft', 'Yahoo'])) {
exit('Only Google, Microsoft and Yahoo OAuth2 providers are currently supported in this script.');
}
//These details are obtained by setting up an app in the Google developer console,
//or whichever provider you're using.
$clientId = 'RANDOMCHARS-----duv1n2.apps.googleusercontent.com';
$clientSecret = 'RANDOMCHARS-----lGyjPcRtvP';
//If this automatic URL doesn't work, set it yourself manually to the URL of this script
$redirectUri = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
//$redirectUri = 'http://localhost/PHPMailer/redirect';
$params = [
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'redirectUri' => $redirectUri,
'accessType' => 'offline'
];
$options = [];
$provider = null;
switch ($providerName) {
case 'Google':
$provider = new Google($params);
$options = [
'scope' => [
'https://mail.google.com/'
]
];
break;
case 'Yahoo':
$provider = new Yahoo($params);
break;
case 'Microsoft':
$provider = new Microsoft($params);
$options = [
'scope' => [
'wl.imap',
'wl.offline_access'
]
];
break;
}
if (null === $provider) {
exit('Provider missing');
}
if (!isset($_GET['code'])) {
2021-04-05 16:29:11 +02:00
//If we don't have an authorization code then get one
2020-01-18 23:03:01 +01:00
$authUrl = $provider->getAuthorizationUrl($options);
$_SESSION['oauth2state'] = $provider->getState();
header('Location: ' . $authUrl);
exit;
2021-04-05 16:29:11 +02:00
//Check given state against previously stored one to mitigate CSRF attack
2020-01-18 23:03:01 +01:00
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
unset($_SESSION['provider']);
exit('Invalid state');
} else {
unset($_SESSION['provider']);
2021-04-05 16:29:11 +02:00
//Try to get an access token (using the authorization code grant)
2020-01-18 23:03:01 +01:00
$token = $provider->getAccessToken(
'authorization_code',
[
'code' => $_GET['code']
]
);
2021-04-05 16:29:11 +02: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
2020-01-18 23:03:01 +01:00
echo 'Refresh Token: ', $token->getRefreshToken();
}