get_oauth_token.php

File

inc/PHPMailer/get_oauth_token.php
View source
  1. <?php
  2. /**
  3. * PHPMailer - PHP email creation and transport class.
  4. * PHP Version 5.5
  5. * @package PHPMailer
  6. * @see https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
  7. * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
  8. * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
  9. * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
  10. * @author Brent R. Matzelle (original founder)
  11. * @copyright 2012 - 2017 Marcus Bointon
  12. * @copyright 2010 - 2012 Jim Jagielski
  13. * @copyright 2004 - 2009 Andy Prevost
  14. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  15. * @note This program is distributed in the hope that it will be useful - WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE.
  18. */
  19. /**
  20. * Get an OAuth2 token from an OAuth2 provider.
  21. * * Install this script on your server so that it's accessible
  22. * as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
  23. * e.g.: http://localhost/phpmailer/get_oauth_token.php
  24. * * Ensure dependencies are installed with 'composer install'
  25. * * Set up an app in your Google/Yahoo/Microsoft account
  26. * * Set the script address as the app's redirect URL
  27. * If no refresh token is obtained when running this file,
  28. * revoke access to your app and run the script again.
  29. */
  30. namespace PHPMailer\PHPMailer;
  31. /**
  32. * Aliases for League Provider Classes
  33. * Make sure you have added these to your composer.json and run `composer install`
  34. * Plenty to choose from here:
  35. * @see http://oauth2-client.thephpleague.com/providers/thirdparty/
  36. */
  37. // @see https://github.com/thephpleague/oauth2-google
  38. use League\OAuth2\Client\Provider\Google;
  39. // @see https://packagist.org/packages/hayageek/oauth2-yahoo
  40. use Hayageek\OAuth2\Client\Provider\Yahoo;
  41. // @see https://github.com/stevenmaguire/oauth2-microsoft
  42. use Stevenmaguire\OAuth2\Client\Provider\Microsoft;
  43. if (!isset($_GET['code']) && !isset($_GET['provider'])) {
  44. ?>
  45. <html>
  46. <body>Select Provider:<br/>
  47. <a href='?provider=Google'>Google</a><br/>
  48. <a href='?provider=Yahoo'>Yahoo</a><br/>
  49. <a href='?provider=Microsoft'>Microsoft/Outlook/Hotmail/Live/Office365</a><br/>
  50. </body>
  51. </html>
  52. <?php
  53. exit;
  54. }
  55. require 'vendor/autoload.php';
  56. session_start();
  57. $providerName = '';
  58. if (array_key_exists('provider', $_GET)) {
  59. $providerName = $_GET['provider'];
  60. $_SESSION['provider'] = $providerName;
  61. } elseif (array_key_exists('provider', $_SESSION)) {
  62. $providerName = $_SESSION['provider'];
  63. }
  64. if (!in_array($providerName, ['Google', 'Microsoft', 'Yahoo'])) {
  65. exit('Only Google, Microsoft and Yahoo OAuth2 providers are currently supported in this script.');
  66. }
  67. //These details are obtained by setting up an app in the Google developer console,
  68. //or whichever provider you're using.
  69. $clientId = 'RANDOMCHARS-----duv1n2.apps.googleusercontent.com';
  70. $clientSecret = 'RANDOMCHARS-----lGyjPcRtvP';
  71. //If this automatic URL doesn't work, set it yourself manually to the URL of this script
  72. $redirectUri = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  73. //$redirectUri = 'http://localhost/PHPMailer/redirect';
  74. $params = [
  75. 'clientId' => $clientId,
  76. 'clientSecret' => $clientSecret,
  77. 'redirectUri' => $redirectUri,
  78. 'accessType' => 'offline'
  79. ];
  80. $options = [];
  81. $provider = null;
  82. switch ($providerName) {
  83. case 'Google':
  84. $provider = new Google($params);
  85. $options = [
  86. 'scope' => [
  87. 'https://mail.google.com/'
  88. ]
  89. ];
  90. break;
  91. case 'Yahoo':
  92. $provider = new Yahoo($params);
  93. break;
  94. case 'Microsoft':
  95. $provider = new Microsoft($params);
  96. $options = [
  97. 'scope' => [
  98. 'wl.imap',
  99. 'wl.offline_access'
  100. ]
  101. ];
  102. break;
  103. }
  104. if (null === $provider) {
  105. exit('Provider missing');
  106. }
  107. if (!isset($_GET['code'])) {
  108. // If we don't have an authorization code then get one
  109. $authUrl = $provider->getAuthorizationUrl($options);
  110. $_SESSION['oauth2state'] = $provider->getState();
  111. header('Location: ' . $authUrl);
  112. exit;
  113. // Check given state against previously stored one to mitigate CSRF attack
  114. } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
  115. unset($_SESSION['oauth2state']);
  116. unset($_SESSION['provider']);
  117. exit('Invalid state');
  118. } else {
  119. unset($_SESSION['provider']);
  120. // Try to get an access token (using the authorization code grant)
  121. $token = $provider->getAccessToken(
  122. 'authorization_code',
  123. [
  124. 'code' => $_GET['code']
  125. ]
  126. );
  127. // Use this to interact with an API on the users behalf
  128. // Use this to get a new access token if the old one expires
  129. echo 'Refresh Token: ', $token->getRefreshToken();
  130. }