Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions rpow.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function rpow_init($config = []) {
'cookieName' => 'rpow' . substr(md5('cookie::' . $defaultCookieSigningKey), 0, 8),
'cookieTtl' => 90,
'stateMachine' => new CRM_Rpow_StateMachine(),
'debug' => 1,
'debug' => 0,
];

global $civirpow;
Expand Down Expand Up @@ -60,25 +60,46 @@ function rpow_init($config = []) {
}

function _rpow_signer($config) {
return new \CRM_Utils_Signer($config['cookieSigningKey'], ['exp']);
if (!class_exists('CRM_Utils_Signer')) {
error_log('civirpow: CRM_Utils_Signer not available (autoload not ready). Skipping signature check.');
return null;
}

error_log('civirpow: CRM_Utils_Signer is available. Proceeding with signature check.');
return new CRM_Utils_Signer($config['cookieSigningKey'], ['exp']);
}


function _rpow_has_cookie($config) {
if (isset($_COOKIE[$config['cookieName']])) {
error_log('civirpow: Found cookie "' . $config['cookieName'] . '"');
$cookie = json_decode($_COOKIE[$config['cookieName']], TRUE);
} else {
error_log('civirpow: No cookie named "' . $config['cookieName'] . '" found.');
return FALSE;
}

if (!isset($cookie['exp']) || $cookie['exp'] <= time()) {
error_log('civirpow: Cookie expired or missing "exp" field.');
return FALSE;
}
else {
$cookie = NULL;

$signer = _rpow_signer($config);
if (!$signer) {
error_log('civirpow: Skipping signature validation due to missing signer.');
return FALSE;
}

if (isset($cookie['exp']) && $cookie['exp'] > time() && _rpow_signer($config)->validate($cookie['sig'], $cookie)) {
if ($signer->validate($cookie['sig'], $cookie)) {
error_log('civirpow: Cookie signature is valid.');
return TRUE;
}
else {
} else {
error_log('civirpow: Cookie signature is INVALID.');
return FALSE;
}
}


function _rpow_update_cookie($config, $db) {
$signer = _rpow_signer($config);
$expires = time() + $config['cookieTtl'];
Expand Down