Completed
Push — master ( 6e52f0...d9a404 )
by Alexandre
02:29
created

AccessTokenRequest::createFromServerRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 7
cp 0
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Alexandre
5
 * Date: 31/12/2017
6
 * Time: 16:45
7
 */
8
9
namespace OAuth2OLD\Endpoint\Server\Messages\AccessToken;
10
use Psr\Http\Message\ServerRequestInterface;
11
12
13
/**
14
 * Class AccessTokenRequest
15
 * @package OAuth2\Endpoints\Server\Messages\AccessToken
16
 *
17
 * @see https://tools.ietf.org/html/rfc6749#section-4.1.3
18
 *
19
 * The client makes a request to the token endpoint by sending the
20
 * following parameters using the "application/x-www-form-urlencoded"
21
 * format per Appendix B with a character encoding of UTF-8 in the HTTP
22
 * request entity-body:
23
 *
24
 * grant_type
25
 * REQUIRED.  Value MUST be set to "authorization_code".
26
 *
27
 * code
28
 * REQUIRED.  The authorization code received from the
29
 * authorization server.
30
 *
31
 * redirect_uri
32
 * REQUIRED, if the "redirect_uri" parameter was included in the
33
 * authorization request as described in Section 4.1.1, and their
34
 * values MUST be identical.
35
 *
36
 * client_id
37
 * REQUIRED, if the client is not authenticating with the
38
 * authorization server as described in Section 3.2.1.
39
 *
40
 * If the client type is confidential or the client was issued client
41
 * credentials (or assigned other authentication requirements), the
42
 * client MUST authenticate with the authorization server as described
43
 * in Section 3.2.1.
44
 *
45
 * For example, the client makes the following HTTP request using TLS
46
 * (with extra line breaks for display purposes only):
47
 *
48
 * POST /token HTTP/1.1
49
 * Host: server.example.com
50
 * Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
51
 * Content-Type: application/x-www-form-urlencoded
52
 *
53
 * grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA
54
 * &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb
55
 *
56
 * The authorization server MUST:
57
 *
58
 * o  require client authentication for confidential clients or for any
59
 * client that was issued client credentials (or with other
60
 * authentication requirements),
61
 *
62
 * o  authenticate the client if client authentication is included,
63
 *
64
 * o  ensure that the authorization code was issued to the authenticated
65
 * confidential client, or if the client is public, ensure that the
66
 * code was issued to "client_id" in the request,
67
 *
68
 * o  verify that the authorization code is valid, and
69
 *
70
 * o  ensure that the "redirect_uri" parameter is present if the
71
 * "redirect_uri" parameter was included in the initial authorization
72
 * request as described in Section 4.1.1, and if included ensure that
73
 * their values are identical.
74
 */
75
class AccessTokenRequest
76
{
77
    private $grantType;
78
    private $code;
79
    private $redirectUri;
80
    private $clientId;
81
82
    public function __construct($grantType, $code, $redirectUri, $clientId = null)
83
    {
84
        $this->grantType = $grantType;
85
        $this->code = $code;
86
        $this->redirectUri = $redirectUri;
87
        $this->clientId = $clientId;
88
    }
89
90
    static public function createFromServerRequest(ServerRequestInterface $request)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
91
    {
92
        $grantType = $request->getParsedBody()['grant_type'] ?? null;
93
        $code = $request->getParsedBody()['code'] ?? null;
94
        $redirectUri = $request->getParsedBody()['redirect_uri'] ?? null;
95
        $clientId = $request->getParsedBody()['client_id'] ?? null;
96
97
        return new self($grantType, $code, $redirectUri, $clientId);
98
    }
99
100
    /**
101
     * @throws \Exception
102
     */
103
    public function validate()
104
    {
105
        if (!$this->clientId) {
106
            throw new \Exception('Missing client_id parameter');
107
        }
108
        return true;
109
    }
110
111
    /**
112
     * @return mixed
113
     */
114
    public function getGrantType()
115
    {
116
        return $this->grantType;
117
    }
118
119
    /**
120
     * @return mixed
121
     */
122
    public function getCode()
123
    {
124
        return $this->code;
125
    }
126
127
    /**
128
     * @return mixed
129
     */
130
    public function getRedirectUri()
131
    {
132
        return $this->redirectUri;
133
    }
134
135
    /**
136
     * @return mixed
137
     */
138
    public function getClientId()
139
    {
140
        return $this->clientId;
141
    }
142
}