FormEncodedBodyParameter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A support() 0 3 1
A authenticate() 0 7 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Alexandre
5
 * Date: 27/05/2018
6
 * Time: 18:13
7
 */
8
9
namespace OAuth2\Roles\ResourceServer\BearerAuthenticationMethods;
10
11
use Psr\Http\Message\ServerRequestInterface;
12
13
/**
14
 * Class FormEncodedBodyParameter
15
 * @package OAuth2\Roles\ResourceServer\BearerAuthenticationMethods
16
 *
17
 * @see https://tools.ietf.org/html/rfc6750#section-2.2
18
 * When sending the access token in the HTTP request entity-body, the
19
 * client adds the access token to the request-body using the
20
 * "access_token" parameter.  The client MUST NOT use this method unless
21
 * all of the following conditions are met:
22
 *
23
 * o  The HTTP request entity-header includes the "Content-Type" header
24
 * field set to "application/x-www-form-urlencoded".
25
 *
26
 * o  The entity-body follows the encoding requirements of the
27
 * "application/x-www-form-urlencoded" content-type as defined by
28
 * HTML 4.01 [W3C.REC-html401-19991224].
29
 *
30
 * o  The HTTP request entity-body is single-part.
31
 *
32
 * o  The content to be encoded in the entity-body MUST consist entirely
33
 * of ASCII [USASCII] characters.
34
 *
35
 * o  The HTTP request method is one for which the request-body has
36
 * defined semantics.  In particular, this means that the "GET"
37
 * method MUST NOT be used.
38
 *
39
 * The entity-body MAY include other request-specific parameters, in
40
 * which case the "access_token" parameter MUST be properly separated
41
 * from the request-specific parameters using "&" character(s) (ASCII
42
 * code 38).
43
 *
44
 * For example, the client makes the following HTTP request using
45
 * transport-layer security:
46
 *
47
 * POST /resource HTTP/1.1
48
 * Host: server.example.com
49
 * Content-Type: application/x-www-form-urlencoded
50
 *
51
 * access_token=mF_9.B5f-4.1JqM
52
 *
53
 * The "application/x-www-form-urlencoded" method SHOULD NOT be used
54
 * except in application contexts where participating browsers do not
55
 * have access to the "Authorization" request header field.  Resource
56
 * servers MAY support this method.
57
 */
58
class FormEncodedBodyParameter implements BearerAuthenticationMethodInterface
59
{
60
    public function support(ServerRequestInterface $request): bool
61
    {
62
        return isset($request->getParsedBody()['access_token']);
63
    }
64
65
    public function authenticate(ServerRequestInterface $request): ?string
66
    {
67
        $contentType = $request->getHeader('Content-Type')[0] ?? null;
68
        if ($contentType == 'application/x-www-form-urlencoded') {
69
            return $request->getParsedBody()['access_token'];
70
        }
71
        return null;
72
    }
73
}