From b68a5c2abb74b48d17b7e6795fac25a3a5a7e0b4 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Tue, 30 Sep 2014 22:16:26 +0100 Subject: [PATCH] Added authentication failure events --- src/Event/ClientAuthenticationFailedEvent.php | 51 +++++++++++++++++++ src/Event/UserAuthenticationFailedEvent.php | 51 +++++++++++++++++++ src/Grant/AuthCodeGrant.php | 3 ++ src/Grant/ClientCredentialsGrant.php | 2 + src/Grant/PasswordGrant.php | 3 ++ src/Grant/RefreshTokenGrant.php | 2 + 6 files changed, 112 insertions(+) create mode 100644 src/Event/ClientAuthenticationFailedEvent.php create mode 100644 src/Event/UserAuthenticationFailedEvent.php diff --git a/src/Event/ClientAuthenticationFailedEvent.php b/src/Event/ClientAuthenticationFailedEvent.php new file mode 100644 index 00000000..473d0937 --- /dev/null +++ b/src/Event/ClientAuthenticationFailedEvent.php @@ -0,0 +1,51 @@ + + * @copyright Copyright (c) Alex Bilbie + * @license http://mit-license.org/ + * @link https://github.com/thephpleague/oauth2-server + */ + +namespace League\OAuth2\Server\Event; + +use League\Event\AbstractEvent; +use Symfony\Component\HttpFoundation\Request; + +class ClientAuthenticationFailedEvent extends AbstractEvent +{ + /** + * Request + * @var \Symfony\Component\HttpFoundation\Request + */ + private $request; + + /** + * Init the event with a request + * @param \Symfony\Component\HttpFoundation\Requesty $request + */ + public function __construct(Request $request) + { + $this->request = $request; + } + + /** + * The name of the event + * @return string + */ + public function getName() + { + return 'error.auth.client'; + } + + /** + * Return session + * @return \Symfony\Component\HttpFoundation\Request + */ + public function getSession() + { + return $this->request; + } +} diff --git a/src/Event/UserAuthenticationFailedEvent.php b/src/Event/UserAuthenticationFailedEvent.php new file mode 100644 index 00000000..12aad47e --- /dev/null +++ b/src/Event/UserAuthenticationFailedEvent.php @@ -0,0 +1,51 @@ + + * @copyright Copyright (c) Alex Bilbie + * @license http://mit-license.org/ + * @link https://github.com/thephpleague/oauth2-server + */ + +namespace League\OAuth2\Server\Event; + +use League\Event\AbstractEvent; +use Symfony\Component\HttpFoundation\Request; + +class UserAuthenticationFailedEvent extends AbstractEvent +{ + /** + * Request + * @var \Symfony\Component\HttpFoundation\Request + */ + private $request; + + /** + * Init the event with a request + * @param \Symfony\Component\HttpFoundation\Requesty $request + */ + public function __construct(Request $request) + { + $this->request = $request; + } + + /** + * The name of the event + * @return string + */ + public function getName() + { + return 'error.auth.user'; + } + + /** + * Return session + * @return \Symfony\Component\HttpFoundation\Request + */ + public function getSession() + { + return $this->request; + } +} diff --git a/src/Grant/AuthCodeGrant.php b/src/Grant/AuthCodeGrant.php index f17769ca..7be2487c 100644 --- a/src/Grant/AuthCodeGrant.php +++ b/src/Grant/AuthCodeGrant.php @@ -19,6 +19,7 @@ use League\OAuth2\Server\Entity\SessionEntity; use League\OAuth2\Server\Entity\AccessTokenEntity; use League\OAuth2\Server\Entity\AuthCodeEntity; use League\OAuth2\Server\Util\SecureKey; +use League\OAuth2\Server\Event; /** * Auth code grant class @@ -92,6 +93,7 @@ class AuthCodeGrant extends AbstractGrant ); if (($client instanceof ClientEntity) === false) { + $this->server->getEventEmitter()->emit(new Event\ClientAuthenticationFailedEvent($this->server->getRequest())); throw new Exception\InvalidClientException(); } @@ -192,6 +194,7 @@ class AuthCodeGrant extends AbstractGrant ); if (($client instanceof ClientEntity) === false) { + $this->server->getEventEmitter()->emit(new Event\ClientAuthenticationFailedEvent($this->server->getRequest())); throw new Exception\InvalidClientException(); } diff --git a/src/Grant/ClientCredentialsGrant.php b/src/Grant/ClientCredentialsGrant.php index b5913e97..11419c28 100644 --- a/src/Grant/ClientCredentialsGrant.php +++ b/src/Grant/ClientCredentialsGrant.php @@ -16,6 +16,7 @@ use League\OAuth2\Server\Entity\ClientEntity; use League\OAuth2\Server\Entity\SessionEntity; use League\OAuth2\Server\Exception; use League\OAuth2\Server\Util\SecureKey; +use League\OAuth2\Server\Event; /** * Client credentials grant class @@ -79,6 +80,7 @@ class ClientCredentialsGrant extends AbstractGrant ); if (($client instanceof ClientEntity) === false) { + $this->server->getEventEmitter()->emit(new Event\ClientAuthenticationFailedEvent($this->server->getRequest())); throw new Exception\InvalidClientException(); } diff --git a/src/Grant/PasswordGrant.php b/src/Grant/PasswordGrant.php index 78f8ab98..8798c0e5 100644 --- a/src/Grant/PasswordGrant.php +++ b/src/Grant/PasswordGrant.php @@ -17,6 +17,7 @@ use League\OAuth2\Server\Entity\RefreshTokenEntity; use League\OAuth2\Server\Entity\SessionEntity; use League\OAuth2\Server\Exception; use League\OAuth2\Server\Util\SecureKey; +use League\OAuth2\Server\Event; /** * Password grant class @@ -102,6 +103,7 @@ class PasswordGrant extends AbstractGrant ); if (($client instanceof ClientEntity) === false) { + $this->server->getEventEmitter()->emit(new Event\ClientAuthenticationFailedEvent($this->server->getRequest())); throw new Exception\InvalidClientException(); } @@ -119,6 +121,7 @@ class PasswordGrant extends AbstractGrant $userId = call_user_func($this->getVerifyCredentialsCallback(), $username, $password); if ($userId === false) { + $this->server->getEventEmitter()->emit(new Event\UserAuthenticationFailedEvent($this->server->getRequest())); throw new Exception\InvalidCredentialsException(); } diff --git a/src/Grant/RefreshTokenGrant.php b/src/Grant/RefreshTokenGrant.php index 74ac13eb..c99f19be 100644 --- a/src/Grant/RefreshTokenGrant.php +++ b/src/Grant/RefreshTokenGrant.php @@ -17,6 +17,7 @@ use League\OAuth2\Server\Util\SecureKey; use League\OAuth2\Server\Entity\RefreshTokenEntity; use League\OAuth2\Server\Entity\AccessTokenEntity; use League\OAuth2\Server\Entity\ClientEntity; +use League\OAuth2\Server\Event; /** * Referesh token grant @@ -83,6 +84,7 @@ class RefreshTokenGrant extends AbstractGrant ); if (($client instanceof ClientEntity) === false) { + $this->server->getEventEmitter()->emit(new Event\ClientAuthenticationFailedEvent($this->server->getRequest())); throw new Exception\InvalidClientException(); }