Download the PHP package pumasoft/puma-api without Composer
On this page you can find all versions of the php package pumasoft/puma-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pumasoft/puma-api
More information about pumasoft/puma-api
Files in pumasoft/puma-api
Package puma-api
Short Description Pumasoft API certificate issuer engine
License proprietary
Informations about the package puma-api
PumaAPI
API Gateway for Puma Services
PumaAPI is a lightweight micro API module designed to parse, validate, and authenticate REST requests based on a simple, file-driven contract architecture. It leverages JWT (JSON Web Token) authentication and provides a clean, declarative way to define your API endpoints through JSON contract files.
Table of Contents
- Features
- Requirements
- Installation
- Quick Start
- Architecture Overview
- Manifest Structure
- Directory Layout
- Contract File Format
- Validation Rules
- JWT Authentication
- Service Configuration
- Making Outbound API Calls
- Certificate Object
- Error Handling
- Global Configuration Flags
- Complete Usage Example
- Security Considerations
- API Reference
Features
- ๐ File-Based Contract System - Define API contracts using simple JSON files organized by HTTP method
- ๐ JWT Authentication - Built-in JWT parsing, validation, and signature verification
- โ Request Validation - Automatic validation of headers, body, and JWT payloads against contracts
- ๐งน Request Sanitization - Returns only contracted fields, preventing data leakage
- ๐ค Outbound API Caller - Built-in HTTP client for making authenticated requests to other services
- ๐ฏ RESTful Design - Supports GET, POST, PUT, and DELETE methods
- โก Lightweight - Minimal dependencies (only
ext-jsonandext-curl) - ๐ก๏ธ Secure by Default - Production-ready security with optional development flags
Requirements
- PHP 5.6 or higher
ext-jsonextensionext-curlextension- Apache with mod_rewrite (recommended)
Installation
Manual Installation
- Clone or download the repository
- Include the autoloader or manually require the necessary files
Quick Start
Architecture Overview
PumaAPI follows a contract-first approach where API behavior is defined through JSON manifest files:
Core Components
| Component | Description |
|---|---|
API |
Main controller that orchestrates the request lifecycle |
Request |
Parses and structures incoming HTTP requests |
Contract |
Loads and validates requests against JSON contracts |
Validator |
Performs field-level validation based on rules |
Tokenizer |
Handles JWT generation, parsing, and verification |
Certificate |
Contains validated request data for controller use |
Caller |
Makes outbound HTTP requests to other services |
Rawr |
Custom exception handler with HTTP response support |
Manifest Structure
Directory Layout
The manifest directory follows a RESTful hierarchy:
URL Mapping:
| HTTP Request | Contract File |
|---|---|
GET /auth/login |
__manifest/get/auth/login.json |
POST /users/create |
__manifest/post/users/create.json |
PUT /users/update |
__manifest/put/users/update.json |
DELETE /users/remove |
__manifest/delete/users/remove.json |
Contract File Format
Each JSON contract defines the expected request format and response structure:
Validation Rules
Validation rules are specified using the <<ruleName>> syntax. Available built-in rules:
| Rule | Description | Example |
|---|---|---|
<<notEmptyString>> |
Non-empty string value | "username": "<<notEmptyString>>" |
<<integer>> |
Integer value | "age": "<<integer>>" |
<<validAlgorithm>> |
JWT algorithm matching service.ini | "alg": "<<validAlgorithm>>" |
<<validTokenType>> |
JWT type matching service.ini | "typ": "<<validTokenType>>" |
<<validIssuer>> |
Issuer registered in service.ini | "iss": "<<validIssuer>>" |
<<validUnixTimestamp>> |
Valid Unix timestamp | "exp": "<<validUnixTimestamp>>" |
Exact Value Matching
If no rule syntax is used, the validator expects an exact match:
This requires the Content-Type header to be exactly application/json.
JWT Authentication
PumaAPI uses JWT Bearer tokens for authentication. Tokens must be sent in the Authorization header:
Token Structure
Generating Tokens
Use the Tokenizer class to generate new tokens:
Service Configuration
Create a service.ini file in your manifest directory:
Configuration Sections
| Section | Purpose |
|---|---|
[ident] |
Service identity (issuer name) |
[token] |
Default JWT header configuration |
[auth] |
Registered issuers and their secret keys |
Making Outbound API Calls
Use the Caller class to make authenticated requests to other services:
Certificate Object
After successful validation, the Certificate object provides access to sanitized request data:
Error Handling
PumaAPI uses the Rawr exception class for structured error handling:
HTTP Status Codes
| Code | Constant | Description |
|---|---|---|
| 400 | Rawr::BAD_REQUEST |
Invalid request format or missing fields |
| 401 | Rawr::UNAUTHORIZED |
Invalid or missing authentication |
| 403 | Rawr::FORBIDDEN |
Access denied |
| 404 | Rawr::NOT_FOUND |
Resource or endpoint not found |
| 405 | Rawr::METHOD_NOT_ALLOWED |
HTTP method not supported |
| 500 | Rawr::INTERNAL_ERROR |
Server-side error |
Error Response Format
Production (default):
Development (with PUMA_API_SEND_EXCEPTIONS_IN_RESPONSE):
Global Configuration Flags
โ ๏ธ Warning: These flags are intended for development only. Never enable them in production!
Define these constants in your bootstrap file before instantiating the API class. The presence of the constant enables the feature (the value is ignored).
Complete Usage Example
1. Project Structure
2. Apache Rewrite Rules (.htaccess)
3. Contract File (__manifest/post/auth/login.json)
4. Service Configuration (__manifest/service.ini)
5. Entry Point (index.php)
6. Controller (controllers/AuthController.php)
Security Considerations
- Keep service.ini secure - This file contains secret keys; ensure it's not web-accessible
- Use HTTPS - Always use SSL/TLS in production
- Rotate secrets regularly - Update JWT signing keys periodically
- Set short expiration times - Use reasonable
expvalues for tokens - Disable development flags - Never use
PUMA_API_*constants in production - Validate all input - Define comprehensive contracts for all endpoints
API Reference
PumaAPI\Controller\API
| Method | Returns | Description |
|---|---|---|
__construct($manifestPath = false) |
API |
Initialize with optional manifest path |
getCertificate() |
Certificate |
Get validated request certificate |
PumaAPI\Model\Certificate
| Method | Returns | Description |
|---|---|---|
getRequestedMethod() |
string |
HTTP method (lowercase) |
getRequestedRoot() |
string |
Controller/root segment |
getRequestedResource() |
string |
Resource path |
getRequestHeaders() |
array |
Validated request headers |
getRequestBody() |
array |
Validated request body |
getRequestedJWTHead() |
array |
JWT header claims |
getRequestedJWTPayload() |
array |
JWT payload claims |
getResponseContract() |
array |
Response contract definition |
PumaAPI\Model\Tokenizer
| Method | Returns | Description |
|---|---|---|
__construct($configPath = false) |
Tokenizer |
Initialize with config path |
generateNewToken($issuer, $head, $body) |
string |
Generate complete JWT |
generateSignatureFor($issuer, $head, $body) |
string |
Generate signature only |
getCurrentIssuer() |
string |
Get configured issuer |
getCurrentAlgorithm() |
string |
Get configured algorithm |
getCurrentTokenType() |
string |
Get configured token type |
isAuthentic($tokenContent, $issuer) |
bool |
Verify token signature |
validExpiryDate($tokenHead) |
bool |
Check if token is expired |
extractJWT($headers) |
array |
Parse JWT from headers (static) |
PumaAPI\Model\Caller
| Method | Returns | Description |
|---|---|---|
__construct($method, $url, $headers, $jwt, $body) |
Caller |
Initialize request |
initRequest() |
void |
Execute the HTTP request |
getResponse() |
array |
Get response (code, headers, body) |
getJWTResponse() |
array |
Parse JWT from response |
Important
This is a personal side project developed independently in my own time, using my own equipment and resources. It has no connection to any employer, client, or commercial work.
All versions of puma-api with dependencies
ext-curl Version *