PHP Class OneLogin_Saml2_Utils

Defines several often used methods
Show file Open project: onelogin/php-saml Class Usage Examples

Public Methods

Method Description
addSign ( string | DomDocument $xml, string $key, string $cert, string $signAlgorithm = XMLSecurityKey::RSA_SHA1 ) : string Adds signature key and senders certificate to an element (Message or Assertion).
calculateX509Fingerprint ( string $x509cert, $alg = 'sha1' ) : null | string Calculates the fingerprint of a x509cert.
castKey ( XMLSecurityKey $key, string $algorithm, string $type = 'public' ) : XMLSecurityKey Converts a XMLSecurityKey to the correct algorithm.
decryptElement ( DOMElement $encryptedData, XMLSecurityKey $inputKey ) : DOMElement Decrypts an encrypted element.
deleteLocalSession ( ) Deletes the local session.
extractOriginalQueryParam ( string $name ) : string Extract a query param - as it was sent - from $_SERVER[QUERY_STRING]
formatCert ( string $cert, boolean $heads = true ) : string Returns a x509 cert (adding header & footer if required).
formatFingerPrint ( string $fingerprint ) : string Formates a fingerprint.
formatPrivateKey ( string $key, boolean $heads = true ) : string Returns a private key (adding header & footer if required).
generateNameId ( string $value, string $spnq, string $format, string | null $cert = null ) : string Generates a nameID.
generateUniqueID ( ) : string Generates an unique string (used for example as ID for assertions).
getBaseURLPath ( ) return string The baseurlpath to be used when constructing URLs
getExpireTime ( string $cacheDuration = null, string $validUntil = null ) : integer Compares 2 dates and returns the earliest.
getProxyVars ( ) return bool
getSelfHost ( ) : string Returns the current host.
getSelfPort ( ) : null | string
getSelfProtocol ( ) : string
getSelfRoutedURLNoQuery ( ) : string Returns the routed URL of the current host + current view.
getSelfURL ( ) : string Returns the URL of the current host + current view + query.
getSelfURLNoQuery ( ) : string Returns the URL of the current host + current view.
getSelfURLhost ( ) : string Returns the protocol + the current host + the port (if different than common ports).
getStatus ( DOMDocument $dom ) : array Gets Status from a Response.
get_string_between ( string $str, string $start, string $end ) : string Extracts a substring between 2 marks
isHTTPS ( ) : boolean Checks if https or http.
isSessionStarted ( ) : boolean Checks if the session is started or not.
loadXML ( DOMDocument $dom, string $xml ) : DOMDocument This function load an XML string in a save way.
parseDuration ( string $duration, integer | null $timestamp = null ) : integer | null Interprets a ISO8601 duration value relative to a given timestamp.
parseSAML2Time ( string $time ) : integer Converts a SAML2 timestamp on the form yyyy-mm-ddThh:mm:ss(\.s+)?Z to a UNIX timestamp. The sub-second part is ignored.
parseTime2SAML ( string $time ) : string Converts a UNIX timestamp to SAML2 timestamp on the form yyyy-mm-ddThh:mm:ss(\.s+)?Z.
query ( DOMDocument $dom, string $query, DomElement $context = null ) : DOMNodeList Extracts nodes from the DOMDocument.
redirect ( string $url, array $parameters = [], boolean $stay = false ) : string | null Executes a redirection to the provided url (or return the target url).
setBaseURL ( $baseurl )
setBaseURLPath ( $baseurlpath )
setProxyVars ( $proxyVars )
setSelfHost ( $host )
setSelfPort ( $port )
setSelfProtocol ( $protocol )
t ( string $msg, array | null $args = [] ) : string Translates any string. Accepts args
validateSign ( string | DomNode $xml, string | null $cert = null, string | null $fingerprint = null, string | null $fingerprintalg = 'sha1', string | null $xpath = null ) : boolean Validates a signature (Message or Assertion).
validateXML ( string | DOMDocument $xml, string $schema, boolean $debug = false ) : string | DOMDocument This function attempts to validate an XML string against the specified schema.

Protected Methods

Method Description
buildWithBaseURLPath ( $info ) : string Returns the part of the URL with the BaseURLPath.
getRawHost ( ) : string

Method Details

addSign() public static method

Adds signature key and senders certificate to an element (Message or Assertion).
public static addSign ( string | DomDocument $xml, string $key, string $cert, string $signAlgorithm = XMLSecurityKey::RSA_SHA1 ) : string
$xml string | DomDocument The element we should sign
$key string The private key
$cert string The public
$signAlgorithm string Signature algorithm method
return string

buildWithBaseURLPath() protected static method

Returns the part of the URL with the BaseURLPath.
protected static buildWithBaseURLPath ( $info ) : string
return string

calculateX509Fingerprint() public static method

Calculates the fingerprint of a x509cert.
public static calculateX509Fingerprint ( string $x509cert, $alg = 'sha1' ) : null | string
$x509cert string x509 cert
return null | string Formatted fingerprint

castKey() public static method

Converts a XMLSecurityKey to the correct algorithm.
public static castKey ( XMLSecurityKey $key, string $algorithm, string $type = 'public' ) : XMLSecurityKey
$key XMLSecurityKey The key.
$algorithm string The desired algorithm.
$type string Public or private key, defaults to public.
return XMLSecurityKey The new key.

decryptElement() public static method

Decrypts an encrypted element.
public static decryptElement ( DOMElement $encryptedData, XMLSecurityKey $inputKey ) : DOMElement
$encryptedData DOMElement The encrypted data.
$inputKey XMLSecurityKey The decryption key.
return DOMElement The decrypted element.

deleteLocalSession() public static method

Deletes the local session.
public static deleteLocalSession ( )

extractOriginalQueryParam() public static method

Extract a query param - as it was sent - from $_SERVER[QUERY_STRING]
public static extractOriginalQueryParam ( string $name ) : string
$name string The param to-be extracted
return string

formatCert() public static method

Returns a x509 cert (adding header & footer if required).
public static formatCert ( string $cert, boolean $heads = true ) : string
$cert string A x509 unformated cert
$heads boolean True if we want to include head and footer
return string $x509 Formatted cert

formatFingerPrint() public static method

Formates a fingerprint.
public static formatFingerPrint ( string $fingerprint ) : string
$fingerprint string fingerprint
return string Formatted fingerprint

formatPrivateKey() public static method

Returns a private key (adding header & footer if required).
public static formatPrivateKey ( string $key, boolean $heads = true ) : string
$key string A private key
$heads boolean True if we want to include head and footer
return string $rsaKey Formatted private key

generateNameId() public static method

Generates a nameID.
public static generateNameId ( string $value, string $spnq, string $format, string | null $cert = null ) : string
$value string fingerprint
$spnq string SP Name Qualifier
$format string SP Format
$cert string | null IdP Public cert to encrypt the nameID
return string $nameIDElement DOMElement | XMLSec nameID

generateUniqueID() public static method

Generates an unique string (used for example as ID for assertions).
public static generateUniqueID ( ) : string
return string A unique string

getBaseURLPath() public static method

return string The baseurlpath to be used when constructing URLs
public static getBaseURLPath ( )

getExpireTime() public static method

Compares 2 dates and returns the earliest.
public static getExpireTime ( string $cacheDuration = null, string $validUntil = null ) : integer
$cacheDuration string The duration, as a string.
$validUntil string The valid until date, as a string or as a timestamp
return integer $expireTime The expiration time.

getProxyVars() public static method

return bool
public static getProxyVars ( )

getRawHost() protected static method

protected static getRawHost ( ) : string
return string The raw host name

getSelfHost() public static method

Returns the current host.
public static getSelfHost ( ) : string
return string $currentHost The current host

getSelfPort() public static method

public static getSelfPort ( ) : null | string
return null | string The port number used for the request

getSelfProtocol() public static method

public static getSelfProtocol ( ) : string
return string http|https

getSelfRoutedURLNoQuery() public static method

Returns the routed URL of the current host + current view.
public static getSelfRoutedURLNoQuery ( ) : string
return string

getSelfURL() public static method

Returns the URL of the current host + current view + query.
public static getSelfURL ( ) : string
return string

getSelfURLNoQuery() public static method

Returns the URL of the current host + current view.
public static getSelfURLNoQuery ( ) : string
return string

getSelfURLhost() public static method

Returns the protocol + the current host + the port (if different than common ports).
public static getSelfURLhost ( ) : string
return string $url

getStatus() public static method

Gets Status from a Response.
public static getStatus ( DOMDocument $dom ) : array
$dom DOMDocument The Response as XML
return array $status The Status, an array with the code and a message.

get_string_between() public static method

Extracts a substring between 2 marks
public static get_string_between ( string $str, string $start, string $end ) : string
$str string The target string
$start string The initial mark
$end string The end mark
return string A substring or an empty string if is not able to find the marks or if there is no string between the marks

isHTTPS() public static method

Checks if https or http.
public static isHTTPS ( ) : boolean
return boolean $isHttps False if https is not active

isSessionStarted() public static method

Checks if the session is started or not.
public static isSessionStarted ( ) : boolean
return boolean true if the sessíon is started

loadXML() public static method

Prevent XEE/XXE Attacks
public static loadXML ( DOMDocument $dom, string $xml ) : DOMDocument
$dom DOMDocument The document where load the xml.
$xml string The XML string to be loaded.
return DOMDocument $dom The result of load the XML at the DomDocument

parseDuration() public static method

Interprets a ISO8601 duration value relative to a given timestamp.
public static parseDuration ( string $duration, integer | null $timestamp = null ) : integer | null
$duration string The duration, as a string.
$timestamp integer | null The unix timestamp we should apply the duration to. Optional, default to the current time.
return integer | null The new timestamp, after the duration is applied.

parseSAML2Time() public static method

Converts a SAML2 timestamp on the form yyyy-mm-ddThh:mm:ss(\.s+)?Z to a UNIX timestamp. The sub-second part is ignored.
public static parseSAML2Time ( string $time ) : integer
$time string The time we should convert (SAML Timestamp).
return integer $timestamp Converted to a unix timestamp.

parseTime2SAML() public static method

Converts a UNIX timestamp to SAML2 timestamp on the form yyyy-mm-ddThh:mm:ss(\.s+)?Z.
public static parseTime2SAML ( string $time ) : string
$time string The time we should convert (DateTime).
return string $timestamp SAML2 timestamp.

query() public static method

Extracts nodes from the DOMDocument.
public static query ( DOMDocument $dom, string $query, DomElement $context = null ) : DOMNodeList
$dom DOMDocument The DOMDocument
$query string Xpath Expresion
$context DomElement Context Node (DomElement)
return DOMNodeList The queried nodes

redirect() public static method

Executes a redirection to the provided url (or return the target url).
public static redirect ( string $url, array $parameters = [], boolean $stay = false ) : string | null
$url string The target url
$parameters array Extra parameters to be passed as part of the url
$stay boolean True if we want to stay (returns the url string) False to redirect
return string | null $url

setBaseURL() public static method

public static setBaseURL ( $baseurl )
$baseurl string The base url to be used when constructing URLs

setBaseURLPath() public static method

public static setBaseURLPath ( $baseurlpath )
$baseurlpath string The baseurl path to use when constructing URLs

setProxyVars() public static method

public static setProxyVars ( $proxyVars )
$proxyVars bool Whether to use `X-Forwarded-*` headers to determine port/domain/protocol

setSelfHost() public static method

public static setSelfHost ( $host )
$host string The host to use when constructing URLs

setSelfPort() public static method

public static setSelfPort ( $port )
$port int The port number to use when constructing URLs

setSelfProtocol() public static method

public static setSelfProtocol ( $protocol )
$protocol string The protocol to identify as using, usually http or https

t() public static method

Translates any string. Accepts args
public static t ( string $msg, array | null $args = [] ) : string
$msg string Message to be translated
$args array | null Arguments
return string $translatedMsg Translated text

validateSign() public static method

Validates a signature (Message or Assertion).
public static validateSign ( string | DomNode $xml, string | null $cert = null, string | null $fingerprint = null, string | null $fingerprintalg = 'sha1', string | null $xpath = null ) : boolean
$xml string | DomNode The element we should validate
$cert string | null The pubic cert
$fingerprint string | null The fingerprint of the public cert
$fingerprintalg string | null The algorithm used to get the fingerprint
$xpath string | null The xpath of the signed element
return boolean

validateXML() public static method

It will parse the string into a DOM document and validate this document against the schema.
public static validateXML ( string | DOMDocument $xml, string $schema, boolean $debug = false ) : string | DOMDocument
$xml string | DOMDocument The XML string or document which should be validated.
$schema string The schema filename which should be used.
$debug boolean To disable/enable the debug mode
return string | DOMDocument $dom string that explains the problem or the DOMDocument