Skip to content

Commit c172312

Browse files
committed
Use constants for storing cookie names
Stroing cookie names in constants allows overriding the cookie names. Eg.: on the VIP Go platform (part of the WordPress.com VIP offering), we do automatically whitelist cookies prefixed with `wordpress_` in our Varnish configuration. Being able to change cookie names allows us to not introduce new Varnish and Nginx rules for this plugin specifically. Also, actually, for clients using the plugin on our platform, we had to modify the names which makes it's updates more involved.
1 parent 1e07ba4 commit c172312

2 files changed

Lines changed: 29 additions & 15 deletions

File tree

onelogin-saml-sso/onelogin_saml.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414
exit;
1515
}
1616

17+
// Allow cookie name overriding by defining following constants prior this point. Eg.: in wp-config.php.
18+
if ( false === defined( 'SAML_LOGIN_COOKIE' ) ) {
19+
define( 'SAML_LOGIN_COOKIE', 'saml_login' );
20+
}
21+
if ( false === defined( 'SAML_NAMEID_COOKIE' ) ) {
22+
define( 'SAML_NAMEID_COOKIE', 'saml_nameid' );
23+
}
24+
if ( false === defined( 'SAML_SESSIONINDEX_COOKIE' ) ) {
25+
define( 'SAML_SESSIONINDEX_COOKIE', 'saml_sessionindex' );
26+
}
27+
if ( false === defined( 'SAML_NAMEID_FORMAT_COOKIE' ) ) {
28+
define( 'SAML_NAMEID_FORMAT_COOKIE', 'saml_nameid_format' );
29+
}
30+
1731
require_once plugin_dir_path(__FILE__)."php/functions.php";
1832
require_once plugin_dir_path(__FILE__)."php/configuration.php";
1933

@@ -39,7 +53,7 @@ function disable_password_reset() { return false; }
3953
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'login';
4054

4155
// Handle SLO
42-
if (isset($_COOKIE['saml_login']) && get_option('onelogin_saml_slo')) {
56+
if (isset($_COOKIE[SAML_LOGIN_COOKIE]) && get_option('onelogin_saml_slo')) {
4357
add_action('init', 'saml_slo', 1);
4458
}
4559

onelogin-saml-sso/php/functions.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ function saml_slo() {
8888
$sessionIndex = null;
8989
$nameIdFormat = null;
9090

91-
if (isset($_COOKIE['saml_nameid'])) {
92-
$nameId = $_COOKIE['saml_nameid'];
91+
if (isset($_COOKIE[SAML_NAMEID_COOKIE])) {
92+
$nameId = $_COOKIE[SAML_NAMEID_COOKIE];
9393
}
94-
if (isset($_COOKIE['saml_sessionindex'])) {
95-
$sessionIndex = $_COOKIE['saml_sessionindex'];
94+
if (isset($_COOKIE[SAML_SESSIONINDEX_COOKIE])) {
95+
$sessionIndex = $_COOKIE[SAML_SESSIONINDEX_COOKIE];
9696
}
97-
if (isset($_COOKIE['saml_nameid_format'])) {
98-
$nameIdFormat = $_COOKIE['saml_nameid_format'];
97+
if (isset($_COOKIE[SAML_NAMEID_FORMAT_COOKIE])) {
98+
$nameIdFormat = $_COOKIE[SAML_NAMEID_FORMAT_COOKIE];
9999
}
100100

101101
$auth = initialize_saml();
@@ -149,9 +149,9 @@ function saml_acs() {
149149
exit();
150150
}
151151

152-
setcookie('saml_nameid', $auth->getNameId(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH );
153-
setcookie('saml_sessionindex', $auth->getSessionIndex(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH );
154-
setcookie('saml_nameid_format', $auth->getNameIdFormat(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH );
152+
setcookie(SAML_NAMEID_COOKIE, $auth->getNameId(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH );
153+
setcookie(SAML_SESSIONINDEX_COOKIE, $auth->getSessionIndex(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH );
154+
setcookie(SAML_NAMEID_FORMAT_COOKIE, $auth->getNameIdFormat(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH );
155155

156156
$attrs = $auth->getAttributes();
157157

@@ -281,7 +281,7 @@ function saml_acs() {
281281
} else if ($user_id) {
282282
wp_set_current_user($user_id);
283283
wp_set_auth_cookie($user_id);
284-
setcookie('saml_login', 1, time() + YEAR_IN_SECONDS, SITECOOKIEPATH );
284+
setcookie(SAML_LOGIN_COOKIE, 1, time() + YEAR_IN_SECONDS, SITECOOKIEPATH );
285285
#do_action('wp_login', $user_id);
286286
#wp_signon($user_id);
287287
}
@@ -318,10 +318,10 @@ function saml_sls() {
318318
$errors = $auth->getErrors();
319319
if (empty($errors)) {
320320
wp_logout();
321-
setcookie('saml_login', 0, time() - 3600, SITECOOKIEPATH );
322-
setcookie('saml_nameid', null, time() - 3600, SITECOOKIEPATH );
323-
setcookie('saml_sessionindex', null, time() - 3600, SITECOOKIEPATH );
324-
setcookie('saml_nameid_format', null, time() - 3600, SITECOOKIEPATH );
321+
setcookie(SAML_LOGIN_COOKIE, 0, time() - 3600, SITECOOKIEPATH );
322+
setcookie(SAML_NAMEID_COOKIE, null, time() - 3600, SITECOOKIEPATH );
323+
setcookie(SAML_SESSIONINDEX_COOKIE, null, time() - 3600, SITECOOKIEPATH );
324+
setcookie(SAML_NAMEID_FORMAT_COOKIE, null, time() - 3600, SITECOOKIEPATH );
325325

326326
if (get_option('onelogin_saml_forcelogin') && get_option('onelogin_saml_customize_stay_in_wordpress_after_slo')) {
327327
wp_redirect(home_url().'/wp-login.php?loggedout=true');

0 commit comments

Comments
 (0)