Skip to content

OAuth2 Server

Build an OAuth 2.0 authorization server for Hyperf with league/oauth2-server.

Installation

shell
composer require friendsofhyperf/oauth2-server
php bin/hyperf.php vendor:publish friendsofhyperf/oauth2-server
php bin/hyperf.php migrate

The publish command creates config/autoload/oauth2-server.php. Configure private and public keys, the encryption key, token lifetimes, enabled grants, and scopes in that file.

Generate a key pair after configuring the private and public key paths:

shell
php bin/hyperf.php oauth2-server:generate-keypair

Configuration

Important configuration keys include:

KeyDescription
authorization_server.private_keyPrivate key path used to sign tokens.
authorization_server.private_key_passphraseOptional private key passphrase.
authorization_server.encryption_keyEncryption key used by the authorization server.
authorization_server.encryption_key_typeplain or another supported EncryptionKeyType.
authorization_server.access_token_ttlAccess token lifetime as a DateInterval.
authorization_server.refresh_token_ttlRefresh token lifetime as a DateInterval.
authorization_server.persist_access_tokenWhether issued access tokens are persisted.
resource_server.public_keyPublic key path used to validate tokens.
resource_server.jwt_leewayOptional JWT clock-skew leeway.
scopes.availableScopes that may be requested.
scopes.defaultScopes assigned when no scope is requested.

Commands

CommandDescription
oauth2-server:clear-expired-tokensRemove expired access and refresh tokens.
oauth2-server:create-clientCreate an OAuth2 client.
oauth2-server:delete-clientDelete an OAuth2 client.
oauth2-server:generate-keypairGenerate a private/public key pair.
oauth2-server:list-clientsList OAuth2 clients.
oauth2-server:update-clientUpdate an OAuth2 client.

Create a client with the grants and redirect URIs your application needs:

shell
php bin/hyperf.php oauth2-server:create-client "My App" \
    --redirect-uri="https://myapp.example/callback" \
    --grant-type="authorization_code" \
    --grant-type="refresh_token"

Token Endpoint

The package provides an authorization server factory. Use it from your own controller or route handler for token requests:

php
use FriendsOfHyperf\Oauth2\Server\Factory\AuthorizationServerFactory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

final class TokenController
{
    public function __construct(private AuthorizationServerFactory $factory)
    {
    }

    public function token(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
    {
        return $this->factory->build()->respondToAccessTokenRequest($request, $response);
    }
}

Protecting Resources

Use ResourceServerMiddleware on protected routes:

php
use FriendsOfHyperf\Oauth2\Server\Middleware\ResourceServerMiddleware;
use Hyperf\HttpServer\Router\Router;

Router::addGroup('/api', function () {
    Router::get('user', [UserController::class, 'index']);
}, [
    'middleware' => [ResourceServerMiddleware::class],
]);

If you need direct validation, build the resource server with ResourceServerFactory and call validateAuthenticatedRequest().