Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/crtl/slim-auth-middleware

Basic Authorization Middleware Library
https://github.com/crtl/slim-auth-middleware

Last synced: 4 days ago
JSON representation

Basic Authorization Middleware Library

Awesome Lists containing this project

README

        

# Slim Authorization Middleware

This package provides a basic middleware to implement authorization of any type.

## Installation

```
composer require crtl/slim-auth-middleware
```

## Usage

The package already comes with a prebuild implementation for HTTP-Basic Authroziation.

```php
add(
new BasicAuthorization([
BasicAuthorization::CONFIG_ENABLE => true,
BasicAuthorization::CONFIG_USER => "secret",
BasicAuthorization::CONFIG_SECRET => "password"
])
);

$app->get("/", function($request, $response) use ($app) {
$body = $response->getBody();
$body->write("Authorized");

return $response->withBody($body),
});

$app->run();
```

If the request is authorized the app will call the next middleware. Otherwise `Crtl\AuthorizationMiddleware\AbstractAuthorization::getErrorResponse` will be called.

To implement a custom authorization just extend `Crtl\AuthorizationMiddleware\AbstractAuthorization` and implement the

`protected isAuthorized() : bool` method.

```php
response;

/* @var \Psr\Http\Message\RequestInterface $request */
$request = $this->request;

/* check if authorized */

return true;

}

}

```