PHP code example of subhashladumor1 / laravel-cybershield

1. Go to this page and download the library: Download subhashladumor1/laravel-cybershield library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

subhashladumor1 / laravel-cybershield example snippets


use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withMiddleware(function (Middleware $middleware) {
        // Option A: Protect all routes globally
        $middleware->append(\CyberShield\Http\Middleware\FirewallMiddleware::class);
        
        // Option B: Register route-level aliases
        $middleware->alias([
            'cybershield.waf'     => \CyberShield\Http\Middleware\FirewallMiddleware::class,
            'cybershield.bot'     => \CyberShield\Http\Middleware\DetectBotMiddleware::class,
            'cybershield.rate'    => \CyberShield\Http\Middleware\IpRateLimiterMiddleware::class,
        ]);
    })
    ->create();

protected $middleware = [
    // ... other global middleware
    \CyberShield\Http\Middleware\FirewallMiddleware::class,
];

protected $middlewareAliases = [
    // All 200+ cybershield.* aliases are auto-registered by the ServiceProvider
];

// routes/api.php
use CyberShield\Http\Middleware\FirewallMiddleware;

Route::middleware([
    'cybershield.block_blacklisted_ip',       // Instant drop for known-bad IPs
    'cybershield.detect_tor_network',         // Block anonymized attackers
    'cybershield.verify_api_key',             // Validate X-API-KEY header
    'cybershield.verify_api_signature',       // HMAC-SHA256 request integrity
    'cybershield.verify_api_nonce',           // Prevent replay attacks
    'cybershield.verify_api_timestamp',       // Reject requests older than 60s
    'cybershield.detect_sql_injection',       // WAF: SQLi detection
    'cybershield.api_rate_limiter',           // Adaptive throttling
    'cybershield.log_security_event',         // Forensic audit trail
])->group(function () {
    Route::post('/api/v1/transactions', [TransactionController::class, 'store']);
});

// app/Http/Controllers/TransactionController.php
class TransactionController extends Controller
{
    public function store(Request $request): JsonResponse
    {
        // Check threat score before processing
        if (is_high_risk()) {
            block_current_ip('High risk score on financial endpoint');
            return response()->json(['error' => 'Access denied.'], 403);
        }

        // Validate payload is not malicious
        $rawPayload = $request->getContent();
        if (is_malicious_payload($rawPayload)) {
            log_threat_event('malicious_payload', ['endpoint' => 'transactions']);
            return response()->json(['error' => 'Invalid payload.'], 422);
        }

        // Verify HMAC signature from client
        $signature = $request->header('X-Signature');
        $secret = config('services.payment_gateway.secret');
        if (!verify_api_signature($rawPayload, $signature, $secret)) {
            return response()->json(['error' => 'Signature mismatch.'], 401);
        }

        // Mask PII in logs
        $logData = [
            'account'  => mask_card($request->input('card_number')),
            'email'    => mask_email($request->input('email')),
            'ip'       => mask_ip(),
        ];
        Log::info('Transaction processed', $logData);

        // Process the transaction...
        return response()->json(['status' => 'success']);
    }
}

// Example: Generating a signed API request (client SDK)
$payload    = json_encode(['amount' => 100, 'to' => 'ACC-9876']);
$nonce      = bin2hex(random_bytes(16));
$timestamp  = time();
$secret     = env('API_SECRET');

// Canonical string: METHOD + URL + PAYLOAD + TIMESTAMP + NONCE
$canonical  = 'POST' . '/api/v1/transactions' . $payload . $timestamp . $nonce;
$signature  = hash_hmac('sha256', $canonical, $secret);

Http::withHeaders([
    'X-API-KEY'   => env('API_KEY'),
    'X-Signature' => $signature,
    'X-Nonce'     => $nonce,
    'X-Timestamp' => $timestamp,
    'Content-Type'=> 'application/json',
])->post('https://yourapp.com/api/v1/transactions', json_decode($payload, true));
bash
# Publish config file, migrations, and views
php artisan vendor:publish --provider="CyberShield\CyberShieldServiceProvider"

# Or publish selectively:
php artisan vendor:publish --tag=cybershield-config
php artisan vendor:publish --tag=cybershield-migrations
php artisan vendor:publish --tag=cybershield-views
bash
php artisan migrate
bash
php artisan security:base init