CAS_Client::retrievePT PHP 메소드

retrievePT() 공개 메소드

This method is used to retrieve PT's from the CAS server thanks to a PGT.
public retrievePT ( string $target_service, &$err_code, &$err_msg ) : a
$target_service string the service to ask for with the PT.
리턴 a Proxy Ticket, or false on error.
    public function retrievePT($target_service, &$err_code, &$err_msg)
    {
        // Argument validation
        if (gettype($target_service) != 'string') {
            throw new CAS_TypeMismatchException($target_service, '$target_service', 'string');
        }
        phpCAS::traceBegin();
        // by default, $err_msg is set empty and $pt to true. On error, $pt is
        // set to false and $err_msg to an error message. At the end, if $pt is false
        // and $error_msg is still empty, it is set to 'invalid response' (the most
        // commonly encountered error).
        $err_msg = '';
        // build the URL to retrieve the PT
        $cas_url = $this->getServerProxyURL() . '?targetService=' . urlencode($target_service) . '&pgt=' . $this->_getPGT();
        // open and read the URL
        if (!$this->_readURL($cas_url, $headers, $cas_response, $err_msg)) {
            phpCAS::trace('could not open URL \'' . $cas_url . '\' to validate (' . $err_msg . ')');
            $err_code = PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE;
            $err_msg = 'could not retrieve PT (no response from the CAS server)';
            phpCAS::traceEnd(false);
            return false;
        }
        $bad_response = false;
        if (!$bad_response) {
            // create new DOMDocument object
            $dom = new DOMDocument();
            // Fix possible whitspace problems
            $dom->preserveWhiteSpace = false;
            // read the response of the CAS server into a DOM object
            if (!$dom->loadXML($cas_response)) {
                phpCAS::trace('dom->loadXML() failed');
                // read failed
                $bad_response = true;
            }
        }
        if (!$bad_response) {
            // read the root node of the XML tree
            if (!($root = $dom->documentElement)) {
                phpCAS::trace('documentElement failed');
                // read failed
                $bad_response = true;
            }
        }
        if (!$bad_response) {
            // insure that tag name is 'serviceResponse'
            if ($root->localName != 'serviceResponse') {
                phpCAS::trace('localName failed');
                // bad root node
                $bad_response = true;
            }
        }
        if (!$bad_response) {
            // look for a proxySuccess tag
            if ($root->getElementsByTagName("proxySuccess")->length != 0) {
                $proxy_success_list = $root->getElementsByTagName("proxySuccess");
                // authentication succeded, look for a proxyTicket tag
                if ($proxy_success_list->item(0)->getElementsByTagName("proxyTicket")->length != 0) {
                    $err_code = PHPCAS_SERVICE_OK;
                    $err_msg = '';
                    $pt = trim($proxy_success_list->item(0)->getElementsByTagName("proxyTicket")->item(0)->nodeValue);
                    phpCAS::trace('original PT: ' . trim($pt));
                    phpCAS::traceEnd($pt);
                    return $pt;
                } else {
                    phpCAS::trace('<proxySuccess> was found, but not <proxyTicket>');
                }
            } else {
                if ($root->getElementsByTagName("proxyFailure")->length != 0) {
                    // look for a proxyFailure tag
                    $proxy_failure_list = $root->getElementsByTagName("proxyFailure");
                    // authentication failed, extract the error
                    $err_code = PHPCAS_SERVICE_PT_FAILURE;
                    $err_msg = 'PT retrieving failed (code=`' . $proxy_failure_list->item(0)->getAttribute('code') . '\', message=`' . trim($proxy_failure_list->item(0)->nodeValue) . '\')';
                    phpCAS::traceEnd(false);
                    return false;
                } else {
                    phpCAS::trace('neither <proxySuccess> nor <proxyFailure> found');
                }
            }
        }
        // at this step, we are sure that the response of the CAS server was
        // illformed
        $err_code = PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE;
        $err_msg = 'Invalid response from the CAS server (response=`' . $cas_response . '\')';
        phpCAS::traceEnd(false);
        return false;
    }

Usage Example

예제 #1
0
 /**
  * Test that we can at least retrieve a proxy-ticket for the service.
  *
  * @return void
  */
 public function testRetrievePT()
 {
     $pt = $this->object->retrievePT('http://www.service.com/my_webservice', $err_code, $err_msg);
     $this->assertEquals('PT-asdfas-dfasgww2323radf3', $pt);
 }
All Usage Examples Of CAS_Client::retrievePT
CAS_Client