diff --git a/class-Session.php b/class-Session.php index 967cc81..60f9656 100644 --- a/class-Session.php +++ b/class-Session.php @@ -1,239 +1,295 @@ . // 60s * 60m * 24h * 7d define_default( 'SESSION_DURATION', 604800 ); // the default user logged-in class define_default( 'SESSIONUSER_CLASS', 'Sessionuser' ); /** * Session handler * * Note that actually we do NOT need server sessions <3 */ class Session { /** * Is the login verified? * * @var bool */ private $ok = false; /** * User currently logged * * @var Sessionuser */ private $user = null; /** * Get the singleton instance * * @return self */ public static function instance() { static $me = false; if( ! $me ) { $me = new self(); } return $me; } /** * Constructor */ public function __construct() { $this->validate(); } /** * Is the user logged? * * @return bool */ public function isLogged() { return null !== $this->getUser(); } /** * Get the currently logged-in user * * @return Sessionuser */ public function getUser() { if( ! $this->ok ) { $this->validate(); } return $this->user; } /* * Login statuses */ const OK = 0; const LOGIN_FAILED = 1; const ALREADY_LOGGED = 2; const EMPTY_USER_UID = 4; const EMPTY_USER_PASSWORD = 8; const USER_DISABLED = 64; /** * Do a login * * @param $status int Login status * @param $uid User UID * @param $pwd User password * @return bool */ public function login( & $status = null, $uid = null, $pwd = null ) { // already logged if( $this->isLogged() ) { $status = self::ALREADY_LOGGED; return true; } // UID from function or POST if( $uid === null ) { if( isset( $_POST['user_uid'] ) ) { $uid = $_POST['user_uid']; } else { return error( 'login without POST-ing user_uid' ); } } // password from parameter or POST if( $pwd === null ) { if( isset( $_POST['user_password'] ) ) { $pwd = $_POST['user_password']; } else { return error( 'login without POST-ing user_password' ); } } + // no credentials no party if( empty( $uid ) ) { $status = self::EMPTY_USER_UID; return false; } if( empty( $pwd ) ) { $status = self::EMPTY_USER_PASSWORD; return false; } // PHP bug $userClass = SESSIONUSER_CLASS; $user = $userClass::factoryFromLogin( $uid, $pwd ) ->queryRow(); if( ! $user ) { $status = self::LOGIN_FAILED; return self::failed( $userClass::sanitizeUID( $uid ), 'POST' ); } if( ! $user->isSessionuserActive() ) { $status = self::USER_DISABLED; return false; } $this->ok = true; $this->user = $user; $time = time(); $duration = $time + SESSION_DURATION; $path = ROOT . _; $force_https = is_https(); setcookie( 'user_uid', $user->getSessionuserUID(), $duration, $path, '', $force_https, false ); setcookie( 'token', $user->generateSessionuserCookieToken(), $duration, $path, '', $force_https, true ); + setcookie( 'csrf', $this->generateCSRF(), $duration, $path, '', $force_https, true ); $status = self::OK; return true; } /** * Validate the user session * * @return bool */ private function validate() { if( $this->ok ) { return true; } if( ! isset( $_COOKIE['user_uid'], $_COOKIE['token'] ) ) { return false; } // PHP Bug $userClass = SESSIONUSER_CLASS; $user = $userClass::factoryFromUID( $_COOKIE['user_uid'] ) ->queryRow(); $this->ok = true; if( ! $user ) { $this->user = null; return false; } if( ! $user->isSessionuserActive() ) { return false; } if( $_COOKIE['token'] !== $user->generateSessionuserCookieToken() ) { $this->destroy(); return self::failed( $userClass::sanitizeUID( $_COOKIE['user_uid'] ), 'cookies' ); } $this->user = $user; return true; } /** * Destroy the session */ public function destroy() { $invalidate = time() - 8000; $path = ROOT . _; setcookie( 'user_uid', 'asd', $invalidate, $path ); setcookie( 'token', 'asd', $invalidate, $path ); $this->ok = true; $this->user = null; } /** * Log in syslog a login fail * * @param $uid string * @param $from string * @return false */ public static function failed( $uid, $from ) { error_log( sprintf( "Login failed by '%s' using %s", $uid, $from ) ); return false; } + + /** + * Get the CSRF token of the current user + * + * Note that it's valid only for logged-in users. + * + * @return string + */ + public function getCSRF() { + // TODO: remove that '@' put for backward compatibility with old sessions + return @$_COOKIE['csrf']; + } + + /** + * Print a form action field with the CSRF + * + * @param string $action + */ + public function formActionWithCSRF( $action ) { + echo HTML::input( 'hidden', 'action', $action ); + echo HTML::input( 'hidden', 'csrf', Session::instance()->getCSRF() ); + } + + /** + * Check if a form action is valid (with the related CSRF) + * + * @param string $action + * @return boolean + */ + public function validateActionAndCSRF( $action ) { + $csrf = $this->getCSRF(); + return + isset( $_POST['action'], $_POST['csrf'] ) + && $_POST['action'] === $action + && $_POST['csrf'] === $csrf; + } + + /** + * Generate a new CSRF token + * + * @return string + */ + private function generateCSRF() { + return bin2hex( openssl_random_pseudo_bytes( 8 ) ); + } + + /** + * Check if a CSRF if valid for the current user + * + * @return boolean + */ + public function validateCSRF( $csrf ) { + return $csrf === $this->getCSRF(); + } } diff --git a/functions.php b/functions.php index e7a1b7c..5123527 100644 --- a/functions.php +++ b/functions.php @@ -1,1190 +1,1209 @@ . /** * Check if the user do not want to be tracked * * See the HTTP 'DNT' header. * * @return boolean */ function do_not_track() { return isset( $_SERVER['HTTP_DNT'] ) && $_SERVER['HTTP_DNT'] === '1'; } /* * Configuration helpers * * Shortcuts useful when you declare a configuration file. */ /** * Define a constant if it does not exist * * @param string $name Constant name * @param string $value Constant value */ function define_default( $name, $value ) { defined( $name ) or define( $name, $value ); } /* * Template helpers * * Shortcuts useful when you build a page */ /** * Return a ' selected="selected"' HTML attribute under some circumstances * * @param mixed $helper If this is the only arg, return * @param mixed $current If this matches $helper, return * @param bool $force If this is true, return */ function selected( $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) { return html_attribute_when_matching( 'selected', 'selected', $helper, $current, $force); } /** * Return a ' checked="checked"' HTML attribute under some circumstances * * This is useful for the HTML tag. * * @param mixed $helper If this is the only arg, return * @param mixed $current If this matches $helper, return * @param bool $force If this is true, return */ function checked( $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) { return html_attribute_when_matching( 'checked', 'checked', $helper, $current, $force); } /** * Return a ' disabled="disabled"' HTML attribute under some circumstances * * @param mixed $helper If this is the only arg, return * @param mixed $current If this matches $helper, return * @param bool $force If this is true, return */ function disabled( $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) { return html_attribute_when_matching( 'disabled', 'disabled', $helper, $current, $force ); } /** * Return an HTML attribute under some circumstances * * @param string $attribute HTML attribute name e.g. 'disabled' * @param string $value HTML attribute value e.g. * @param mixed $helper If this is the only arg, return * @param mixed $current If this matches $helper, return * @param bool $force If this is true, return */ function html_attribute_when_matching( $attribute, $value, $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) { if( $helper === $current || $helper && PHP_INT_MAX === $current || $force ) { return HTML::property( $attribute, $value ); } return ''; } /** * Get an HTML attribute ' value="$v"' * * The value will be sanitized. * * @param string $v * @return string */ function value( $v ) { return HTML::property( 'value', $v ); } /** * Shortcut for htmlentities() * * Return an HTML-sanitized untrusted string to be safe from XSS. * * @param string $s e.g. 'Hello