00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class SecurityComponent extends Object {
00036
00037
00038
00039
00040
00041
00042 var $blackHoleCallback = null;
00043
00044
00045
00046
00047
00048
00049
00050 var $requirePost = array();
00051
00052
00053
00054
00055
00056
00057
00058 var $requireGet = array();
00059
00060
00061
00062
00063
00064
00065
00066 var $requirePut = array();
00067
00068
00069
00070
00071
00072
00073
00074 var $requireDelete = array();
00075
00076
00077
00078
00079
00080
00081
00082 var $requireSecure = array();
00083
00084
00085
00086
00087
00088
00089
00090 var $requireAuth = array();
00091
00092
00093
00094
00095
00096
00097
00098 var $requireLogin = array();
00099
00100
00101
00102
00103
00104
00105
00106 var $loginOptions = array('type' => '', 'prompt' => null);
00107
00108
00109
00110
00111
00112
00113
00114
00115 var $loginUsers = array();
00116
00117
00118
00119
00120
00121
00122
00123
00124 var $allowedControllers = array();
00125
00126
00127
00128
00129
00130
00131
00132
00133 var $allowedActions = array();
00134
00135
00136
00137
00138
00139
00140 var $disabledFields = array();
00141
00142
00143
00144
00145
00146
00147
00148 var $validatePost = true;
00149
00150
00151
00152
00153
00154
00155 var $components = array('RequestHandler', 'Session');
00156
00157
00158
00159
00160
00161 var $_action = null;
00162
00163
00164
00165
00166
00167
00168 function startup(&$controller) {
00169 $this->_action = strtolower($controller->action);
00170 $this->_methodsRequired($controller);
00171 $this->_secureRequired($controller);
00172 $this->_authRequired($controller);
00173 $this->_loginRequired($controller);
00174
00175 $isPost = ($this->RequestHandler->isPost() || $this->RequestHandler->isPut());
00176 $isRequestAction = (
00177 !isset($controller->params['requested']) ||
00178 $controller->params['requested'] != 1
00179 );
00180
00181 if ($isPost && $isRequestAction && $this->validatePost) {
00182 if ($this->_validatePost($controller) === false) {
00183 if (!$this->blackHole($controller, 'auth')) {
00184 return null;
00185 }
00186 }
00187 }
00188 $this->_generateToken($controller);
00189 }
00190
00191
00192
00193
00194
00195
00196 function requirePost() {
00197 $args = func_get_args();
00198 $this->_requireMethod('Post', $args);
00199 }
00200
00201
00202
00203
00204
00205
00206 function requireGet() {
00207 $args = func_get_args();
00208 $this->_requireMethod('Get', $args);
00209 }
00210
00211
00212
00213
00214
00215
00216 function requirePut() {
00217 $args = func_get_args();
00218 $this->_requireMethod('Put', $args);
00219 }
00220
00221
00222
00223
00224
00225
00226 function requireDelete() {
00227 $args = func_get_args();
00228 $this->_requireMethod('Delete', $args);
00229 }
00230
00231
00232
00233
00234
00235
00236 function requireSecure() {
00237 $args = func_get_args();
00238 $this->_requireMethod('Secure', $args);
00239 }
00240
00241
00242
00243
00244
00245
00246 function requireAuth() {
00247 $args = func_get_args();
00248 $this->_requireMethod('Auth', $args);
00249 }
00250
00251
00252
00253
00254
00255
00256 function requireLogin() {
00257 $args = func_get_args();
00258 $base = $this->loginOptions;
00259
00260 foreach ($args as $i => $arg) {
00261 if (is_array($arg)) {
00262 $this->loginOptions = $arg;
00263 unset($args[$i]);
00264 }
00265 }
00266 $this->loginOptions = array_merge($base, $this->loginOptions);
00267 $this->_requireMethod('Login', $args);
00268
00269 if (isset($this->loginOptions['users'])) {
00270 $this->loginUsers =& $this->loginOptions['users'];
00271 }
00272 }
00273
00274
00275
00276
00277
00278
00279
00280 function loginCredentials($type = null) {
00281 switch (strtolower($type)) {
00282 case 'basic':
00283 $login = array('username' => env('PHP_AUTH_USER'), 'password' => env('PHP_AUTH_PW'));
00284 if (!empty($login['username'])) {
00285 return $login;
00286 }
00287 break;
00288 case 'digest':
00289 default:
00290 $digest = null;
00291
00292 if (version_compare(PHP_VERSION, '5.1') != -1) {
00293 $digest = env('PHP_AUTH_DIGEST');
00294 } elseif (function_exists('apache_request_headers')) {
00295 $headers = apache_request_headers();
00296 if (isset($headers['Authorization']) && !empty($headers['Authorization']) && substr($headers['Authorization'], 0, 7) == 'Digest ') {
00297 $digest = substr($headers['Authorization'], 7);
00298 }
00299 } else {
00300
00301 trigger_error(__('SecurityComponent::loginCredentials() - Server does not support digest authentication', true), E_USER_WARNING);
00302 }
00303
00304 if (!empty($digest)) {
00305 return $this->parseDigestAuthData($digest);
00306 }
00307 break;
00308 }
00309 return null;
00310 }
00311
00312
00313
00314
00315
00316
00317
00318 function loginRequest($options = array()) {
00319 $options = array_merge($this->loginOptions, $options);
00320 $this->_setLoginDefaults($options);
00321 $auth = 'WWW-Authenticate: ' . ucfirst($options['type']);
00322 $out = array('realm="' . $options['realm'] . '"');
00323
00324 if (strtolower($options['type']) == 'digest') {
00325 $out[] = 'qop="auth"';
00326 $out[] = 'nonce="' . uniqid("") . '"';
00327 $out[] = 'opaque="' . md5($options['realm']).'"';
00328 }
00329
00330 return $auth . ' ' . join(',', $out);
00331 }
00332
00333
00334
00335
00336
00337
00338
00339 function parseDigestAuthData($digest) {
00340 if (substr($digest, 0, 7) == 'Digest ') {
00341 $digest = substr($digest, 7);
00342 }
00343 $keys = array();
00344 $match = array();
00345 $req = array('nonce' => 1, 'nc' => 1, 'cnonce' => 1, 'qop' => 1, 'username' => 1, 'uri' => 1, 'response' => 1);
00346 preg_match_all('@(\w+)=([\'"]?)([a-zA-Z0-9=./\_-]+)\2@', $digest, $match, PREG_SET_ORDER);
00347
00348 foreach ($match as $i) {
00349 $keys[$i[1]] = $i[3];
00350 unset($req[$i[1]]);
00351 }
00352
00353 if (empty($req)) {
00354 return $keys;
00355 }
00356 return null;
00357 }
00358
00359
00360
00361
00362
00363
00364
00365
00366 function generateDigestResponseHash($data) {
00367 return md5(
00368 md5($data['username'] . ':' . $this->loginOptions['realm'] . ':' . $this->loginUsers[$data['username']]) .
00369 ':' . $data['nonce'] . ':' . $data['nc'] . ':' . $data['cnonce'] . ':' . $data['qop'] . ':' .
00370 md5(env('REQUEST_METHOD') . ':' . $data['uri'])
00371 );
00372 }
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383 function blackHole(&$controller, $error = '') {
00384 $this->Session->del('_Token');
00385
00386 if ($this->blackHoleCallback == null) {
00387 $code = 404;
00388 if ($error == 'login') {
00389 $code = 401;
00390 $controller->header($this->loginRequest());
00391 }
00392 $controller->redirect(null, $code, true);
00393 } else {
00394 return $this->_callback($controller, $this->blackHoleCallback, array($error));
00395 }
00396 }
00397
00398
00399
00400
00401
00402
00403
00404
00405 function _requireMethod($method, $actions = array()) {
00406 $this->{'require' . $method} = (empty($actions)) ? array('*'): $actions;
00407 }
00408
00409
00410
00411
00412
00413
00414
00415 function _methodsRequired(&$controller) {
00416 foreach (array('Post', 'Get', 'Put', 'Delete') as $method) {
00417 $property = 'require' . $method;
00418 if (is_array($this->$property) && !empty($this->$property)) {
00419 $require = array_map('strtolower', $this->$property);
00420
00421 if (in_array($this->_action, $require) || $this->$property == array('*')) {
00422 if (!$this->RequestHandler->{'is' . $method}()) {
00423 if (!$this->blackHole($controller, strtolower($method))) {
00424 return null;
00425 }
00426 }
00427 }
00428 }
00429 }
00430 return true;
00431 }
00432
00433
00434
00435
00436
00437
00438
00439 function _secureRequired(&$controller) {
00440 if (is_array($this->requireSecure) && !empty($this->requireSecure)) {
00441 $requireSecure = array_map('strtolower', $this->requireSecure);
00442
00443 if (in_array($this->_action, $requireSecure) || $this->requireSecure == array('*')) {
00444 if (!$this->RequestHandler->isSSL()) {
00445 if (!$this->blackHole($controller, 'secure')) {
00446 return null;
00447 }
00448 }
00449 }
00450 }
00451 return true;
00452 }
00453
00454
00455
00456
00457
00458
00459
00460 function _authRequired(&$controller) {
00461 if (is_array($this->requireAuth) && !empty($this->requireAuth) && !empty($controller->data)) {
00462 $requireAuth = array_map('strtolower', $this->requireAuth);
00463
00464 if (in_array($this->_action, $requireAuth) || $this->requireAuth == array('*')) {
00465 if (!isset($controller->data['_Token'] )) {
00466 if (!$this->blackHole($controller, 'auth')) {
00467 return null;
00468 }
00469 }
00470
00471 if ($this->Session->check('_Token')) {
00472 $tData = unserialize($this->Session->read('_Token'));
00473
00474 if (!empty($tData['allowedControllers']) && !in_array($controller->params['controller'], $tData['allowedControllers']) || !empty($tData['allowedActions']) && !in_array($controller->params['action'], $tData['allowedActions'])) {
00475 if (!$this->blackHole($controller, 'auth')) {
00476 return null;
00477 }
00478 }
00479 } else {
00480 if (!$this->blackHole($controller, 'auth')) {
00481 return null;
00482 }
00483 }
00484 }
00485 }
00486 return true;
00487 }
00488
00489
00490
00491
00492
00493
00494
00495 function _loginRequired(&$controller) {
00496 if (is_array($this->requireLogin) && !empty($this->requireLogin)) {
00497 $requireLogin = array_map('strtolower', $this->requireLogin);
00498
00499 if (in_array($this->_action, $requireLogin) || $this->requireLogin == array('*')) {
00500 $login = $this->loginCredentials($this->loginOptions['type']);
00501
00502 if ($login == null) {
00503 $controller->header($this->loginRequest());
00504
00505 if (!empty($this->loginOptions['prompt'])) {
00506 $this->_callback($controller, $this->loginOptions['prompt']);
00507 } else {
00508 $this->blackHole($controller, 'login');
00509 }
00510 } else {
00511 if (isset($this->loginOptions['login'])) {
00512 $this->_callback($controller, $this->loginOptions['login'], array($login));
00513 } else {
00514 if (strtolower($this->loginOptions['type']) == 'digest') {
00515 if ($login && isset($this->loginUsers[$login['username']])) {
00516 if ($login['response'] == $this->generateDigestResponseHash($login)) {
00517 return true;
00518 }
00519 }
00520 $this->blackHole($controller, 'login');
00521 } else {
00522 if (
00523 !(in_array($login['username'], array_keys($this->loginUsers)) &&
00524 $this->loginUsers[$login['username']] == $login['password'])
00525 ) {
00526 $this->blackHole($controller, 'login');
00527 }
00528 }
00529 }
00530 }
00531 }
00532 }
00533 return true;
00534 }
00535
00536
00537
00538
00539
00540
00541
00542 function _validatePost(&$controller) {
00543 if (empty($controller->data)) {
00544 return true;
00545 }
00546 $data = $controller->data;
00547
00548 if (!isset($data['_Token']) || !isset($data['_Token']['fields'])) {
00549 return false;
00550 }
00551 $token = $data['_Token']['key'];
00552
00553 if ($this->Session->check('_Token')) {
00554 $tokenData = unserialize($this->Session->read('_Token'));
00555
00556 if ($tokenData['expires'] < time() || $tokenData['key'] !== $token) {
00557 return false;
00558 }
00559 }
00560
00561 $locked = null;
00562 $check = $controller->data;
00563 $token = urldecode($check['_Token']['fields']);
00564
00565 if (strpos($token, ':')) {
00566 list($token, $locked) = explode(':', $token, 2);
00567 }
00568 unset($check['_Token']);
00569
00570 $lockedFields = array();
00571 $fields = Set::flatten($check);
00572 $fieldList = array_keys($fields);
00573 $locked = unserialize(str_rot13($locked));
00574 $multi = array();
00575
00576 foreach ($fieldList as $i => $key) {
00577 if (preg_match('/\.\d+$/', $key)) {
00578 $multi[$i] = preg_replace('/\.\d+$/', '', $key);
00579 unset($fieldList[$i]);
00580 }
00581 }
00582 if (!empty($multi)) {
00583 $fieldList += array_unique($multi);
00584 }
00585
00586 foreach ($fieldList as $i => $key) {
00587 $isDisabled = false;
00588 $isLocked = (is_array($locked) && in_array($key, $locked));
00589
00590 if (!empty($this->disabledFields)) {
00591 foreach ((array)$this->disabledFields as $disabled) {
00592 $disabled = explode('.', $disabled);
00593 $field = array_values(array_intersect(explode('.', $key), $disabled));
00594 $isDisabled = ($field === $disabled);
00595 if ($isDisabled) {
00596 break;
00597 }
00598 }
00599 }
00600
00601 if ($isDisabled || $isLocked) {
00602 unset($fieldList[$i]);
00603 if ($isLocked) {
00604 $lockedFields[$key] = $fields[$key];
00605 }
00606 }
00607 }
00608 sort($fieldList, SORT_STRING);
00609 ksort($lockedFields, SORT_STRING);
00610
00611 $fieldList += $lockedFields;
00612 $check = Security::hash(serialize($fieldList) . Configure::read('Security.salt'));
00613 return ($token === $check);
00614 }
00615
00616
00617
00618
00619
00620
00621
00622 function _generateToken(&$controller) {
00623 if (isset($controller->params['requested']) && $controller->params['requested'] === 1) {
00624 return false;
00625 }
00626 $authKey = Security::generateAuthKey();
00627 $expires = strtotime('+' . Security::inactiveMins() . ' minutes');
00628 $token = array(
00629 'key' => $authKey,
00630 'expires' => $expires,
00631 'allowedControllers' => $this->allowedControllers,
00632 'allowedActions' => $this->allowedActions,
00633 'disabledFields' => $this->disabledFields
00634 );
00635
00636 if (!isset($controller->data)) {
00637 $controller->data = array();
00638 }
00639
00640 if ($this->Session->check('_Token')) {
00641 $tokenData = unserialize($this->Session->read('_Token'));
00642 $valid = (
00643 isset($tokenData['expires']) &&
00644 $tokenData['expires'] > time() &&
00645 isset($tokenData['key'])
00646 );
00647
00648 if ($valid) {
00649 $token['key'] = $tokenData['key'];
00650 }
00651 }
00652 $controller->params['_Token'] = $token;
00653 $this->Session->write('_Token', serialize($token));
00654
00655 return true;
00656 }
00657
00658
00659
00660
00661
00662
00663
00664 function _setLoginDefaults(&$options) {
00665 $options = array_merge(array(
00666 'type' => 'basic',
00667 'realm' => env('SERVER_NAME'),
00668 'qop' => 'auth',
00669 'nonce' => String::uuid()
00670 ), array_filter($options));
00671 $options = array_merge(array('opaque' => md5($options['realm'])), $options);
00672 }
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682 function _callback(&$controller, $method, $params = array()) {
00683 if (is_callable(array($controller, $method))) {
00684 return call_user_func_array(array(&$controller, $method), empty($params) ? null : $params);
00685 } else {
00686 // Debug::warning('Callback method ' . $method . ' in controller ' . get_class($controller)
00687 return null;
00688 }
00689 }
00690 }
00691
00692 ?>