PHP Class phpCAS

显示文件 Open project: jasig/phpcas Class Usage Examples

Public Methods

Method Description
addRebroadcastHeader ( String $header ) : void This method is used to add header parameters when rebroadcasting pgtIou/pgtId or logoutRequest.
addRebroadcastNode ( string $rebroadcastNodeUrl ) : void Add a pgtIou/pgtId and logoutRequest rebroadcast node.
allowProxyChain ( CAS_ProxyChain_Interface $proxy_chain ) : void If you want your service to be proxied you have to enable it (default disabled) and define an accepable list of proxies that are allowed to proxy your service.
checkAuthentication ( ) : true This method is called to check if the user is already authenticated locally or has a global cas session. A already existing cas session is determined by a cas gateway call.(cas login call without any interactive prompt)
client ( string $server_version, string $server_hostname, string $server_port, string $server_uri, boolean $changeSessionID = true ) : a phpCAS client initializer.
error ( string $msg ) : void This method is used by interface methods to print an error and where the function was originally called from.
forceAuthentication ( ) : boolean This method is called to force authentication if the user was not already authenticated. If the user is not authenticated, halt by redirecting to the CAS server.
getAttribute ( string $key ) : mixed Answer an attribute for the authenticated user.
getAttributes ( ) : array Answer attributes about the authenticated user.
getProxiedService ( string $type ) : CAS_ProxiedService Answer a proxy-authenticated service handler.
getProxies ( ) : array Answer an array of proxies that are sitting in front of this application.
getServerLoginURL ( ) : the This method returns the URL to be used to login.
getServerLogoutURL ( ) : the This method returns the URL to be used to login.
getServiceURL ( ) : string Get the URL that is set as the CAS service parameter.
getUser ( ) : string This method returns the CAS user's login name.
getVerbose ( ) : boot Show is verbose mode is on
getVersion ( ) : the This method returns the phpCAS version.
handleLogoutRequests ( boolean $check_client = true, array $allowed_clients = false ) : void Handle logout requests.
hasAttribute ( string $key ) : boolean Answer true if an attribute exists for the authenticated user.
hasAttributes ( ) : boolean Answer true if there are attributes for the authenticated user.
initializeProxiedService ( CAS_ProxiedService $proxiedService ) : void Initialize a proxied-service handler with the proxy-ticket it should use.
isAuthenticated ( ) : true This method is called to check if the user is authenticated (previously or by tickets given in the URL).
isInitialized ( ) : boolean Answer whether or not the client or proxy has been initialized
isSessionAuthenticated ( ) : boolean Checks whether authenticated based on $_SESSION. Useful to avoid server calls.
log ( string $str ) : void Logs a string in debug mode.
logout ( string $params = "" ) : void This method is used to logout from CAS.
logoutWithRedirectService ( string $service ) : void This method is used to logout from CAS. Halts by redirecting to the CAS server.
logoutWithRedirectServiceAndUrl ( string $service, string $url ) : void This method is used to logout from CAS. Halts by redirecting to the CAS server.
logoutWithUrl ( string $url ) : void This method is used to logout from CAS. Halts by redirecting to the CAS server.
proxy ( string $server_version, string $server_hostname, string $server_port, string $server_uri, boolean $changeSessionID = true ) : a phpCAS proxy initializer.
renewAuthentication ( ) : void This method is called to renew the authentication.
retrievePT ( string $target_service, &$err_code, &$err_msg ) : string Retrieve a Proxy Ticket from the CAS server.
serviceMail ( string $url, string $service, string $flags, &$err_code, &$err_msg, &$pt ) : object This method is used to access an IMAP/POP3/NNTP service.
serviceWeb ( string $url, &$err_code, &$output ) : boolean This method is used to access an HTTP[S] service.
setCacheTimesForAuthRecheck ( integer $n ) : void Set the times authentication will be cached before really accessing the CAS server in gateway mode: - -1: check only once, and then never again (until you pree login) - 0: always check - n: check every "n" time
setCasAttributeParserCallback ( string $function, array $additionalArgs = [] ) : void Set a callback function to be run when receiving CAS attributes
setCasClient ( CAS_Client $client ) : void For testing purposes, use this method to set the client to a test double
setCasServerCACert ( string $cert, boolean $validate_cn = true ) : void Set the certificate of the CAS server CA and if the CN should be properly verified.
setDebug ( string $filename = '' ) : void Set/unset debug mode
setExtraCurlOption ( string $key, string $value ) : void Change CURL options.
setFixedCallbackURL ( string $url = '' ) : void Set the fixed URL that will be used by the CAS server to transmit the PGT. When this method is not called, a phpCAS script uses its own URL for the callback.
setFixedServiceURL ( string $url ) : void Set the fixed URL that will be set as the CAS service parameter. When this method is not called, a phpCAS script uses its own URL.
setHTMLFooter ( string $footer ) : void This method sets the HTML footer used for all outputs.
setHTMLHeader ( string $header ) : void This method sets the HTML header used for all outputs.
setLang ( string $lang ) : void This method is used to set the language used by phpCAS.
setNoCasServerValidation ( ) : void Set no SSL validation for the CAS server.
setNoClearTicketsFromUrl ( ) : void Disable the removal of a CAS-Ticket from the URL when authenticating DISABLING POSES A SECURITY RISK: We normally remove the ticket by an additional redirect as a security precaution to prevent a ticket in the HTTP_REFERRER or be carried over in the URL parameter
setPGTStorage ( CAS_PGTStorage $storage ) : void This method can be used to set a custom PGT storage object.
setPGTStorageDb ( string $dsn_or_pdo, string $username = '', string $password = '', string $table = '', string $driver_options = null ) : void This method is used to tell phpCAS to store the response of the CAS server to PGT requests in a database.
setPGTStorageFile ( string $path = '' ) : void This method is used to tell phpCAS to store the response of the CAS server to PGT requests onto the filesystem.
setPostAuthenticateCallback ( string $function, array $additionalArgs = [] ) : void Set a callback function to be run when a user authenticates.
setServerLoginURL ( string $url = '' ) : void Set the login URL of the CAS server.
setServerLogoutURL ( string $url = '' ) : void Set the logout URL of the CAS server.
setServerProxyValidateURL ( string $url = '' ) : void Set the proxyValidate URL of the CAS server.
setServerSamlValidateURL ( string $url = '' ) : void Set the samlValidate URL of the CAS server.
setServerServiceValidateURL ( string $url = '' ) : void Set the serviceValidate URL of the CAS server.
setSingleSignoutCallback ( string $function, array $additionalArgs = [] ) : void Set a callback function to be run when a single-signout request is received. The callback function will be passed a $logoutTicket as its first parameter, followed by any $additionalArgs you pass. The $logoutTicket parameter is an opaque string that can be used to map a session-id to the logout request in order to support single-signout in applications that manage their own sessions (rather than letting phpCAS start and destroy the session).
setVerbose ( boolean $verbose ) : void Enable verbose errors messages in the website output This is a security relevant since internal status info may leak an may help an attacker. Default is therefore false
trace ( string $str ) : void This method is used to log something in debug mode.
traceBegin ( ) : void This method is used to indicate the start of the execution of a function in debug mode.
traceEnd ( string $res = '' ) : void This method is used to indicate the end of the execution of a function in debug mode.
traceExit ( ) : void This method is used to indicate the end of the execution of the program

Private Methods

Method Description
_validateClientExists ( ) : void Checks if a client already exists
_validateProxyExists ( ) : void Checks of a proxy client aready exists

Method Details

addRebroadcastHeader() public static method

This method is used to add header parameters when rebroadcasting pgtIou/pgtId or logoutRequest.
public static addRebroadcastHeader ( String $header ) : void
$header String Header to send when rebroadcasting.
return void

addRebroadcastNode() public static method

Add a pgtIou/pgtId and logoutRequest rebroadcast node.
public static addRebroadcastNode ( string $rebroadcastNodeUrl ) : void
$rebroadcastNodeUrl string The rebroadcast node URL. Can be hostname or IP.
return void

allowProxyChain() public static method

Add each allowed proxy definition object. For the normal CAS_ProxyChain class, the constructor takes an array of proxies to match. The list is in reverse just as seen from the service. Proxies have to be defined in reverse from the service to the user. If a user hits service A and gets proxied via B to service C the list of acceptable on C would be array(B,A). The definition of an individual proxy can be either a string or a regexp (preg_match is used) that will be matched against the proxy list supplied by the cas server when validating the proxy tickets. The strings are compared starting from the beginning and must fully match with the proxies in the list. Example: phpCAS::allowProxyChain(new CAS_ProxyChain(array( 'https://app.example.com/' ))); phpCAS::allowProxyChain(new CAS_ProxyChain(array( '/^https:\/\/app[0-9]\.example\.com\/rest\//', 'http://client.example.com/' ))); For quick testing or in certain production screnarios you might want to allow allow any other valid service to proxy your service. To do so, add the "Any" chain: phpcas::allowProxyChain(new CAS_ProxyChain_Any); THIS SETTING IS HOWEVER NOT RECOMMENDED FOR PRODUCTION AND HAS SECURITY IMPLICATIONS: YOU ARE ALLOWING ANY SERVICE TO ACT ON BEHALF OF A USER ON THIS SERVICE.
public static allowProxyChain ( CAS_ProxyChain_Interface $proxy_chain ) : void
$proxy_chain CAS_ProxyChain_Interface A proxy-chain that will be matched against the proxies requesting access
return void

checkAuthentication() public static method

This method is called to check if the user is already authenticated locally or has a global cas session. A already existing cas session is determined by a cas gateway call.(cas login call without any interactive prompt)
public static checkAuthentication ( ) : true
return true when the user is authenticated, false when a previous gateway login failed or the function will not return if the user is redirected to the cas server for a gateway login attempt

client() public static method

phpCAS client initializer.
public static client ( string $server_version, string $server_hostname, string $server_port, string $server_uri, boolean $changeSessionID = true ) : a
$server_version string the version of the CAS server
$server_hostname string the hostname of the CAS server
$server_port string the port the CAS server is running on
$server_uri string the URI the CAS server is responding on
$changeSessionID boolean Allow phpCAS to change the session_id (Single Sign Out/handleLogoutRequests is based on that change)
return a newly created CAS_Client object

error() public static method

This method is used by interface methods to print an error and where the function was originally called from.
public static error ( string $msg ) : void
$msg string the message to print
return void

forceAuthentication() public static method

This method is called to force authentication if the user was not already authenticated. If the user is not authenticated, halt by redirecting to the CAS server.
public static forceAuthentication ( ) : boolean
return boolean Authentication

getAttribute() public static method

Answer an attribute for the authenticated user.
public static getAttribute ( string $key ) : mixed
$key string attribute name
return mixed string for a single value or an array if multiple values exist.

getAttributes() public static method

Answer attributes about the authenticated user.
public static getAttributes ( ) : array
return array

getProxiedService() public static method

Answer a proxy-authenticated service handler.
public static getProxiedService ( string $type ) : CAS_ProxiedService
$type string The service type. One of PHPCAS_PROXIED_SERVICE_HTTP_GET; PHPCAS_PROXIED_SERVICE_HTTP_POST; PHPCAS_PROXIED_SERVICE_IMAP
return CAS_ProxiedService

getProxies() public static method

This method will only return a non-empty array if we have received and validated a Proxy Ticket.
Since: 6/25/09
public static getProxies ( ) : array
return array

getServerLoginURL() public static method

or phpCAS::isAuthenticated().
public static getServerLoginURL ( ) : the
return the login name of the authenticated user

getServerLogoutURL() public static method

or phpCAS::isAuthenticated().
public static getServerLogoutURL ( ) : the
return the login name of the authenticated user

getServiceURL() public static method

Get the URL that is set as the CAS service parameter.
public static getServiceURL ( ) : string
return string Service Url

getUser() public static method

This method returns the CAS user's login name.
public static getUser ( ) : string
return string the login name of the authenticated user

getVerbose() public static method

Show is verbose mode is on
public static getVerbose ( ) : boot
return boot verbose

getVersion() public static method

This method returns the phpCAS version.
public static getVersion ( ) : the
return the phpCAS version.

handleLogoutRequests() public static method

Handle logout requests.
public static handleLogoutRequests ( boolean $check_client = true, array $allowed_clients = false ) : void
$check_client boolean additional safety check
$allowed_clients array array of allowed clients
return void

hasAttribute() public static method

Answer true if an attribute exists for the authenticated user.
public static hasAttribute ( string $key ) : boolean
$key string attribute name
return boolean

hasAttributes() public static method

Answer true if there are attributes for the authenticated user.
public static hasAttributes ( ) : boolean
return boolean

initializeProxiedService() public static method

Initialize a proxied-service handler with the proxy-ticket it should use.
public static initializeProxiedService ( CAS_ProxiedService $proxiedService ) : void
$proxiedService CAS_ProxiedService Proxied Service Handler
return void

isAuthenticated() public static method

This method is called to check if the user is authenticated (previously or by tickets given in the URL).
public static isAuthenticated ( ) : true
return true when the user is authenticated.

isInitialized() public static method

Answer whether or not the client or proxy has been initialized
public static isInitialized ( ) : boolean
return boolean

isSessionAuthenticated() public static method

Checks whether authenticated based on $_SESSION. Useful to avoid server calls.
Since: 0.4.22 by Brendan Arnold
public static isSessionAuthenticated ( ) : boolean
return boolean true if authenticated, false otherwise.

log() public static method

Logs a string in debug mode.
public static log ( string $str ) : void
$str string the string to write
return void

logout() public static method

This method is used to logout from CAS.
public static logout ( string $params = "" ) : void
$params string an array that contains the optional url and service parameters that will be passed to the CAS server
return void

logoutWithRedirectService() public static method

This method is used to logout from CAS. Halts by redirecting to the CAS server.
public static logoutWithRedirectService ( string $service ) : void
$service string a URL that will be transmitted to the CAS server
return void

logoutWithRedirectServiceAndUrl() public static method

This method is used to logout from CAS. Halts by redirecting to the CAS server.
Deprecation: The url parameter has been removed from the CAS server as of version 3.3.5.1
public static logoutWithRedirectServiceAndUrl ( string $service, string $url ) : void
$service string a URL that will be transmitted to the CAS server
$url string a URL that will be transmitted to the CAS server
return void

logoutWithUrl() public static method

This method is used to logout from CAS. Halts by redirecting to the CAS server.
Deprecation: The url parameter has been removed from the CAS server as of version 3.3.5.1
public static logoutWithUrl ( string $url ) : void
$url string a URL that will be transmitted to the CAS server
return void

proxy() public static method

phpCAS proxy initializer.
public static proxy ( string $server_version, string $server_hostname, string $server_port, string $server_uri, boolean $changeSessionID = true ) : a
$server_version string the version of the CAS server
$server_hostname string the hostname of the CAS server
$server_port string the port the CAS server is running on
$server_uri string the URI the CAS server is responding on
$changeSessionID boolean Allow phpCAS to change the session_id (Single Sign Out/handleLogoutRequests is based on that change)
return a newly created CAS_Client object

renewAuthentication() public static method

This method is called to renew the authentication.
public static renewAuthentication ( ) : void
return void

retrievePT() public static method

Retrieve a Proxy Ticket from the CAS server.
public static retrievePT ( string $target_service, &$err_code, &$err_msg ) : string
$target_service string Url string of service to proxy
return string Proxy Ticket

serviceMail() public static method

This method is used to access an IMAP/POP3/NNTP service.
public static serviceMail ( string $url, string $service, string $flags, &$err_code, &$err_msg, &$pt ) : object
$url string a string giving the URL of the service, including the mailing box for IMAP URLs, as accepted by imap_open().
$service string a string giving for CAS retrieve Proxy ticket
$flags string options given to imap_open().
return object IMAP stream on success, false otherwise (in this later case, $err_code gives the reason why it failed and $err_msg contains an error message).

serviceWeb() public static method

This method is used to access an HTTP[S] service.
public static serviceWeb ( string $url, &$err_code, &$output ) : boolean
$url string the service to access.
return boolean true on success, false otherwise (in this later case, $err_code gives the reason why it failed and $output contains an error message).

setCacheTimesForAuthRecheck() public static method

Set the times authentication will be cached before really accessing the CAS server in gateway mode: - -1: check only once, and then never again (until you pree login) - 0: always check - n: check every "n" time
public static setCacheTimesForAuthRecheck ( integer $n ) : void
$n integer an integer.
return void

setCasAttributeParserCallback() public static method

The callback function will be passed an $success_elements payload of the response (\DOMElement) as its first parameter.
public static setCasAttributeParserCallback ( string $function, array $additionalArgs = [] ) : void
$function string Callback function
$additionalArgs array optional array of arguments
return void

setCasClient() public static method

For testing purposes, use this method to set the client to a test double
public static setCasClient ( CAS_Client $client ) : void
$client CAS_Client
return void

setCasServerCACert() public static method

Set the certificate of the CAS server CA and if the CN should be properly verified.
public static setCasServerCACert ( string $cert, boolean $validate_cn = true ) : void
$cert string CA certificate file name
$validate_cn boolean Validate CN in certificate (default true)
return void

setDebug() public static method

Set/unset debug mode
public static setDebug ( string $filename = '' ) : void
$filename string the name of the file used for logging, or false to stop debugging.
return void

setExtraCurlOption() public static method

CURL is used to connect through HTTPS to CAS server
public static setExtraCurlOption ( string $key, string $value ) : void
$key string the option key
$value string the value to set
return void

setFixedCallbackURL() public static method

Set the fixed URL that will be used by the CAS server to transmit the PGT. When this method is not called, a phpCAS script uses its own URL for the callback.
public static setFixedCallbackURL ( string $url = '' ) : void
$url string the URL
return void

setFixedServiceURL() public static method

Set the fixed URL that will be set as the CAS service parameter. When this method is not called, a phpCAS script uses its own URL.
public static setFixedServiceURL ( string $url ) : void
$url string the URL
return void

setHTMLFooter() public static method

This method sets the HTML footer used for all outputs.
public static setHTMLFooter ( string $footer ) : void
$footer string the HTML footer.
return void

setHTMLHeader() public static method

This method sets the HTML header used for all outputs.
public static setHTMLHeader ( string $header ) : void
$header string the HTML header.
return void

setLang() public static method

This method is used to set the language used by phpCAS.
public static setLang ( string $lang ) : void
$lang string string representing the language.
return void

setNoCasServerValidation() public static method

Set no SSL validation for the CAS server.
public static setNoCasServerValidation ( ) : void
return void

setNoClearTicketsFromUrl() public static method

Disable the removal of a CAS-Ticket from the URL when authenticating DISABLING POSES A SECURITY RISK: We normally remove the ticket by an additional redirect as a security precaution to prevent a ticket in the HTTP_REFERRER or be carried over in the URL parameter
public static setNoClearTicketsFromUrl ( ) : void
return void

setPGTStorage() public static method

This method can be used to set a custom PGT storage object.
public static setPGTStorage ( CAS_PGTStorage $storage ) : void
$storage CAS_PGTStorage a PGT storage object that inherits from the CAS_PGTStorage class
return void

setPGTStorageDb() public static method

This method is used to tell phpCAS to store the response of the CAS server to PGT requests in a database.
public static setPGTStorageDb ( string $dsn_or_pdo, string $username = '', string $password = '', string $table = '', string $driver_options = null ) : void
$dsn_or_pdo string a dsn string to use for creating a PDO object or a PDO object
$username string the username to use when connecting to the database
$password string the password to use when connecting to the database
$table string the table to use for storing and retrieving PGT's
$driver_options string any driver options to use when connecting to the database
return void

setPGTStorageFile() public static method

This method is used to tell phpCAS to store the response of the CAS server to PGT requests onto the filesystem.
public static setPGTStorageFile ( string $path = '' ) : void
$path string the path where the PGT's should be stored
return void

setPostAuthenticateCallback() public static method

The callback function will be passed a $logoutTicket as its first parameter, followed by any $additionalArgs you pass. The $logoutTicket parameter is an opaque string that can be used to map the session-id to logout request in order to support single-signout in applications that manage their own sessions (rather than letting phpCAS start the session). phpCAS::forceAuthentication() will always exit and forward client unless they are already authenticated. To perform an action at the moment the user logs in (such as registering an account, performing logging, etc), register a callback function here.
public static setPostAuthenticateCallback ( string $function, array $additionalArgs = [] ) : void
$function string Callback function
$additionalArgs array optional array of arguments
return void

setServerLoginURL() public static method

Set the login URL of the CAS server.
Since: 0.4.21 by Wyman Chan
public static setServerLoginURL ( string $url = '' ) : void
$url string the login URL
return void

setServerLogoutURL() public static method

Set the logout URL of the CAS server.
Since: 0.4.21 by Wyman Chan
public static setServerLogoutURL ( string $url = '' ) : void
$url string the logout URL
return void

setServerProxyValidateURL() public static method

Used for all CAS 2.0 validations
public static setServerProxyValidateURL ( string $url = '' ) : void
$url string the proxyValidate URL
return void

setServerSamlValidateURL() public static method

Set the samlValidate URL of the CAS server.
public static setServerSamlValidateURL ( string $url = '' ) : void
$url string the samlValidate URL
return void

setServerServiceValidateURL() public static method

Used only in CAS 1.0 validations
public static setServerServiceValidateURL ( string $url = '' ) : void
$url string the serviceValidate URL
return void

setSingleSignoutCallback() public static method

Set a callback function to be run when a single-signout request is received. The callback function will be passed a $logoutTicket as its first parameter, followed by any $additionalArgs you pass. The $logoutTicket parameter is an opaque string that can be used to map a session-id to the logout request in order to support single-signout in applications that manage their own sessions (rather than letting phpCAS start and destroy the session).
public static setSingleSignoutCallback ( string $function, array $additionalArgs = [] ) : void
$function string Callback function
$additionalArgs array optional array of arguments
return void

setVerbose() public static method

Enable verbose errors messages in the website output This is a security relevant since internal status info may leak an may help an attacker. Default is therefore false
public static setVerbose ( boolean $verbose ) : void
$verbose boolean enable verbose output
return void

trace() public static method

This method is used to log something in debug mode.
public static trace ( string $str ) : void
$str string string to log
return void

traceBegin() public static method

This method is used to indicate the start of the execution of a function in debug mode.
public static traceBegin ( ) : void
return void

traceEnd() public static method

This method is used to indicate the end of the execution of a function in debug mode.
public static traceEnd ( string $res = '' ) : void
$res string the result of the function
return void

traceExit() public static method

This method is used to indicate the end of the execution of the program
public static traceExit ( ) : void
return void