get_oauth_token.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 - 2020 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($_POST['provider'])) {
  44. ?>
  45. <html>
  46. <body>
  47. <form method="post">
  48. <h1>Select Provider</h1>
  49. <input type="radio" name="provider" value="Google" id="providerGoogle">
  50. <label for="providerGoogle">Google</label><br>
  51. <input type="radio" name="provider" value="Yahoo" id="providerYahoo">
  52. <label for="providerYahoo">Yahoo</label><br>
  53. <input type="radio" name="provider" value="Microsoft" id="providerMicrosoft">
  54. <label for="providerMicrosoft">Microsoft</label><br>
  55. <h1>Enter id and secret</h1>
  56. <p>These details are obtained by setting up an app in your provider's developer console.
  57. </p>
  58. <p>ClientId: <input type="text" name="clientId"><p>
  59. <p>ClientSecret: <input type="text" name="clientSecret"></p>
  60. <input type="submit" value="Continue">
  61. </form>
  62. </body>
  63. </html>
  64. <?php
  65. exit;
  66. }
  67. require 'vendor/autoload.php';
  68. session_start();
  69. $providerName = '';
  70. $clientId = '';
  71. $clientSecret = '';
  72. if (array_key_exists('provider', $_POST)) {
  73. $providerName = $_POST['provider'];
  74. $clientId = $_POST['clientId'];
  75. $clientSecret = $_POST['clientSecret'];
  76. $_SESSION['provider'] = $providerName;
  77. $_SESSION['clientId'] = $clientId;
  78. $_SESSION['clientSecret'] = $clientSecret;
  79. } elseif (array_key_exists('provider', $_SESSION)) {
  80. $providerName = $_SESSION['provider'];
  81. $clientId = $_SESSION['clientId'];
  82. $clientSecret = $_SESSION['clientSecret'];
  83. }
  84. //If you don't want to use the built-in form, set your client id and secret here
  85. //$clientId = 'RANDOMCHARS-----duv1n2.apps.googleusercontent.com';
  86. //$clientSecret = 'RANDOMCHARS-----lGyjPcRtvP';
  87. //If this automatic URL doesn't work, set it yourself manually to the URL of this script
  88. $redirectUri = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  89. //$redirectUri = 'http://localhost/PHPMailer/redirect';
  90. $params = [
  91. 'clientId' => $clientId,
  92. 'clientSecret' => $clientSecret,
  93. 'redirectUri' => $redirectUri,
  94. 'accessType' => 'offline'
  95. ];
  96. $options = [];
  97. $provider = null;
  98. switch ($providerName) {
  99. case 'Google':
  100. $provider = new Google($params);
  101. $options = [
  102. 'scope' => [
  103. 'https://mail.google.com/'
  104. ]
  105. ];
  106. break;
  107. case 'Yahoo':
  108. $provider = new Yahoo($params);
  109. break;
  110. case 'Microsoft':
  111. $provider = new Microsoft($params);
  112. $options = [
  113. 'scope' => [
  114. 'wl.imap',
  115. 'wl.offline_access'
  116. ]
  117. ];
  118. break;
  119. }
  120. if (null === $provider) {
  121. exit('Provider missing');
  122. }
  123. if (!isset($_GET['code'])) {
  124. //If we don't have an authorization code then get one
  125. $authUrl = $provider->getAuthorizationUrl($options);
  126. $_SESSION['oauth2state'] = $provider->getState();
  127. header('Location: ' . $authUrl);
  128. exit;
  129. //Check given state against previously stored one to mitigate CSRF attack
  130. } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
  131. unset($_SESSION['oauth2state']);
  132. unset($_SESSION['provider']);
  133. exit('Invalid state');
  134. } else {
  135. unset($_SESSION['provider']);
  136. //Try to get an access token (using the authorization code grant)
  137. $token = $provider->getAccessToken(
  138. 'authorization_code',
  139. [
  140. 'code' => $_GET['code']
  141. ]
  142. );
  143. //Use this to interact with an API on the users behalf
  144. //Use this to get a new access token if the old one expires
  145. echo 'Refresh Token: ', $token->getRefreshToken();
  146. }