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

Fix/response issuer #150

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 5 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: ['7.4', '8.0', '8.1']
php-versions: ['7.1', '8.2']

name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}

Expand Down Expand Up @@ -50,7 +50,10 @@ jobs:
run: bin/download_idp_metadata.php example/idp_metadata

- name: Coding standard
run: find . | grep 'php$' | grep -v vendor | grep -v tests | xargs ./vendor/bin/phpcs --standard=PSR2
run: composer code-style

- name: Coding quality
run: composer static-analysis

- name: Run tests
run: ./vendor/bin/phpunit --stderr --testdox tests
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
<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-php channel](https://img.shields.io/badge/Slack%20channel-%23spid--php-blue.svg?logo=slack)](https://developersitalia.slack.com/messages/CB6DCK274)
[![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)
[![Build Status](https://travis-ci.org/italia/spid-php-lib.svg?branch=master)](https://travis-ci.org/italia/spid-php-lib)

> **CURRENT VERSION: v0.35**
# Notice: Unofficial Release
Main differences with official release:
- implements logging
- uses PSR-12 style guide

# spid-php-lib
PHP package for SPID authentication.
Expand Down
29 changes: 25 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "italia/spid-php-lib",
"name": "intervieweb/spid-php-lib",
"description": "PHP package for SPID authentication",
"type": "library",
"license": "BSD-3-Clause",
Expand All @@ -10,15 +10,36 @@
{
"name": "Paolo Greppi",
"email": "[email protected]"
},
{
"name": "Nico Caprioli",
"email": "[email protected]"
}
],
"require": {
"ext-dom": "*",
"ext-openssl": "*",
"ext-simplexml": "*",
"ext-zlib": "*",
"robrichards/xmlseclibs": "^3.0",
"php": "^7.4 || ^8.0"
"php": "^7.1 || ^8.0",
"psr/log": "1.1.4 || ^3.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.3",
"phpunit/phpunit": "^9.5"
"squizlabs/php_codesniffer": "*",
"phpunit/phpunit": "^10",
"phpstan/phpstan": "^1.10"
},
"scripts": {
"test": [
"./vendor/phpunit/phpunit/phpunit --stderr --testdox tests"
],
"code-style": [
"./vendor/squizlabs/php_codesniffer/bin/phpcs --standard=PSR12 -q ./src/"
],
"static-analysis": [
"./vendor/phpstan/phpstan/phpstan analyze -c phpstan.neon"
]
},
"autoload": {
"psr-4": {
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 3
paths:
- src
- tests
31 changes: 15 additions & 16 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<phpunit
bootstrap="vendor/autoload.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true">

<testsuite name="spid-php-lib tests">
<directory>./tests</directory>
</testsuite>

<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>

<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
cacheDirectory=".phpunit.cache"
displayDetailsOnTestsThatTriggerWarnings="true">
<coverage/>
<testsuite name="spid-php-lib tests">
<directory>./tests</directory>
</testsuite>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
20 changes: 16 additions & 4 deletions src/Sp.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

namespace Italia\Spid;

use Exception;
use Italia\Spid\Spid\Interfaces\LoggerSelector;

/**
* @mixin Spid\Saml
*/
class Sp
{
/*
Expand All @@ -13,25 +19,31 @@ class Sp
*/
private $protocol;

public function __construct(array $settings, String $protocol = null, $autoconfigure = true)
/**
* @throws Exception
*/
public function __construct(LoggerSelector $logger, array $settings, string $protocol = null, $autoconfigure = true)
{
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
switch ($protocol) {
case 'saml':
$this->protocol = new Spid\Saml($settings, $autoconfigure);
$this->protocol = new Spid\Saml($logger, $settings, $autoconfigure);
break;
default:
$this->protocol = new Spid\Saml($settings, $autoconfigure);
$this->protocol = new Spid\Saml($logger, $settings, $autoconfigure);
}
}

/**
* @throws Exception
*/
public function __call($method, $arguments)
{
$methods_implemented = get_class_methods($this->protocol);
if (!in_array($method, $methods_implemented)) {
throw new \Exception("Invalid method [$method] requested", 1);
throw new Exception("Invalid method [$method] requested", 1);
}
return call_user_func_array(array($this->protocol, $method), $arguments);
}
Expand Down
24 changes: 24 additions & 0 deletions src/Spid/Exceptions/SpidException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Italia\Spid\Spid\Exceptions;

use Exception;
use Throwable;

class SpidException extends Exception
{
private $context;

public function __construct($message = "", $code = 0, ?Throwable $previous = null, $context = null)
{
parent::__construct($message, $code, $previous);
$this->context = $context;
}

public function getContext()
{
return $this->context;
}
}
10 changes: 6 additions & 4 deletions src/Spid/Interfaces/IdpInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Italia\Spid\Spid\Interfaces;

use Italia\Spid\Spid\Session;
Expand All @@ -18,17 +20,17 @@ public function loadFromXml($xmlFile);
// $level: SPID level (1, 2 or 3)
// $returnTo: return url
// $shouldRedirect: tells if the function should emit headers and redirect to login URL or return the URL as string
// returns and empty string if $shouldRedirect = true, the login URL otherwhise
public function authnRequest($ass, $attr, $level, $returnTo = null, $shouldRedirect = true) : string;
// returns and empty string if $shouldRedirect = true, the login URL otherwise
public function authnRequest($ass, $attr, $binding, $level = 1, $redirectTo = null, $shouldRedirect = true): string;

// generate a LogoutRequest
// $session: the currently active login session
// $slo: index of singlelogout service as per the SP metadata
// $binding: HTTP Redirect or HTTP POST binding
// $returnTo: return url
// $shouldRedirect: tells if the function should emit headers and redirect to login URL or return the URL as string
// returns and empty string if $shouldRedirect = true, the logout URL otherwhise
public function logoutRequest(Session $session, $slo, $binding, $returnTo = null, $shouldRedirect = true) : string;
// returns and empty string if $shouldRedirect = true, the logout URL otherwise
public function logoutRequest(Session $session, $slo, $binding, $redirectTo = null, $shouldRedirect = true): string;

//generates a logoutResponse in response to an Idp initiated logout request
public function logoutResponse(): string;
Expand Down
23 changes: 23 additions & 0 deletions src/Spid/Interfaces/LoggerSelector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Italia\Spid\Spid\Interfaces;

use DOMDocument;
use Italia\Spid\Spid\Exceptions\SpidException;
use Psr\Log\LoggerInterface;

interface LoggerSelector
{
public function getPermanentLogger(): ?LoggerInterface;
public function getTemporaryLogger(): ?LoggerInterface;

/**
* @param DOMDocument $xml
* @param $message
* @return never
* @throws SpidException
*/
public function logAndThrow(DOMDocument $xml, $message): void;
}
6 changes: 4 additions & 2 deletions src/Spid/Interfaces/RequestInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Italia\Spid\Spid\Interfaces;

interface RequestInterface
Expand All @@ -8,8 +10,8 @@ public function generateXml();

// prepare HTTP-Redirect binding and return it as a string
// https://github.com/italia/spid-perl/blob/master/lib/Net/SPID/SAML/Out/AuthnRequest.pm#L61
public function redirectUrl($redirectTo = null) : string;
public function redirectUrl($redirectTo = null): string;

// prepare HTTP-POST binding and return the html form as a string
public function httpPost($redirectTo = null) : string;
public function httpPost($redirectTo = null): string;
}
8 changes: 6 additions & 2 deletions src/Spid/Interfaces/ResponseInterface.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php

declare(strict_types=1);

namespace Italia\Spid\Spid\Interfaces;

use DOMDocument;

interface ResponseInterface
{
// Validates a received response.
// Throws exceptions on missing or invalid values.
// returns false if resposne code is not success
// returns false if response code is not success
// returns true otherwise
public function validate($xml, $hasAssertion) : bool;
public function validate(DOMDocument $xml, $hasAssertion): bool;
}
37 changes: 19 additions & 18 deletions src/Spid/Interfaces/SAMLInterface.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

declare(strict_types=1);

namespace Italia\Spid\Spid\Interfaces;

// service provider class
use Italia\Spid\Spid\Saml\Idp;

interface SAMLInterface
{
Expand Down Expand Up @@ -40,10 +41,10 @@ interface SAMLInterface
// ...
// ];
//
// $autoconfigure: boolean value, determines if SP key and cert files should be autogenerated
// based on values provided in settings.
// If set to false this step will be skipped
public function __construct(array $settings, $autoconfigure = true);
// $autoconfigure: boolean value.
// Determines if SP key and cert files should be autogenerated based on values provided in settings.
// If set to false this step will be skipped
public function __construct(LoggerSelector $logger, array $settings, $autoconfigure = true);

// loads an Idp object by parsing the provided XML at $filename
// $filename: file name of the IdP to be loaded. Only the file, without the path, needs to be provided.
Expand All @@ -54,14 +55,14 @@ public function loadIdpFromFile(string $filename);
// the individual files are loaded with loadIdpFromFile($filename)
// returns an array mapping filename (without extension) => entityID (used for spid-smart-button)
// if no IdPs are found returns an empty array
public function getIdpList() : array;
public function getIdpList(): array;

// alias of loadIdpFromFile
public function getIdp(string $filename);

// returns SP metadata as a string
public function getSPMetadata() : string;
public function getSPMetadata(): string;

// performs login with REDIRECT binding
// $idpFilename: shortname of IdP, same as the name of corresponding IdP metadata file, without .xml extension
// $assertID: index of assertion consumer service as per the SP metadata
Expand All @@ -70,11 +71,11 @@ public function getSPMetadata() : string;
// $returnTo: url to redirect to after login
// $shouldRedirect: tells if the function should emit headers and redirect to login URL or return the URL as string
// returns false is already logged in
// returns an empty string if $shouldRedirect = true, the login URL otherwhise
// returns an empty string if $shouldRedirect = true, the login URL otherwise
public function login(
string $idpFilename,
int $assertID,
int $attrID,
string $idpName,
int $assertId,
int $attrId,
$level = 1,
string $redirectTo = null,
$shouldRedirect = true
Expand All @@ -83,9 +84,9 @@ public function login(
// performs login with POST Binding
// uses the same parameters and return values as login
public function loginPost(
string $idpFilename,
int $assertID,
int $attrID,
string $idpName,
int $assertId,
int $attrId,
$level = 1,
string $redirectTo = null,
$shouldRedirect = true
Expand All @@ -101,14 +102,14 @@ public function loginPost(
// SESSION AND STORING USER ATTRIBUTES.
// SIMILARLY, AFTER A LOGOUT() CALLING THIS METHOD WILL VALIDATE THE RESULT AND DESTROY THE SESSION.
// LOGIN() AND LOGOUT() ALONE INTERACT WITH THE IDP, BUT DON'T CHECK FOR RESULTS AND UPDATE THE SP
public function isAuthenticated() : bool;
public function isAuthenticated(): bool;

// performs logout with REDIRECT binding
// $slo: index of the singlelogout service as per the SP metadata
// $returnTo: url to redirect to after logout
// $shouldRedirect: tells if the function should emit headers and redirect to logout URL or return the URL as string
// returns false if not logged in
// returns an empty string if $shouldRedirect = true, the logout URL otherwhise
// returns an empty string if $shouldRedirect = true, the logout URL otherwise
public function logout(int $slo, string $redirectTo = null, $shouldRedirect = true);

// performs logout with POST Binding
Expand All @@ -117,5 +118,5 @@ public function logoutPost(int $slo, string $redirectTo = null, $shouldRedirect

// returns attributes as an array or an empty array if not authenticated
// example: array('name' => 'Franco', 'familyName' => 'Rossi', 'fiscalNumber' => 'FFFRRR88A12T4441R',)
public function getAttributes() : array;
public function getAttributes(): array;
}
Loading