sunkan/aws-auth-policy

Library to help with generating auth policies for AWS Gateway API Authorizers

1.0.0 2024-02-12 11:53 UTC

This package is auto-updated.

Last update: 2024-11-12 13:34:21 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status

Installation

$ composer require sunkan/aws-auth-policy

Usage

use Sunkan\AwsAuthPolicy\AuthPolicy;

$policy = new AuthPolicy(
    'me',
    '50505050',
    [
        'region' => 'eu-west-1',
        'stage' => 'prod',
    ],
);

$policy->allowAll();

echo json_encode($policy->build());

Usage with Bref

use Bref\Context\Context;
use Bref\Event\Handler;
use Sunkan\AwsAuthPolicy\AuthPolicy;

final class AuthorizerAction implements Handler
{
    public function handle($event, Context $context)
    {
        $policy = AuthPolicy::fromMethodArn($event['methodArn']);
        // validate $event['authorizationToken']
        if ($validToken) {
            $policy->allowAll();
        }
        else {
            $policy->denyAll();
        }

        return $policy;
    }
}