Download the PHP package newfrontiers/php-saml without Composer

On this page you can find all versions of the php package newfrontiers/php-saml. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-saml

This is a fork by newfrontiers. Check OneLogin's original library!

OneLogin's SAML PHP Toolkit

Build Status Coverage Status License

Add SAML support to your PHP software using this library. Forget those complicated libraries and use this open source library provided and supported by OneLogin Inc.

The 3.X branch is compatible with PHP > 7.1, so if you are using that PHP version, use it and not the 2.X or the master branch

Warning

Version 2.18.0 introduces the 'rejectUnsolicitedResponsesWithInResponseTo' setting parameter, by default disabled, that will allow invalidate unsolicited SAMLResponse. This version as well will reject SAMLResponse if requestId was provided to the validator but the SAMLResponse does not contain a InResponseTo attribute. And an additional setting parameter 'destinationStrictlyMatches', by default disabled, that will force that the Destination URL should strictly match to the address that process the SAMLResponse.

Version 2.17.1 updates xmlseclibs to 3.0.4 (CVE-2019-3465), but php-saml was not directly affected since it implements additional checks that prevent to exploit that vulnerability.

Version 2.17.0 sets strict mode active by default

Update php-saml to 2.15.0, this version includes a security patch related to XEE attacks

php-saml is not affected by 201803-01

Update php-saml to 2.10.4, this version includes a security patch related to signature validations on LogoutRequests/LogoutResponses

Update php-saml to 2.10.0, this version includes a security patch that contains extra validations that will prevent signature wrapping attacks. CVE-2016-1000253

php-saml < v2.10.0 is vulnerable and allows signature wrapping!

Security Guidelines

If you believe you have discovered a security vulnerability in this toolkit, please report it at https://www.onelogin.com/security with a description. We follow responsible disclosure guidelines, and will work with you to quickly find a resolution.

Why add SAML support to my software?

SAML is an XML-based standard for web browser single sign-on and is defined by the OASIS Security Services Technical Committee. The standard has been around since 2002, but lately it is becoming popular due its advantages:

General description

OneLogin's SAML PHP toolkit let you build a SP (Service Provider) over your PHP application and connect it to any IdP (Identity Provider).

Supports:

Key features:

Integrate your PHP toolkit at OneLogin using this guide: https://developers.onelogin.com/page/saml-toolkit-for-php

Installation

Dependencies

Since PHP 5.3 is officially unsupported we recommend you to use a newer PHP version.

Code

Option 1. clone the repository from github

git clone [email protected]:onelogin/php-saml.git

Option 2. Download from github

The toolkit is hosted on github. You can download it from:

Copy the core of the library inside the php application. (each application has its structure so take your time to locate the PHP SAML toolkit in the best place). See the "Guide to add SAML support to my app" to know how.

Take in mind that the compressed file only contains the main files. If you plan to play with the demos, use the Option 1.

Option 3. Composer

The toolkit supports composer. You can find the onelogin/php-saml package at https://packagist.org/packages/onelogin/php-saml

In order to import the saml toolkit to your current php project, execute

After installation has completed you will find at the vendor/ folder a new folder named onelogin and inside the php-saml. Make sure you are including the autoloader provided by composer. It can be found at vendor/autoload.php.

Important In this option, the x509 certs must be stored at vendor/onelogin/php-saml/certs and settings file stored at vendor/onelogin/php-saml.

Your settings are at risk of being deleted when updating packages using composer update or similar commands. So it is highly recommended that instead of using settings files, you pass the settings as an array directly to the constructor (explained later in this document). If you do not use this approach your settings are at risk of being deleted when updating packages using composer update or similar commands.

Compatibility

This 2.0 version has a new library. The toolkit is still compatible.

The old code that you used in order to add SAML support will continue working with minor changes. You only need to load the files of the lib/Saml folder. (notice that the compatibility.php file do that).

The old-demo folder contains code from an old app that uses the old version of the toolkit (v.1). Take a look.

Sometimes the names of the classes of the old code could be a bit different and if that is your case you must change them for OneLogin_Saml_Settings, OneLogin_Saml_Response, OneLogin_Saml_AuthRequest or OneLogin_Saml_Metadata.

We recommend that you migrate the old code to the new one to be able to use the new features that the new library Saml2 carries.

Namespaces

If you are using the library with a framework like Symfony that contains namespaces, remember that calls to the class must be done by adding a backslash (\) to the start, for example to use the static method getSelfURLNoQuery use:

\OneLogin_Saml2_Utils::getSelfURLNoQuery()

Security warning

In production, the strict parameter MUST be set as "true" and the signatureAlgorithm and digestAlgorithm under security must be set to something other than SHA1 (see https://shattered.io/ ). Otherwise your environment is not secure and will be exposed to attacks.

In production also we highly recommended to register on the settings the IdP certificate instead of using the fingerprint method. The fingerprint, is a hash, so at the end is open to a collision attack that can end on a signature validation bypass. Other SAML toolkits deprecated that mechanism, we maintain it for compatibility and also to be used on test environment.

Getting started

Knowing the toolkit

The new OneLogin SAML Toolkit contains different folders (certs, endpoints, extlib, lib, demo, etc.) and some files.

Let's start describing the folders:

certs/

SAML requires a x509 cert to sign and encrypt elements like NameID, Message, Assertion, Metadata.

If our environment requires sign or encrypt support, this folder may contain the x509 cert and the private key that the SP will use:

Or also we can provide those data in the setting file at the $settings['sp']['x509cert'] and the $settings['sp']['privateKey'].

Sometimes we could need a signature on the metadata published by the SP, in this case we could use the x509 cert previously mentioned or use a new x509 cert: metadata.crt and metadata.key.

Use sp_new.crt if you are in a key rollover process and you want to publish that x509 certificate on Service Provider metadata.

extlib/

This folder contains the 3rd party libraries that the toolkit uses. At the moment only uses the xmlseclibs (author Robert Richards, BSD Licensed) which handle the sign and the encryption of xml elements.

lib/

This folder contains the heart of the toolkit, the libraries:

doc/

This folder contains the API documentation of the toolkit.

endpoints/

The toolkit has three endpoints:

You can use the files provided by the toolkit or create your own endpoints files when adding SAML support to your applications. Take in mind that those endpoints files uses the setting file of the toolkit's base folder.

locale/

Locale folder contains some translations: en_US and es_ES as a proof of concept. Currently there are no translations but we will eventually localize the messages and support multiple languages.

Other important files

Miscellaneous

How it works

Settings

First of all we need to configure the toolkit. The SP's info, the IdP's info, and in some cases, configure advanced security issues like signatures and encryption.

There are two ways to provide the settings information:

There is a template file, settings_example.php, so you can make a copy of this file, rename and edit it.

In addition to the required settings data (IdP, SP), there is extra information that could be defined. In the same way that a template exists for the basic info, there is a template for that advanced info located at the base folder of the toolkit and named advanced_settings_example.php that you can copy and rename it as advanced_settings.php

The compression settings allow you to instruct whether or not the IdP can accept data that has been compressed using gzip ('requests' and 'responses'). But if we provide a $deflate boolean parameter to the getRequest or getResponse method it will have priority over the compression settings.

In the security section, you can set the way that the SP will handle the messages and assertions. Contact the admin of the IdP and ask him what the IdP expects, and decide what validations will handle the SP and what requirements the SP will have and communicate them to the IdP's admin too.

Once we know what kind of data could be configured, let's talk about the way settings are handled within the toolkit.

The settings files described (settings.php and advanced_settings.php) are loaded by the toolkit if no other array with settings info is provided in the constructor of the toolkit. Let's see some examples.

You can declare the $settingsInfo in the file that contains the constructor execution or locate them in any file and load the file in order to get the array available as we see in the following example:

How load the library

In order to use the toolkit library you need to import the _toolkit_loader.php file located on the base folder of the toolkit. You can load this file in this way:

After that line we will be able to use the classes (and their methods) of the toolkit (because the external and the Saml2 libraries files are loaded).

If you wrote the code of your SAML app for the version 1 of the PHP-SAML toolkit you will need to load the compatibility.php, file which loads the SAML library files, in addition to the the _toolkit_loader.php.

That SAML library uses the new classes and methods of the latest version of the toolkits but maintain the old classes, methods, and workflow of the old process to accomplish the same things.

We strongly recommend migrating your old code and use the new API of the new toolkit due there are a lot of new features that you can't handle with the old code.

Initiate SSO

In order to send an AuthNRequest to the IdP:

The AuthNRequest will be sent signed or unsigned based on the security info of the advanced_settings.php ('authnRequestsSigned').

The IdP will then return the SAML Response to the user's client. The client is then forwarded to the Attribute Consumer Service of the SP with this information. If we do not set a 'url' param in the login method and we are using the default ACS provided by the toolkit (endpoints/acs.php), then the ACS endpoint will redirect the user to the file that launched the SSO request.

We can set a 'returnTo' url to change the workflow and redirect the user to the other PHP file.

The login method can receive other six optional parameters:

If a match on the future SAMLResponse ID and the AuthNRequest ID to be sent is required, that AuthNRequest ID must to be extracted and saved.

The SP Endpoints

Related to the SP there are three important views: The metadata view, the ACS view and the SLS view. The toolkit provides examples of those views in the endpoints directory.

SP Metadata endpoints/metadata.php

This code will provide the XML metadata file of our SP, based on the info that we provided in the settings files.

The getSPMetadata will return the metadata signed or not based on the security info of the advanced_settings.php ('signMetadata').

Before the XML metadata is exposed, a check takes place to ensure that the info to be provided is valid.

Instead of use the Auth object, you can directly use

to get the settings object and with the true parameter we will avoid the IdP Settings validation.

Attribute Consumer Service(ACS) endpoints/acs.php

This code handles the SAML response that the IdP forwards to the SP through the user's client.

The SAML response is processed and then checked that there are no errors. It also verifies that the user is authenticated and stored the userdata in session.

At that point there are two possible alternatives:

  1. If no RelayState is provided, we could show the user data in this view or however we wanted.

  2. If RelayState is provided, a redirection takes place.

Notice that we saved the user data in the session before the redirection to have the user data available at the RelayState view.

The getAttributes method

In order to retrieve attributes we can use:

With this method we get all the user data provided by the IdP in the Assertion of the SAML Response.

If we execute we could get:

Each attribute name can be used as an index into $attributes to obtain the value. Every attribute value is an array - a single-valued attribute is an array of a single element.

The following code is equivalent:

Before trying to get an attribute, check that the user is authenticated. If the user isn't authenticated or if there were no attributes in the SAML assertion, an empty array will be returned. For example, if we call to getAttributes before a $auth->processResponse, the getAttributes() will return an empty array.

Single Logout Service (SLS) endpoints/sls.php

This code handles the Logout Request and the Logout Responses.

If the SLS endpoints receives a Logout Response, the response is validated and the session could be closed

If the SLS endpoints receives an Logout Request, the request is validated, the session is closed and a Logout Response is sent to the SLS endpoint of the IdP.

If you aren't using the default PHP session, or otherwise need a manual way to destroy the session, you can pass a callback method to the processSLO method as the fourth parameter

If we don't want that processSLO to destroy the session, pass a true parameter to the processSLO method

Initiate SLO

In order to send a Logout Request to the IdP:

Also there are eight optional parameters that can be set:

The Logout Request will be sent signed or unsigned based on the security info of the advanced_settings.php ('logoutRequestSigned').

The IdP will return the Logout Response through the user's client to the Single Logout Service of the SP. If we do not set a 'url' param in the logout method and are using the default SLS provided by the toolkit (endpoints/sls.php), then the SLS endpoint will redirect the user to the file that launched the SLO request.

We can set an 'returnTo' url to change the workflow and redirect the user to other php file.

A more complex logout with all the parameters:

If a match on the future LogoutResponse ID and the LogoutRequest ID to be sent is required, that LogoutRequest ID must to be extracted and stored.

Example of a view that initiates the SSO request and handles the response (is the acs target)

We can code a unique file that initiates the SSO process, handle the response, get the attributes, initiate the SLO and processes the logout response.

Note: Review the demo1 folder that contains that use case; in a later section we explain the demo1 use case further in detail.

Example (using Composer) that initiates the SSO request and handles the response (is the acs target)

Install package via composer:

Create an index.php:

URL-guessing methods

php-saml toolkit uses a bunch of methods in OneLogin_Saml2_Utils that try to guess the URL where the SAML messages are processed.

getSelfURLNoQuery and getSelfRoutedURLNoQuery are used to calculate the currentURL in order to validate SAML elements like Destination or Recipient.

When the PHP application is behind a proxy or a load balancer we can execute setProxyVars(true) and setSelfPort and isHTTPS will take care of the $_SERVER["HTTP_X_FORWARDED_PORT"] and $_SERVER['HTTP_X_FORWARDED_PROTO'] vars (otherwise they are ignored).

Also a developer can use setSelfProtocol, setSelfHost, setSelfPort and getBaseURLPath to define a specific value to be returned by isHTTPS, getSelfHost, getSelfPort and getBaseURLPath. And define a setBasePath to be used on the getSelfURL and getSelfRoutedURLNoQuery to replace the data extracted from $_SERVER["REQUEST_URI"].

At the settings the developer will be able to set a 'baseurl' parameter that automatically will use setBaseURL to set values for setSelfProtocol, setSelfHost, setSelfPort and setBaseURLPath.

Working behind load balancer

Is possible that asserting request URL and Destination attribute of SAML response fails when working behind load balancer with SSL offload.

You should be able to workaround this by configuring your server so that it is aware of the proxy and returns the original url when requested.

Or by using the method described on the previous section.

SP Key rollover

If you plan to update the SP x509cert and privateKey you can define the new x509cert as $settings['sp']['x509certNew'] and it will be published on the SP metadata so Identity Providers can read them and get ready for rollover.

IdP with multiple certificates

In some scenarios the IdP uses different certificates for signing/encryption, or is under key rollover phase and more than one certificate is published on IdP metadata.

In order to handle that the toolkit offers the $settings['idp']['x509certMulti'] parameter.

When that parameter is used, 'x509cert' and 'certFingerprint' values will be ignored by the toolkit.

The 'x509certMulti' is an array with 2 keys:

Replay attacks

In order to avoid replay attacks, you can store the ID of the SAML messages already processed, to avoid processing them twice. Since the Messages expires and will be invalidated due that fact, you don't need to store those IDs longer than the time frame that you currently accepting.

Get the ID of the last processed message/assertion with the getLastMessageId/getLastAssertionId methods of the Auth object.

Main classes and methods

Described below are the main classes and methods that can be invoked.

The Old Saml library

Lets start describing the classes and methods of the SAML library, an evolution of the old v.1 toolkit that is provided to keep the backward compability. Most of them use classes and methods of the new SAML2 library.

OneLogin_Saml_AuthRequest - AuthRequest.php

Has the protected attribute $auth, an OneLogin_Saml2_Auth object.

OneLogin_Saml_Response - Response.php
OneLogin_Saml_Settings - Settings.php

A simple class used to build the Setting object used in the v1.0 of the toolkit.

OneLogin_Saml_Metadata - Metadata.php
OneLogin_Saml_XmlSec - XmlSec.php

Auxiliary class that contains methods to validate the SAML Response: validateNumAssertions, validateTimestamps, isValid (which uses the other two previous methods and also validate the signature of SAML Response).

Saml2 library

Lets describe now the classes and methods of the SAML2 library.

OneLogin_Saml2_Auth - Auth.php

Main class of OneLogin PHP Toolkit

OneLogin_Saml2_AuthnRequest - AuthnRequest.php

SAML 2 Authentication Request class

OneLogin_Saml2_Response - Response.php

SAML 2 Authentication Response class

OneLogin_Saml2_LogoutRequest - LogoutRequest.php

SAML 2 Logout Request class

OneLogin_Saml2_LogoutResponse - LogoutResponse.php

SAML 2 Logout Response class

OneLogin_Saml2_Settings - Settings.php

Configuration of the OneLogin PHP Toolkit

OneLogin_Saml2_Metadata - Metadata.php

A class that contains functionality related to the metadata of the SP

OneLogin_Saml2_Utils - Utils.php

Auxiliary class that contains several methods

OneLogin_Saml2_IdPMetadataParser - IdPMetadataParser.php

Auxiliary class that contains several methods to retrieve and process IdP metadata

The class does not validate in any way the URL that is introduced on methods like parseRemoteXML in order to retrieve the remove XML. Usually is the same administrator that handles the Service Provider the ones that set the URL that should belong to a trusted third-party IdP. But there are other scenarios, like a SAAS app where the administrator of the app delegates on other administrators. In such case, extra protection should be taken in order to validate such URL inputs and avoid attacks like SSRF.

For more info, look at the source code; each method is documented and details about what it does and how to use it are provided. Make sure to also check the doc folder where HTML documentation about the classes and methods is provided for SAML and SAML2.

Demos included in the toolkit

The toolkit includes three demo apps to teach how use the toolkit, take a look on it.

Demos require that SP and IdP are well configured before test it.

Demo1

SP setup

The Onelogin's PHP Toolkit allows you to provide the settings info in two ways:

In this demo we provide the data in the second way, using a setting array named $settingsInfo. This array users the settings_example.php included as a template to create the settings.php settings and store it in the demo1/ folder. Configure the SP part and later review the metadata of the IdP and complete the IdP info.

If you check the code of the index.php file you will see that the settings.php file is loaded in order to get the $settingsInfo var to be used in order to initialize the Setting class.

Notice that in this demo, the setting.php file that could be defined at the base folder of the toolkit is ignored and the libs are loaded using the _toolkit_loader.php located at the base folder of the toolkit.

IdP setup

Once the SP is configured, the metadata of the SP is published at the metadata.php file. Configure the IdP based on that information.

How it works

  1. First time you access to index.php view, you can select to login and return to the same view or login and be redirected to the attrs.php view.

  2. When you click:

    2.1 in the first link, we access to (index.php?sso) an AuthNRequest is sent to the IdP, we authenticate at the IdP and then a Response is sent through the user's client to the SP, specifically the Assertion Consumer Service view: index.php?acs. Notice that a RelayState parameter is set to the url that initiated the process, the index.php view.

    2.2 in the second link we access to (attrs.php) have the same process described at 2.1 with the difference that as RelayState is set the attrs.php.

  3. The SAML Response is processed in the ACS (index.php?acs), if the Response is not valid, the process stops here and a message is shown. Otherwise we are redirected to the RelayState view. a) index.php or b) attrs.php.

  4. We are logged in the app and the user attributes are showed. At this point, we can test the single log out functionality.

  5. The single log out functionality could be tested by two ways.

    5.1 SLO Initiated by SP. Click on the "logout" link at the SP, after that a Logout Request is sent to the IdP, the session at the IdP is closed and replies through the client to the SP with a Logout Response (sent to the Single Logout Service endpoint). The SLS endpoint (index.php?sls) of the SP process the Logout Response and if is valid, close the user session of the local app. Notice that the SLO Workflow starts and ends at the SP.

    5.2 SLO Initiated by IdP. In this case, the action takes place on the IdP side, the logout process is initiated at the idP, sends a Logout Request to the SP (SLS endpoint, index.php?sls). The SLS endpoint of the SP process the Logout Request and if is valid, close the session of the user at the local app and send a Logout Response to the IdP (to the SLS endpoint of the IdP). The IdP receives the Logout Response, process it and close the session at of the IdP. Notice that the SLO Workflow starts and ends at the IdP.

Notice that all the SAML Requests and Responses are handled by a unique file, the index.php file and how GET parameters are used to know the action that must be done.

Demo2

SP setup

The Onelogin's PHP Toolkit allows you to provide the settings info in two ways:

The first is the case of the demo2 app. The setting.php file and the setting_extended.php file should be defined at the base folder of the toolkit. Review the setting_example.php and the advanced_settings_example.php to learn how to build them.

In this case as Attribute Consume Service and Single Logout Service we are going to use the files located in the endpoint folder (acs.php and sls.php).

IdP setup

Once the SP is configured, the metadata of the SP is published at the metadata.php file. Based on that info, configure the IdP.

How it works

In demo1, we saw how all the SAML Request and Responses were handler at an unique file, the index.php file. This demo1 uses high-level programming.

In demo2, we have several views: index.php, sso.php, slo.php, consume.php and metadata.php. As we said, we will use the endpoints that are defined in the toolkit (acs.php, sls.php of the endpoints folder). This demo2 uses low-level programming.

Notice that the SSO action can be initiated at index.php or sso.php.

The SAML workflow that take place is similar that the workflow defined in the demo1, only changes the targets.

  1. When you access index.php or sso.php for the first time, an AuthNRequest is sent to the IdP automatically, (as RelayState is sent the origin url). We authenticate at the IdP and then a Response is sent to the SP, to the ACS endpoint, in this case acs.php of the endpoints folder.

  2. The SAML Response is processed in the ACS, if the Response is not valid, the process stops here and a message is shown. Otherwise we are redirected to the RelayState view (sso.php or index.php). The sso.php detects if the user is logged and redirects to index.php, so we will be in the index.php at the end.

  3. We are logged into the app and the user attributes (if any) are shown. At this point, we can test the single log out functionality.

  4. The single log out functionality could be tested by two ways.

    4.1 SLO Initiated by SP. Click on the "logout" link at the SP, after that we are redirected to the slo.php view and there a Logout Request is sent to the IdP, the session at the IdP is closed and replies to the SP a Logout Response (sent to the Single Logout Service endpoint). In this case The SLS endpoint of the SP process the Logout Response and if is valid, close the user session of the local app. Notice that the SLO Workflow starts and ends at the SP.

    4.2 SLO Initiated by IdP. In this case, the action takes place on the IdP side, the logout process is initiated at the idP, sends a Logout Request to the SP (SLS endpoint sls.php of the endpoint folder). The SLS endpoint of the SP process the Logout Request and if is valid, close the session of the user at the local app and sends a Logout Response to the IdP (to the SLS endpoint of the IdP).The IdP receives the Logout Response, process it and close the session at of the IdP. Notice that the SLO Workflow starts and ends at the IdP.

Demo Old

SP setup

This demo uses the old style of the version 1 of the toolkit. An object of the class OneLogin_Saml_Settings must be provided to the constructor of the AuthRequest.

You will find an example_settings.php file at the demo-old's folder that could be used as a template for your settings.php file.

In that template, SAML settings are divided into two parts, the application specific (const_assertion_consumer_service_url, const_issuer, const_name_identifier_format) and the user/account specific idp_sso_target_url, x509certificate). You'll need to add your own code here to identify the user or user origin (e.g. by subdomain, ip_address etc.).

IdP setup

Once the SP is configured, the metadata of the SP is published at the metadata.php file. After that, configure the IdP based on that information.

How it works

At the metadata.php view is published the metadata of the SP.

The index.php file acts as an initiater for the SAML conversation if it should should be initiated by the application. This is called Service Provider Initiated SAML. The service provider creates a SAML Authentication Request and sends it to the identity provider (IdP).

The consume.php is the ACS endpoint. Receives the SAML assertion. After Response validation, the userdata and the nameID will be available, using getNameId() or getAttributes() we obtain them.

Since the version 1 of the php toolkit does not support SLO we don't show how handle SLO in this demo-old.


All versions of php-saml with dependencies

PHP Build Version
Package Version
Requires php Version >=7.3
robrichards/xmlseclibs Version >=3.1.1
ext-dom Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package newfrontiers/php-saml contains the following files

Loading the files please wait ....