Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Body Parsing Middleware reference #40

Open
wants to merge 6 commits into
base: 2.2.x
Choose a base branch
from

Conversation

cvigorsICBF
Copy link

@cvigorsICBF cvigorsICBF commented Dec 14, 2021

Mention the requirement for Body Parsing Middleware when parsing application/json requests

Q A
Documentation yes
Bugfix no
BC Break no
New Feature no
RFC no
QA no

Description

When requesting an access token from the authentication server using JSON, the response from the server will always be "error": "unsupported_grant_type" unless the body parsing middleware helper is included in the application. Updating the docs to highlight the requirement for this library.

Fixes #39

Mention the requirement for Body Parsing Middleware when parsing application/json requests
@Ocramius Ocramius added Documentation Enhancement New feature or request labels Dec 14, 2021
@Ocramius Ocramius added this to the 2.2.0 milestone Dec 14, 2021
@Ocramius Ocramius removed this from the 2.2.0 milestone Dec 23, 2021
@cvigorsICBF
Copy link
Author

Header added as requested

docs/book/v1/authorization-server.md Outdated Show resolved Hide resolved
docs/book/v1/authorization-server.md Outdated Show resolved Hide resolved
docs/book/v1/authorization-server.md Show resolved Hide resolved
Copy link
Author

@cvigorsICBF cvigorsICBF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example provided as requested

```php
use Mezzio\Helper\BodyParams\BodyParamsMiddleware;

$app->pipe(BodyParamsMiddleware::class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally speaking, we recommend AGAINST piping the body parsing middleware as generic middleware; instead, we recommend piping it into a route-specific pipeline:

use Mezzio\Authentication\OAuth2;
use Mezzio\Helper\BodyParams\BodyParamsMiddleware;

$app->post('/oauth2/token', [
    BodyParamsMiddleware::class,
    OAuth2\TokenEndpointHandler::class
], 'auth.token');

This ensures we only parse it when we are actually expecting a content body.

@@ -6,6 +6,7 @@ for your application.
Since there are authorization flows that require user interaction,
**your application is expected to provide the middleware to handle this**.


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be reverted; it adds an extra line between content, which our own linter will reject.

Comment on lines 26 to 43
For example:

```php
use Mezzio\Helper\BodyParams\BodyParamsMiddleware;

$app->pipe(BodyParamsMiddleware::class);
```
Generally speaking, we recommend AGAINST piping the body parsing middleware as generic middleware; instead, we recommend piping it into a route-specific pipeline:
```php
use Mezzio\Authentication\OAuth2;
use Mezzio\Helper\BodyParams\BodyParamsMiddleware;

$app->post('/oauth2/token', [
BodyParamsMiddleware::class,
OAuth2\TokenEndpointHandler::class
], 'auth.token');
```
This ensures we only parse it when we are actually expecting a content body.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The correct way of usage must be shown, not the wrong one.

Suggested change
For example:
```php
use Mezzio\Helper\BodyParams\BodyParamsMiddleware;
$app->pipe(BodyParamsMiddleware::class);
```
Generally speaking, we recommend AGAINST piping the body parsing middleware as generic middleware; instead, we recommend piping it into a route-specific pipeline:
```php
use Mezzio\Authentication\OAuth2;
use Mezzio\Helper\BodyParams\BodyParamsMiddleware;
$app->post('/oauth2/token', [
BodyParamsMiddleware::class,
OAuth2\TokenEndpointHandler::class
], 'auth.token');
```
This ensures we only parse it when we are actually expecting a content body.
For example:
```php
use Mezzio\Authentication\OAuth2;
use Mezzio\Helper\BodyParams\BodyParamsMiddleware;
$app->post('/oauth2/token', [
BodyParamsMiddleware::class,
OAuth2\TokenEndpointHandler::class
], 'auth.token');
```
WARNING: Do not piping the body parsing middleware as generic middleware.
This ensures that the content body is only parsed when it is actually expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Body Parsing Middleware Requirement
4 participants