tipbr / silverstripe-restfulserver-jwt-auth
JWT Authentication for SilverStripe RestfulServer - provides secure API authentication with proper permission integration
Installs: 6
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 1
Type:silverstripe-vendormodule
Requires
- firebase/php-jwt: ^6.0
- silverstripe/admin: ^3.0
- silverstripe/crontask: ^4.0
- silverstripe/framework: ^6.0
- silverstripe/restfulserver: 4.x-dev
Requires (Dev)
- phpstan/extension-installer: ^1.3
- phpunit/phpunit: ^11.3
- silverstripe/standards: ^1
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2025-09-29 09:24:39 UTC
README
This module provides JWT (JSON Web Token) authentication for SilverStripe's RestfulServer module, enabling secure API access with proper permission integration.
Features
- JWT Token Authentication: Secure API authentication using industry-standard JWT tokens
- RestfulServer Integration: Seamlessly integrates with SilverStripe's RestfulServer module
- Permission Checking: Respects DataObject
canView()
,canEdit()
,canDelete()
, andcanCreate()
methods - Automatic Token Renewal: Tokens are automatically renewed when close to expiry
- CORS Support: Built-in CORS headers for cross-domain API access
- Auth API Endpoints: Login, logout, token refresh, password reset functionality
Quick Start
1. Installation
composer require tipbr/silverstripe-restfulserver-jwt-auth
2. Configuration
Set your JWT secret in your environment file:
# .env
JWT_SECRET=your-super-secret-jwt-key-here
The module comes pre-configured but you can customize settings in _config.yml
:
# Configure JWT Service Tipbr\Services\JWTService: lifetime: 604800 # 7 days in seconds renewal_threshold: 3600 # 1 hour in seconds algorithm: 'HS256'
3. Enable API Access on Your DataObjects
<?php class MyDataObject extends DataObject { private static $api_access = true; private static $db = [ 'Title' => 'Varchar(255)', 'Content' => 'Text' ]; // Permission methods are automatically respected public function canView($member = null) { return $member && $member->exists(); } public function canEdit($member = null) { return $member && $member->inGroup('editors'); } }
Usage
Authentication
Get a JWT Token
curl -X POST http://yoursite.com/auth/login \ -H "Content-Type: application/json" \ -d '{"Email": "user@example.com", "Password": "password"}'
Response:
{ "success": true, "data": { "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..." } }
API Access
Once you have a JWT token, use it to access RestfulServer endpoints:
# Get a DataObject curl -X GET http://yoursite.com/api/MyDataObject/1 \ -H "Authorization: Bearer YOUR_JWT_TOKEN" # Create a DataObject curl -X POST http://yoursite.com/api/MyDataObject \ -H "Authorization: Bearer YOUR_JWT_TOKEN" \ -H "Content-Type: application/json" \ -d '{"Title": "My New Object"}' # Update a DataObject curl -X PUT http://yoursite.com/api/MyDataObject/1 \ -H "Authorization: Bearer YOUR_JWT_TOKEN" \ -H "Content-Type: application/json" \ -d '{"Title": "Updated Title"}' # Delete a DataObject curl -X DELETE http://yoursite.com/api/MyDataObject/1 \ -H "Authorization: Bearer YOUR_JWT_TOKEN"
Authentication Endpoints
POST /auth/login
- Authenticate and get a JWT tokenGET /auth/verify
- Verify the current token and get user infoPOST /auth/refresh
- Get a fresh JWT tokenPOST /auth/register
- Register a new user accountPOST /auth/forgotPassword
- Request a password resetPOST /auth/resetPassword
- Reset password with tokenPOST /auth/changePassword
- Change password for authenticated userPOST /auth/logout
- Invalidate current session
Permission Integration
The authenticator integrates seamlessly with SilverStripe's permission system. RestfulServer automatically calls the appropriate permission methods on your DataObjects:
canView()
for GET requestscanEdit()
for PUT requestscanDelete()
for DELETE requestscanCreate()
for POST requests
The authenticated user is available via Security::getCurrentUser()
in these methods.
Documentation
Requirements
- SilverStripe Framework 6.0+
- SilverStripe Admin 3.0+
- SilverStripe RestfulServer 4.x
- Firebase JWT 6.0+
Testing
Run the test suite:
vendor/bin/phpunit tests/php/Authentication/
Support
For issues and support, please visit the GitHub repository.