Install the Package
composer require cpj/cpjoauth2-php
Ensure your Laravel project meets the PHP (≥5.3.2) and Symfony HttpFoundation (≥2.0.0, <2.4.0-dev) requirements.
Basic OAuth2 Server Initialization
Register a route to handle OAuth2 requests (e.g., /oauth2):
use Cpj\OAuth2\Server;
use Symfony\Component\HttpFoundation\Request;
Route::get('/oauth2', function (Request $request) {
$server = new Server();
$response = $server->handleRequest($request);
return $response;
});
First Use Case: Token Endpoint
Configure a storage adapter (e.g., PDO for databases) and define a Storage class to persist tokens/clients:
use Cpj\OAuth2\Storage\PDO as Storage;
$storage = new Storage(new PDO('mysql:host=localhost;dbname=oauth', 'user', 'pass'));
$server = new Server($storage);
Client Registration
Client class to create OAuth2 clients:
$client = new \Cpj\OAuth2\Client(
'client_id',
'client_secret',
'http://client.example.com/callback',
'confidential' // or 'public'
);
$storage->setClient($client);
Authorization Code Flow
$authUrl = $server->getAuthorizationUrl(
'client_id',
'http://client.example.com/callback',
['scope' => 'read write']
);
$token = $server->getAccessToken('client_id', 'client_secret', 'authorization_code', ['code' => $code]);
Resource Owner Password Flow
$token = $server->getAccessToken(
'client_id',
'client_secret',
'password',
['username' => 'user', 'password' => 'pass', 'scope' => 'read']
);
Server::handleRequest() in middleware to validate tokens for protected routes:
public function handle($request, Closure $next) {
$server = new Server($this->storage);
$request->attributes->add(['oauth_token' => $server->verifyAccessToken($request)]);
return $next($request);
}
Symfony\Component\HttpFoundation\Request/Response as inputs/outputs. For PSR-7 (e.g., Slim Framework), adapt requests/responses manually.Scope class or storage layer.Draft-20 vs. Draft-10
Storage Layer Quirks
Storage class (e.g., PDO, Redis). Default implementations are minimal; extend for complex logic:
class CustomStorage extends \Cpj\OAuth2\Storage\PDO {
public function getClient($clientId) {
// Add custom logic (e.g., soft-deletes)
return parent::getClient($clientId);
}
}
HttpFoundation Dependency
HttpFoundation. For Laravel, ensure no conflicts with native request/response handling.Symfony\Component\HttpFoundation\Request::createFromGlobals() to bridge Laravel’s Illuminate\Http\Request.Token Validation
verifyAccessToken() returns false on failure. Always check:
if (!$token = $server->verifyAccessToken($request)) {
abort(401, 'Invalid token');
}
error_details in the server constructor:
$server = new Server($storage, ['error_details' => true]);
Storage class to log SQL/Redis operations for debugging.Custom Grant Types
Extend the GrantType class to support custom flows (e.g., JWT bearer):
class JwtGrantType extends \Cpj\OAuth2\GrantType\AbstractGrantType {
public function validate() { /* Custom logic */ }
}
Register it in the server:
$server->addGrantType('urn:ietf:params:oauth:grant-type:jwt-bearer', new JwtGrantType());
Token Enhancements
Add custom claims to tokens by overriding the getAccessToken() method in your storage layer.
Event System
Use Laravel’s events to hook into OAuth2 flows (e.g., oauth2.token.issued). Example:
event(new OAuth2TokenIssued($token));
How can I help you explore Laravel packages today?