Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/class wrapper; fixes #2, #12, #13, #22 #26

Merged
merged 71 commits into from
Aug 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
7787f08
added base class wrapper files
Jul 20, 2018
b2a44e5
added default config and strategy pattern
Jul 20, 2018
769a0f0
implemented login
Jul 20, 2018
a5fe8ce
implemented logout
Jul 20, 2018
4095b43
added settings validation
Jul 20, 2018
61cdee1
code cleanup and added settings management class
Jul 20, 2018
3d40520
added automatic idp metadata configuration
Jul 20, 2018
7a20325
changed idp metadata storage to xml
Jul 20, 2018
439104f
added interface methods to generic PhpSaml class
Jul 20, 2018
5f92ac7
WIP readme
Jul 20, 2018
5362c0e
modifica getmetadata idp
Jul 26, 2018
30262b8
added sp cert settings
Jul 26, 2018
d00cbc5
added namespaces
Jul 26, 2018
0b9e85e
WIP: basic example
Jul 26, 2018
bec70c0
moving all settings to array format
Jul 26, 2018
88c69c1
Fix namespace and test settings generation
Jul 26, 2018
cf34f1a
improved settings generation
Jul 26, 2018
af5b721
fix variable references
Jul 26, 2018
7edd483
fixed xml parsing
Jul 26, 2018
991d6e1
WIP: sp metadata generation
Jul 26, 2018
adecf84
fixed SP metadata generation
Jul 27, 2018
1a806c6
fixed methods return data
Jul 27, 2018
98f3a20
removed useless dependencies and fix case sensitive names
Jul 27, 2018
c005a20
removed session in login
Jul 27, 2018
43f8f67
saving userdata in class instance
Jul 27, 2018
85a1502
fix login idp
Jul 27, 2018
0b0545d
added getAttributes method
Jul 27, 2018
b0f1249
WIP: login example
Jul 27, 2018
5633b9e
cleanup + added login response handler
Jul 27, 2018
770f580
added requestid for login validation
Jul 27, 2018
26de813
exclude and remove .vscode
Jul 30, 2018
73ae19b
clean up Makefile
Jul 30, 2018
30ce2b9
exclude and remove .DS_store files
Jul 30, 2018
3a5be35
unify src/config to src/Config
Jul 30, 2018
f66963a
use paths for php-saml v 3.x
Jul 30, 2018
cdf607a
add links to index, and assertion consuming service page
Jul 30, 2018
7d86a9d
implement logout
Jul 30, 2018
a4849c3
configure acs and slo
Jul 30, 2018
8af2459
fix login status
Jul 30, 2018
0d37f5c
Merge branch 'master' of https://github.com/simevo/spid-php2 into fea…
Jul 30, 2018
3340091
Merge remote-tracking branch 'origin/feature/class-wrapper' into feat…
Jul 30, 2018
138be5b
testing session test saving request id to session
Jul 30, 2018
1426c79
WIP saving request id to session
Jul 30, 2018
3ded4aa
fix esempio login funzionante
Jul 31, 2018
e8ef335
moved logout function to check auth
Jul 31, 2018
ee7a556
better example and check authentication status
Aug 2, 2018
9aa48ba
bugfix method name
Aug 2, 2018
095c9e9
bugfix
Aug 2, 2018
2aded59
test requesting attributes
Aug 2, 2018
fb6aaca
testing settings - request attributes
Aug 2, 2018
155e2be
fixed namespace
Aug 2, 2018
6750bc7
move the attrCS key under sp
Aug 2, 2018
f8120da
cleanup patched files
Aug 3, 2018
9ef1595
composer update
Aug 3, 2018
a836d92
refresh patches; patc php-saml to send optional attribute AttributeCo…
Aug 3, 2018
2c7a36e
aggiunta cartella idp e lista idp supportati
Aug 3, 2018
576bc41
fix exception
Aug 3, 2018
1b423bf
better init strategy
Aug 3, 2018
8bb62a0
made project folder independant
Aug 3, 2018
b8f7d7c
fix root folder path
Aug 3, 2018
de10e76
WIP : better handling of supported IDP list
Aug 3, 2018
cfd7963
metadata endpoint OK
Aug 9, 2018
5944028
PSR2
Aug 9, 2018
2466c99
cleanup and complete round-trip
Aug 10, 2018
d3ce513
make patches generic
Aug 10, 2018
e1152a9
unpack atttributes if they are sent nack as arrays
Aug 10, 2018
ae88827
document settings and get rid of spBaseUrl
Aug 10, 2018
2c3448f
polish example
Aug 10, 2018
ea7ef30
we have no scripts yet
Aug 10, 2018
7292687
rename namespace and classes; move SpInterface.php to src/Interfaces
Aug 10, 2018
10df3f7
move to Italia\Spid2 namespace; move idp_metadata inside example; bru…
Aug 10, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
sp.key
sp.crt
vendor
tmp
www/settings.php
www2/settings.php
config.yaml
.directory
.vscode
.DS_Store
AuthnRequest.patched
LogoutRequest.patched
vendor
example/sp.key
example/sp.crt
example/idp_metadata/*.xml
10 changes: 7 additions & 3 deletions AuthnRequest.diff
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
56,57c56
72,73c72
< Format="{$nameIDPolicyFormat}"
< AllowCreate="true" />
---
> Format="{$nameIDPolicyFormat}" />
130c129
143,145c142,144
< ProtocolBinding="{$spData['assertionConsumerService']['binding']}"
< AssertionConsumerServiceURL="{$acsUrl}">
< <saml:Issuer>{$spEntityId}</saml:Issuer>
---
> <saml:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity" NameQualifier="http://sp2.simevo.com:8000">{$spEntityId}</saml:Issuer>
> AssertionConsumerServiceIndex="1"
> AttributeConsumingServiceIndex="1">
> <saml:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity" NameQualifier="{$spEntityId}">{$spEntityId}</saml:Issuer>
4 changes: 2 additions & 2 deletions LogoutRequest.diff
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
107c107
128c128
< <saml:Issuer>{$spEntityId}</saml:Issuer>
---
> <saml:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity" NameQualifier="http://sp2.simevo.com:8000">{$spEntityId}</saml:Issuer>
> <saml:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity" NameQualifier="{$spEntityId}">{$spEntityId}</saml:Issuer>
29 changes: 14 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
all: sp.key AuthnRequest.patched LogoutRequest.patched
# clean up twig cache
rm -rf tmp
mkdir -p tmp
./bin/configure.php > www/settings.php
cp www/settings.php www2/settings.php
all: example/sp.key AuthnRequest.patched LogoutRequest.patched

AuthnRequest.patched: TO_PATCH:=vendor/onelogin/php-saml/src/Saml2/AuthnRequest.php
AuthnRequest.patched: AuthnRequest.diff
if [ -e $@ ]; then patch -R "${TO_PATCH}" $@; fi
patch -N "${TO_PATCH}" $<
cp $< $@
if [ -e $@ ]; then patch -R vendor/onelogin/php-saml/src/Saml2/AuthnRequest.php $@; fi
patch -N vendor/onelogin/php-saml/src/Saml2/AuthnRequest.php $<
cp AuthnRequest.diff $@

LogoutRequest.patched: TO_PATCH=vendor/onelogin/php-saml/src/Saml2/LogoutRequest.php
LogoutRequest.patched: LogoutRequest.diff
if [ -e $@ ]; then patch -R "$(TO_PATCH)" $@; fi
patch -N "${TO_PATCH}" $<
cp $< $@
if [ -e $@ ]; then patch -R vendor/onelogin/php-saml/src/Saml2/LogoutRequest.php $@; fi
patch -N vendor/onelogin/php-saml/src/Saml2/LogoutRequest.php $<
cp LogoutRequest.diff $@

sp.key:
openssl req -x509 -nodes -sha256 -days 365 -newkey rsa:2048 -subj "/C=IT/ST=Italy/L=Rome/O=testenv2/CN=localhost" -keyout sp.key -out sp.crt
example/sp.key:
openssl req -x509 -nodes -sha256 -days 365 -newkey rsa:2048 -subj "/C=IT/ST=Italy/L=Rome/O=testenv2/CN=localhost" -keyout example/sp.key -out example/sp.crt

clean:
rm -rf tmp vendor www/settings.php AuthnRequest.patched LogoutRequest.patched
rm -rf vendor
rm -f AuthnRequest.patched
rm -f LogoutRequest.patched
rm -f example/idp_metadata/*.xml
rm -f example/sp.crt example/sp.key
175 changes: 126 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
<img src="https://github.com/italia/spid-graphics/blob/master/spid-logos/spid-logo-b-lb.png" alt="SPID" data-canonical-src="https://github.com/italia/spid-graphics/blob/master/spid-logos/spid-logo-b-lb.png" width="500" height="98" />

[![Join the #spid-perl channel](https://img.shields.io/badge/Slack%20channel-%23spid--perl-blue.svg?logo=slack)](https://developersitalia.slack.com/messages/C7ESTMQDQ)
[![Get invited](https://slack.developers.italia.it/badge.svg)](https://slack.developers.italia.it/)
[![SPID on forum.italia.it](https://img.shields.io/badge/Forum-SPID-blue.svg)](https://forum.italia.it/c/spid)

> ⚠️ **WORK IN PROGRESS (but should be useable)** ⚠️

# spid-php2
PHP package for SPID authentication based on [php-saml](https://github.com/onelogin/php-saml).

Software Development Kit (SDK) for easy SPID SSO integration based on [php-saml](https://github.com/onelogin/php-saml).
This PHP package is aimed at implementing SPID **Service Providers**. [SPID](https://www.spid.gov.it/) is the Italian digital identity system, which enables citizens to access all public services with a single set of credentials. This package provides a layer of abstraction over the SAML protocol by exposing just the subset required in order to implement SPID authentication in a web application.

This component acts as a SPID SP (Service Provider) and logs you in via an external IDP (IDentity Provider). It does not support Attribute Authority.
Features:
- **routing-agnostic**, can be integrated in any web framework / CMS
- **sessionless** (apart from a short-lived internal session used to store the request ID and IdP name until the IdP responds)
- does not currently support Attribute Authority (AA).

Alternative SDK: [spid-php](https://github.com/italia/spid-php) based on [SimpleSAMLphp](https://simplesamlphp.org/).
Alternatives for PHP:
- [spid-php](https://github.com/italia/spid-php) based on [SimpleSAMLphp](https://simplesamlphp.org/)
- [spid-php3](https://github.com/simevo/spid-php3), a lean implementation that does not rely on external SAML packages

## Features
Alternatives for other languages:
- [spid-perl](https://github.com/italia/spid-perl)
- [spid-ruby](https://github.com/italia/spid-ruby)

## Compliance

|<img src="https://github.com/italia/spid-graphics/blob/master/spid-logos/spid-logo-c-lb.png?raw=true" width="100" /><br />_Compliance with [SPID regulations](http://www.agid.gov.it/sites/default/files/circolari/spid-regole_tecniche_v1.pdf) (for Service Providers)_|status (! = TODO)|comments|
|:---|:---|:---|
Expand Down Expand Up @@ -73,84 +91,143 @@ Alternative SDK: [spid-php](https://github.com/italia/spid-php) based on [Simple
|generation of AttributeQuery XML||Attribute Authority is unsupported|
|SOAP binding (client)||Attribute Authority is unsupported|

## Prerequisites
## Repository layout

Tested on Debian 10.x buster with PHP 7.2.
* [bin/](bin/) auxiliary scripts
* [example/](example/) contains a demo application
* [src/](src/) contains the implementation
* [test/](test/) will contain the unit tests

Perform these steps to install the prerequisites:
```
## Getting Started

Tested on Debian 9.5 (stretch, current stable) and 10 (buster, current unstable) with PHP 7-0-7.2.

### Prerequisites

```sh
sudo apt install composer make openssl php-curl php-zip php-xml
```
if you have PHP <= 7.1 (i.e. Debian 9.4 stretch or earlier), then you also need:
```
apt install php-mcrypt
```

Then install PHP dependencies; if you have PHP 7.2 (i.e. Debian 10.x buster):
### Configuring and Installing

Before using this package, you must:

1. Install prerequisites with composer

2. Download and verify the Identity Provider (IdP) metadata files; it is advised to place them in a separate directory, for example [example/idp_metadata/](example/idp_metadata/). A convenience tool is provided for this purpose: [bin/download_idp_metadata.php](bin/download_idp_metadata.php).

3. Generate key and certificate for the Service Provider (SP) and patch the php-saml package to comply with the SPID standard. To do that, you can use the provided [Makefile](Makefile).

All steps can be performed with:
```sh
composer install --no-dev
pushd example && ../bin/download_idp_metadata.php && popd
make
```
composer install

**NOTE**: during testing, it is highly adviced to use the test Identity Provider [spid-testenv2](https://github.com/italia/spid-testenv2).

### Usage

All classes provided by this package reside in the `Italia\Spid2` namespace.

Load them using the composer-generated autoloader:
```php
require_once(__DIR__ . "/../vendor/autoload.php");
```
if you have PHP <= 7.1 (i.e. Debian 9.4 stretch or earlier), then use the v2.x branch of php-saml:

The main class is `Italia\Spid2\Sp` (service provider), sample instantiation:

```php
$base = "http://localhost:8000";
$settings = [
'spEntityId' => $base,
'spAcsUrl' => $base . "/acs.php",
'spSloUrl' => $base . "/logout.php",
'spKeyFile' => "./sp.key",
'spCrtFile' => "./sp.crt",
'idpMetadataFolderPath' => $home . "/idp_metadata",
'idpList' => array(
'testenv2'
)
];
$sp = new Italia\Spid2\Sp($settings);
```
rm composer.*
composer require onelogin/php-saml
composer require twig/twig
composer require symfony/yaml

The service provider is now ready for use, as in:
```php
$idp_name = 'idp_1';
$return_to = 'https://example.com/return_to_url';
$spid_level = 1;
$sp->login($idp_name, $return_to, $spid_level);
$attributes = $sp->getAttributes();
var_dump($attributes);
$sp->logout();
```

## Demo
### Example

The demo is based on php-saml demo1.
A basic demo application is provided in the [example/](example/) directory.

To set it up and run it:
To use:

1. copy `config.yaml.example` to `config.yaml` and customize it as required (you should at least set `idp_metadata_url` to match your IDP metadata endpoint)
1. in `example/settings.php`:

2. auto-configure:
```
make
```
- adapt the base url (`$base`) to your needs (use am IP address or a hostname that is visible to the IdP)
- make sure the IdP metadata corresponding to the IdPs listed in the `idpList` key are present in `example/idp_metadata`

2. in `example/login.php` change the IdP that will be used to login

3. Start PHP's builtin webserver in the root of the repo:
```
php -S localhost:8000 -t www
```
if you have php-saml v2.x (i.e. Debian 9.4 stretch), then run it from the www2 dir:
```
php -S localhost:8000 -t www2
```sh
php -S 0.0.0.0:8000 -t example
```

4. visit http://localhost:8000/metadata.php to get the SP (Service Provider) metadata, then copy these over to the IDP
4. visit http://localhost:8000/metadata.php to get the SP (Service Provider) metadata, then copy these over to the IdP

5. visit: http://localhost:8000 and click `login`.

This screencast shows what you should see if all goes well:

![img](images/screencast.gif)

## Troubleshooting

- install a browser plugin to trace SAML messages:
It is advised to install a browser plugin to trace SAML messages:

- Firefox:
- Firefox:

- [SAML-tracer by Olav Morken, Jaime Perez](https://addons.mozilla.org/en-US/firefox/addon/saml-tracer/)
- [SAML Message Decoder by Magnus Suther](https://addons.mozilla.org/en-US/firefox/addon/saml-message-decoder-extension/)
- [SAML-tracer by Olav Morken, Jaime Perez](https://addons.mozilla.org/en-US/firefox/addon/saml-tracer/)
- [SAML Message Decoder by Magnus Suther](https://addons.mozilla.org/en-US/firefox/addon/saml-message-decoder-extension/)

- Chrome/Chromium:
- Chrome/Chromium:

- [SAML Message Decoder by Magnus Suther](https://chrome.google.com/webstore/detail/saml-message-decoder/mpabchoaimgbdbbjjieoaeiibojelbhm)
- [SAML Chrome Panel by MLai](https://chrome.google.com/webstore/detail/saml-chrome-panel/paijfdbeoenhembfhkhllainmocckace)
- [SAML DevTools extension by stefan.rasmusson.as](https://chrome.google.com/webstore/detail/saml-devtools-extension/jndllhgbinhiiddokbeoeepbppdnhhio)
- [SAML Message Decoder by Magnus Suther](https://chrome.google.com/webstore/detail/saml-message-decoder/mpabchoaimgbdbbjjieoaeiibojelbhm)
- [SAML Chrome Panel by MLai](https://chrome.google.com/webstore/detail/saml-chrome-panel/paijfdbeoenhembfhkhllainmocckace)
- [SAML DevTools extension by stefan.rasmusson.as](https://chrome.google.com/webstore/detail/saml-devtools-extension/jndllhgbinhiiddokbeoeepbppdnhhio)

- use the [SAML Developer Tools](https://www.samltool.com/online_tools.php) provided by onelogin to understand what is going on
In addition, you can use the [SAML Developer Tools](https://www.samltool.com/online_tools.php) provided by onelogin to understand what is going on

## Contributing
## Testing

### Unit tests

TODO

Unit tests will be performed with PHPunit.

Your code **should** comply with the [PSR-2: Coding Style Guide](https://www.php-fig.org/psr/psr-2/).
Check your changes with:
### Linting

This project complies with the [PSR-2: Coding Style Guide](https://www.php-fig.org/psr/psr-2/).

Lint the code with:
```
./vendor/bin/phpcs --standard=PSR2 bin/configure.php
...
./vendor/bin/phpcs --standard=PSR2 xxx.php
```

You **must** use the [git-flow workflow](https://danielkummer.github.io/git-flow-cheatsheet/).
## Contributing

For your contributions please use the [git-flow workflow](https://danielkummer.github.io/git-flow-cheatsheet/).

## Legalese

Expand Down
63 changes: 0 additions & 63 deletions bin/configure.php

This file was deleted.

Loading