phpSPO icon indicating copy to clipboard operation
phpSPO copied to clipboard

Authentication once per session

Open agamayaservers opened this issue 7 years ago • 1 comments

my application with phpSPO is now very slow each page is loading about five or more seconds is it possible to make it faster, for example, do not authenticate with each SPO request? usually every services give some token which can be then used for some time for multiple requests Is it possible with phpSPO library ?

agamayaservers avatar Jan 23 '18 21:01 agamayaservers

@agamayaservers here is a potential answer that works when tested that requires changes to the library so it isn't ideal:

$authCtx = new AuthenticationContext($Settings['Url']);
$token =  "token_text"; //store in a session variable?
$token = $authCtx->acquireTokenForUser($Settings['UserName'], $Settings['Password'], $token);

AuthenticationContext needs changing to:

public function acquireTokenForUser($username, $password, &$token)
    {
        $this->provider = new SamlTokenProvider($this->authorityUrl);

        if (isset($token)){
            if ($this->provider->acquireAuthenticationCookies($token))
                return $token;
        }

        $parameters = array(
            'username' => $username,
            'password' => $password
        );
        return $this->provider->acquireToken($parameters);
    }

SamlTokenProvider needs changing to:

public function acquireAuthenticationCookies($token)
    {
        $urlInfo = parse_url($this->authorityUrl);
        $url =  $urlInfo['scheme'] . '://' . $urlInfo['host'] . self::$SignInPageUrl;
        $response = Requests::post($url,null,$token,true);
        $cookies = Requests::parseCookies($response);
        if (isset($cookies['FedAuth'])) {
            $this->FedAuth = $cookies['FedAuth'];
            $this->rtFa = $cookies['rtFa'];
            return true;
        }
        return false;
    }

   public function acquireToken($parameters)
    {
        $token = $this->acquireSecurityToken($parameters['username'], $parameters['password']);
        $this->acquireAuthenticationCookies($token);
        return $token;
    }

@vgrem if this doesn't compromise anything in the library it may be worth looking at making a similar change?

Sorry yet another edit: this saves between 500ms and 1200ms in my tests. The slowest part is still list lookups.

uswross avatar May 17 '18 08:05 uswross