<commit_before><?php
/**
 * SURFconext EngineBlock
 *
 * LICENSE
 *
 * Copyright 2011 SURFnet bv, The Netherlands
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 *
 * @category  SURFconext EngineBlock
 * @package
 * @copyright Copyright © 2010-2011 SURFnet SURFnet bv, The Netherlands (http://www.surfnet.nl)
 * @license   http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
 */

class EngineBlock_Corto_Filter_Command_SetPersistentId extends EngineBlock_Corto_Filter_Command_Abstract
{
    const PERSISTENT_NAMEID_SALT = 'COIN:';

    const SAML2_NAME_ID_FORMAT_UNSPECIFIED  = 'urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified';
    const SAML2_NAME_ID_FORMAT_TRANSIENT    = 'urn:oasis:names:tc:SAML:2.0:nameid-format:transient';
    const SAML2_NAME_ID_FORMAT_PERSISTENT   = 'urn:oasis:names:tc:SAML:2.0:nameid-format:persistent';

    private $SUPPORTED_NAMEID_FORMATS = array(
        self::SAML2_NAME_ID_FORMAT_UNSPECIFIED,
        self::SAML2_NAME_ID_FORMAT_TRANSIENT,
        self::SAML2_NAME_ID_FORMAT_PERSISTENT,
    );

    public function getResponse()
    {
        return $this->_response;
    }

    /**
     * This command may modify the response attributes
     *
     * @return array
     */
    public function getResponseAttributes()
    {
        return $this->_responseAttributes;
    }

    public function execute()
    {
        $nameIdFormat = $this->_getNameIdFormat($this->_request, $this->_spMetadata);

        if ($nameIdFormat === self::SAML2_NAME_ID_FORMAT_UNSPECIFIED) {
            $nameId = $this->_response['__']['IntendedNameId'];
        }
        else {
            $nameId = $this->_getPersistentNameId(
                $this->_collabPersonId,
                $this->_spMetadata['EntityId']
            );
        }

        // Adjust the NameID in the NEW response, set the collab:person uid
        $this->_response['saml:Assertion']['saml:Subject']['saml:NameID'] = array(
            '_Format' => $nameIdFormat,
            '__v'     => $nameId,
        );

        // Add the eduPersonTargetedId
        $this->_responseAttributes['urn:mace:dir:attribute-def:eduPersonTargetedID'] = array(
            array(
                "saml:NameID" => $this->_response['saml:Assertion']['saml:Subject']['saml:NameID'],
            )
        );
    }

    protected function _getNameIdFormat($request, $spEntityMetadata)<commit_msg>BACKLOG-525 Fix transient NAMEID support in EB<commit_after><?php
/**
 * SURFconext EngineBlock
 *
 * LICENSE
 *
 * Copyright 2011 SURFnet bv, The Netherlands
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 *
 * @category  SURFconext EngineBlock
 * @package
 * @copyright Copyright © 2010-2011 SURFnet SURFnet bv, The Netherlands (http://www.surfnet.nl)
 * @license   http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
 */

class EngineBlock_Corto_Filter_Command_SetPersistentId extends EngineBlock_Corto_Filter_Command_Abstract
{
    const PERSISTENT_NAMEID_SALT = 'COIN:';

    const SAML2_NAME_ID_FORMAT_UNSPECIFIED  = 'urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified';
    const SAML2_NAME_ID_FORMAT_TRANSIENT    = 'urn:oasis:names:tc:SAML:2.0:nameid-format:transient';
    const SAML2_NAME_ID_FORMAT_PERSISTENT   = 'urn:oasis:names:tc:SAML:2.0:nameid-format:persistent';

    private $SUPPORTED_NAMEID_FORMATS = array(
        self::SAML2_NAME_ID_FORMAT_UNSPECIFIED,
        self::SAML2_NAME_ID_FORMAT_TRANSIENT,
        self::SAML2_NAME_ID_FORMAT_PERSISTENT,
    );

    public function getResponse()
    {
        return $this->_response;
    }

    /**
     * This command may modify the response attributes
     *
     * @return array
     */
    public function getResponseAttributes()
    {
        return $this->_responseAttributes;
    }

    public function execute()
    {
        $nameIdFormat = $this->_getNameIdFormat($this->_request, $this->_spMetadata);

        if ($nameIdFormat === self::SAML2_NAME_ID_FORMAT_UNSPECIFIED) {
            $nameId = $this->_response['__']['IntendedNameId'];
        } else if ($nameIdFormat === self::SAML2_NAME_ID_FORMAT_TRANSIENT) {
            $nameId = $this->_getTransientNameId(
                $this->_spMetadata['EntityId'], $this->_idpMetadata['EntityId']
            );
        } else {
            $nameId = $this->_getPersistentNameId(
                $this->_collabPersonId,
                $this->_spMetadata['EntityId']
            );
        }

        // Adjust the NameID in the NEW response, set the collab:person uid
        $this->_response['saml:Assertion']['saml:Subject']['saml:NameID'] = array(
            '_Format' => $nameIdFormat,
            '__v'     => $nameId,
        );

        // Add the eduPersonTargetedId
        $this->_responseAttributes['urn:mace:dir:attribute-def:eduPersonTargetedID'] = array(
            array(
                "saml:NameID" => $this->_response['saml:Assertion']['saml:Subject']['saml:NameID'],
            )
        );
    }

    /**
     * Load transient Name ID from session or generate a new one
     *
     * @param type $spId
     * @param type $idpId
     * @return type
     */
    protected function _getTransientNameId($spId, $idpId)
    {
        if (!empty($_SESSION[$spId][$idpId])) {
            return $_SESSION[$spId][$idpId];
        }

        $nameId = sha1((string)mt_rand(0, mt_getrandmax()));

        // store to session
        $_SESSION[$spId][$idpId] = $nameId;

        return $nameId;
    }

    protected function _getNameIdFormat($request, $spEntityMetadata)
{'fraction_non_alphanumeric': 0.11254216498006746, 'fraction_numerical': 0.011039558417663294, 'mean_word_length': 2.984728161270617, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6344161', 'n_tokens_mistral': 2074, 'n_tokens_neox': 1974, 'n_words': 436}