PHPackages                             awuniversity/oauth2-aw - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. awuniversity/oauth2-aw

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

awuniversity/oauth2-aw
======================

OAuth 2.0 Client AW

1.0.8(5y ago)0561MITPHP

Since Sep 18Pushed 5y ago1 watchersCompare

[ Source](https://github.com/awuniversity/oauth2-aw)[ Packagist](https://packagist.org/packages/awuniversity/oauth2-aw)[ RSS](/packages/awuniversity-oauth2-aw/feed)WikiDiscussions master Synced yesterday

READMEChangelog (3)Dependencies (1)Versions (10)Used By (1)

AW Provider for OAuth 2.0 Client
================================

[](#aw-provider-for-oauth-20-client)

[![Join the chat](https://camo.githubusercontent.com/2de4c3b205256fa6697b46aa7b43084118c60392fbe141787bad98b07df1e30f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6769747465722d6a6f696e2d3144434537332e737667)](https://gitter.im/awuniversity/oauth2-aw)[![Build Status](https://camo.githubusercontent.com/60230605a565c061251f717285c28297d97840b323bd79bf0f9966feb44082dc/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6177756e69766572736974792f6f61757468322d61772e737667)](https://travis-ci.org/awuniversity/oauth2-aw)[![Code Coverage](https://camo.githubusercontent.com/f7dc3b4b42abb52fd4fc49769c98cdd5ba31bbccdac5090932c773af68ab75bf/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6177756e69766572736974792f6f61757468322d61772e737667)](https://coveralls.io/r/awuniversity/oauth2-aw)[![Code Quality](https://camo.githubusercontent.com/8041dac0467e585014c71e83239889ab843f80b680b33bf0e0845e42b42cb05d/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6177756e69766572736974792f6f61757468322d61772e737667)](https://scrutinizer-ci.com/g/awuniversity/oauth2-aw/)[![License](https://camo.githubusercontent.com/0237a0e811b24f12295a8a4d8ed732940957dc3d773596744d53feb7f6a69f18/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f4177556e69766572736974792f6f61757468322d61772e737667)](https://github.com/awuniversity/oauth2-aw/blob/master/LICENSE)[![Latest Stable Version](https://camo.githubusercontent.com/7905fd5e4367815562cc45a8825d6567c40d067a0b10244654efb6fc03af0d8f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f4177556e69766572736974792f6f61757468322d61772e737667)](https://packagist.org/packages/AwUniversity/oauth2-aw)

This package provides Google OAuth 2.0 support for the PHP AwUniversity's [OAuth 2.0 Client](https://github.com/awuniversity/oauth2-client).

This package is compliant with [PSR-1](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md), [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) and [PSR-4](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md). If you notice compliance oversights, please send a patch via pull request.

Requirements
------------

[](#requirements)

The following versions of PHP are supported.

- PHP 7.0
- PHP 7.1
- PHP 7.2
- PHP 7.3

This package uses [OpenID Connect](https://awuniversity...) to authenticate users with Google accounts.

To use this package, it will be necessary to have a Google client ID and client secret. These are referred to as `{aw-client-id}` and `{aw-client-secret}`in the documentation.

Please follow the [Google instructions](https://awuniversity...) to create the required credentials.

Installation
------------

[](#installation)

To install, use composer:

```
composer require awuniversity/oauth2-aw
```

Usage
-----

[](#usage)

### Authorization Code Flow

[](#authorization-code-flow)

```
use AwU\OAuth2\Client\Provider\Google;

$provider = new Google([
    'clientId'     => '{aw-client-id}',
    'clientSecret' => '{aw-client-secret}',
    'redirectUri'  => 'https://example.com/callback-url',
    'hostedDomain' => 'example.com', // optional; used to restrict access to users on your G Suite/Google Apps for Business accounts
]);

if (!empty($_GET['error'])) {

    // Got an error, probably user denied access
    exit('Got error: ' . htmlspecialchars($_GET['error'], ENT_QUOTES, 'UTF-8'));

} elseif (empty($_GET['code'])) {

    // If we don't have an authorization code then get one
    $authUrl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: ' . $authUrl);
    exit;

} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

    // State is invalid, possible CSRF attack in progress
    unset($_SESSION['oauth2state']);
    exit('Invalid state');

} else {

    // Try to get an access token (using the authorization code grant)
    $token = $provider->getAccessToken('authorization_code', [
        'code' => $_GET['code']
    ]);

    // Optional: Now you have a token you can look up a users profile data
    try {

        // We got an access token, let's now get the owner details
        $ownerDetails = $provider->getResourceOwner($token);

        // Use these details to create a new profile
        printf('Hello %s!', $ownerDetails->getFirstName());

    } catch (Exception $e) {

        // Failed to get user details
        exit('Something went wrong: ' . $e->getMessage());

    }

    // Use this to interact with an API on the users behalf
    echo $token->getToken();

    // Use this to get a new access token if the old one expires
    echo $token->getRefreshToken();

    // Unix timestamp at which the access token expires
    echo $token->getExpires();
}
```

#### Available Options

[](#available-options)

The `Google` provider has the following [options](https://awuniversity...):

- `accessType` to use online or offline access
- `hostedDomain` to authenticate G Suite users
- `prompt` to modify the prompt that the user will see
- `scopes` to request access to additional user information

#### Accessing Token JWT

[](#accessing-token-jwt)

Google provides a [JSON Web Token](https://jwt.io/) (JWT) with all access tokens. This token [contains basic information](https://awuniversity...) about the authenticated user. The JWT can be accessed from the `id_token` value of the access token:

```
/** @var AwU\OAuth2\Client\Token\AccessToken $token */
$values = $token->getValues();

/** @var string */
$jwt = $values['id_token'];
```

Parsing the JWT will require a [JWT parser](https://awuniversity...). Refer to parser documentation for instructions.

### Refreshing a Token

[](#refreshing-a-token)

Refresh tokens are only provided to applications which request offline access. You can specify offline access by setting the `accessType` option in your provider:

```
use AwU\OAuth2\Client\Provider\Google;

$provider = new Google([
    'clientId'     => '{aw-client-id}',
    'clientSecret' => '{aw-client-secret}',
    'redirectUri'  => 'https://example.com/callback-url',
    'accessType'   => 'offline',
]);
```

It is important to note that the refresh token is only returned on the first request after this it will be `null`. You should securely store the refresh token when it is returned:

```
$token = $provider->getAccessToken('authorization_code', [
    'code' => $code
]);

// persist the token in a database
$refreshToken = $token->getRefreshToken();
```

If you ever need to get a new refresh token you can request one by forcing the approval prompt:

```
$authUrl = $provider->getAuthorizationUrl(['approval_prompt' => 'force']);
```

Now you have everything you need to refresh an access token using a refresh token:

```
use AwU\OAuth2\Client\Provider\Google;
use AwU\OAuth2\Client\Grant\RefreshToken;

$provider = new Google([
    'clientId'     => '{aw-client-id}',
    'clientSecret' => '{aw-client-secret}',
    'redirectUri'  => 'https://example.com/callback-url',
]);

$grant = new RefreshToken();
$token = $provider->getAccessToken($grant, ['refresh_token' => $refreshToken]);
```

Scopes
------

[](#scopes)

Additional [scopes](https://awuniversity...) can be set by using the `scope` parameter when generating the authorization URL:

```
$authorizationUrl = $provider->getAuthorizationUrl([
    'scope' => [
        'scope-url-here'
    ],
]);
```

Testing
-------

[](#testing)

Tests can be run with:

```
composer test
```

Style checks can be run with:

```
composer check
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/awuniversity/oauth2-aw/blob/master/CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- [Woody Gilk](https://github.com/shadowhand)
- [All Contributors](https://github.com/awuniversity/oauth2-aw/contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](https://github.com/awuniversity/oauth2-aw/blob/master/LICENSE) for more information.

oauth2-aw
=========

[](#oauth2-aw)

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity64

Established project with proven stability

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~38 days

Recently: every ~75 days

Total

9

Last Release

2121d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/80e22063e4878aead3cf06cd0535f54c29ac1e0fc1f17aa4d3f9d48c87f763b7?d=identicon)[AwUniversity](/maintainers/AwUniversity)

### Embed Badge

![Health badge](/badges/awuniversity-oauth2-aw/health.svg)

```
[![Health](https://phpackages.com/badges/awuniversity-oauth2-aw/health.svg)](https://phpackages.com/packages/awuniversity-oauth2-aw)
```

###  Alternatives

[namshi/jose

JSON Object Signing and Encryption library for PHP.

1.8k99.6M101](/packages/namshi-jose)[league/oauth1-client

OAuth 1.0 Client Library

99698.8M106](/packages/league-oauth1-client)[gesdinet/jwt-refresh-token-bundle

Implements a refresh token system over Json Web Tokens in Symfony

70516.4M35](/packages/gesdinet-jwt-refresh-token-bundle)[league/oauth2-google

Google OAuth 2.0 Client Provider for The PHP League OAuth2-Client

41721.2M118](/packages/league-oauth2-google)[illuminate/auth

The Illuminate Auth package.

9327.3M1.0k](/packages/illuminate-auth)[beatswitch/lock

A flexible, driver based Acl package for PHP 5.4+

870304.7k2](/packages/beatswitch-lock)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
