tipbr/silverstripe-restfulserver-jwt-auth
Composer 安装命令:
composer require tipbr/silverstripe-restfulserver-jwt-auth
包简介
JWT Authentication for SilverStripe RestfulServer - provides secure API authentication with proper permission integration
README 文档
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.
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2025-09-29