123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace OC\Session;
- use OCP\IConfig;
- use OCP\IRequest;
- use OCP\ISession;
- use OCP\Security\ICrypto;
- use OCP\Security\ISecureRandom;
- class CryptoWrapper {
- public const COOKIE_NAME = 'oc_sessionPassphrase';
-
- protected $config;
-
- protected $session;
-
- protected $crypto;
-
- protected $random;
-
- protected $passphrase;
-
- public function __construct(IConfig $config,
- ICrypto $crypto,
- ISecureRandom $random,
- IRequest $request) {
- $this->crypto = $crypto;
- $this->config = $config;
- $this->random = $random;
- if (!is_null($request->getCookie(self::COOKIE_NAME))) {
- $this->passphrase = $request->getCookie(self::COOKIE_NAME);
- } else {
- $this->passphrase = $this->random->generate(128);
- $secureCookie = $request->getServerProtocol() === 'https';
-
- if (!defined('PHPUNIT_RUN')) {
- $webRoot = \OC::$WEBROOT;
- if ($webRoot === '') {
- $webRoot = '/';
- }
- setcookie(
- self::COOKIE_NAME,
- $this->passphrase,
- [
- 'expires' => 0,
- 'path' => $webRoot,
- 'domain' => '',
- 'secure' => $secureCookie,
- 'httponly' => true,
- 'samesite' => 'Lax',
- ]
- );
- }
- }
- }
-
- public function wrapSession(ISession $session) {
- if (!($session instanceof CryptoSessionData)) {
- return new CryptoSessionData($session, $this->crypto, $this->passphrase);
- }
- return $session;
- }
- }
|