Repository navigation
Expand file tree
/
Copy pathCrowd-REST.php
More file actions
416 lines (360 loc) · 14.2 KB
/
Copy pathCrowd-REST.php
File metadata and controls
416 lines (360 loc) · 14.2 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
// Copyright 2012 Palantir Technologies
//
// 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
//
// 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.
/**
* REST API as documented here:
* https://developer.atlassian.com/display/CROWDDEV/Crowd+REST+Resources#CrowdRESTResources-UserResource
*/
class CrowdREST {
const CONFIG_SSO_ENABLED = 'CONFIG_SSO_ENABLED';
const CROWD_REST_API_PATH = '/rest/usermanagement/1';
private $crowd_config;
private $cookies = null;
private $base_url = null;
private $crowd_cookie_config = null;
public function CrowdREST($crowd_config) {
$this->crowd_config = $crowd_config;
$this->base_url = $crowd_config['crowd_url'] . self::CROWD_REST_API_PATH;
if(!array_key_exists(self::CONFIG_SSO_ENABLED,$this->crowd_config)) {
$this->crowd_config[self::CONFIG_SSO_ENABLED] = true;
}
}
/**
* Returns an associative array containing:
* - info => metadata array returned by curl_info
* - response => response body
* - headers => response headers (as unparsed text)
*/
private function curlDo($url, $attrs = null, $post_body = false) {
// locallize this variable for terser syntax
$crowd_config = $this->crowd_config;
// build up the full url/query string
$query = "";
if($attrs != null && count($attrs) > 0) {
$query = "?" . http_build_query($attrs);
}
$full_url = $this->base_url . $url . $query;
// get a curl handle
$curl = curl_init($full_url);
// Crowd REST uses HTTP auth for the app name and credential
curl_setopt($curl, CURLOPT_USERPWD, $crowd_config['app_name'] . ':' . $crowd_config['app_credential'] );
// get back the response document
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// set our content-type correctly (we get a 415 with text/xml)
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/xml"));
// only post if we have a body
if($post_body) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_body);
}
// optional supress peer checking to deal with broken SSL configs - may be common with Crowd
if (array_key_exists('verify_ssl_peer',$crowd_config)) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $crowd_config['verify_ssl_peer']);
}
// give us back the response headers (so we can scrape cookies)
curl_setopt($curl, CURLOPT_HEADER, true);
// load our cookies from early requests
$this->curl_load_cookies($curl);
// fire off the request
$response = curl_exec($curl);
// check for curl errors
if (curl_errno($curl)) {
throw new Exception("curl error (${full_url}): " . curl_error($curl));
}
// extract response metadata
$info = curl_getinfo($curl);
// split headers out of the response
$rc = $this->curl_split_headers($response,$info);
// add in the metadata
$rc['metadata'] = $info;
// store cookies for later reuse
$this->curl_store_cookies($rc);
curl_close($curl);
return $rc;
}
private function curl_split_headers($raw_response,$info) {
$header_size = $info['header_size']; // offset to the end of the headers
$headers = substr($raw_response, 0, $header_size);
$response = substr($raw_response, $header_size);
return array('headers' => $headers, 'response' => $response);
}
private function curl_store_cookies($rc) {
$headers = $rc['headers'];
preg_match_all('|Set-Cookie: (.*);|U', $headers, $matches);
$this->cookies = implode('; ', $matches[1]);
}
private function curl_load_cookies($curl) {
if($this->cookies) {
curl_setopt($curl,CURLOPT_COOKIE,$this->cookies);
}
}
/**
* Reads a curl response, tests http return code and logs
* accordingly
*
* returns false on error, true on no error.
*/
private function curl_logerror($rc, $msg_prefix = "curl error") {
$http_response_code = $rc['metadata']['http_code'];
if($http_response_code != 200) {
error_log("${msg_prefix}:\n" . $rc['response']);
return false;
}
return true;
}
/**
* Reads XML response document and checks for errors. Logs errors
* accordingly.
*
* Returns null on error, the XML response on no error.
*/
private function crowd_xml_logerror($rc, $msg_prefix = "error in xml response") {
// got back a valid XML response (hopefully)
$xmlResponse = new SimpleXMLElement($rc['response']);
if($xmlResponse[0]->getName() == "error") {
$reason = (string)$xmlResponse->{reason};
$message = (string)$xmlResponse->{message};
error_log("${msg_prefix}: ${reason} - ${message}");
return null;
}
return $xmlResponse;
}
function isSSOEnabled() {
if($this->crowd_config[self::CONFIG_SSO_ENABLED]) {
return true;
}
return false;
}
/**
* Attempts token auth and then falls back to password
* authentication if credentials are provided
*/
function authenticateUser($username, $password) {
if($this->isSSOEnabled() && empty($username) && empty($password)) {
// SSO uses a different auth mechanism and handles cookies
$authenticated_username = $this->tokenAuth();
if($authenticated_username != null) {
return $authenticated_username; // success!
};
}
if(!empty($username) && !empty($password)) {
if($this->isSSOEnabled()) {
// authenticate and create SSO tokens
return $this->tokenAuthCreateSession($username,$password);
} else {
return $this->simpleAuth($username, $password);
}
}
return false;
}
function simpleAuth($username, $password) {
$xmlBody = $this->generateSimpleAuthXML($password);
$rc = $this->curlDo("/authentication", array("username" => $username),$xmlBody);
// check to make sure we got a 200 and response from the server
if(!$this->curl_logerror($rc,"Error in performing simple authentication:\n")){
return false;
}
// got back a valid XML response (hopefully)
$xmlResponse = $this->crowd_xml_logerror($rc,"Error returned in Crowd XML response");
if($xmlResponse) {
$authenticated_username = $xmlResponse->user['name'];
if($authenticated_username == $username) {
// authentication successful
return $authenticated_username;
} else {
error_log("Got unexpected Crowd XML response to auth query:\n" . $rc['response']);
}
}
return false;
}
function tokenAuth() {
$cookie_config = $this->getCrowdCookieConfig();
if($cookie_config == null) {
// errors have already been logged in this case
return null;
} else {
$cookie_name = $this->getCookieName();
if(array_key_exists($cookie_name, $_COOKIE)) {
$token = $_COOKIE[$cookie_name];
if($token) {
$tokenCheckXML = $this->generateTokenVerificationXML($token);
$rc = $this->curlDo("/session/${token}", null, $tokenCheckXML);
$http_response_code = $rc['metadata']['http_code'];
if ($http_response_code == 200) {
// good to go, token is renewed
// extract username from response
$xmlResponse = new SimpleXMLElement($rc['response']);
$userElement = $xmlResponse->user;
$authenticated_username = (string)$userElement['name'];
// confirm that tokens match (just in case)
$authenticated_token = (string)$xmlResponse->token;
if($authenticated_token != $token) {
error_log("Crowd SSO inconsistency: sent token ${token}, got back ${authenticated_token}: mismatch");
}
return $authenticated_username; // SSO successful
} elseif($http_response_code == 404) {
// bad/expired token
} elseif ($http_response_code == 400) {
// validation factors did not match token - IP address changed
// see https://answers.atlassian.com/questions/65240/how-is-the-remote-address-in-crowd-s-validationfactor-enforced
error_log("Crowd SSO inconsistency: validation factors failed for token ${token} - malfeasance?");
} else {
error_log("Crowd SSO: got back unexpected HTTP response code: ${http_response_code}");
}
return null;
}
} else {
error_log("Crowd SSO Failure: Did not receive cookie name from Crowd or error. Odd that.");
return null;
}
}
}
function tokenAuthCreateSession($username,$password) {
$cookie_config = $this->getCrowdCookieConfig();
if($cookie_config == null) {
// errors have already been logged in this case
return null;
} else {
$tokenCreationXML = $this->generateTokenCreationXML($username, $password);
$rc = $this->curlDo("/session", null, $tokenCreationXML);
$http_response_code = $rc['metadata']['http_code'];
if ($http_response_code == 201) { // 201 == created
// good to go, token is created and authentication verified
// extract username from response
$xmlResponse = new SimpleXMLElement($rc['response']);
$userElement = $xmlResponse->user;
$authenticated_username = (string)$userElement['name'];
// confirm that tokens match (just in case)
$authenticated_token = (string)$xmlResponse->token;
// set the cookie
$name = $cookie_config['name'];
$domain = $cookie_config['domain'];
$secure = (bool)$cookie_config['secure'];
$value = $authenticated_token;
$session_only = 0;
setcookie($name, $value, $session_only, '/', $domain, $secure);
error_log("Auth sucess: ${authenticated_username} ${authenticated_token}");
return $authenticated_username; // authentication successful and token set
} elseif($http_response_code == 400) {
// authentication failed - bad credentials
} elseif ($http_response_code == 403) {
// inactive user
} else {
error_log("Crowd SSO: got back unexpected HTTP response code: ${http_response_code}");
}
return null;
}
}
function getCrowdCookieConfig() {
if ($this->crowd_cookie_config == null) {
$rc = $this->curlDo("/config/cookie");
if(!$this->curl_logerror($rc,"Error while retrieving Crowd cookie config")){
return null;
} else {
$xmlResponse = $this->crowd_xml_logerror($rc,"Crowd error while retrieving cookie config");
$cookie_config = array();
$cookie_config['domain'] = (string)$xmlResponse->domain;
$cookie_config['secure'] = (string)$xmlResponse->secure;
$cookie_config['name'] = (string)$xmlResponse->name;
$this->crowd_cookie_config = $cookie_config;
return $cookie_config;
}
} else {
return $this->crowd_cookie_config;
}
return null;
}
function userIsInGroup($username, $groupname) {
$rc = $this->curlDo("/user/group/nested",array("username" => $username, "groupname" => $groupname));
$http_response_code = $rc['metadata']['CURLINFO_HTTP_CODE'];
if($http_response_code == 200) {
// user belongs to group
return true;
} elseif ($http_response_code == 404) {
// user not in group
return false;
} else {
// some other error
curl_logerror($rc, "Error while confirming membership of '${username}' in group '${groupname}'");
return false;
}
}
function getUserInfo($username) {
$rc = $this->curlDo('/user', array('username' => $username));
if (!$this->curl_logerror($rc,"Error while retrieving user info for username '${username}'")){
return null;
}
// got back a valid XML response (hopefully)
$xmlResponse = $this->crowd_xml_logerror($rc,"Error returned in Crowd XML response");
if($xmlResponse) {
if($xmlResponse[0]->getName() == "user") {
// break out from the XML
$firstname = (string)$xmlResponse->{'first-name'};
$lastname = (string)$xmlResponse->{'last-name'};
$email = (string)$xmlResponse->{'email'};
$display_name = (string)$xmlResponse->{'display-name'};
// seed the array to be used for user creation
$userData = array(
'user_login' => $username,
'user_nicename' => strip_tags("${firstname} ${lastname}"),
'user_email' => $email,
'display_name' => strip_tags("${firstname} ${lastname}"),
'first_name' => $firstname,
'last_name' => $lastname
);
return $userData;
} else {
error_log("Got unexpected Crowd XML response to auth query:\n" . $rc['response']);
}
}
}
private function generateSimpleAuthXML($password) {
$document = new DOMDocument("1.0","UTF-8");
$passwordNode = $document->appendChild($document->createElement("password"));
$value = $passwordNode->appendChild($document->createElement("value"));
$cdata = $document->createCDATASection($password);
$value->appendChild($cdata);
$xml = $document->saveXML();
return $xml;
}
private function generateTokenVerificationXML() {
$document = new DOMDocument("1.0","UTF-8");
$validationFactors = $document->appendChild($document->createElement("validation-factors"));
$validationFactor = $validationFactors->appendChild($document->createElement("validation-factor"));
$validationFactor->appendChild($document->createElement('name','remote_address'));
$validationFactor->appendChild($document->createElement('value',$_SERVER['REMOTE_ADDR']));
$xml = $document->saveXML();
return $xml;
}
private function generateTokenCreationXML($username, $password) {
$document = new DOMDocument("1.0","UTF-8");
$authenticationContext = $document->appendChild($document->createElement("authentication-context"));
$authenticationContext->appendChild($document->createElement("username",$username));
$passwordNode = $authenticationContext->appendChild($document->createElement("password"));
$cdata = $document->createCDATASection($password);
$passwordNode->appendChild($cdata);
$validationFactors = $authenticationContext->appendChild($document->createElement("validation-factors"));
$validationFactor = $validationFactors->appendChild($document->createElement("validation-factor"));
$validationFactor->appendChild($document->createElement('name','remote_address'));
$validationFactor->appendChild($document->createElement('value',$_SERVER['REMOTE_ADDR']));
$xml = $document->saveXML();
return $xml;
}
private function getCookieName(){
$crowd_cookie_config = $this->getCrowdCookieConfig();
// replace all dots in the name with underscores
return preg_replace('/\\./', "_", $crowd_cookie_config['name']);
}
}
?>