All Projects → thephpleague → Oauth2 Google

thephpleague / Oauth2 Google

Licence: mit
Google Provider for the OAuth 2.0 Client

Projects that are alternatives of or similar to Oauth2 Google

Netcore Postgres Oauth Boiler
A basic .NET Core website boilerplate using PostgreSQL for storage, Adminer for db management, Let's Encrypt for SSL certificates and NGINX for routing.
Stars: ✭ 57 (-78.73%)
Mutual labels:  google, oauth2
Assent
Multi-provider framework in Elixir
Stars: ✭ 126 (-52.99%)
Mutual labels:  google, oauth2
Outlookgooglecalendarsync
Sync your Outlook and Google calendars
Stars: ✭ 1,113 (+315.3%)
Mutual labels:  google, oauth2
Cloudfront Auth
An AWS CloudFront [email protected] function to authenticate requests using Google Apps, Microsoft, Auth0, OKTA, and GitHub login
Stars: ✭ 471 (+75.75%)
Mutual labels:  google, oauth2
Pow assent
Multi-provider authentication for your Pow enabled app
Stars: ✭ 236 (-11.94%)
Mutual labels:  google, oauth2
Sns auth
通用第三方登录SDK,支持微信,微信扫码,QQ,微博登录,支付宝登录,Facebook,Line,Twitter,Google
Stars: ✭ 520 (+94.03%)
Mutual labels:  google, oauth2
Yup Oauth2
An oauth2 client implementation providing the Device, Installed and Service Account flows.
Stars: ✭ 122 (-54.48%)
Mutual labels:  google, oauth2
Oauth2 Family Barrel
OAuth2全家桶项目。本项目演示了如何使用spring-boot、spring-security以及spring-security-oauth快速构建OAuth2服务框架体系。
Stars: ✭ 188 (-29.85%)
Mutual labels:  oauth2, oauth2-provider
React Native Google Sign In
React Native Wrapper for Latest Google Sign-In OAuth SDK / API
Stars: ✭ 213 (-20.52%)
Mutual labels:  google, oauth2
Gam
command line management for Google Workspace
Stars: ✭ 2,558 (+854.48%)
Mutual labels:  google, oauth2
Oauth
🔗 OAuth 2.0 implementation for various providers in one place.
Stars: ✭ 336 (+25.37%)
Mutual labels:  google, oauth2
SimpleOAuth
Simple OAuth 2.0 for Android
Stars: ✭ 15 (-94.4%)
Mutual labels:  oauth2, oauth2-provider
Light Oauth2
A fast, light and cloud native OAuth 2.0 authorization microservices based on light-4j
Stars: ✭ 247 (-7.84%)
Mutual labels:  oauth2, oauth2-provider
Socialite
Socialite is an OAuth2 Authentication tool. It is inspired by laravel/socialite, you can easily use it without Laravel.
Stars: ✭ 1,026 (+282.84%)
Mutual labels:  google, oauth2
Authlib
The ultimate Python library in building OAuth, OpenID Connect clients and servers. JWS,JWE,JWK,JWA,JWT included.
Stars: ✭ 2,854 (+964.93%)
Mutual labels:  oauth2, oauth2-provider
Ueberauth google
Google OAuth2 Strategy for Überauth.
Stars: ✭ 110 (-58.96%)
Mutual labels:  google, oauth2
Doorkeeper Provider App
An example OAuth 2 provider application using the Doorkeeper gem, Rails and Devise
Stars: ✭ 146 (-45.52%)
Mutual labels:  oauth2, oauth2-provider
Oauth2
OAuth 2.0 server library for the Go programming language.
Stars: ✭ 2,173 (+710.82%)
Mutual labels:  oauth2, oauth2-provider
Aerogear Ios Oauth2
Client library for OAuth2/OpenID Connect
Stars: ✭ 151 (-43.66%)
Mutual labels:  google, oauth2
Hackathon Starter Kit
A Node-Typescript/Express Boilerplate with Authentication(Local, Github, Facebook, Twitter, Google, Dropbox, LinkedIn, Discord, Slack), Authorization, and CRUD functionality + PWA Support!
Stars: ✭ 242 (-9.7%)
Mutual labels:  google, oauth2

Google Provider for OAuth 2.0 Client

Join the chat Build Status Code Coverage Code Quality License Latest Stable Version

This package provides Google OAuth 2.0 support for the PHP League's OAuth 2.0 Client.

This package is compliant with PSR-1, PSR-2 and PSR-4. If you notice compliance oversights, please send a patch via pull request.

Requirements

The following versions of PHP are supported.

  • PHP 7.2
  • PHP 7.3
  • PHP 7.4
  • PHP 8.0

This package uses OpenID Connect 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 {google-client-id} and {google-client-secret} in the documentation.

Please follow the Google instructions to create the required credentials.

Installation

To install, use composer:

composer require league/oauth2-google

Usage

Authorization Code Flow

require __DIR__ . '/vendor/autoload.php';

use League\OAuth2\Client\Provider\Google;

session_start(); // Remove if session.auto_start=1 in php.ini

$provider = new Google([
    'clientId'     => '{google-client-id}',
    'clientSecret' => '{google-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

The Google provider has the following options:

  • 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

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

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

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

Parsing the JWT will require a JWT parser. Refer to parser documentation for instructions.

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 League\OAuth2\Client\Provider\Google;

$provider = new Google([
    'clientId'     => '{google-client-id}',
    'clientSecret' => '{google-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 consent prompt:

$authUrl = $provider->getAuthorizationUrl(['prompt' => 'consent']);

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

use League\OAuth2\Client\Provider\Google;
use League\OAuth2\Client\Grant\RefreshToken;

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

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

Scopes

Additional scopes can be set by using the scope parameter when generating the authorization URL:

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

Testing

Tests can be run with:

composer test

Style checks can be run with:

composer check

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].