Download the PHP package plai2010/php-oauth2 without Composer
On this page you can find all versions of the php package plai2010/php-oauth2. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package php-oauth2
OAuth2 Utilities for PHP
This is a utility package for handling OAuth2 in PHP applications. It allows obtaining OAuth2 access token through an authorization code grant with just a browser and a command shell running side by side; there is no need to set up a working web endpoint for the OAuth2 provider to call back.
On-line authorization flow with working redirect is also supported. See this section for how it may be set up in an Laravel application.
The use case for this package was SMTP XOAUTH2 authentication in a Laravel (10.x) application. A Laravel service provider is included.
This package depends on league/oauth2-client,
its GenericProvider
in particular.
Installation
The package can be installed from Packagist:
One may also clone the source repository from Github
:
Example: Obtaining Access Token for Outlook SMTP
Let us say a web application has been registered in Micrsoft Azure AD:
* Application ID - 11111111-2222-3333-4444-567890abcdef
* Application secret - v8rstf8eVD5My89xDOTw8CoKG6rIw9dukIjHYzPU
* Redirect URI - http://localhost/example
For our purpose the redirect URI should not point to an actual web site. It can be any URL in proper format; just test with a browser to ake sure it yields a 404 error.
Create a PHP script, say outlook-oauth2.php
:
To obtain an OAuth2 token for SMTP login (XOAUTH2), start an interactive PHP shell:
Obtain an authorization URL:
An URL like this is printed as the result (line breaks inserted):
Do not end the interactive PHP shell. Copy the URL into a browser and go through the steps to authorize access. At the end the browser will be redirected to an non-existing page with URL that matches the redirect URI. Go back to the the interactive PHP shell to process that URL:
The token can be saved to some storage (e.g. database) for use by an application. If the token has expiration and is refreshable, the application would request a refresh. This is illustrated as follows:
Provider Manager
An application may interact with multiple OAuth2 providers.
For example, it may need authorization from Google to access
files in Google Drive and from Microsoft to send email via
Outlook SMTP. OAuth2Manager
makes
multiple OAuth2 providers available. For example,
Then to interact with Microsoft to obtain access token for SMTP login, one would do this:
Token Repository
It is up to an application to manage the storage of OAuth2 tokens. This
package does includes TokenRepository
and some implementation as a model, however. In this model, a token is
identified by a two-part key: provider:
usage. The two parts
correspond to parameters of OAuth2Manager::get()
(src/OAuth2Manager.php),
so that a repository and refresh tokens as needed. The key scheme can
be extended to suit an application. For example, to allow per-user OAuth2
tokens, provider:
usage:
user_id may suffice.
AbstractTokenRepository
is an abstract implementation. Just provide tokenLoad()
and tokenSave()
methods.
Laravel Integration
This package includes a Laravel service provider that makes
a singleton OAuth2Manager
available
two ways:
* Abstract 'oauth2' in the application container, i.e. `app('oauth2')`.
* Facade alias 'OAuth2', i.e. `OAuth2::`.
The configuration file is config/oauth2.php
. It returns an
associative array of provider configurations by names, like this:
There is no TokenRepository
setup,
but here is an example of making available as 'oauth2_tokens' a singleton
DirectoryTokenRepository
:
Example: Online Flow in Laravel Application
Let's say a Laravel application at example.com
is using this package. One
can add a route in routes/web.php
for starting an authorization code grant
flow:
Using Microsoft SMTP as in the Provider Manager example,
request https://example.com/oauth2/authorize/microsoft/smtp
would redirect
to Microsoft's login.microsoftonline.com
.
A route named oauth2.callback
is assumed above for receiving authorization
code. Here is what it would look like:
On Microsoft side, OAuth2 should be configured to allow redirect URI
https://example.com/oauth2/callback/microsoft/smtp
.