|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015 François Kooman <[email protected]>. |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace fkooman\RemoteStorage\OAuth; |
|
20
|
|
|
|
|
21
|
|
|
use fkooman\Http\Exception\BadRequestException; |
|
22
|
|
|
use fkooman\Http\Request; |
|
23
|
|
|
|
|
24
|
|
|
class RequestValidation |
|
25
|
|
|
{ |
|
26
|
|
|
public static function validateAuthorizeRequest(Request $request, $requireState = true) |
|
27
|
|
|
{ |
|
28
|
|
|
// REQUIRED client_id |
|
29
|
|
|
$clientId = self::validateParameter( |
|
30
|
|
|
$request->getUrl()->getQueryParameter('client_id'), |
|
31
|
|
|
'clientId' |
|
32
|
|
|
); |
|
33
|
|
|
// REQUIRED response_type |
|
34
|
|
|
$responseType = self::validateParameter( |
|
35
|
|
|
$request->getUrl()->getQueryParameter('response_type'), |
|
36
|
|
|
'responseType' |
|
37
|
|
|
); |
|
38
|
|
|
// REQUIRED redirect_uri |
|
39
|
|
|
$redirectUri = self::validateParameter( |
|
40
|
|
|
$request->getUrl()->getQueryParameter('redirect_uri'), |
|
41
|
|
|
'redirectUri' |
|
42
|
|
|
); |
|
43
|
|
|
// REQUIRED scope |
|
44
|
|
|
$scope = self::validateParameter( |
|
45
|
|
|
$request->getUrl()->getQueryParameter('scope'), |
|
46
|
|
|
'scope' |
|
47
|
|
|
); |
|
48
|
|
|
// REQUIRED state (but allow override with flag) |
|
49
|
|
|
$state = self::validateParameter( |
|
50
|
|
|
$request->getUrl()->getQueryParameter('state'), |
|
51
|
|
|
'state', |
|
52
|
|
|
$requireState |
|
53
|
|
|
); |
|
54
|
|
|
if (is_null($state)) { |
|
55
|
|
|
$state = 'xxx_client_should_set_state_xxx'; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return [ |
|
59
|
|
|
'client_id' => $clientId, |
|
60
|
|
|
'response_type' => $responseType, |
|
61
|
|
|
'redirect_uri' => $redirectUri, |
|
62
|
|
|
'scope' => $scope, |
|
63
|
|
|
'state' => $state, |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public static function validatePostAuthorizeRequest(Request $request, $requireState = true) |
|
68
|
|
|
{ |
|
69
|
|
|
$requestData = self::validateAuthorizeRequest($request, $requireState); |
|
70
|
|
|
|
|
71
|
|
|
// REQUIRED approval |
|
72
|
|
|
$approval = self::validateParameter($request->getPostParameter('approval'), 'approval'); |
|
73
|
|
|
|
|
74
|
|
|
$requestData['approval'] = $approval; |
|
75
|
|
|
|
|
76
|
|
|
return $requestData; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public static function validateTokenRequest(Request $request) |
|
80
|
|
|
{ |
|
81
|
|
|
// REQUIRED grant_type |
|
82
|
|
|
$grantType = self::validateParameter($request->getPostParameter('grant_type'), 'grantType'); |
|
83
|
|
|
// REQUIRED client_id |
|
84
|
|
|
$clientId = self::validateParameter($request->getPostParameter('client_id'), 'clientId'); |
|
85
|
|
|
// REQUIRED code |
|
86
|
|
|
$code = self::validateParameter($request->getPostParameter('code'), 'code'); |
|
87
|
|
|
// REQUIRED redirect_uri |
|
88
|
|
|
$redirectUri = self::validateParameter($request->getPostParameter('redirect_uri'), 'redirectUri'); |
|
89
|
|
|
|
|
90
|
|
|
return [ |
|
91
|
|
|
'grant_type' => $grantType, |
|
92
|
|
|
'client_id' => $clientId, |
|
93
|
|
|
'code' => $code, |
|
94
|
|
|
'redirect_uri' => $redirectUri, |
|
95
|
|
|
]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public static function validateIntrospectRequest(Request $request) |
|
99
|
|
|
{ |
|
100
|
|
|
// REQUIRED token |
|
101
|
|
|
$token = self::validateParameter($request->getPostParameter('token'), 'token'); |
|
102
|
|
|
|
|
103
|
|
|
return [ |
|
104
|
|
|
'token' => $token, |
|
105
|
|
|
]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public static function validateParameter($parameterValue, $validatorMethod, $isRequired = true) |
|
109
|
|
|
{ |
|
110
|
|
|
if (is_null($parameterValue)) { |
|
111
|
|
|
if ($isRequired) { |
|
112
|
|
|
throw new BadRequestException(sprintf('missing %s', $validatorMethod)); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if (false === InputValidation::$validatorMethod($parameterValue)) { |
|
119
|
|
|
throw new BadRequestException(sprintf('invalid %s', $validatorMethod)); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $parameterValue; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|