Try to clean up get_oauth_token

This commit is contained in:
Marcus 2016-03-05 21:52:56 +01:00
parent a1b90c1ee9
commit 26c676f1e0
3 changed files with 45 additions and 130 deletions

View File

@ -19,7 +19,7 @@ Build status: [![Build Status](https://travis-ci.org/PHPMailer/PHPMailer.svg)](h
- SMTP authentication with LOGIN, PLAIN, NTLM, CRAM-MD5 and Google's XOAUTH2 mechanisms over SSL and TLS transports
- Error messages in 47 languages!
- DKIM and S/MIME signing support
- Compatible with PHP 5.0 and later
- Compatible with PHP 5.4 and later
- Much more!
## Why you might need it
@ -60,7 +60,7 @@ If you don't speak git or just want a tarball, click the 'zip' button on the rig
### Minimal installation
While installing the entire package manually or with composer is simple, convenient and reliable, you may want to include only vital files in your project. At the very least you will need [src/PHPMailer.php](src/PHPMailer.php). If you're using SMTP, you'll need [src/SMTP.php](src/SMTP.php), and if you're using POP-before SMTP, you'll need [src/POP3.php](src/POP3.php). You can skip the [language](language/) folder if you're not showing errors to users and can make do with English-only errors. If you're using Google XOAUTH2 you will need `src/PHPMailerOAuth.php` and `src/OAuthProvider/Google.php` classes, as well as the composer dependencies. Really, it's much easier to use composer!
While installing the entire package manually or with composer is simple, convenient and reliable, you may want to include only vital files in your project. At the very least you will need [src/PHPMailer.php](https://github.com/PHPMailer/PHPMailer/tree/master/src/PHPMailer.php). If you're using SMTP, you'll need [src/SMTP.php](https://github.com/PHPMailer/PHPMailer/tree/master/src/SMTP.php), and if you're using POP-before SMTP, you'll need [src/POP3.php](https://github.com/PHPMailer/PHPMailer/tree/master/src/POP3.php). You can skip the [language](https://github.com/PHPMailer/PHPMailer/tree/master/language/) folder if you're not showing errors to users and can make do with English-only errors. If you're using Google XOAUTH2 you will need `src/PHPMailerOAuth.php` and `src/OAuthProvider/Google.php` classes, as well as the composer dependencies. Really, it's much easier to use composer!
## A Simple Example
@ -104,30 +104,30 @@ if(!$mail->send()) {
}
```
You'll find plenty of examples to play with in the [examples](examples/) folder. They are saved with a `.phps` extension which will make them display as highlighted source in a browser, avoiding the possibility of them running in default installations. You can run them directly from a command line client, or rename them with a `.php` extension and run them via your web server.
You'll find plenty of examples to play with in the [examples](https://github.com/PHPMailer/PHPMailer/tree/master/examples/) folder. They are saved with a `.phps` extension which will make them display as highlighted source in a browser, avoiding the possibility of them running in default installations. You can run them directly from a command line client, or rename them with a `.php` extension and run them via your web server.
You'll find plenty more to play with in the [examples](https://github.com/PHPMailer/PHPMailer/tree/master/examples) folder.
That's it. You should now be ready to use PHPMailer!
## Localization
PHPMailer defaults to English, but in the [language](https://github.com/PHPMailer/PHPMailer/language/) folder you'll find numerous (46 at the time of writing!) translations for PHPMailer error messages that you may encounter. Their filenames contain [ISO 639-1](http://en.wikipedia.org/wiki/ISO_639-1) language code for the translations, for example `fr` for French. To specify a language, you need to tell PHPMailer which one to use, like this:
PHPMailer defaults to English, but in the [language](https://github.com/PHPMailer/PHPMailer/tree/master/language/) folder you'll find numerous (46 at the time of writing!) translations for PHPMailer error messages that you may encounter. Their filenames contain [ISO 639-1](http://en.wikipedia.org/wiki/ISO_639-1) language code for the translations, for example `fr` for French. To specify a language, you need to tell PHPMailer which one to use, like this:
```php
// To load the French version
$mail->setLanguage('fr', '/optional/path/to/language/directory/');
```
We welcome corrections and new languages - if you're looking for corrections to do, run the [phpmailerLangTest.php](https://github.com/PHPMailer/PHPMailer/test/phpmailerLangTest.php) script in the tests folder and it will show any missing translations.
We welcome corrections and new languages - if you're looking for corrections to do, run the [phpmailerLangTest.php](https://github.com/PHPMailer/PHPMailer/tree/master/test/phpmailerLangTest.php) script in the tests folder and it will show any missing translations.
## Documentation
Start reading at the [GitHub wiki](https://github.com/PHPMailer/PHPMailer/wiki). If you're having trouble, this should be the first place you look as it's the most frequently updated.
Examples of how to use PHPMailer for common scenarios can be found in the [examples](https://github.com/PHPMailer/PHPMailer/tree/master/examples) folder. If you're looking for a good starting point, we recommend you start with [the Gmail example](https://github.com/PHPMailer/PHPMailer/examples/gmail.phps).
Examples of how to use PHPMailer for common scenarios can be found in the [examples](https://github.com/PHPMailer/PHPMailer/tree/master/examples) folder. If you're looking for a good starting point, we recommend you start with [the Gmail example](https://github.com/PHPMailer/PHPMailer/tree/master/examples/gmail.phps).
Complete generated API documentation is [available online](http://phpmailer.github.io/PHPMailer/).
You'll find some basic user-level docs in the [docs](https://github.com/PHPMailer/PHPMailer/tree/master/docs/) folder, and you can generate complete API-level documentation using the [generatedocs.sh](https://github.com/PHPMailer/PHPMailer/docs/generatedocs.sh) shell script in the docs folder, though you'll need to install [PHPDocumentor](http://www.phpdoc.org) first. You may find [the unit tests](https://github.com/PHPMailer/PHPMailer/test/phpmailerTest.php) a good source of how to do various operations such as encryption.
You'll find some basic user-level docs in the [docs](https://github.com/PHPMailer/PHPMailer/tree/master/docs/) folder, and you can generate complete API-level documentation using the [generatedocs.sh](https://github.com/PHPMailer/PHPMailer/tree/master/docs/generatedocs.sh) shell script in the docs folder, though you'll need to install [PHPDocumentor](http://www.phpdoc.org) first. You may find [the unit tests](https://github.com/PHPMailer/PHPMailer/tree/master/test/phpmailerTest.php) a good source of how to do various operations such as encryption.
If the documentation doesn't cover what you need, search the [many questions on Stack Overflow](http://stackoverflow.com/questions/tagged/phpmailer), and before you ask a question about "SMTP Error: Could not connect to SMTP host.", [read the troubleshooting guide](https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting).

View File

@ -29,6 +29,10 @@
* revoke access to your app and run the script again.
*/
namespace PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\OAuthProvider\Base;
if (!isset($_GET['code']) && !isset($_GET['provider'])) {
?>
<html>
@ -41,15 +45,8 @@ if (!isset($_GET['code']) && !isset($_GET['provider'])) {
exit;
}
namespace League\OAuth2\Client\Provider;
require 'vendor/autoload.php';
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
use Psr\Http\Message\ResponseInterface;
session_start();
$providerName = '';
@ -60,118 +57,26 @@ if (array_key_exists('provider', $_GET)) {
} elseif (array_key_exists('provider', $_SESSION)) {
$providerName = $_SESSION['provider'];
}
if (!preg_match('/^(Google|Microsoft|Yahoo)$/', $providerName)) {
if (!in_array($providerName, ['Google', 'Microsoft', 'Yahoo'])) {
exit("Only Google, Microsoft and Yahoo OAuth2 providers are currently supported.");
}
//Alter this to point at the URL of this script on your own server
//Should be an HTTPS URL
$redirectUri = 'https://example.com/PHPMailer/get_oauth_token.php';
$redirectUri = 'http://phpmailer.oc.local/get_oauth_token.php';
$providerClass = '\\PHPMailer\\PHPMailer\\OAuthProvider\\'.$providerName;
/**
* @var Base $provider
*/
$provider = new $providerClass(
'{YOUR_APP_ID}',
'{YOUR_APP_SECRET}',
$redirectUri,
'offline'
);
class Google extends AbstractProvider
{
use BearerAuthorizationTrait;
const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'id';
/**
* @var string If set, this will be sent to google as the "access_type" parameter.
* @link https://developers.google.com/accounts/docs/OAuth2WebServer#offline
*/
protected $accessType;
/**
* @var string If set, this will be sent to google as the "hd" parameter.
* @link https://developers.google.com/accounts/docs/OAuth2Login#hd-param
*/
protected $hostedDomain;
/**
* @var string If set, this will be sent to google as the "scope" parameter.
* @link https://developers.google.com/gmail/api/auth/scopes
*/
protected $scope;
public function getBaseAuthorizationUrl()
{
return 'https://accounts.google.com/o/oauth2/auth';
}
public function getBaseAccessTokenUrl(array $params)
{
return 'https://accounts.google.com/o/oauth2/token';
}
public function getResourceOwnerDetailsUrl(AccessToken $token)
{
return ' ';
}
protected function getAuthorizationParameters(array $options)
{
if (is_array($this->scope)) {
$separator = $this->getScopeSeparator();
$this->scope = implode($separator, $this->scope);
}
$params = array_merge(
parent::getAuthorizationParameters($options),
array_filter(
[
'hd' => $this->hostedDomain,
'access_type' => $this->accessType,
'scope' => $this->scope,
// if the user is logged in with more than one account ask which one to use for the login!
'authuser' => '-1'
]
)
);
return $params;
}
protected function getDefaultScopes()
{
return [
'email',
'openid',
'profile',
];
}
protected function getScopeSeparator()
{
return ' ';
}
protected function checkResponse(ResponseInterface $response, $data)
{
if (!empty($data['error'])) {
$code = 0;
$error = $data['error'];
if (is_array($error)) {
$code = $error['code'];
$error = $error['message'];
}
throw new IdentityProviderException($error, $code, $data);
}
}
protected function createResourceOwner(array $response, AccessToken $token)
{
return new GoogleUser($response);
}
}
$options = [];
if (!isset($_GET['code'])) {
// If we don't have an authorization code then get one
@ -191,19 +96,16 @@ if (!isset($_GET['code'])) {
unset($_SESSION['provider']);
// Try to get an access token (using the authorization code grant)
$token = $provider->getAccessToken('authorization_code', array(
'code' => $_GET['code']
));
// Use this to interact with an API on the users behalf
echo 'Token: ' . $token->getToken() . '<br>';
$token = $provider->getAccessToken(
$token = $provider->getToken(
'authorization_code',
[
'code' => $_GET['code']
]
);
// Use this to interact with an API on the users behalf
echo 'Token: ' . $token->getToken() . '<br>';
// Use this to get a new access token if the old one expires
echo 'Refresh Token: ' . $token->getRefreshToken() . '<br>';

View File

@ -19,21 +19,25 @@
namespace PHPMailer\PHPMailer\OAuthProvider;
use League\OAuth2\Client\Grant\RefreshToken;
use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Token\AccessToken;
/**
* PHPMailer OAuthProvider Base class.
* An abstract base class for service-provider-specific OAuth implementations.
* @author @hayageek
* Acts as a wrapper around the League OAuth2 classes.
* @author Ravishanker Kusuma (hayageek@gmail.com)
*/
abstract class Base
{
/**
* @var League\OAuth2\Client\Provider\AbstractProvider
* @var AbstractProvider
*/
protected $provider = null;
/**
* @var League\OAuth2\Client\Token\AccessToken
* @var AccessToken
*/
protected $oauthToken = null;
@ -70,7 +74,7 @@ abstract class Base
}
/**
* @return League\OAuth2\Client\Provider\AbstractProvider
* @return AbstractProvider
*/
abstract public function getProvider();
@ -84,21 +88,22 @@ abstract class Base
}
/**
* @return \League\OAuth2\Client\Grant\RefreshToken
* @return RefreshToken
*/
protected function getGrant()
{
return new \League\OAuth2\Client\Grant\RefreshToken();
return new RefreshToken();
}
/**
* @return League\OAuth2\Client\Token\AccessToken
* @return AccessToken
*/
protected function getToken()
public function getToken()
{
$provider = $this->getProvider();
$grant = $this->getGrant();
return $provider->getAccessToken($grant, ['refresh_token' => $this->oauthRefreshToken]);
return $this->getProvider()->getAccessToken(
$this->getGrant(),
['refresh_token' => $this->oauthRefreshToken]
);
}
/**
@ -126,4 +131,12 @@ abstract class Base
}
return $this->getProvider()->getAuthorizationUrl($options);
}
/**
* @return mixed
*/
public function getState()
{
return $this->getProvider()->getState();
}
}