district5/php-postman-detect

Postman detector is a PHP library that detects if a request has been made by Postman.

0.0.1 2023-07-05 20:01 UTC

This package is auto-updated.

Last update: 2024-05-06 10:27:26 UTC


README

Prevent or restrict access to certain API endpoints when using Postman for mock requests.

Usage...

"repositories":[
    {
        "type": "vcs",
        "url": "git@github.com:district-5/php-postman-detect.git"
    }
],

"require": {
    "district5/php-postman-detect": ">=0.0.1"
}

Code usage...

<?php

$_SERVER['HTTP_USER_AGENT'] = 'PostmanRuntime/7.26.8';
\District5\PostmanDetect\PostmanDetector::isPostman(); // true

$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko)';
\District5\PostmanDetect\PostmanDetector::isPostman(); // false


$_SERVER['HTTP_USER_AGENT'] = 'PostmanRuntime/7.26.8';

// Disallow this endpoint with Postman always
\District5\PostmanDetect\PostmanDetector::disallowAlways(); // throws exception

// Current environment is 'local'
// disallow on production
\District5\PostmanDetect\PostmanDetector::disallowOnEnvs('local', ['prod']); // no exception
// disallow on production and staging
\District5\PostmanDetect\PostmanDetector::disallowOnEnvs('local', ['prod', 'staging']); // no exception

// Current environment is 'prod'
// disallow on production
\District5\PostmanDetect\PostmanDetector::disallowOnEnvs('prod', ['prod']); // throws exception
// disallow on production and staging
\District5\PostmanDetect\PostmanDetector::disallowOnEnvs('prod', ['prod', 'staging']); // throws exception

Testing...

composer install
./vendor/bin/phpunit