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

Support transactions #53

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
93 changes: 93 additions & 0 deletions src/Events/TransactionEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* This class implements a Laravel Event for SPIDAuth Package.
*
* @license BSD-3-clause
*/

namespace Italia\SPIDAuth\Events;

use DOMDocument;
use OneLogin\Saml2\Utils as SAMLUtils;

class TransactionEvent
{
/**
* AuthnRequest type.
*
* @var string the AuthnRequest type
*/
public const AUTHNREQUEST = 'AuthnRequest';

/**
* Response type.
*
* @var string the Response type
*/
public const RESPONSE = 'Response';

/**
* The transaction type in the current event.
*
* @var string the type
*/
protected $transactionType;

/**
* The actual SAML message in the current event.
*
* @var string the SAML message
*/
protected $SAMLMessage;

/**
* The actual SAML message document in the current event.
*
* @var DOMDocument the SAML message document
*/
protected $SAMLMessageDocument;

/**
* Create a new event instance.
*
* @param string $transactionType current transaction type
* @param string $SAMLMessage current SAML message
*/
public function __construct(string $transactionType, string $SAMLMessage)
{
$this->transactionType = $transactionType;
$this->SAMLMessage = $SAMLMessage;
$this->SAMLMessageDocument = new DOMDocument();
$this->SAMLMessageDocument->loadXML($SAMLMessage);
}

/**
* Return transactionType.
*
* @return string the transacion type
*/
public function getTransactionType(): string
{
return $this->transactionType;
}

/**
* Return SAMLMessage.
*
* @return string the actual SAML message
*/
public function getSAMLMessage(): string
{
return $this->SAMLMessage;
}

/**
* Return AuthnRequest Id.
*
* @return string the id attribute of the AuthnRequest
*/
public function getAuthnRequestId(): string
{
return SAMLUtils::query($this->SAMLMessageDocument, '/samlp:AuthnRequest')->item(0)->getAttribute('id');
}
}
11 changes: 9 additions & 2 deletions src/SPIDAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Illuminate\Support\Facades\Cookie;
use Italia\SPIDAuth\Events\LoginEvent;
use Italia\SPIDAuth\Events\LogoutEvent;
use Italia\SPIDAuth\Events\TransactionEvent;
use Italia\SPIDAuth\Exceptions\SPIDConfigurationException;
use Italia\SPIDAuth\Exceptions\SPIDLoginAnomalyException;
use Italia\SPIDAuth\Exceptions\SPIDLoginException;
Expand Down Expand Up @@ -65,7 +66,8 @@ public function doLogin(): RedirectResponse

$idpRedirectTo = $this->getSAML($idp)->login(null, [], true, false, true);
$requestDocument = new DOMDocument();
SAMLUtils::loadXML($requestDocument, $this->getSAML($idp)->getLastRequestXML());
$lastRequestXML = $this->getSAML($idp)->getLastRequestXML();
SAMLUtils::loadXML($requestDocument, $lastRequestXML);
$requestIssueInstant = $requestDocument->documentElement->getAttribute('IssueInstant');
$lastRequestId = $this->getSAML($idp)->getLastRequestID();

Expand All @@ -79,6 +81,8 @@ public function doLogin(): RedirectResponse
]], 300);
}

event(new TransactionEvent(TransactionEvent::AUTHNREQUEST, $lastRequestXML));

return redirect($idpRedirectTo);
}

Expand Down Expand Up @@ -107,6 +111,10 @@ public function acs(): RedirectResponse

$this->checkIdp($idp);

$lastResponseXML = $this->getSAML($idp)->getLastResponseXML();

event(new TransactionEvent(TransactionEvent::RESPONSE, $lastResponseXML));

if (empty($lastRequestId)) {
throw new SPIDLoginException('Last request id not found', SPIDLoginException::SAML_REQUEST_ID_MISSING);
}
Expand All @@ -133,7 +141,6 @@ public function acs(): RedirectResponse
throw new SPIDLoginException('SAML authentication error: ' . $lastErrorReason, SPIDLoginException::SAML_AUTHENTICATION_ERROR);
}

$lastResponseXML = $this->getSAML($idp)->getLastResponseXML();
$this->validateLoginResponse($lastResponseXML, $lastRequestIssueInstant);

try {
Expand Down