get_oauth_token.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * PHPMailer - PHP email creation and transport class.
  4. * PHP Version 5.5
  5. * @package PHPMailer
  6. * @link 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 - 2016 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. * @link http://oauth2-client.thephpleague.com/providers/thirdparty/
  36. */
  37. // @link https://github.com/thephpleague/oauth2-google
  38. use League\OAuth2\Client\Provider\Google;
  39. // @link https://packagist.org/packages/hayageek/oauth2-yahoo
  40. use Hayageek\OAuth2\Client\Provider\Yahoo;
  41. // @link 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. <?php
  52. exit;
  53. }
  54. require 'vendor/autoload.php';
  55. session_start();
  56. $providerName = '';
  57. if (array_key_exists('provider', $_GET)) {
  58. $providerName = $_GET['provider'];
  59. $_SESSION['provider'] = $providerName;
  60. } elseif (array_key_exists('provider', $_SESSION)) {
  61. $providerName = $_SESSION['provider'];
  62. }
  63. if (!in_array($providerName, ['Google', 'Microsoft', 'Yahoo'])) {
  64. exit('Only Google, Microsoft and Yahoo OAuth2 providers are currently supported in this script.');
  65. }
  66. //These details obtained are by setting up app in Google developer console.
  67. $clientId = 'RANDOMCHARS-----duv1n2.apps.googleusercontent.com';
  68. $clientSecret = 'RANDOMCHARS-----lGyjPcRtvP';
  69. //If this automatic URL doesn't work, set it yourself manually
  70. $redirectUri = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  71. //$redirectUri = 'http://localhost/PHPMailer/redirect';
  72. $params = [
  73. 'clientId' => $clientId,
  74. 'clientSecret' => $clientSecret,
  75. 'redirectUri' => $redirectUri,
  76. 'accessType' => 'offline'
  77. ];
  78. $options = [];
  79. switch ($providerName) {
  80. case 'Google':
  81. $provider = new Google($params);
  82. $options = [
  83. 'scope' => [
  84. 'https://mail.google.com/'
  85. ]
  86. ];
  87. break;
  88. case 'Yahoo':
  89. $provider = new Yahoo($params);
  90. break;
  91. case 'Microsoft':
  92. $provider = new Microsoft($params);
  93. $options = [
  94. 'scope' => [
  95. 'wl.imap',
  96. 'wl.offline_access'
  97. ]
  98. ];
  99. break;
  100. }
  101. if (!isset($_GET['code'])) {
  102. // If we don't have an authorization code then get one
  103. $authUrl = $provider->getAuthorizationUrl($options);
  104. $_SESSION['oauth2state'] = $provider->getState();
  105. header('Location: ' . $authUrl);
  106. exit;
  107. // Check given state against previously stored one to mitigate CSRF attack
  108. } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
  109. unset($_SESSION['oauth2state']);
  110. unset($_SESSION['provider']);
  111. exit('Invalid state');
  112. } else {
  113. unset($_SESSION['provider']);
  114. // Try to get an access token (using the authorization code grant)
  115. $token = $provider->getAccessToken(
  116. 'authorization_code',
  117. [
  118. 'code' => $_GET['code']
  119. ]
  120. );
  121. // Use this to interact with an API on the users behalf
  122. // Use this to get a new access token if the old one expires
  123. echo 'Refresh Token: ' . $token->getRefreshToken();
  124. }