setCookie() public static method

Set a cookie.
Author: Andjelko Horvat
Author: Jaime Perez, UNINETT AS (jaime.perez@uninett.no)
public static setCookie ( string $name, string | null $value, array | null $params = null, boolean $throw = true )
$name string The name of the cookie.
$value string | null The value of the cookie. Set to NULL to delete the cookie.
$params array | null Cookie parameters.
$throw boolean Whether to throw exception if setcookie() fails.
 /**
  * Set the previous authentication source.
  *
  * This method remembers the authentication source that the user selected
  * by storing its name in a cookie.
  *
  * @param string $source Name of the authentication source the user selected.
  */
 public function setPreviousSource($source)
 {
     assert('is_string($source)');
     $cookieName = 'multiauth_source_' . $this->authId;
     $config = SimpleSAML_Configuration::getInstance();
     $params = array('lifetime' => 60 * 60 * 24 * 90, 'path' => $config->getBasePath(), 'httponly' => FALSE);
     \SimpleSAML\Utils\HTTP::setCookie($cookieName, $source, $params, FALSE);
 }
} else {
    $password = '';
}
$errorCode = NULL;
$errorParams = NULL;
if (!empty($_REQUEST['username']) || !empty($password)) {
    // Either username or password set - attempt to log in
    if (array_key_exists('forcedUsername', $state)) {
        $username = $state['forcedUsername'];
    }
    if ($source->getRememberUsernameEnabled()) {
        $sessionHandler = SimpleSAML_SessionHandler::getSessionHandler();
        $params = $sessionHandler->getCookieParams();
        $params['expire'] = time();
        $params['expire'] += isset($_REQUEST['remember_username']) && $_REQUEST['remember_username'] == 'Yes' ? 31536000 : -300;
        \SimpleSAML\Utils\HTTP::setCookie($source->getAuthId() . '-username', $username, $params, FALSE);
    }
    if ($source->isRememberMeEnabled()) {
        if (array_key_exists('remember_me', $_REQUEST) && $_REQUEST['remember_me'] === 'Yes') {
            $state['RememberMe'] = TRUE;
            $authStateId = SimpleSAML_Auth_State::saveState($state, sspmod_core_Auth_UserPassBase::STAGEID);
        }
    }
    try {
        sspmod_core_Auth_UserPassBase::handleLogin($authStateId, $username, $password);
    } catch (SimpleSAML_Error_Error $e) {
        /* Login failed. Extract error code and parameters, to display the error. */
        $errorCode = $e->getErrorCode();
        $errorParams = $e->getParameters();
    }
}
Example #3
0
<?php

/**
 *
 *
 * @author Mathias Meisfjordskar, University of Oslo.
 *         <*****@*****.**>
 * @package SimpleSAMLphp
 */
$params = array('expire' => mktime(0, 0, 0, 1, 1, 2038), 'secure' => FALSE, 'httponly' => TRUE);
\SimpleSAML\Utils\HTTP::setCookie('NEGOTIATE_AUTOLOGIN_DISABLE_PERMANENT', 'True', $params, FALSE);
$globalConfig = SimpleSAML_Configuration::getInstance();
$session = SimpleSAML_Session::getSessionFromRequest();
$session->setData('negotiate:disable', 'session', FALSE, 24 * 60 * 60);
$t = new SimpleSAML_XHTML_Template($globalConfig, 'negotiate:disable.php');
$t->show();
Example #4
0
 /**
  * Save the current IdP choice to a cookie.
  *
  * This function overrides the corresponding function in the parent class, to add support for common domain cookie.
  *
  * @param string $idp The entityID of the IdP.
  */
 protected function setPreviousIdP($idp)
 {
     assert('is_string($idp)');
     if ($this->cdcDomain === null) {
         parent::setPreviousIdP($idp);
         return;
     }
     $list = $this->getCDC();
     $prevIndex = array_search($idp, $list, true);
     if ($prevIndex !== false) {
         unset($list[$prevIndex]);
     }
     $list[] = $idp;
     foreach ($list as &$value) {
         $value = base64_encode($value);
     }
     $newCookie = implode(' ', $list);
     while (strlen($newCookie) > 4000) {
         // the cookie is too long. Remove the oldest elements until it is short enough
         $tmp = explode(' ', $newCookie, 2);
         if (count($tmp) === 1) {
             // we are left with a single entityID whose base64 representation is too long to fit in a cookie
             break;
         }
         $newCookie = $tmp[1];
     }
     $params = array('lifetime' => $this->cdcLifetime, 'domain' => $this->cdcDomain, 'secure' => true, 'httponly' => false);
     \SimpleSAML\Utils\HTTP::setCookie('_saml_idp', $newCookie, $params, false);
 }
Example #5
0
 /**
  * Update session cookies.
  *
  * @param array $params The parameters for the cookies.
  */
 public function updateSessionCookies($params = null)
 {
     $sessionHandler = SimpleSAML_SessionHandler::getSessionHandler();
     if ($this->sessionId !== null) {
         $sessionHandler->setCookie($sessionHandler->getSessionCookieName(), $this->sessionId, $params);
     }
     if ($this->authToken !== null) {
         $globalConfig = SimpleSAML_Configuration::getInstance();
         \SimpleSAML\Utils\HTTP::setCookie($globalConfig->getString('session.authtoken.cookiename', 'SimpleSAMLAuthToken'), $this->authToken, $params);
     }
 }
Example #6
0
 /**
  * Save cookie with the given name and value.
  *
  * This function will save a cookie with the given name and value for the current discovery
  * service type.
  *
  * @param string $name The name of the cookie.
  * @param string $value The value of the cookie.
  */
 protected function setCookie($name, $value)
 {
     $prefixedName = 'idpdisco_' . $this->instance . '_' . $name;
     $params = array('lifetime' => 60 * 60 * 24 * 90, 'path' => '/' . $this->config->getBaseUrl(), 'httponly' => false);
     \SimpleSAML\Utils\HTTP::setCookie($prefixedName, $value, $params, false);
 }
Example #7
0
 /**
  * Build a CDC cookie string.
  *
  * @param array $list  The list of IdPs.
  * @return string  The CDC cookie value.
  */
 function setCDC(array $list)
 {
     foreach ($list as &$value) {
         $value = base64_encode($value);
     }
     $cookie = implode(' ', $list);
     while (strlen($cookie) > 4000) {
         /* The cookie is too long. Remove the oldest elements until it is short enough. */
         $tmp = explode(' ', $cookie, 2);
         if (count($tmp) === 1) {
             /*
              * We are left with a single entityID whose base64
              * representation is too long to fit in a cookie.
              */
             break;
         }
         $cookie = $tmp[1];
     }
     $params = array('lifetime' => $this->cookieLifetime, 'path' => '/', 'domain' => '.' . $this->domain, 'secure' => TRUE, 'httponly' => FALSE);
     \SimpleSAML\Utils\HTTP::setCookie('_saml_idp', $cookie, $params, FALSE);
 }
Example #8
0
 /**
  * This method will attempt to set the user-selected language in a cookie. It will do nothing if the language
  * specified is not in the list of available languages, or the headers have already been sent to the browser.
  *
  * @param string $language The language set by the user.
  */
 public static function setLanguageCookie($language)
 {
     assert('is_string($language)');
     $language = strtolower($language);
     $config = \SimpleSAML_Configuration::getInstance();
     $availableLanguages = $config->getArray('language.available', array('en'));
     if (!in_array($language, $availableLanguages, true) || headers_sent()) {
         return;
     }
     $name = $config->getString('language.cookie.name', 'language');
     $params = array('lifetime' => $config->getInteger('language.cookie.lifetime', 60 * 60 * 24 * 900), 'domain' => $config->getString('language.cookie.domain', null), 'path' => $config->getString('language.cookie.path', '/'), 'httponly' => false);
     HTTP::setCookie($name, $language, $params, false);
 }
Example #9
0
 /**
  * @deprecated This method will be removed in SSP 2.0. Please use SimpleSAML\Utils\HTTP::setCookie() instead.
  */
 public static function setCookie($name, $value, array $params = NULL, $throw = TRUE)
 {
     \SimpleSAML\Utils\HTTP::setCookie($name, $value, $params, $throw);
 }
 /**
  * This function logs the user out by deleting the session information from memcache.
  */
 private function doLogout()
 {
     $cookieName = $this->getCookieName();
     // check if we have a valid cookie
     if (!array_key_exists($cookieName, $_COOKIE)) {
         return;
     }
     $sessionID = $_COOKIE[$cookieName];
     // delete the session from memcache
     $memcache = $this->getMemcache();
     $memcache->delete($sessionID);
     // delete the session cookie
     \SimpleSAML\Utils\HTTP::setCookie($cookieName, null);
 }
<?php

require_once '_include.php';
/**
 * This page clears the user's IdP discovery choices.
 */
// The base path for cookies. This should be the installation directory for SimpleSAMLphp.
$config = SimpleSAML_Configuration::getInstance();
$cookiePath = '/' . $config->getBaseUrl();
// We delete all cookies which starts with 'idpdisco_'
foreach ($_COOKIE as $cookieName => $value) {
    if (substr($cookieName, 0, 9) !== 'idpdisco_') {
        /* Not a idpdisco cookie. */
        continue;
    }
    /* Delete the cookie. We delete it once without the secure flag and once with the secure flag. This
     * ensures that the cookie will be deleted in any case.
     */
    \SimpleSAML\Utils\HTTP::setCookie($cookieName, NULL, array('path' => $cookiePath, 'httponly' => FALSE), FALSE);
}
/* Find where we should go now. */
if (array_key_exists('ReturnTo', $_REQUEST)) {
    $returnTo = \SimpleSAML\Utils\HTTP::checkURLAllowed($_REQUEST['ReturnTo']);
} else {
    /* Return to the front page if no other destination is given. This is the same as the base cookie path. */
    $returnTo = $cookiePath;
}
/* Redirect to destination. */
\SimpleSAML\Utils\HTTP::redirectTrustedURL($returnTo);
Example #12
0
 /**
  * Helper function for setting a cookie.
  *
  * @param string      $name  Name of the cookie.
  * @param string|null $value Value of the cookie. Set this to null to delete the cookie.
  *
  * @return void
  */
 private function _setConsentCookie($name, $value)
 {
     assert('is_string($name)');
     assert('is_string($value) || is_null($value)');
     $globalConfig = SimpleSAML_Configuration::getInstance();
     $params = array('lifetime' => 90 * 24 * 60 * 60, 'path' => $globalConfig->getBasePath(), 'httponly' => false);
     if (\SimpleSAML\Utils\HTTP::isHTTPS()) {
         // Enable secure cookie for https-requests
         $params['secure'] = true;
     } else {
         $params['secure'] = false;
     }
     \SimpleSAML\Utils\HTTP::setCookie($name, $value, $params, false);
 }
 /**
  * Set a session cookie.
  *
  * @param string      $name The name of the session cookie.
  * @param string|null $value The value of the cookie. Set to null to delete the cookie.
  * @param array|null  $params Additional params to use for the session cookie.
  */
 public function setCookie($name, $value, array $params = null)
 {
     assert('is_string($name)');
     assert('is_string($value) || is_null($value)');
     if ($params !== null) {
         $params = array_merge($this->getCookieParams(), $params);
     } else {
         $params = $this->getCookieParams();
     }
     \SimpleSAML\Utils\HTTP::setCookie($name, $value, $params);
 }
 *
 * The file extra/auth_memcookie.conf contains an example of how Auth Memcookie can be configured
 * to use SimpleSAMLphp.
 */
// load SimpleSAMLphp configuration
$ssp_cf = \SimpleSAML_Configuration::getInstance();
// load Auth MemCookie configuration
$amc_cf = AuthMemCookie::getInstance();
$sourceId = $amc_cf->getAuthSource();
$s = new SimpleSAML_Auth_Simple($sourceId);
// check if the user is authorized. We attempt to authenticate the user if not
$s->requireAuth();
// generate session id and save it in a cookie
$sessionID = Utils\Random::generateID();
$cookieName = $amc_cf->getCookieName();
\SimpleSAML\Utils\HTTP::setCookie($cookieName, $sessionID);
// generate the authentication information
$attributes = $s->getAttributes();
$authData = array();
// username
$usernameAttr = $amc_cf->getUsernameAttr();
if (!array_key_exists($usernameAttr, $attributes)) {
    throw new SimpleSAML_Error_Exception("The user doesn't have an attribute named '" . $usernameAttr . "'. This attribute is expected to contain the username.");
}
$authData['UserName'] = $attributes[$usernameAttr];
// groups
$groupsAttr = $amc_cf->getGroupsAttr();
if ($groupsAttr !== null) {
    if (!array_key_exists($groupsAttr, $attributes)) {
        throw new SimpleSAML_Error_Exception("The user doesn't have an attribute named '" . $groupsAttr . "'. This attribute is expected to contain the groups the user is a member of.");
    }
 /**
  * Set a session cookie.
  *
  * @param string $sessionName The name of the session.
  * @param string|null $sessionID The session ID to use. Set to null to delete the cookie.
  * @param array|null $cookieParams Additional parameters to use for the session cookie.
  *
  * @throws \SimpleSAML\Error\CannotSetCookie If we can't set the cookie.
  */
 public function setCookie($sessionName, $sessionID, array $cookieParams = null)
 {
     assert('is_string($sessionName)');
     assert('is_string($sessionID) || is_null($sessionID)');
     if ($cookieParams !== null) {
         $params = array_merge($this->getCookieParams(), $cookieParams);
     } else {
         $params = $this->getCookieParams();
     }
     \SimpleSAML\Utils\HTTP::setCookie($sessionName, $sessionID, $params, true);
 }
Example #16
0
 /**
  * @deprecated This method will be removed in SSP 2.0. Please use SimpleSAML\Utils\HTTP::setCookie() instead.
  */
 public static function setCookie($name, $value, array $params = null, $throw = true)
 {
     \SimpleSAML\Utils\HTTP::setCookie($name, $value, $params, $throw);
 }