This repository has been archived by the owner on Sep 10, 2021. It is now read-only.
/
AuthorizeController.php
184 lines (168 loc) · 6.13 KB
/
AuthorizeController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/*=========================================================================
MIDAS Server
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
All rights reserved.
More information http://www.kitware.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
/** Handles grant authorization requests from the user agent */
class Oauth_AuthorizeController extends Oauth_AppController
{
public $_models = array('User');
public $_moduleModels = array('Client', 'Code');
/**
* Renders the oauth login screen. See http://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-4.1.1
* @param response_type Should be set to "code".
* @param client_id The identifier of the client
* @param redirect_uri The redirect URI to redirect the end user to upon successful login
* @param [scope] JSON-encoded array of scope constants (see api module constants). Defaults to ALL if not set
* @param [state] Opaque value that will be passed back when redirecting user on success
*/
function indexAction()
{
if(!$_SERVER['HTTPS'])
{
$this->_redirect('https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
return;
}
$this->disableLayout();
$responseType = $this->_getParam('response_type');
$redirectUri = $this->_getParam('redirect_uri');
$scope = $this->_getParam('scope');
$clientId = $this->_getParam('client_id');
if(!isset($clientId))
{
throw new Zend_Exception('Must pass a client_id parameter', 400);
}
if(!isset($redirectUri))
{
throw new Zend_Exception('Must pass a redirect_uri parameter', 400);
}
if(!isset($scope))
{
$scope = JsonComponent::encode(array(MIDAS_API_PERMISSION_SCOPE_ALL));
}
if(!isset($responseType) || $responseType !== 'code')
{
throw new Zend_Exception('Only the "code" response type is supported currently', 400);
}
$client = $this->Oauth_Client->load($clientId);
if(!$client)
{
throw new Zend_Exception('Invalid clientId', 400);
}
$scopeRegistry = Zend_Registry::get('permissionScopeMap');
$scopeArray = JsonComponent::decode($scope);
$scopeStrings = array();
foreach($scopeArray as $scopeEntry)
{
$scopeStrings[] = $scopeRegistry[$scopeEntry];
}
$this->view->scopeStrings = $scopeStrings;
$this->view->state = $this->_getParam('state');
$this->view->scope = $scope;
$this->view->redirectUri = $redirectUri;
$this->view->client = $client;
}
/**
* Submit login form. Will redirect the user to the redirect_uri on success
* @param redirect_uri The client's desired redirect URI
* @param login The user's login
* @param password The user's password
* @param allowOrDeny Whether to allow or deny the request. Set to 'Allow' to allow.
* @param [state] Opaque state pointer string to be passed back to the client appended to the redirect URI
*/
function submitAction()
{
$this->disableLayout();
$this->disableView();
$redirectUri = $this->_getParam('redirect_uri');
$scope = $this->_getParam('scope');
$clientId = $this->_getParam('client_id');
$state = $this->_getParam('state');
$login = $this->_getParam('login');
$password = $this->_getParam('password');
$allow = $this->_getParam('allowOrDeny');
if(!isset($clientId))
{
throw new Zend_Exception('Must pass a client_id parameter', 400);
}
if(!isset($login))
{
throw new Zend_Exception('Must pass a login parameter', 400);
}
if(!isset($password))
{
throw new Zend_Exception('Must pass a password parameter', 400);
}
if(!isset($redirectUri))
{
throw new Zend_Exception('Must pass a redirect_uri parameter', 400);
}
if(!isset($scope))
{
$scope = JsonComponent::encode(array(MIDAS_API_PERMISSION_SCOPE_ALL));
}
if($allow !== 'Allow')
{
$url = $redirectUri;
$url .= strpos($redirectUri, '?') === false ? '?' : '&';
$url .= 'error=access_denied';
if($state)
{
$url .= '&state='.$state;
}
echo JsonComponent::encode(array('status' => 'ok', 'redirect' => $url));
return;
}
$client = $this->Oauth_Client->load($clientId);
if(!$client)
{
$url = $redirectUri;
$url .= strpos($redirectUri, '?') === false ? '?' : '&';
$url .= 'error=invalid_request&error_description='.urlencode('Invalid client_id');
if($state)
{
$url .= '&state='.$state;
}
echo JsonComponent::encode(array('status' => 'ok', 'redirect' => $url));
return;
}
$userDao = $this->User->getByEmail($login);
if($userDao === false)
{
echo JsonComponent::encode(array('status' => 'error', 'message' => 'Invalid username or password'));
return;
}
$instanceSalt = Zend_Registry::get('configGlobal')->password->prefix;
$passwordHash = hash($userDao->getHashAlg(), $instanceSalt.$userDao->getSalt().$password);
if($this->User->hashExists($passwordHash))
{
$codeDao = $this->Oauth_Code->create($userDao, $client, JsonComponent::decode($scope));
$url = $redirectUri;
$url .= strpos($redirectUri, '?') === false ? '?' : '&';
$url .= 'code='.$codeDao->getCode();
if($state)
{
$url .= '&state='.$state;
}
echo JsonComponent::encode(array('status' => 'ok', 'redirect' => $url));
return;
}
else
{
echo JsonComponent::encode(array('status' => 'error', 'message' => 'Invalid username or password'));
return;
}
}
} // end class
?>