Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

View File

@@ -0,0 +1,149 @@
<?php
/**
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* Jasig licenses this file to you 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.
*
* PHP Version 7
*
* @file CAS/ProxiedService/Abstract.php
* @category Authentication
* @package PhpCAS
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
*/
/**
* This class implements common methods for ProxiedService implementations included
* with phpCAS.
*
* @class CAS_ProxiedService_Abstract
* @category Authentication
* @package PhpCAS
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
*/
abstract class CAS_ProxiedService_Abstract
implements CAS_ProxiedService, CAS_ProxiedService_Testable
{
/**
* The proxy ticket that can be used when making service requests.
* @var string $_proxyTicket;
*/
private $_proxyTicket;
/**
* Register a proxy ticket with the Proxy that it can use when making requests.
*
* @param string $proxyTicket proxy ticket
*
* @return void
* @throws InvalidArgumentException If the $proxyTicket is invalid.
* @throws CAS_OutOfSequenceException If called after a proxy ticket has
* already been initialized/set.
*/
public function setProxyTicket ($proxyTicket)
{
if (empty($proxyTicket)) {
throw new CAS_InvalidArgumentException(
'Trying to initialize with an empty proxy ticket.'
);
}
if (!empty($this->_proxyTicket)) {
throw new CAS_OutOfSequenceException(
'Already initialized, cannot change the proxy ticket.'
);
}
$this->_proxyTicket = $proxyTicket;
}
/**
* Answer the proxy ticket to be used when making requests.
*
* @return string
* @throws CAS_OutOfSequenceException If called before a proxy ticket has
* already been initialized/set.
*/
protected function getProxyTicket ()
{
if (empty($this->_proxyTicket)) {
throw new CAS_OutOfSequenceException(
'No proxy ticket yet. Call $this->initializeProxyTicket() to aquire the proxy ticket.'
);
}
return $this->_proxyTicket;
}
/**
* @var CAS_Client $_casClient;
*/
private $_casClient;
/**
* Use a particular CAS_Client->initializeProxiedService() rather than the
* static phpCAS::initializeProxiedService().
*
* This method should not be called in standard operation, but is needed for unit
* testing.
*
* @param CAS_Client $casClient cas client
*
* @return void
* @throws CAS_OutOfSequenceException If called after a proxy ticket has
* already been initialized/set.
*/
public function setCasClient (CAS_Client $casClient)
{
if (!empty($this->_proxyTicket)) {
throw new CAS_OutOfSequenceException(
'Already initialized, cannot change the CAS_Client.'
);
}
$this->_casClient = $casClient;
}
/**
* Fetch our proxy ticket.
*
* Descendent classes should call this method once their service URL is available
* to initialize their proxy ticket.
*
* @return void
* @throws CAS_OutOfSequenceException If called after a proxy ticket has
* already been initialized.
*/
protected function initializeProxyTicket()
{
if (!empty($this->_proxyTicket)) {
throw new CAS_OutOfSequenceException(
'Already initialized, cannot initialize again.'
);
}
// Allow usage of a particular CAS_Client for unit testing.
if (empty($this->_casClient)) {
phpCAS::initializeProxiedService($this);
} else {
$this->_casClient->initializeProxiedService($this);
}
}
}
?>

View File

@@ -0,0 +1,46 @@
<?php
/**
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* Jasig licenses this file to you 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.
*
* PHP Version 7
*
* @file CAS/ProxiedService/Exception.php
* @category Authentication
* @package PhpCAS
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
*/
/**
* An Exception for problems communicating with a proxied service.
*
* @class CAS_ProxiedService_Exception
* @category Authentication
* @package PhpCAS
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
*/
class CAS_ProxiedService_Exception
extends Exception
implements CAS_Exception
{
}
?>

View File

@@ -0,0 +1,91 @@
<?php
/**
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* Jasig licenses this file to you 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.
*
* PHP Version 7
*
* @file CAS/ProxiedService/Http.php
* @category Authentication
* @package PhpCAS
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
*/
/**
* This interface defines methods that clients should use for configuring, sending,
* and receiving proxied HTTP requests.
*
* @class CAS_ProxiedService_Http
* @category Authentication
* @package PhpCAS
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
*/
interface CAS_ProxiedService_Http
{
/*********************************************************
* Configure the Request
*********************************************************/
/**
* Set the URL of the Request
*
* @param string $url Url to set
*
* @return void
* @throws CAS_OutOfSequenceException If called after the Request has been sent.
*/
public function setUrl ($url);
/*********************************************************
* 2. Send the Request
*********************************************************/
/**
* Perform the request.
*
* @return bool TRUE on success, FALSE on failure.
* @throws CAS_OutOfSequenceException If called multiple times.
*/
public function send ();
/*********************************************************
* 3. Access the response
*********************************************************/
/**
* Answer the headers of the response.
*
* @return array An array of header strings.
* @throws CAS_OutOfSequenceException If called before the Request has been sent.
*/
public function getResponseHeaders ();
/**
* Answer the body of response.
*
* @return string
* @throws CAS_OutOfSequenceException If called before the Request has been sent.
*/
public function getResponseBody ();
}
?>

View File

@@ -0,0 +1,360 @@
<?php
/**
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* Jasig licenses this file to you 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.
*
* PHP Version 7
*
* @file CAS/ProxiedService/Http/Abstract.php
* @category Authentication
* @package PhpCAS
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
*/
/**
* This class implements common methods for ProxiedService implementations included
* with phpCAS.
*
* @class CAS_ProxiedService_Http_Abstract
* @category Authentication
* @package PhpCAS
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
*/
abstract class CAS_ProxiedService_Http_Abstract extends
CAS_ProxiedService_Abstract implements CAS_ProxiedService_Http
{
/**
* The HTTP request mechanism talking to the target service.
*
* @var CAS_Request_RequestInterface $requestHandler
*/
protected $requestHandler;
/**
* The storage mechanism for cookies set by the target service.
*
* @var CAS_CookieJar $_cookieJar
*/
private $_cookieJar;
/**
* Constructor.
*
* @param CAS_Request_RequestInterface $requestHandler request handler object
* @param CAS_CookieJar $cookieJar cookieJar object
*
* @return void
*/
public function __construct(CAS_Request_RequestInterface $requestHandler,
CAS_CookieJar $cookieJar
) {
$this->requestHandler = $requestHandler;
$this->_cookieJar = $cookieJar;
}
/**
* The target service url.
* @var string $_url;
*/
private $_url;
/**
* Answer a service identifier (URL) for whom we should fetch a proxy ticket.
*
* @return string
* @throws Exception If no service url is available.
*/
public function getServiceUrl()
{
if (empty($this->_url)) {
throw new CAS_ProxiedService_Exception(
'No URL set via ' . get_class($this) . '->setUrl($url).'
);
}
return $this->_url;
}
/*********************************************************
* Configure the Request
*********************************************************/
/**
* Set the URL of the Request
*
* @param string $url url to set
*
* @return void
* @throws CAS_OutOfSequenceException If called after the Request has been sent.
*/
public function setUrl($url)
{
if ($this->hasBeenSent()) {
throw new CAS_OutOfSequenceException(
'Cannot set the URL, request already sent.'
);
}
if (!is_string($url)) {
throw new CAS_InvalidArgumentException('$url must be a string.');
}
$this->_url = $url;
}
/*********************************************************
* 2. Send the Request
*********************************************************/
/**
* Perform the request.
*
* @return void
* @throws CAS_OutOfSequenceException If called multiple times.
* @throws CAS_ProxyTicketException If there is a proxy-ticket failure.
* The code of the Exception will be one of:
* PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE
* PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE
* PHPCAS_SERVICE_PT_FAILURE
* @throws CAS_ProxiedService_Exception If there is a failure sending the
* request to the target service.
*/
public function send()
{
if ($this->hasBeenSent()) {
throw new CAS_OutOfSequenceException(
'Cannot send, request already sent.'
);
}
phpCAS::traceBegin();
// Get our proxy ticket and append it to our URL.
$this->initializeProxyTicket();
$url = $this->getServiceUrl();
if (strstr($url, '?') === false) {
$url = $url . '?ticket=' . $this->getProxyTicket();
} else {
$url = $url . '&ticket=' . $this->getProxyTicket();
}
try {
$this->makeRequest($url);
} catch (Exception $e) {
phpCAS::traceEnd();
throw $e;
}
}
/**
* Indicator of the number of requests (including redirects performed.
*
* @var int $_numRequests;
*/
private $_numRequests = 0;
/**
* The response headers.
*
* @var array $_responseHeaders;
*/
private $_responseHeaders = array();
/**
* The response status code.
*
* @var int $_responseStatusCode;
*/
private $_responseStatusCode = '';
/**
* The response headers.
*
* @var string $_responseBody;
*/
private $_responseBody = '';
/**
* Build and perform a request, following redirects
*
* @param string $url url for the request
*
* @return void
* @throws CAS_ProxyTicketException If there is a proxy-ticket failure.
* The code of the Exception will be one of:
* PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE
* PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE
* PHPCAS_SERVICE_PT_FAILURE
* @throws CAS_ProxiedService_Exception If there is a failure sending the
* request to the target service.
*/
protected function makeRequest($url)
{
// Verify that we are not in a redirect loop
$this->_numRequests++;
if ($this->_numRequests > 4) {
$message = 'Exceeded the maximum number of redirects (3) in proxied service request.';
phpCAS::trace($message);
throw new CAS_ProxiedService_Exception($message);
}
// Create a new request.
$request = clone $this->requestHandler;
$request->setUrl($url);
// Add any cookies to the request.
$request->addCookies($this->_cookieJar->getCookies($url));
// Add any other parts of the request needed by concrete classes
$this->populateRequest($request);
// Perform the request.
phpCAS::trace('Performing proxied service request to \'' . $url . '\'');
if (!$request->send()) {
$message = 'Could not perform proxied service request to URL`'
. $url . '\'. ' . $request->getErrorMessage();
phpCAS::trace($message);
throw new CAS_ProxiedService_Exception($message);
}
// Store any cookies from the response;
$this->_cookieJar->storeCookies($url, $request->getResponseHeaders());
// Follow any redirects
if ($redirectUrl = $this->getRedirectUrl($request->getResponseHeaders())
) {
phpCAS::trace('Found redirect:' . $redirectUrl);
$this->makeRequest($redirectUrl);
} else {
$this->_responseHeaders = $request->getResponseHeaders();
$this->_responseBody = $request->getResponseBody();
$this->_responseStatusCode = $request->getResponseStatusCode();
}
}
/**
* Add any other parts of the request needed by concrete classes
*
* @param CAS_Request_RequestInterface $request request interface object
*
* @return void
*/
abstract protected function populateRequest(
CAS_Request_RequestInterface $request
);
/**
* Answer a redirect URL if a redirect header is found, otherwise null.
*
* @param array $responseHeaders response header to extract a redirect from
*
* @return string|null
*/
protected function getRedirectUrl(array $responseHeaders)
{
// Check for the redirect after authentication
foreach ($responseHeaders as $header) {
if ( preg_match('/^(Location:|URI:)\s*([^\s]+.*)$/', $header, $matches)
) {
return trim(array_pop($matches));
}
}
return null;
}
/*********************************************************
* 3. Access the response
*********************************************************/
/**
* Answer true if our request has been sent yet.
*
* @return bool
*/
protected function hasBeenSent()
{
return ($this->_numRequests > 0);
}
/**
* Answer the headers of the response.
*
* @return array An array of header strings.
* @throws CAS_OutOfSequenceException If called before the Request has been sent.
*/
public function getResponseHeaders()
{
if (!$this->hasBeenSent()) {
throw new CAS_OutOfSequenceException(
'Cannot access response, request not sent yet.'
);
}
return $this->_responseHeaders;
}
/**
* Answer HTTP status code of the response
*
* @return int
* @throws CAS_OutOfSequenceException If called before the Request has been sent.
*/
public function getResponseStatusCode()
{
if (!$this->hasBeenSent()) {
throw new CAS_OutOfSequenceException(
'Cannot access response, request not sent yet.'
);
}
return $this->_responseStatusCode;
}
/**
* Answer the body of response.
*
* @return string
* @throws CAS_OutOfSequenceException If called before the Request has been sent.
*/
public function getResponseBody()
{
if (!$this->hasBeenSent()) {
throw new CAS_OutOfSequenceException(
'Cannot access response, request not sent yet.'
);
}
return $this->_responseBody;
}
/**
* Answer the cookies from the response. This may include cookies set during
* redirect responses.
*
* @return array An array containing cookies. E.g. array('name' => 'val');
*/
public function getCookies()
{
return $this->_cookieJar->getCookies($this->getServiceUrl());
}
}
?>

View File

@@ -0,0 +1,85 @@
<?php
/**
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* Jasig licenses this file to you 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.
*
* PHP Version 7
*
* @file CAS/ProxiedService/Http/Get.php
* @category Authentication
* @package PhpCAS
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
*/
/**
* This class is used to make proxied service requests via the HTTP GET method.
*
* Usage Example:
*
* try {
* $service = phpCAS::getProxiedService(PHPCAS_PROXIED_SERVICE_HTTP_GET);
* $service->setUrl('http://www.example.com/path/');
* $service->send();
* if ($service->getResponseStatusCode() == 200)
* return $service->getResponseBody();
* else
* // The service responded with an error code 404, 500, etc.
* throw new Exception('The service responded with an error.');
*
* } catch (CAS_ProxyTicketException $e) {
* if ($e->getCode() == PHPCAS_SERVICE_PT_FAILURE)
* return "Your login has timed out. You need to log in again.";
* else
* // Other proxy ticket errors are from bad request format
* // (shouldn't happen) or CAS server failure (unlikely)
* // so lets just stop if we hit those.
* throw $e;
* } catch (CAS_ProxiedService_Exception $e) {
* // Something prevented the service request from being sent or received.
* // We didn't even get a valid error response (404, 500, etc), so this
* // might be caused by a network error or a DNS resolution failure.
* // We could handle it in some way, but for now we will just stop.
* throw $e;
* }
*
* @class CAS_ProxiedService_Http_Get
* @category Authentication
* @package PhpCAS
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
*/
class CAS_ProxiedService_Http_Get
extends CAS_ProxiedService_Http_Abstract
{
/**
* Add any other parts of the request needed by concrete classes
*
* @param CAS_Request_RequestInterface $request request interface
*
* @return void
*/
protected function populateRequest (CAS_Request_RequestInterface $request)
{
// do nothing, since the URL has already been sent and that is our
// only data.
}
}
?>

View File

@@ -0,0 +1,152 @@
<?php
/**
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* Jasig licenses this file to you 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.
*
* PHP Version 7
*
* @file CAS/ProxiedService/Http/Post.php
* @category Authentication
* @package PhpCAS
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
*/
/**
* This class is used to make proxied service requests via the HTTP POST method.
*
* Usage Example:
*
* try {
* $service = phpCAS::getProxiedService(PHPCAS_PROXIED_SERVICE_HTTP_POST);
* $service->setUrl('http://www.example.com/path/');
* $service->setContentType('text/xml');
* $service->setBody('<?xml version="1.0"?'.'><methodCall><methodName>example.search</methodName></methodCall>');
* $service->send();
* if ($service->getResponseStatusCode() == 200)
* return $service->getResponseBody();
* else
* // The service responded with an error code 404, 500, etc.
* throw new Exception('The service responded with an error.');
*
* } catch (CAS_ProxyTicketException $e) {
* if ($e->getCode() == PHPCAS_SERVICE_PT_FAILURE)
* return "Your login has timed out. You need to log in again.";
* else
* // Other proxy ticket errors are from bad request format
* // (shouldn't happen) or CAS server failure (unlikely) so lets just
* // stop if we hit those.
* throw $e;
* } catch (CAS_ProxiedService_Exception $e) {
* // Something prevented the service request from being sent or received.
* // We didn't even get a valid error response (404, 500, etc), so this
* // might be caused by a network error or a DNS resolution failure.
* // We could handle it in some way, but for now we will just stop.
* throw $e;
* }
*
* @class CAS_ProxiedService_Http_Post
* @category Authentication
* @package PhpCAS
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
*/
class CAS_ProxiedService_Http_Post
extends CAS_ProxiedService_Http_Abstract
{
/**
* The content-type of this request
*
* @var string $_contentType
*/
private $_contentType;
/**
* The body of the this request
*
* @var string $_body
*/
private $_body;
/**
* Set the content type of this POST request.
*
* @param string $contentType content type
*
* @return void
* @throws CAS_OutOfSequenceException If called after the Request has been sent.
*/
public function setContentType ($contentType)
{
if ($this->hasBeenSent()) {
throw new CAS_OutOfSequenceException(
'Cannot set the content type, request already sent.'
);
}
$this->_contentType = $contentType;
}
/**
* Set the body of this POST request.
*
* @param string $body body to set
*
* @return void
* @throws CAS_OutOfSequenceException If called after the Request has been sent.
*/
public function setBody ($body)
{
if ($this->hasBeenSent()) {
throw new CAS_OutOfSequenceException(
'Cannot set the body, request already sent.'
);
}
$this->_body = $body;
}
/**
* Add any other parts of the request needed by concrete classes
*
* @param CAS_Request_RequestInterface $request request interface class
*
* @return void
*/
protected function populateRequest (CAS_Request_RequestInterface $request)
{
if (empty($this->_contentType) && !empty($this->_body)) {
throw new CAS_ProxiedService_Exception(
"If you pass a POST body, you must specify a content type via "
.get_class($this).'->setContentType($contentType).'
);
}
$request->makePost();
if (!empty($this->_body)) {
$request->addHeader('Content-Type: '.$this->_contentType);
$request->addHeader('Content-Length: '.strlen($this->_body));
$request->setPostBody($this->_body);
}
}
}
?>

View File

@@ -0,0 +1,281 @@
<?php
/**
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* Jasig licenses this file to you 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.
*
* PHP Version 7
*
* @file CAS/ProxiedService/Imap.php
* @category Authentication
* @package PhpCAS
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
*/
/**
* Provides access to a proxy-authenticated IMAP stream
*
* @class CAS_ProxiedService_Imap
* @category Authentication
* @package PhpCAS
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
*/
class CAS_ProxiedService_Imap
extends CAS_ProxiedService_Abstract
{
/**
* The username to send via imap_open.
*
* @var string $_username;
*/
private $_username;
/**
* Constructor.
*
* @param string $username Username
*
* @return void
*/
public function __construct ($username)
{
if (!is_string($username) || !strlen($username)) {
throw new CAS_InvalidArgumentException('Invalid username.');
}
$this->_username = $username;
}
/**
* The target service url.
* @var string $_url;
*/
private $_url;
/**
* Answer a service identifier (URL) for whom we should fetch a proxy ticket.
*
* @return string
* @throws Exception If no service url is available.
*/
public function getServiceUrl ()
{
if (empty($this->_url)) {
throw new CAS_ProxiedService_Exception(
'No URL set via '.get_class($this).'->getServiceUrl($url).'
);
}
return $this->_url;
}
/*********************************************************
* Configure the Stream
*********************************************************/
/**
* Set the URL of the service to pass to CAS for proxy-ticket retrieval.
*
* @param string $url Url to set
*
* @return void
* @throws CAS_OutOfSequenceException If called after the stream has been opened.
*/
public function setServiceUrl ($url)
{
if ($this->hasBeenOpened()) {
throw new CAS_OutOfSequenceException(
'Cannot set the URL, stream already opened.'
);
}
if (!is_string($url) || !strlen($url)) {
throw new CAS_InvalidArgumentException('Invalid url.');
}
$this->_url = $url;
}
/**
* The mailbox to open. See the $mailbox parameter of imap_open().
*
* @var string $_mailbox
*/
private $_mailbox;
/**
* Set the mailbox to open. See the $mailbox parameter of imap_open().
*
* @param string $mailbox Mailbox to set
*
* @return void
* @throws CAS_OutOfSequenceException If called after the stream has been opened.
*/
public function setMailbox ($mailbox)
{
if ($this->hasBeenOpened()) {
throw new CAS_OutOfSequenceException(
'Cannot set the mailbox, stream already opened.'
);
}
if (!is_string($mailbox) || !strlen($mailbox)) {
throw new CAS_InvalidArgumentException('Invalid mailbox.');
}
$this->_mailbox = $mailbox;
}
/**
* A bit mask of options to pass to imap_open() as the $options parameter.
*
* @var int $_options
*/
private $_options = null;
/**
* Set the options for opening the stream. See the $options parameter of
* imap_open().
*
* @param int $options Options for the stream
*
* @return void
* @throws CAS_OutOfSequenceException If called after the stream has been opened.
*/
public function setOptions ($options)
{
if ($this->hasBeenOpened()) {
throw new CAS_OutOfSequenceException(
'Cannot set options, stream already opened.'
);
}
if (!is_int($options)) {
throw new CAS_InvalidArgumentException('Invalid options.');
}
$this->_options = $options;
}
/*********************************************************
* 2. Open the stream
*********************************************************/
/**
* Open the IMAP stream (similar to imap_open()).
*
* @return resource Returns an IMAP stream on success
* @throws CAS_OutOfSequenceException If called multiple times.
* @throws CAS_ProxyTicketException If there is a proxy-ticket failure.
* The code of the Exception will be one of:
* PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE
* PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE
* PHPCAS_SERVICE_PT_FAILURE
* @throws CAS_ProxiedService_Exception If there is a failure sending the
* request to the target service.
*/
public function open ()
{
if ($this->hasBeenOpened()) {
throw new CAS_OutOfSequenceException('Stream already opened.');
}
if (empty($this->_mailbox)) {
throw new CAS_ProxiedService_Exception(
'You must specify a mailbox via '.get_class($this)
.'->setMailbox($mailbox)'
);
}
phpCAS::traceBegin();
// Get our proxy ticket and append it to our URL.
$this->initializeProxyTicket();
phpCAS::trace('opening IMAP mailbox `'.$this->_mailbox.'\'...');
$this->_stream = @imap_open(
$this->_mailbox, $this->_username, $this->getProxyTicket(),
$this->_options
);
if ($this->_stream) {
phpCAS::trace('ok');
} else {
phpCAS::trace('could not open mailbox');
// @todo add localization integration.
$message = 'IMAP Error: '.$this->_url.' '. var_export(imap_errors(), true);
phpCAS::trace($message);
throw new CAS_ProxiedService_Exception($message);
}
phpCAS::traceEnd();
return $this->_stream;
}
/**
* Answer true if our request has been sent yet.
*
* @return bool
*/
protected function hasBeenOpened ()
{
return !empty($this->_stream);
}
/*********************************************************
* 3. Access the result
*********************************************************/
/**
* The IMAP stream
*
* @var resource $_stream
*/
private $_stream;
/**
* Answer the IMAP stream
*
* @return resource
* @throws CAS_OutOfSequenceException if stream is not opened yet
*/
public function getStream ()
{
if (!$this->hasBeenOpened()) {
throw new CAS_OutOfSequenceException(
'Cannot access stream, not opened yet.'
);
}
return $this->_stream;
}
/**
* CAS_Client::serviceMail() needs to return the proxy ticket for some reason,
* so this method provides access to it.
*
* @return string
* @throws CAS_OutOfSequenceException If called before the stream has been
* opened.
*/
public function getImapProxyTicket ()
{
if (!$this->hasBeenOpened()) {
throw new CAS_OutOfSequenceException(
'Cannot access errors, stream not opened yet.'
);
}
return $this->getProxyTicket();
}
}
?>

View File

@@ -0,0 +1,75 @@
<?php
/**
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* Jasig licenses this file to you 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.
*
* PHP Version 7
*
* @file CAS/ProxiedService/Testabel.php
* @category Authentication
* @package PhpCAS
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
*/
/**
* This interface defines methods that allow proxy-authenticated service handlers
* to be tested in unit tests.
*
* Classes implementing this interface SHOULD store the CAS_Client passed and
* initialize themselves with that client rather than via the static phpCAS
* method. For example:
*
* / **
* * Fetch our proxy ticket.
* * /
* protected function initializeProxyTicket() {
* // Allow usage of a particular CAS_Client for unit testing.
* if (is_null($this->casClient))
* phpCAS::initializeProxiedService($this);
* else
* $this->casClient->initializeProxiedService($this);
* }
*
* @class CAS_ProxiedService_Testabel
* @category Authentication
* @package PhpCAS
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
*/
interface CAS_ProxiedService_Testable
{
/**
* Use a particular CAS_Client->initializeProxiedService() rather than the
* static phpCAS::initializeProxiedService().
*
* This method should not be called in standard operation, but is needed for unit
* testing.
*
* @param CAS_Client $casClient Cas client object
*
* @return void
* @throws CAS_OutOfSequenceException If called after a proxy ticket has
* already been initialized/set.
*/
public function setCasClient (CAS_Client $casClient);
}
?>