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

AccessTokenResponse::__construct()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 8
nop 6
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
ccs 0
cts 24
cp 0
crap 20
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
11
12
use GuzzleHttp\Psr7\Response;
13
use Psr\Http\Message\UriInterface;
14
15
/**
16
 * Class AccessTokenResponse
17
 * @package OAuth2\Endpoints\Server\Messages\AccessToken
18
 *
19
 * @see https://tools.ietf.org/html/rfc6749#section-5.1
20
 *
21
 * Successful Response
22
 *
23
 *     The authorization server issues an access token and optional refresh
24
 * token, and constructs the response by adding the following parameters
25
 * to the entity-body of the HTTP response with a 200 (OK) status code:
26
 *
27
 * access_token
28
 * REQUIRED.  The access token issued by the authorization server.
29
 *
30
 * token_type
31
 * REQUIRED.  The type of the token issued as described in
32
 * Section 7.1.  Value is case insensitive.
33
 *
34
 * expires_in
35
 * RECOMMENDED.  The lifetime in seconds of the access token.  For
36
 * example, the value "3600" denotes that the access token will
37
 * expire in one hour from the time the response was generated.
38
 * If omitted, the authorization server SHOULD provide the
39
 * expiration time via other means or document the default value.
40
 *
41
 * refresh_token
42
 * OPTIONAL.  The refresh token, which can be used to obtain new
43
 * access tokens using the same authorization grant as described
44
 * in Section 6.
45
 *
46
 * scope
47
 * OPTIONAL, if identical to the scope requested by the client;
48
 * otherwise, REQUIRED.  The scope of the access token as
49
 * described by Section 3.3.
50
 *
51
 * The parameters are included in the entity-body of the HTTP response
52
 * using the "application/json" media type as defined by [RFC4627].  The
53
 * parameters are serialized into a JavaScript Object Notation (JSON)
54
 * structure by adding each parameter at the highest structure level.
55
 * Parameter names and string values are included as JSON strings.
56
 * Numerical values are included as JSON numbers.  The order of
57
 * parameters does not matter and can vary.
58
 *
59
 * The authorization server MUST include the HTTP "Cache-Control"
60
 * response header field [RFC2616] with a value of "no-store" in any
61
 * response containing tokens, credentials, or other sensitive
62
 * information, as well as the "Pragma" response header field [RFC2616]
63
 * with a value of "no-cache".
64
 *
65
 * For example:
66
 *
67
 * HTTP/1.1 200 OK
68
 * Content-Type: application/json;charset=UTF-8
69
 * Cache-Control: no-store
70
 * Pragma: no-cache
71
 *
72
 * {
73
 * "access_token":"2YotnFZFEjr1zCsicMWpAA",
74
 * "token_type":"example",
75
 * "expires_in":3600,
76
 * "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA",
77
 * "example_parameter":"example_value"
78
 * }
79
 *
80
 * The client MUST ignore unrecognized value names in the response.  The
81
 * sizes of tokens and other values received from the authorization
82
 * server are left undefined.  The client should avoid making
83
 * assumptions about value sizes.  The authorization server SHOULD
84
 * document the size of any value it issues.
85
 */
86
class AccessTokenResponse extends Response
87
{
88
    /**
89
     * @var string
90
     */
91
    private $redirectUri;
92
    /**
93
     * @var string
94
     */
95
    private $accessToken;
96
    /**
97
     * @var string
98
     */
99
    private $tokenType;
100
    /**
101
     * @var int|null
102
     */
103
    private $expiresIn;
104
    /**
105
     * @var null|string
106
     */
107
    private $refreshToken;
108
    /**
109
     * @var null|string
110
     */
111
    private $scope;
112
113
    /**
114
     * Response constructor.
115
     * @param UriInterface $redirectUri
116
     * @param string $accessToken
117
     * @param string $tokenType
118
     * @param int|null $expiresIn
119
     * @param null|string $refreshToken
120
     * @param null|string $scope
121
     */
122
    public function __construct(UriInterface $redirectUri, string $accessToken, string $tokenType,
123
                                ?int $expiresIn = 3600, ?string $refreshToken = null, ?string $scope = null)
124
    {
125
        $redirectUri = $redirectUri->__toString();
126
        $body = [
127
            'access_token' => $accessToken,
128
            'token_type' => $tokenType,
129
        ];
130
        if (isset($expires_in)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $expires_in does not exist. Did you maybe mean $expiresIn?
Loading history...
131
            $body['expires_in'] = $expiresIn;
132
        }
133
        if (isset($refreshToken)) {
134
            $body['refresh_token'] = $refreshToken;
135
        }
136
        if (isset($scope)) {
137
            $body['scope'] = $scope;
138
        }
139
        parent::__construct(200, ['Location' => $redirectUri], json_encode($body));
140
        $this->redirectUri = $redirectUri;
141
        $this->accessToken = $accessToken;
142
        $this->tokenType = $tokenType;
143
        $this->expiresIn = $expiresIn;
144
        $this->refreshToken = $refreshToken;
145
        $this->scope = $scope;
146
    }
147
}