Convert Figma logo to code with AI

thephpleague logooauth2-server

A spec compliant, secure by default PHP OAuth 2.0 Server

6,516
1,118
6,516
95

Top Related Projects

A library for implementing an OAuth2 Server in php

Easy integration with OAuth 2.0 service providers.

9,353

PHP package for JWT

7,251

A simple library to work with JSON Web Token and JSON Web Signature

3,238

Platform-Agnostic Security Tokens

Quick Overview

The OAuth2-Server by The PHP League is a robust and standards-compliant OAuth 2.0 authorization server implementation for PHP. It provides a secure and flexible framework for implementing OAuth 2.0 in PHP applications, allowing developers to easily add authentication and authorization capabilities to their projects.

Pros

  • Fully compliant with OAuth 2.0 specifications
  • Highly customizable and extensible
  • Well-documented with comprehensive guides
  • Active community and regular updates

Cons

  • Steep learning curve for beginners
  • Requires thorough understanding of OAuth 2.0 concepts
  • Limited built-in support for some advanced OAuth 2.0 features
  • May require additional libraries for complete functionality

Code Examples

  1. Creating an Authorization Server
$server = new \League\OAuth2\Server\AuthorizationServer(
    $clientRepository,
    $accessTokenRepository,
    $scopeRepository,
    $privateKey,
    $encryptionKey
);
  1. Adding Grant Types
$server->enableGrantType(
    new \League\OAuth2\Server\Grant\AuthCodeGrant(
        $authCodeRepository,
        $refreshTokenRepository,
        new \DateInterval('PT10M')
    ),
    new \DateInterval('PT1H')
);
  1. Handling Authorization Request
$authRequest = $server->validateAuthorizationRequest($request);
$authRequest->setUser(new UserEntity());
$authRequest->setAuthorizationApproved(true);
return $server->completeAuthorizationRequest($authRequest, $response);

Getting Started

  1. Install the library via Composer:
composer require league/oauth2-server
  1. Set up your repositories and entities:
$clientRepository = new ClientRepository();
$scopeRepository = new ScopeRepository();
$accessTokenRepository = new AccessTokenRepository();
$privateKey = new CryptKey('file://path/to/private.key');
$encryptionKey = 'lxZFUEsBCJ2Yb14IF2ygAHI5N4+ZAUXXaSeeJm6+twsUmIen';
  1. Create and configure the server:
$server = new \League\OAuth2\Server\AuthorizationServer(
    $clientRepository,
    $accessTokenRepository,
    $scopeRepository,
    $privateKey,
    $encryptionKey
);

$server->enableGrantType(
    new \League\OAuth2\Server\Grant\AuthCodeGrant(
        $authCodeRepository,
        $refreshTokenRepository,
        new \DateInterval('PT10M')
    ),
    new \DateInterval('PT1H')
);

Competitor Comparisons

A library for implementing an OAuth2 Server in php

Pros of oauth2-server-php

  • Longer history and more established in the PHP community
  • Supports a wider range of grant types out of the box
  • More extensive documentation and examples available

Cons of oauth2-server-php

  • Less active development and maintenance in recent years
  • May not fully support the latest OAuth 2.0 specifications and best practices
  • Potentially more complex setup and configuration process

Code Comparison

oauth2-server-php:

$server = new OAuth2\Server($storage);
$server->addGrantType(new OAuth2\GrantType\AuthorizationCode($storage));
$server->handleTokenRequest(OAuth2\Request::createFromGlobals())->send();

oauth2-server:

$server = new \League\OAuth2\Server\AuthorizationServer(
    $clientRepository,
    $accessTokenRepository,
    $scopeRepository,
    $privateKey,
    $encryptionKey
);
$server->enableGrantType(new \League\OAuth2\Server\Grant\AuthCodeGrant($authCodeRepository, $refreshTokenRepository, $authCodeTTL));

Both libraries provide similar functionality for implementing OAuth 2.0 servers in PHP. oauth2-server-php offers more grant types out of the box and has a longer history, while oauth2-server (by The PHP League) has more active development and adheres more closely to modern PHP practices and the latest OAuth 2.0 specifications.

Easy integration with OAuth 2.0 service providers.

Pros of oauth2-client

  • Simplifies the implementation of OAuth 2.0 for client-side applications
  • Provides a wide range of pre-built provider integrations (e.g., Google, Facebook, GitHub)
  • Offers a consistent interface for working with various OAuth 2.0 providers

Cons of oauth2-client

  • Limited to client-side functionality, not suitable for building OAuth 2.0 servers
  • May require additional configuration for custom OAuth 2.0 providers
  • Less flexibility in terms of customizing the OAuth 2.0 flow compared to oauth2-server

Code Comparison

oauth2-client:

$provider = new \League\OAuth2\Client\Provider\GenericProvider([
    'clientId'     => 'your-client-id',
    'clientSecret' => 'your-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'
]);

oauth2-server:

$server = new \League\OAuth2\Server\AuthorizationServer(
    $clientRepository,
    $accessTokenRepository,
    $scopeRepository,
    $privateKey,
    $encryptionKey
);
$server->enableGrantType(new \League\OAuth2\Server\Grant\AuthCodeGrant($authCodeRepository, $refreshTokenRepository, new \DateInterval('PT10M')));
9,353

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

Cons of php-jwt

  • Limited to JWT functionality, not a complete OAuth 2.0 solution
  • Requires additional implementation for OAuth 2.0 flows
  • Less comprehensive documentation compared to oauth2-server

Code Comparison

php-jwt:

use Firebase\JWT\JWT;

$payload = ['user_id' => 123];
$jwt = JWT::encode($payload, $key, 'HS256');
$decoded = JWT::decode($jwt, new Key($key, 'HS256'));

oauth2-server:

$server = new \League\OAuth2\Server\AuthorizationServer(
    $clientRepository,
    $accessTokenRepository,
    $scopeRepository,
    $privateKey,
    $encryptionKey
);
$response = $server->respondToAccessTokenRequest($request, $response);

Summary

php-jwt is a lightweight library focused on JWT handling, making it easy to integrate into existing projects. However, it lacks the comprehensive OAuth 2.0 functionality provided by oauth2-server. oauth2-server offers a complete OAuth 2.0 implementation but requires more setup and has a steeper learning curve. The choice between the two depends on the specific requirements of your project and whether you need a full OAuth 2.0 solution or just JWT handling.

7,251

A simple library to work with JSON Web Token and JSON Web Signature

Pros of JWT

  • Lightweight and focused solely on JSON Web Tokens
  • More flexible for use in various authentication scenarios beyond OAuth2
  • Easier to integrate into existing systems that don't require full OAuth2 implementation

Cons of JWT

  • Lacks built-in OAuth2 server functionality
  • Requires additional components to implement a complete OAuth2 solution
  • May need more configuration and setup for OAuth2-specific use cases

Code Comparison

oauth2-server (Authorization Code Grant):

$server->respondToAccessTokenRequest($request, $response);

JWT (Token Creation):

$token = $config->builder()
    ->issuedBy('https://example.com')
    ->withClaim('uid', 1)
    ->getToken($config->signer(), $config->signingKey());

Summary

OAuth2-server is a comprehensive OAuth2 implementation, while JWT focuses on JSON Web Token handling. OAuth2-server is better suited for full OAuth2 server setups, whereas JWT offers more flexibility for various token-based authentication scenarios. The choice depends on specific project requirements and existing infrastructure.

3,238

Platform-Agnostic Security Tokens

Pros of PASETO

  • Simpler and more secure by design, reducing the risk of implementation errors
  • Provides stronger encryption and authentication mechanisms out of the box
  • Supports local (symmetric) and public (asymmetric) tokens, offering more flexibility

Cons of PASETO

  • Less widely adopted compared to OAuth 2.0, potentially limiting ecosystem support
  • May require more effort to integrate with existing systems that expect OAuth 2.0
  • Lacks some OAuth 2.0 features like scopes and refresh tokens (though these can be implemented)

Code Comparison

PASETO token creation:

use ParagonIE\Paseto\Builder;
use ParagonIE\Paseto\Keys\SymmetricKey;

$key = new SymmetricKey('YELLOW SUBMARINE, BLACK WIZARDRY');
$token = (new Builder())
    ->setKey($key)
    ->setIssuedAt()
    ->setExpiration(new \DateTime('+1 hour'))
    ->setClaims(['user_id' => 123])
    ->toString();

OAuth 2.0 access token creation:

use League\OAuth2\Server\AuthorizationServer;

$server = new AuthorizationServer(
    $clientRepository,
    $accessTokenRepository,
    $scopeRepository,
    $privateKey,
    $encryptionKey
);

$accessToken = $server->respondToAccessTokenRequest($request, $response);

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

PHP OAuth 2.0 Server

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

league/oauth2-server is a standards compliant implementation of an OAuth 2.0 authorization server written in PHP which makes working with OAuth 2.0 trivial. You can easily configure an OAuth 2.0 server to protect your API with access tokens, or allow clients to request new access tokens and refresh them.

Out of the box it supports the following grants:

  • Authorization code grant
  • Client credentials grant
  • Device authorization grant
  • Implicit grant
  • Refresh grant
  • Resource owner password credentials grant

The following RFCs are implemented:

This library was created by Alex Bilbie. Find him on Twitter at @alexbilbie.

Requirements

The latest version of this package supports the following versions of PHP:

  • PHP 8.1
  • PHP 8.2
  • PHP 8.3

The openssl and json extensions are also required.

All HTTP messages passed to the server should be PSR-7 compliant. This ensures interoperability with other packages and frameworks.

Installation

composer require league/oauth2-server

Documentation

The library documentation can be found at https://oauth2.thephpleague.com. You can contribute to the documentation in the gh-pages branch.

Testing

The library uses PHPUnit for unit tests.

vendor/bin/phpunit

Continuous Integration

We use Github Actions, Scrutinizer, and StyleCI for continuous integration. Check out our configuration files if you'd like to know more.

Community Integrations

Changelog

See the project changelog

Contributing

Contributions are always welcome. Please see CONTRIBUTING.md and CODE_OF_CONDUCT.md for details.

Support

Bugs and feature request are tracked on GitHub.

If you have any questions about OAuth please open a ticket here; please don't email the address below.

Security

If you discover any security related issues, please email andrew@noexceptions.io instead of using the issue tracker.

License

This package is released under the MIT License. See the bundled LICENSE file for details.

Credits

This code is principally developed and maintained by Andy Millington.

Between 2012 and 2017 this library was developed and maintained by Alex Bilbie.

PHP OAuth 2.0 Server is one of many packages provided by The PHP League. To find out more, please visit our website.

Special thanks to all of these awesome contributors.

Additional thanks go to the Mozilla Secure Open Source Fund for funding a security audit of this library.

The initial code was developed as part of the Linkey project which was funded by JISC under the Access and Identity Management programme.