Security.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\template\helper;
  9. /**
  10. * The `Security` helper is responsible for various tasks associated with verifying the authenticity
  11. * of requests, including embedding secure tokens to protect against CSRF attacks.
  12. *
  13. * @see lithium\security\validation\RequestToken
  14. */
  15. class Security extends \lithium\template\Helper {
  16. protected $_classes = array(
  17. 'requestToken' => 'lithium\security\validation\RequestToken'
  18. );
  19. /**
  20. * Configures the helper with the default settings for interacting with security tokens.
  21. *
  22. * @param array $config
  23. */
  24. public function __construct(array $config = array()) {
  25. $defaults = array('sessionKey' => 'security.token', 'salt' => null);
  26. parent::__construct($config + $defaults);
  27. }
  28. /**
  29. * Generates a request key used to protect your forms against CSRF attacks. See the
  30. * `RequestToken` class for examples and proper usage.
  31. *
  32. * @see lithium\security\validation\RequestToken
  33. * @param array $options Options used as HTML when generating the field.
  34. * @return string Returns a hidden `<input />` field containing a request-specific CSRF token
  35. * key.
  36. */
  37. public function requestToken(array $options = array()) {
  38. $defaults = array('name' => 'security.token', 'id' => false);
  39. $options += $defaults;
  40. $requestToken = $this->_classes['requestToken'];
  41. $flags = array_intersect_key($this->_config, array('sessionKey' => '', 'salt' => ''));
  42. $value = $requestToken::key($flags);
  43. $name = $options['name'];
  44. unset($options['name']);
  45. return $this->_context->form->hidden($name, compact('value') + $options);
  46. }
  47. }
  48. ?>