<commit_before><?php


namespace Office365\Runtime\Auth;


use DOMDocument;
use DOMNode;
use DOMNodeList;
use DOMXPath;
use Exception;
use Office365\Runtime\Http\Requests;
use Office365\Runtime\Http\Response;
use RuntimeException;

class SamlTokenProvider extends BaseTokenProvider
{

    /**
     * External Security Token Service
     * @var string
     */
    private static $StsUrl = 'https://login.microsoftonline.com/extSTS.srf';

    /**
     * RST2 URL
     * @var string
     */
    private static $RST2Url = 'https://login.microsoftonline.com/rst2.srf';

    /**
     * To Get the STS authentication url if $StsUrl request fails.
     * @var string
     */
    private static $RealmUrlTemplate = 'https://login.microsoftonline.com/getuserrealm.srf?login={username}&xml=1';


    /**
     * Form Url to submit SAML token
     * @var string
     */
    private static $SignInPageUrl = '/_forms/default.aspx?wa=wsignin1.0';


    /**
     * Boolean to determine whether the system is using Federated STS or not.
     * @var
     */
    protected $useFederatedSTS;

    /**
     * Form Url to submit SAML token if Federated STS is set.
     * @var string
     */
    private static $IDCRLSVCPageUrl = '/_vti_bin/idcrl.svc/';


    /**
     * @var string
     */
    protected $authorityUrl;


    public function __construct($authorityUrl)
    {
        $this->authorityUrl = $authorityUrl;
        $this->useFederatedSTS = false;
    }


    /**
     * @param array $parameters
     * @return string
     * @throws Exception
     */
    public function acquireToken($parameters)
    {
        return $this->acquireSecurityToken($parameters['username'], $parameters['password']);
    }


    /**
     * Acquire SharePoint Online authentication cookies
     * @param mixed $token
     * @return array
     * @throws Exception
     */
    public function acquireAuthenticationCookies($token)
    {
        $hostInfo = parse_url($this->authorityUrl);
        $url =  $hostInfo['scheme'] . '://' . $hostInfo['host'] . self::$SignInPageUrl;
        if ($this->useFederatedSTS) {
            $url =  $hostInfo['scheme'] . '://' . $hostInfo['host'] . self::$IDCRLSVCPageUrl;

            $headers = array();
            $headers['User-Agent'] = '';
            $headers['X-IDCRL_ACCEPTED'] = 't';
            $headers['Authorization'] = 'BPOSIDCRL ' . $token;
            $headers['Content-Type'] = 'application/x-www-form-urlencoded';

            $response = Requests::head($url,$headers,true);
            return Requests::parseCookies($response->getContent());
        }
        else {
            $response = Requests::post($url,null,$token,true);
            return Requests::parseCookies($response->getContent());
        }
    }


    /**
     * Acquire the service token from STS
     *
     * @param string $username
     * @param string $password
     * @return string
     * @throws Exception
     */
    protected function acquireSecurityToken($username, $password)
    {
        $data = $this->prepareSecurityTokenRequest($username, $password, $this->authorityUrl);
        $response = Requests::post(self::$StsUrl, null, $data);

        try {
            return $this->processSecurityTokenResponse($response->getContent());
        } catch (Exception $e) {
            // Try to get the token with a federated authentication.
            $response = $this->acquireSecurityTokenFromFederatedSTS($username, $password);<commit_msg>Handle auth exception fix (#197)<commit_after><?php


namespace Office365\Runtime\Auth;


use DOMDocument;
use DOMNode;
use DOMNodeList;
use DOMXPath;
use Exception;
use Office365\Runtime\Http\Requests;
use Office365\Runtime\Http\Response;
use RuntimeException;

class SamlTokenProvider extends BaseTokenProvider
{

    /**
     * External Security Token Service
     * @var string
     */
    private static $StsUrl = 'https://login.microsoftonline.com/extSTS.srf';

    /**
     * RST2 URL
     * @var string
     */
    private static $RST2Url = 'https://login.microsoftonline.com/rst2.srf';

    /**
     * To Get the STS authentication url if $StsUrl request fails.
     * @var string
     */
    private static $RealmUrlTemplate = 'https://login.microsoftonline.com/getuserrealm.srf?login={username}&xml=1';


    /**
     * Form Url to submit SAML token
     * @var string
     */
    private static $SignInPageUrl = '/_forms/default.aspx?wa=wsignin1.0';


    /**
     * Boolean to determine whether the system is using Federated STS or not.
     * @var
     */
    protected $useFederatedSTS;

    /**
     * Form Url to submit SAML token if Federated STS is set.
     * @var string
     */
    private static $IDCRLSVCPageUrl = '/_vti_bin/idcrl.svc/';


    /**
     * @var string
     */
    protected $authorityUrl;


    public function __construct($authorityUrl)
    {
        $this->authorityUrl = $authorityUrl;
        $this->useFederatedSTS = false;
    }


    /**
     * @param array $parameters
     * @return string
     * @throws Exception
     */
    public function acquireToken($parameters)
    {
        return $this->acquireSecurityToken($parameters['username'], $parameters['password']);
    }


    /**
     * Acquire SharePoint Online authentication cookies
     * @param mixed $token
     * @return array
     * @throws Exception
     */
    public function acquireAuthenticationCookies($token)
    {
        $hostInfo = parse_url($this->authorityUrl);
        $url =  $hostInfo['scheme'] . '://' . $hostInfo['host'] . self::$SignInPageUrl;
        if ($this->useFederatedSTS) {
            $url =  $hostInfo['scheme'] . '://' . $hostInfo['host'] . self::$IDCRLSVCPageUrl;

            $headers = array();
            $headers['User-Agent'] = '';
            $headers['X-IDCRL_ACCEPTED'] = 't';
            $headers['Authorization'] = 'BPOSIDCRL ' . $token;
            $headers['Content-Type'] = 'application/x-www-form-urlencoded';

            $response = Requests::head($url,$headers,true);
            return Requests::parseCookies($response->getContent());
        }
        else {
            $response = Requests::post($url,null,$token,true);
            return Requests::parseCookies($response->getContent());
        }
    }


    /**
     * Acquire the service token from STS
     *
     * @param string $username
     * @param string $password
     * @return string
     * @throws Exception
     */
    protected function acquireSecurityToken($username, $password)
    {
        $data = $this->prepareSecurityTokenRequest($username, $password, $this->authorityUrl);
        $response = Requests::post(self::$StsUrl, null, $data);

        try {
            return $this->processSecurityTokenResponse($response->getContent());
        } catch (Exception $e) {
            // Try to get the token with a federated authentication.
            $response = $this->acquireSecurityTokenFromFederatedSTS($username, $password);
            if(is_null($response))
                throw $e;
{'fraction_non_alphanumeric': 0.1323571633649153, 'fraction_numerical': 0.004737295434969853, 'mean_word_length': 2.4939819458375125, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '2218747', 'n_tokens_mistral': 2095, 'n_tokens_neox': 2005, 'n_words': 440}