OwsRequest
==========

This PHP library is designed to construct requests that are sent between Orchard microservices. This library is compliant with the following Orchard standards:

Requirements
------------

* PHP 7.4+
* [Security Requirements](
    https://docs.google.com/document/d/1eHoI_BddTFMi15yCaHS6KvhSSoTrMEd3WwJINIpgNpM/)
* [Logging Requirements](
    https://docs.google.com/document/d/1rafa6dzWNcrrUengXvAEbC210i85ItzyUBoiw5vw7PY/)
* [Service Discovery](
    https://docs.google.com/a/theorchard.com/document/d/1gkrtBTOf8CvGp_czbquK8kdftbzw-c-FbYwd8TWAg1Q/)

Installation
------------
```bash
composer require orchard/owsrequest
```

Setup
-----
Ensure composer.phar is installed and available on your path.

```bash
git clone git@github.com:theorchard/php-owsrequest.git php-owsrequest
cd php-owsrequest/
composer install
```

Testing
-------
Ensure ant is installed and available on your path.

```bash
cd build/
ant phpunit
```

or

```bash
vendor/bin/phpunit tests/
```

Linting
-------
Ensure ant is installed and available on your path.

```bash
cd build
ant phpcs
```

or

```bash
vendor/bin/phpcs -p --standard=PSR2 src/
vendor/bin/phpcs -p --standard=PSR2 tests/
```

Dedicated M2M JWT Support
-------------------------
This version adds support for dedicated machine-to-machine (M2M) JWT tokens, enabling services to authenticate securely without relying on user context.

Local Environment Setup
-----------------------
Before using M2M JWT support, ensure your environment is configured properly:
```
Environment=qa
awsume prod
```
This sets the active environment to QA while assuming the production AWS role for M2M secret retrieval.

Tokens are securely fetched from AWS Secrets Manager using a structured secret name:
`{environment}/{service}/M2M_JWT_ACCESS_TOKEN`
Token responses are cached to minimize unnecessary secret manager calls.
Tokens respect an expiration buffer via configurable leeway (default: 60 seconds).

```
use Orchard\OwsRequest\M2MTokenManager;

// Initialize your secrets manager and cache implementation
$m2mTokenManager = new M2MTokenManager(
    $secretsManager,
    'env',
    'my-service',
    $cache,        // Optional, defaults to InMemoryCache
    $logger        // Optional
);

$tokenString = $m2mTokenManager->getTokenString();
```
🗃️ Pluggable Cache Layer
Caching is now handled via a CacheInterface, allowing flexible integration with your preferred backend.

Available Cache Implementations:

InMemoryCache – Default in-memory store

ApcuCache – Uses PHP’s APCu extension

MemcachedCache – Integrates with Memcached

RedisCache – Integrates with Redis

Example:
```
use Orchard\OwsRequest\Cache\ApcuCache;

$cache = new ApcuCache();

$m2mTokenManager = new M2MTokenManager(
    $secretsManager,
    'env',
    'service-name',
    $cache
);
```

🛠️ CacheInterface
You can create a custom cache implementation by implementing the CacheInterface

Releasing Versions
------------------

To release a new version of Owsrequest library, create an atomic PR, consisting of one change to the composer.json file:

```json
"version": "NEW_VERSION_HERE"
```

Then, run a comparison between the last stable tag to master using this URL - `https://github.com/theorchard/php-owsrequest/compare/{source_tag}...master`. All you need to change is the `{source_tag}`.

Please copy all commit messages from the comparison result and convert that into the release notes for the new version. Please use bullet points for each note.

Once you have the release notes, put them in the description field of the PR and send it for review. Upon approval and merge, create a new tag using Github.
