Skip to content
Snippets Groups Projects
simplesamlphp-sp.md 8.3 KiB
Newer Older
Olav Morken's avatar
Olav Morken committed
SimpleSAMLphp Service Provider QuickStart
=========================================
Andreas Åkre Solberg's avatar
Andreas Åkre Solberg committed

<!-- 
	This file is written in Markdown syntax. 
	For more information about how to use the Markdown syntax, read here:
	http://daringfireball.net/projects/markdown/syntax
-->


This guide will describe how to configure SimpleSAMLphp as a service provider (SP). You should previously have installed SimpleSAMLphp as described in [the SimpleSAMLphp installation instructions](simplesamlphp-install).
Olav Morken's avatar
Olav Morken committed
Configuring the SP
------------------
The SP is configured by an entry in `config/authsources.php`.
This is a minimal `authsources.php` for a SP:
Olav Morken's avatar
Olav Morken committed
    <?php
Olav Morken's avatar
Olav Morken committed

        /* This is the name of this authentication source, and will be used to access it later. */
        'default-sp' => [
Olav Morken's avatar
Olav Morken committed
            'saml:SP',
For more information about additional options available for the SP, see the [`saml:SP` reference](./saml:sp).
If you want multiple Service Providers in the same site and installation, you can add more entries in the `authsources.php` configuration. If so remember to set the EntityID explicitly. Here is an example:
		'entityID' => 'https://sp1.example.org/',
		'entityID' => 'https://sp2.example.org/',
Olav Morken's avatar
Olav Morken committed
### Enabling a certificate for your Service Provider

Some Identity Providers / Federations may require that your Service Providers holds a certificate. If you enable a certificate for your Service Provider, it may be able to sign requests and response sent to the Identity Provider, as well as receiving encrypted responses.

Create a self-signed certificate in the `cert/` directory.

	cd cert
	openssl req -newkey rsa:3072 -new -x509 -days 3652 -nodes -out saml.crt -keyout saml.pem


Then edit your `authsources.php` entry, and add references to your certificate:

	'default-sp' => [
	    'saml:SP',
	    'privatekey' => 'saml.pem',
	    'certificate' => 'saml.crt',
Olav Morken's avatar
Olav Morken committed
Adding IdPs to the SP
---------------------
The service provider you are configuring needs to know about the identity providers you are going to connect to it. This is configured by metadata stored in `metadata/saml20-idp-remote.php` and `metadata/shib13-idp-remote.php`.
Olav Morken's avatar
Olav Morken committed
This is a minimal example of a `metadata/saml20-idp-remote.php` metadata file:
Olav Morken's avatar
Olav Morken committed
    <?php
    $metadata['https://example.com'] = [
        'SingleSignOnService'  => 'https://example.com/simplesaml/saml2/idp/SSOService.php',
        'SingleLogoutService'  => 'https://example.com/simplesaml/saml2/idp/SingleLogoutService.php',
        'certificate'          => 'example.pem',
`example.pem` under your `cert/` directory contains the certificate the identity provider uses for signing assertions.

For more information about available options in the idp-remote metadata files, see the [IdP remote reference](simplesamlphp-reference-idp-remote).
If you have the metadata of the remote IdP as an XML file, you can use the built-in XML to SimpleSAMLphp metadata converter, which by default is available as `/admin/metadata-converter.php` in your SimpleSAMLphp installation.
Olav Morken's avatar
Olav Morken committed
Note that the idp-remote file lists all IdPs you trust. You should remove all IdPs that you don't use.
Olav Morken's avatar
Olav Morken committed
Setting the default IdP
-----------------------
Olav Morken's avatar
Olav Morken committed
An option in the authentication source allows you to configure which IdP should be used.
This is the `idp` option.
Olav Morken's avatar
Olav Morken committed
    <?php
        'default-sp' => [
Olav Morken's avatar
Olav Morken committed
            'saml:SP',
Olav Morken's avatar
Olav Morken committed
            /*
             * The entity ID of the IdP this should SP should contact.
             * Can be NULL/unset, in which case the user will be shown a list of available IdPs.
             */
            'idp' => 'https://idp.example.com',
Andreas Åkre Solberg's avatar
Andreas Åkre Solberg committed


Exchange metadata with the IdP
------------------------------

In order to complete the connection between your SP and an IdP, you must exchange the metadata of your SP with the IdP.
The metadata of your SP can be found in the *Federation* tab of the web interface. Copy the SAML 2.0 XML Metadata document
automatically generated by SimpleSAMLphp and send it to the administrator of the IdP. You can also send them the dedicated
URL of your metadata, so that they can fetch it periodically and obtain automatically any changes that you may perform to
your SP.

You will also need to add the metadata of the IdP. Ask them to provide you with their metadata, and parse it using the *XML to
SimpleSAMLphp metadata converter* tool available also in the *Federation* tab of the web interface. Copy the resulting
parsed metadata and paste it with a text editor into the `metadata/saml20-idp-remote.php` file in your SimpleSAMLphp
directory.

If you intend to add your SP to a federation, the procedure for managing trust in federations differ, but the common part is
that you would need to provide the *SAML 2.0 metadata of your SP*, and register that with the federation administration.
You will probably be required too to consume the federation metadata periodically. Read more about
[automated metadata management](simplesamlphp-automated_metadata) to learn more about that. 
Olav Morken's avatar
Olav Morken committed
Test the SP
Andreas Åkre Solberg's avatar
Andreas Åkre Solberg committed
-----------------------------

After the metadata is configured on the IdP, you should be able to test the configuration.
The installation page of SimpleSAMLphp has a link to test authentication sources.
Olav Morken's avatar
Olav Morken committed
When you click the link, you should receive a list of authentication sources, including the one you have created for the SP.
Olav Morken's avatar
Olav Morken committed
After you click the link for that authentication source, you will be redirected to the IdP.
After entering your credentials, you should be redirected back to the test page.
The test page should contain a list of your attributes:
![Screenshot of the status page after a user has succesfully authenticated](resources/simplesamlphp-sp/screenshot-example.png)

For a better looking, more advanced Discovery Service with tabs and live search, you may want to use the `discopower` module
contained in the SimpleSAMLphp distribution.
Andreas Åkre Solberg's avatar
Andreas Åkre Solberg committed
Integrating authentication with your own application
----------------------------------------------------

The API is documented in [the SP API reference](simplesamlphp-sp-api).
Andreas Åkre Solberg's avatar
Andreas Åkre Solberg committed
For those web resources you want to protect, you must add a few
lines of PHP code:

-   Register the SimpleSAMLphp classes with the PHP autoloader.
Olav Morken's avatar
Olav Morken committed
    Require authentication of the user for those places it is required.
Olav Morken's avatar
Olav Morken committed
    Access the users attributes.
Olav Morken's avatar
Olav Morken committed
Example code:
We start off with loading a file which registers the SimpleSAMLphp classes with the autoloader.
Olav Morken's avatar
Olav Morken committed
    require_once('../../lib/_autoload.php');
Olav Morken's avatar
Olav Morken committed
We select our authentication source:
    $as = new \SimpleSAML\Auth\Simple('default-sp');
Olav Morken's avatar
Olav Morken committed
We then require authentication:
Olav Morken's avatar
Olav Morken committed
    $as->requireAuth();
Olav Morken's avatar
Olav Morken committed
And print the attributes:
Olav Morken's avatar
Olav Morken committed
    $attributes = $as->getAttributes();
Andreas Åkre Solberg's avatar
Andreas Åkre Solberg committed
    print_r($attributes);

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.
We can also request authentication with a specific IdP:

        'saml:idp' => 'https://idp.example.org/',

Other options are also available.
Take a look in the documentation for the [SP module](./saml:sp) for a list of all parameters.
If we are using PHP sessions in SimpleSAMLphp and in the application we are protecting, SimpleSAMLphp will close any
existing session when invoked for the first time, and its own session will prevail afterwards. If you want to restore
your own session after calling SimpleSAMLphp, you can do so by cleaning up the session like this:

    $session = \SimpleSAML\Session::getSessionFromRequest();
    $session->cleanup();

If you don't cleanup SimpleSAMLphp's session and try to use $_SESSION afterwards, you won't be using your own session
and all your data is likely to get lost or inaccessible.


Andreas Åkre Solberg's avatar
Andreas Åkre Solberg committed
Support
-------

If you need help to make this work, or want to discuss SimpleSAMLphp with other users of the software, you are fortunate: Around SimpleSAMLphp there is a great Open source community, and you are welcome to join! The forums are open for you to ask questions, contribute answers other further questions, request improvements or contribute with code or plugins of your own.
-  [SimpleSAMLphp homepage](https://simplesamlphp.org)
-  [List of all available SimpleSAMLphp documentation](https://simplesamlphp.org/docs/)
-  [Join the SimpleSAMLphp user's mailing list](https://simplesamlphp.org/lists)