|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Alexandre |
|
5
|
|
|
* Date: 18/02/2018 |
|
6
|
|
|
* Time: 18:08 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace OAuth2\AuthorizationGrantTypes\Flows; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use OAuth2\Endpoints\Authorization\AuthorizationRequest; |
|
13
|
|
|
use OAuth2\Endpoints\Authorization\AuthorizationRequestInterface; |
|
14
|
|
|
use OAuth2\Endpoints\TokenEndpoint; |
|
15
|
|
|
use OAuth2\AuthorizationGrantTypes\AbstractGrantType; |
|
16
|
|
|
use OAuth2\Helper; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class ImplicitFlow |
|
21
|
|
|
* @package OAuth2\AuthorizationGrantTypes\Flows |
|
22
|
|
|
* |
|
23
|
|
|
* @see https://tools.ietf.org/html/rfc6749#section-1.3.2 |
|
24
|
|
|
* The implicit grant is a simplified authorization code flow optimized |
|
25
|
|
|
* for clients implemented in a browser using a scripting language such |
|
26
|
|
|
* as JavaScript. In the implicit flow, instead of issuing the client |
|
27
|
|
|
* an authorization code, the client is issued an access token directly |
|
28
|
|
|
* (as the result of the resource owner authorization). The grant type |
|
29
|
|
|
* is implicit, as no intermediate credentials (such as an authorization |
|
30
|
|
|
* code) are issued (and later used to obtain an access token). |
|
31
|
|
|
* |
|
32
|
|
|
* When issuing an access token during the implicit grant flow, the |
|
33
|
|
|
* authorization server does not authenticate the client. In some |
|
34
|
|
|
* cases, the client identity can be verified via the redirection URI |
|
35
|
|
|
* used to deliver the access token to the client. The access token may |
|
36
|
|
|
* be exposed to the resource owner or other applications with access to |
|
37
|
|
|
* the resource owner's user-agent. |
|
38
|
|
|
* |
|
39
|
|
|
* Implicit grants improve the responsiveness and efficiency of some |
|
40
|
|
|
* clients (such as a client implemented as an in-browser application), |
|
41
|
|
|
* since it reduces the number of round trips required to obtain an |
|
42
|
|
|
* access token. However, this convenience should be weighed against |
|
43
|
|
|
* the security implications of using implicit grants, such as those |
|
44
|
|
|
* described in Sections 10.3 and 10.16, especially when the |
|
45
|
|
|
* authorization code grant type is available. |
|
46
|
|
|
* |
|
47
|
|
|
* @see https://tools.ietf.org/html/rfc6749#section-4.2 |
|
48
|
|
|
* The implicit grant type is used to obtain access tokens (it does not |
|
49
|
|
|
* support the issuance of refresh tokens) and is optimized for public |
|
50
|
|
|
* clients known to operate a particular redirection URI. These clients |
|
51
|
|
|
* are typically implemented in a browser using a scripting language |
|
52
|
|
|
* such as JavaScript. |
|
53
|
|
|
* |
|
54
|
|
|
* Since this is a redirection-based flow, the client must be capable of |
|
55
|
|
|
* interacting with the resource owner's user-agent (typically a web |
|
56
|
|
|
* browser) and capable of receiving incoming requests (via redirection) |
|
57
|
|
|
* from the authorization server. |
|
58
|
|
|
* |
|
59
|
|
|
* Unlike the authorization code grant type, in which the client makes |
|
60
|
|
|
* separate requests for authorization and for an access token, the |
|
61
|
|
|
* client receives the access token as the result of the authorization |
|
62
|
|
|
* request. |
|
63
|
|
|
* |
|
64
|
|
|
* The implicit grant type does not include client authentication, and |
|
65
|
|
|
* relies on the presence of the resource owner and the registration of |
|
66
|
|
|
* the redirection URI. Because the access token is encoded into the |
|
67
|
|
|
* redirection URI, it may be exposed to the resource owner and other |
|
68
|
|
|
* applications residing on the same device. |
|
69
|
|
|
* |
|
70
|
|
|
* See Sections 1.3.2 and 9 for background on using the implicit grant. |
|
71
|
|
|
* See Sections 10.3 and 10.16 for important security considerations |
|
72
|
|
|
* when using the implicit grant. |
|
73
|
|
|
*/ |
|
74
|
|
|
class ImplicitFlow extends AbstractGrantType implements FlowInterface |
|
75
|
|
|
{ |
|
76
|
|
|
/** |
|
77
|
|
|
* @return array |
|
78
|
|
|
* |
|
79
|
|
|
* @see https://tools.ietf.org/html/rfc6749#section-4.2.1 |
|
80
|
|
|
* response_type |
|
81
|
|
|
* REQUIRED. Value MUST be set to "token". |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getResponseTypes(): array |
|
84
|
|
|
{ |
|
85
|
|
|
return ['token']; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param AuthorizationRequest $authorizationRequest |
|
90
|
|
|
* @return array |
|
91
|
|
|
* |
|
92
|
|
|
* @see https://tools.ietf.org/html/rfc6749#section-4.2.2 |
|
93
|
|
|
* If the resource owner grants the access request, the authorization |
|
94
|
|
|
* server issues an access token and delivers it to the client by adding |
|
95
|
|
|
* the following parameters to the fragment component of the redirection |
|
96
|
|
|
* URI using the "application/x-www-form-urlencoded" format, per |
|
97
|
|
|
* Appendix B: |
|
98
|
|
|
* |
|
99
|
|
|
* access_token |
|
100
|
|
|
* REQUIRED. The access token issued by the authorization server. |
|
101
|
|
|
* |
|
102
|
|
|
* token_type |
|
103
|
|
|
* REQUIRED. The type of the token issued as described in |
|
104
|
|
|
* Section 7.1. Value is case insensitive. |
|
105
|
|
|
* |
|
106
|
|
|
* expires_in |
|
107
|
|
|
* RECOMMENDED. The lifetime in seconds of the access token. For |
|
108
|
|
|
* example, the value "3600" denotes that the access token will |
|
109
|
|
|
* expire in one hour from the time the response was generated. |
|
110
|
|
|
* If omitted, the authorization server SHOULD provide the |
|
111
|
|
|
* expiration time via other means or document the default value. |
|
112
|
|
|
* |
|
113
|
|
|
* scope |
|
114
|
|
|
* OPTIONAL, if identical to the scope requested by the client; |
|
115
|
|
|
* otherwise, REQUIRED. The scope of the access token as |
|
116
|
|
|
* described by Section 3.3. |
|
117
|
|
|
* |
|
118
|
|
|
* state |
|
119
|
|
|
* REQUIRED if the "state" parameter was present in the client |
|
120
|
|
|
* authorization request. The exact value received from the |
|
121
|
|
|
* client. |
|
122
|
|
|
* |
|
123
|
|
|
* The authorization server MUST NOT issue a refresh token. |
|
124
|
|
|
* |
|
125
|
|
|
* For example, the authorization server redirects the user-agent by |
|
126
|
|
|
* sending the following HTTP response (with extra line breaks for |
|
127
|
|
|
* display purposes only): |
|
128
|
|
|
* |
|
129
|
|
|
* HTTP/1.1 302 Found |
|
130
|
|
|
* Location: http://example.com/cb#access_token=2YotnFZFEjr1zCsicMWpAA |
|
131
|
|
|
* &state=xyz&token_type=example&expires_in=3600 |
|
132
|
|
|
* |
|
133
|
|
|
* Developers should note that some user-agents do not support the |
|
134
|
|
|
* inclusion of a fragment component in the HTTP "Location" response |
|
135
|
|
|
* header field. Such clients will require using other methods for |
|
136
|
|
|
* redirecting the client than a 3xx redirection response -- for |
|
137
|
|
|
* example, returning an HTML page that includes a 'continue' button |
|
138
|
|
|
* with an action linked to the redirection URI. |
|
139
|
|
|
* |
|
140
|
|
|
* The client MUST ignore unrecognized response parameters. The access |
|
141
|
|
|
* token string size is left undefined by this specification. The |
|
142
|
|
|
* client should avoid making assumptions about value sizes. The |
|
143
|
|
|
* authorization server SHOULD document the size of any value it issues. |
|
144
|
|
|
*/ |
|
145
|
|
|
public function handleAuthorizationRequest(AuthorizationRequestInterface $authorizationRequest): array |
|
146
|
|
|
{ |
|
147
|
|
|
$data = $this->issueAccessToken( |
|
148
|
|
|
$authorizationRequest->getScopes(), |
|
149
|
|
|
$authorizationRequest->getClient()->getIdentifier(), |
|
150
|
|
|
$authorizationRequest->getResourceOwner()->getIdentifier() |
|
151
|
|
|
); |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @see https://tools.ietf.org/html/rfc6749#section-3.3 |
|
155
|
|
|
* The authorization and token endpoints allow the client to specify the |
|
156
|
|
|
* scope of the access request using the "scope" request parameter. In |
|
157
|
|
|
* turn, the authorization server uses the "scope" response parameter to |
|
158
|
|
|
* inform the client of the scope of the access token issued. |
|
159
|
|
|
*/ |
|
160
|
|
|
if (!Helper::array_equals($authorizationRequest->getScopes(), $authorizationRequest->getRequestedScopes())) { |
|
161
|
|
|
$data['scope'] = implode(' ', $authorizationRequest->getScopes()); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return $data; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function getDefaultResponseMode(): string |
|
168
|
|
|
{ |
|
169
|
|
|
return 'fragment'; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function getUnsupportedResponseModes(): array |
|
173
|
|
|
{ |
|
174
|
|
|
return ['query']; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function isRegistrationOfRedirectUriRequired(): bool |
|
178
|
|
|
{ |
|
179
|
|
|
return true; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function getGrantTypes(): array |
|
183
|
|
|
{ |
|
184
|
|
|
return []; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function handleAccessTokenRequest(TokenEndpoint $tokenEndpoint, array $requestData): array |
|
188
|
|
|
{ |
|
189
|
|
|
throw new \BadMethodCallException(); |
|
190
|
|
|
} |
|
191
|
|
|
} |