Grima 2018-08
Whispering into Alma's ear
Public Member Functions | Private Attributes | List of all members
EncryptedCookieSession Class Reference
Inheritance diagram for EncryptedCookieSession:

Public Member Functions

  __construct ($key)
 
  open ($save_path, $session_name)
 
  close ()
 
  destroy ($session_id)
 
  gc ($maxlifetime)
 
  read ($session_id)
 
  write ($session_id, $session_data)
 

Private Attributes

  $save_path
 
  $session_name
 
  $session_key
 
  $last_read
 

Detailed Description

Definition at line 5052 of file grima-lib.php.

Constructor & Destructor Documentation

◆ __construct()

EncryptedCookieSession::__construct (   $key )

Definition at line 5061 of file grima-lib.php.

5062 {
5063 $this->session_key = $key;
5064 $this->last_read = "Not This";
5065 }

Member Function Documentation

◆ close()

EncryptedCookieSession::close ( )

Definition at line 5075 of file grima-lib.php.

5076 {
5077 //error_log("session_close()");
5078 return true;
5079 }

◆ destroy()

EncryptedCookieSession::destroy (   $session_id )

Definition at line 5081 of file grima-lib.php.

5082 {
5083 //error_log("sessionDestroy($session_id)");
5084 $params = session_get_cookie_params();
5085 $params['expires'] = time() - 42000;
5086 unset($params['lifetime']);
5087 setcookie($this->session_name, '', $params);
5088 return true;
5089 }

◆ gc()

EncryptedCookieSession::gc (   $maxlifetime )

Definition at line 5091 of file grima-lib.php.

5092 {
5093 //error_log("session_gc($maxlifetime)");
5094 return true;
5095 }

◆ open()

EncryptedCookieSession::open (   $save_path,
  $session_name 
)

Definition at line 5067 of file grima-lib.php.

5068 {
5069 $this->save_path = $save_path;
5070 $this->session_name = $session_name;
5071 //error_log("session_open($save_path,$session_name)");
5072 return true;
5073 }

References $save_path, and $session_name.

◆ read()

EncryptedCookieSession::read (   $session_id )

Definition at line 5097 of file grima-lib.php.

5098 {
5099 //error_log("session_read($session_id)");
5100 $nonce_ciphertext_b64 = isset($_COOKIE[$this->session_name]) ? $_COOKIE[$this->session_name] : "";
5101 if ($nonce_ciphertext_b64) {
5102 $nonce_ciphertext = base64_decode($nonce_ciphertext_b64);
5103 if (strlen($nonce_ciphertext)>24+16) {
5104 $nonce = substr($nonce_ciphertext, 0, 24);
5105 $ciphertext = substr($nonce_ciphertext, 24);
5106 $plaintext = sodium_crypto_secretbox_open($ciphertext, $nonce, $this->session_key);
5107 // error_log("session_read -> $plaintext");
5108 $this->last_read = $plaintext;
5109 return $plaintext ?: "";
5110 } else {
5111 error_log("cookie too short: $nonce_ciphertext_b64");
5112 }
5113 } else {
5114 //error_log("cookie not set or empty");
5115 }
5116 return "";
5117 }

References $session_name.

◆ write()

EncryptedCookieSession::write (   $session_id,
  $session_data 
)

Definition at line 5119 of file grima-lib.php.

5120 {
5121 // error_log("session_write($session_id,$session_data)");
5122 if (!$session_data) { return true;
5123 }
5124 if ($this->last_read == $session_data) { return true;
5125 }
5126 $nonce = random_bytes(24);
5127 $ciphertext = sodium_crypto_secretbox($session_data, $nonce, $this->session_key);
5128 $nonce_ciphertext = $nonce.$ciphertext;
5129 $nonce_ciphertext_b64 = base64_encode($nonce_ciphertext);
5130 $options = session_get_cookie_params();
5131 if (isset($options['expires']) && $options['expires']) {
5132 $options['expires'] += time();
5133 }
5134 unset($options['lifetime']);
5135 setcookie($this->session_name, $nonce_ciphertext_b64, $options);
5136 return true;
5137 }

Member Data Documentation

◆ $last_read

EncryptedCookieSession::$last_read
private

Definition at line 5059 of file grima-lib.php.

◆ $save_path

EncryptedCookieSession::$save_path
private

Definition at line 5056 of file grima-lib.php.

Referenced by open().

◆ $session_key

EncryptedCookieSession::$session_key
private

Definition at line 5058 of file grima-lib.php.

◆ $session_name

EncryptedCookieSession::$session_name
private

Definition at line 5057 of file grima-lib.php.

Referenced by open(), and read().


The documentation for this class was generated from the following file: