Top Related Projects
PHP package for JWT
A library for implementing an OAuth2 Server in php
Quick Overview
The OAuth 2.0 Client Library for PHP is a powerful and flexible toolkit for integrating OAuth 2.0 authorization into PHP applications. It provides a set of tools to easily implement OAuth 2.0 clients, supporting various grant types and providers.
Pros
- Easy to use and integrate with existing PHP projects
- Supports multiple OAuth 2.0 providers out of the box
- Extensible architecture allows for custom provider implementations
- Well-documented and actively maintained
Cons
- Requires PHP 7.3 or newer, which may not be suitable for older projects
- Some advanced OAuth 2.0 features may require additional configuration
- Learning curve for developers new to OAuth 2.0 concepts
Code Examples
- Basic usage with Google provider:
$provider = new \League\OAuth2\Client\Provider\Google([
'clientId' => 'your-client-id',
'clientSecret' => 'your-client-secret',
'redirectUri' => 'https://example.com/callback-url',
]);
$authUrl = $provider->getAuthorizationUrl();
header('Location: ' . $authUrl);
exit;
- Handling the callback and retrieving user details:
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
$user = $provider->getResourceOwner($token);
echo 'Hello, ' . $user->getFirstName();
- Refreshing an access token:
$newAccessToken = $provider->getAccessToken('refresh_token', [
'refresh_token' => $existingRefreshToken
]);
Getting Started
-
Install the library using Composer:
composer require league/oauth2-client
-
Choose a provider (e.g., Google) and install its package:
composer require league/oauth2-google
-
Create a new provider instance and use it in your application:
$provider = new \League\OAuth2\Client\Provider\Google([ 'clientId' => 'your-client-id', 'clientSecret' => 'your-client-secret', 'redirectUri' => 'https://example.com/callback-url', ]); // Use $provider to generate authorization URL, handle callbacks, etc.
-
Implement the OAuth 2.0 flow in your application using the provided methods.
Competitor Comparisons
PHP package for JWT
Pros of php-jwt
- Lightweight and focused solely on JWT handling
- Simple to use with minimal setup required
- Can be easily integrated into existing projects without additional dependencies
Cons of php-jwt
- Limited to JWT functionality, lacking broader OAuth 2.0 features
- Requires manual implementation of OAuth 2.0 flow if needed
- Less comprehensive documentation compared to oauth2-client
Code Comparison
php-jwt:
use Firebase\JWT\JWT;
$payload = ['user_id' => 123];
$jwt = JWT::encode($payload, $key, 'HS256');
$decoded = JWT::decode($jwt, $key, ['HS256']);
oauth2-client:
$provider = new \League\OAuth2\Client\Provider\GenericProvider([
'clientId' => 'client_id',
'clientSecret' => 'client_secret',
'redirectUri' => 'https://example.com/callback-url',
'urlAuthorize' => 'https://example.com/oauth/authorize',
'urlAccessToken' => 'https://example.com/oauth/token',
'urlResourceOwnerDetails' => 'https://example.com/api/user'
]);
$accessToken = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
The php-jwt library is focused on JWT encoding and decoding, while oauth2-client provides a complete OAuth 2.0 client implementation. php-jwt is simpler to use for basic JWT operations, but oauth2-client offers more comprehensive OAuth 2.0 functionality out of the box.
A library for implementing an OAuth2 Server in php
Pros of oauth2-server-php
- More comprehensive OAuth2 server implementation
- Supports a wider range of grant types
- Better suited for building custom OAuth2 servers
Cons of oauth2-server-php
- Less actively maintained (last commit over 2 years ago)
- Steeper learning curve for beginners
- Fewer integrations with popular PHP frameworks
Code Comparison
oauth2-server-php:
$server = new \OAuth2\Server($storage);
$server->addGrantType(new \OAuth2\GrantType\AuthorizationCode($storage));
$server->handleTokenRequest(\OAuth2\Request::createFromGlobals())->send();
oauth2-client:
$provider = new \League\OAuth2\Client\Provider\GenericProvider([
'clientId' => 'your-client-id',
'clientSecret' => 'your-client-secret',
'redirectUri' => 'https://example.com/callback-url',
]);
$accessToken = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
oauth2-server-php is more focused on server-side implementation, while oauth2-client is designed for client-side OAuth2 interactions. The former offers more flexibility for custom OAuth2 server setups, while the latter provides a simpler interface for integrating with existing OAuth2 providers.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
OAuth 2.0 Client
This package provides a base for integrating with OAuth 2.0 service providers.
The OAuth 2.0 login flow, seen commonly around the web in the form of "Connect with Facebook/Google/etc." buttons, is a common integration added to web applications, but it can be tricky and tedious to do right. To help, we've created the league/oauth2-client
package, which provides a base for integrating with various OAuth 2.0 providers, without overburdening your application with the concerns of RFC 6749.
This OAuth 2.0 client library will work with any OAuth 2.0 provider that conforms to the OAuth 2.0 Authorization Framework. Out-of-the-box, we provide a GenericProvider
class to connect to any service provider that uses Bearer tokens. See our basic usage guide for examples using GenericProvider
.
Many service providers provide additional functionality above and beyond the OAuth 2.0 specification. For this reason, you may extend and wrap this library to support additional behavior. There are already many official and third-party provider clients available (e.g., Facebook, GitHub, Google, Instagram, LinkedIn, etc.). If your provider isn't in the list, feel free to add it.
This package is compliant with PSR-1, PSR-2, PSR-4, and PSR-7. If you notice compliance oversights, please send a patch via pull request. If you're interested in contributing to this library, please take a look at our contributing guidelines.
Requirements
We support the following versions of PHP:
- PHP 8.2
- PHP 8.1
- PHP 8.0
- PHP 7.4
- PHP 7.3
- PHP 7.2
- PHP 7.1
- PHP 7.0
- PHP 5.6
Provider Clients
We provide a list of official PHP League provider clients, as well as third-party provider clients.
To build your own provider client, please refer to "Implementing a Provider Client."
Usage
For usage and code examples, check out our basic usage guide.
Contributing
Please see our contributing guidelines for details.
License
The MIT License (MIT). Please see LICENSE for more information.
Top Related Projects
PHP package for JWT
A library for implementing an OAuth2 Server in php
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot