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

Problem Using Gmail with XOAUTH2 - get_oauth_token.php does not result in refresh token... redirects to OJS homepage #7754

Closed
mohsincheema opened this issue Mar 10, 2022 · 2 comments

Comments

@mohsincheema
Copy link

Describe the bug
We are trying to configure OJS for Gmail SMTP as we have acquired Google workspace services and want OJS to send email via google workspace instead of the local mail server. We configured the config.inc.php file as guided here: https://docs.pkp.sfu.ca/admin-guide/en/email#configuring-the-system-to-use-gmail-smtp . In config.inc.php our email settings are provided in additional information. However, email sending error happened, so we tried to configure Gmail with XOAUTH2 and followed the process as guided here: https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2 . However, when we run the get_oauth_token.php in browser and select the provider as google, then sign in (with same account from where we generated client ID), and then allow the permissions, it redirects us to the OJS homepage instead of showing the refresh token. We have tried to reach PHPMailer support and link to their ongoing response is: PHPMailer/PHPMailer#2646.

NOTE: In localhost simple configuration as recommended https://docs.pkp.sfu.ca/admin-guide/en/email#configuring-the-system-to-use-gmail-smtp worked and email sent via my_email@gmail.com. Similiarly, in localhost get_oauth_token.php resulted in displaying refresh token and not redirected to homepage as is the case in live website.

To Reproduce
Email settings in config.inc.php as recommended at https://docs.pkp.sfu.ca/admin-guide/en/email#configuring-the-system-to-use-gmail-smtp
For Using Gmail with XOAUTH2 steps followed as recommended at https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2 and at Fetch the token step:

  1. Go to 'sub_domain.my_domain_name.com/lib/pkp/lib/vendor/phpmailer/phpmailer/get_oauth_token.php'
  2. Click on 'select provider as google'
  3. Sign in with Google 'used the same email from where generated the client ID and secret'
  4. Allow access to user account
  5. Redirects to: 'sub_domain.my_domain_name.com/index.php/journal

What application are you using?
OJS, version 3.3.0.7.

Additional information
Email settings in config.inc.php:
`[email]

; Use SMTP for sending mail instead of mail()
smtp = On

; SMTP server settings
smtp_server = smtp.gmail.com
smtp_port = 465

; Enable SMTP authentication
; Supported smtp_auth: ssl, tls (see PHPMailer SMTPSecure)
smtp_auth = ssl
smtp_username = "my_email@my_domain_name.com"
smtp_password = "my_email_password"
;
; Supported smtp_authtype: RAM-MD5, LOGIN, PLAIN, XOAUTH2 (see PHPMailer AuthType)
; (Leave blank to try them in that order)
; smtp_authtype =

; The following are required for smtp_authtype = XOAUTH2 (e.g. GMail OAuth)
; (See https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2)
; smtp_oauth_provider = Google
; smtp_oauth_email =
; smtp_oauth_clientid =
; smtp_oauth_clientsecret =
; smtp_oauth_refreshtoken =

; Enable suppressing verification of SMTP certificate in PHPMailer
; Note: this is not recommended per PHPMailer documentation
; smtp_suppress_cert_check = On
`
The code in get_oauth_token.php:

`<?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 = 'my_client_id.apps.googleusercontent.com';
$clientSecret = 'my_client_secret';

//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://subdomain.my_domain_name.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();
}`

@asmecher
Copy link
Member

@mohsincheema, I see you've already posted this on our support forum:
https://forum.pkp.sfu.ca/t/ojs-deployed-on-subdomain-wordpress-on-main-domain-unable-to-complete-gmail-xoauth2-via-get-oauth-token-php/72270/5

That's the best place to get help.

@mohsincheema
Copy link
Author

@asmecher yes, I have posted the issue on forum first to get assistance. Now I tried a workaround as suggested by the PHPMailer support i.e.:

I don't know where your issue is, but you probably don't need to make it work on the live site if you can make it work on localhost. Once you have the access and refresh tokens you can install them on your live server where they will work fine. They are not tied to your callback URL.
PHPMailer/PHPMailer#2645 (reply in thread)

I was able to fetch the refresh token on localhost. But when I used these in config.inc.php on live website, I was still unable to send email. Error log recorded following errors:

[10-Mar-2022 17:00:36 UTC] PHP Warning: Declaration of EmailReviewerForm::execute($submission) should be compatible with Form::execute(...$functionArgs) in /home/username/public_html/subdomain/lib/pkp/controllers/grid/users/reviewer/form/EmailReviewerForm.inc.php on line 18
[10-Mar-2022 17:00:52 UTC] PHP Warning: Declaration of EmailReviewerForm::execute($submission) should be compatible with Form::execute(...$functionArgs) in /home/username/public_html/subdomain/lib/pkp/controllers/grid/users/reviewer/form/EmailReviewerForm.inc.php on line 18
[10-Mar-2022 17:00:53 UTC] SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

and when PHPMailer support said:

hose declaration errors have nothing to do with PHPMailer?

I am trying to connect via google smtp for months now. the forum response is very slow which I understand as normal in volunteer support programs. So asked here. Anyhow thanks if anyone can help me where I am making a mistake.

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