Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_oauth_token.php does not result in refresh token... redirects to homepage... no error, #2646

Open
mohsincheema opened this issue Mar 8, 2022 · 3 comments

Comments

@mohsincheema
Copy link

Hi,
I am trying to use PHPMailer get_oauth_token.php to enable XOAUTH2 authorization for gmail in Open Journal System (OJS) software published by PKP. As guided here at https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2 , I followed all the steps and when I run the vendor/phpmailer/phpmailer/get_oauth_token.php in browser and complete the subsequent steps fo selecting provider (google) then sign in, then allow user consent, it results in redirection to homepage and does not display the refresh token. No error is showing in browser or error log. I am unable to figure out what is going wrong. I have ask the OJS PKP support, and they said I should ask PHPMailer support.
Please help me to resolve this issue. Thanks in anticipation.

Originally posted by @mohsincheema in #2645

@mohsincheema
Copy link
Author

Code to reproduce is:

`<?php

/**

/**

  • Get an OAuth2 token from an OAuth2 provider.
    • Install this script on your server so that it's accessible
  • as [https/http]:////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;

/**

if (!isset($_GET['code']) && !isset($_GET['provider'])) {
?>

Select Provider:
Google
Yahoo
Microsoft/Outlook/Hotmail/Live/Office365
//require 'vendor/autoload.php';
require '/home/username/public_html/subdomain/lib/pkp/lib/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 = 'entered the clientID here';
$clientSecret = 'entered the clientSecret here';

//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 = 'https://sd.mydomainname.com/lib/pkp/lib/vendor/phpmailer/phpmailer/get_oauth_token.php';

$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'])) {
// If we don't have an authorization code then get one
$authUrl = $provider->getAuthorizationUrl($options);
$_SESSION['oauth2state'] = $provider->getState();
header('Location: ' . $authUrl);
exit;
// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
unset($_SESSION['provider']);
exit('Invalid state');
} else {
unset($_SESSION['provider']);
// Try to get an access token (using the authorization code grant)
$token = $provider->getAccessToken(
'authorization_code',
[
'code' => $_GET['code']
]
);
// Use this to interact with an API on the users behalf
// Use this to get a new access token if the old one expires
echo 'Refresh Token: ', $token->getRefreshToken();`

@Synchro
Copy link
Member

Synchro commented Mar 8, 2022

It's quite likely your app will refuse to serve a file directly from a vendor folder, which should not normally be inside the web root. This might manifest as serving your home page, so perhaps copy this script to the web root and adjust the callback URL to match.

@mohsincheema
Copy link
Author

Thanks Synchro! the normal directory path of get_oauth_token.php is public_html/lib/pkp/lib/vendor/phpmailer/phpmailer/get_oauth_token.php in Open Journal Systems (OJS). We did not modified it in original.

It's quite likely your app will refuse to serve a file directly from a vendor folder, which should not normally be inside the web root. This might manifest as serving your home page, so perhaps copy this script to the web root and adjust the callback URL to match.

I have copied the get_oauth_token.php to public_html/OJS_installation_folder. and then modified the $redirectUri to as following:
$redirectUri = 'https://sd.mydomainname.com/get_oauth_token.php';
Also changed the Authorized redirect URI und Client ID for Web application in Google APIs and Services to match this one. This exactly behaved the same way as earlier. Selection of provider, sign in, allowing user permissions, and then the homepage (no refresh token).

Looking forward to more guidance on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants