diff --git a/.gitignore b/.gitignore index a859c02f9..1969ee4d4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ /vendor -/composer.lock +**/composer.lock phpunit.xml .phpunit.result.cache .idea diff --git a/composer.json b/composer.json index 6d01aecd7..2064a0e5f 100644 --- a/composer.json +++ b/composer.json @@ -6,8 +6,8 @@ "require": { "php": "^7.2 || ^8.0", "ext-openssl": "*", - "league/event": "^2.2", "lcobucci/jwt": "^3.4 || ^4.0", + "psr/event-dispatcher": "^1.0", "psr/http-message": "^1.0.1", "defuse/php-encryption": "^2.2.1", "ext-json": "*" @@ -19,6 +19,9 @@ "phpstan/phpstan-phpunit": "^0.12.16", "roave/security-advisories": "dev-master" }, + "suggest": { + "league/event": "Required to use the EventDispatcher (^3.0)." + }, "repositories": [ { "type": "git", diff --git a/examples/composer.json b/examples/composer.json index d265472e6..469fa9265 100644 --- a/examples/composer.json +++ b/examples/composer.json @@ -1,13 +1,16 @@ { "require": { - "slim/slim": "^3.12.3" + "slim/slim": "^3.12.3", + "league/event": "^3.0" }, "require-dev": { - "league/event": "^2.2", + "php": "^7.2 || ^8.0", + "ext-openssl": "*", + "ext-json": "*", "lcobucci/jwt": "^3.4 || ^4.0", + "psr/event-dispatcher": "^1.0", "psr/http-message": "^1.0.1", - "defuse/php-encryption": "^2.2.1", - "laminas/laminas-diactoros": "^2.5.0" + "defuse/php-encryption": "^2.2.1" }, "autoload": { "psr-4": { diff --git a/examples/composer.lock b/examples/composer.lock deleted file mode 100644 index f7affe0e9..000000000 --- a/examples/composer.lock +++ /dev/null @@ -1,740 +0,0 @@ -{ - "_readme": [ - "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", - "This file is @generated automatically" - ], - "content-hash": "1f38bc4bb33ddc5527b3097d1118b227", - "packages": [ - { - "name": "nikic/fast-route", - "version": "v1.3.0", - "source": { - "type": "git", - "url": "https://github.com/nikic/FastRoute.git", - "reference": "181d480e08d9476e61381e04a71b34dc0432e812" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/nikic/FastRoute/zipball/181d480e08d9476e61381e04a71b34dc0432e812", - "reference": "181d480e08d9476e61381e04a71b34dc0432e812", - "shasum": "" - }, - "require": { - "php": ">=5.4.0" - }, - "require-dev": { - "phpunit/phpunit": "^4.8.35|~5.7" - }, - "type": "library", - "autoload": { - "psr-4": { - "FastRoute\\": "src/" - }, - "files": [ - "src/functions.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Nikita Popov", - "email": "nikic@php.net" - } - ], - "description": "Fast request router for PHP", - "keywords": [ - "router", - "routing" - ], - "time": "2018-02-13T20:26:39+00:00" - }, - { - "name": "pimple/pimple", - "version": "v3.3.1", - "source": { - "type": "git", - "url": "https://github.com/silexphp/Pimple.git", - "reference": "21e45061c3429b1e06233475cc0e1f6fc774d5b0" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/silexphp/Pimple/zipball/21e45061c3429b1e06233475cc0e1f6fc774d5b0", - "reference": "21e45061c3429b1e06233475cc0e1f6fc774d5b0", - "shasum": "" - }, - "require": { - "php": ">=7.2.5", - "psr/container": "^1.0" - }, - "require-dev": { - "symfony/phpunit-bridge": "^5.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.3.x-dev" - } - }, - "autoload": { - "psr-0": { - "Pimple": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Fabien Potencier", - "email": "fabien@symfony.com" - } - ], - "description": "Pimple, a simple Dependency Injection Container", - "homepage": "https://pimple.symfony.com", - "keywords": [ - "container", - "dependency injection" - ], - "time": "2020-11-24T20:35:42+00:00" - }, - { - "name": "psr/container", - "version": "1.0.0", - "source": { - "type": "git", - "url": "https://github.com/php-fig/container.git", - "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/php-fig/container/zipball/b7ce3b176482dbbc1245ebf52b181af44c2cf55f", - "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f", - "shasum": "" - }, - "require": { - "php": ">=5.3.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "psr-4": { - "Psr\\Container\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "PHP-FIG", - "homepage": "http://www.php-fig.org/" - } - ], - "description": "Common Container Interface (PHP FIG PSR-11)", - "homepage": "https://github.com/php-fig/container", - "keywords": [ - "PSR-11", - "container", - "container-interface", - "container-interop", - "psr" - ], - "time": "2017-02-14T16:28:37+00:00" - }, - { - "name": "psr/http-message", - "version": "1.0.1", - "source": { - "type": "git", - "url": "https://github.com/php-fig/http-message.git", - "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", - "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", - "shasum": "" - }, - "require": { - "php": ">=5.3.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "psr-4": { - "Psr\\Http\\Message\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "PHP-FIG", - "homepage": "http://www.php-fig.org/" - } - ], - "description": "Common interface for HTTP messages", - "homepage": "https://github.com/php-fig/http-message", - "keywords": [ - "http", - "http-message", - "psr", - "psr-7", - "request", - "response" - ], - "time": "2016-08-06T14:39:51+00:00" - }, - { - "name": "slim/slim", - "version": "3.12.3", - "source": { - "type": "git", - "url": "https://github.com/slimphp/Slim.git", - "reference": "1c9318a84ffb890900901136d620b4f03a59da38" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/slimphp/Slim/zipball/1c9318a84ffb890900901136d620b4f03a59da38", - "reference": "1c9318a84ffb890900901136d620b4f03a59da38", - "shasum": "" - }, - "require": { - "ext-json": "*", - "ext-libxml": "*", - "ext-simplexml": "*", - "nikic/fast-route": "^1.0", - "php": ">=5.5.0", - "pimple/pimple": "^3.0", - "psr/container": "^1.0", - "psr/http-message": "^1.0" - }, - "provide": { - "psr/http-message-implementation": "1.0" - }, - "require-dev": { - "phpunit/phpunit": "^4.0", - "squizlabs/php_codesniffer": "^2.5" - }, - "type": "library", - "autoload": { - "psr-4": { - "Slim\\": "Slim" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Josh Lockhart", - "email": "hello@joshlockhart.com", - "homepage": "https://joshlockhart.com" - }, - { - "name": "Andrew Smith", - "email": "a.smith@silentworks.co.uk", - "homepage": "http://silentworks.co.uk" - }, - { - "name": "Rob Allen", - "email": "rob@akrabat.com", - "homepage": "http://akrabat.com" - }, - { - "name": "Gabriel Manricks", - "email": "gmanricks@me.com", - "homepage": "http://gabrielmanricks.com" - } - ], - "description": "Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs", - "homepage": "https://slimframework.com", - "keywords": [ - "api", - "framework", - "micro", - "router" - ], - "time": "2019-11-28T17:40:33+00:00" - } - ], - "packages-dev": [ - { - "name": "defuse/php-encryption", - "version": "v2.2.1", - "source": { - "type": "git", - "url": "https://github.com/defuse/php-encryption.git", - "reference": "0f407c43b953d571421e0020ba92082ed5fb7620" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/defuse/php-encryption/zipball/0f407c43b953d571421e0020ba92082ed5fb7620", - "reference": "0f407c43b953d571421e0020ba92082ed5fb7620", - "shasum": "" - }, - "require": { - "ext-openssl": "*", - "paragonie/random_compat": ">= 2", - "php": ">=5.4.0" - }, - "require-dev": { - "nikic/php-parser": "^2.0|^3.0|^4.0", - "phpunit/phpunit": "^4|^5" - }, - "bin": [ - "bin/generate-defuse-key" - ], - "type": "library", - "autoload": { - "psr-4": { - "Defuse\\Crypto\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Taylor Hornby", - "email": "taylor@defuse.ca", - "homepage": "https://defuse.ca/" - }, - { - "name": "Scott Arciszewski", - "email": "info@paragonie.com", - "homepage": "https://paragonie.com" - } - ], - "description": "Secure PHP Encryption Library", - "keywords": [ - "aes", - "authenticated encryption", - "cipher", - "crypto", - "cryptography", - "encrypt", - "encryption", - "openssl", - "security", - "symmetric key cryptography" - ], - "time": "2018-07-24T23:27:56+00:00" - }, - { - "name": "laminas/laminas-diactoros", - "version": "2.5.0", - "source": { - "type": "git", - "url": "https://github.com/laminas/laminas-diactoros.git", - "reference": "4ff7400c1c12e404144992ef43c8b733fd9ad516" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/laminas/laminas-diactoros/zipball/4ff7400c1c12e404144992ef43c8b733fd9ad516", - "reference": "4ff7400c1c12e404144992ef43c8b733fd9ad516", - "shasum": "" - }, - "require": { - "laminas/laminas-zendframework-bridge": "^1.0", - "php": "^7.3 || ~8.0.0", - "psr/http-factory": "^1.0", - "psr/http-message": "^1.0" - }, - "conflict": { - "phpspec/prophecy": "<1.9.0" - }, - "provide": { - "psr/http-factory-implementation": "1.0", - "psr/http-message-implementation": "1.0" - }, - "replace": { - "zendframework/zend-diactoros": "^2.2.1" - }, - "require-dev": { - "ext-curl": "*", - "ext-dom": "*", - "ext-gd": "*", - "ext-libxml": "*", - "http-interop/http-factory-tests": "^0.8.0", - "laminas/laminas-coding-standard": "~1.0.0", - "php-http/psr7-integration-tests": "^1.1", - "phpspec/prophecy-phpunit": "^2.0", - "phpunit/phpunit": "^9.1" - }, - "type": "library", - "extra": { - "laminas": { - "config-provider": "Laminas\\Diactoros\\ConfigProvider", - "module": "Laminas\\Diactoros" - } - }, - "autoload": { - "files": [ - "src/functions/create_uploaded_file.php", - "src/functions/marshal_headers_from_sapi.php", - "src/functions/marshal_method_from_sapi.php", - "src/functions/marshal_protocol_version_from_sapi.php", - "src/functions/marshal_uri_from_sapi.php", - "src/functions/normalize_server.php", - "src/functions/normalize_uploaded_files.php", - "src/functions/parse_cookie_header.php", - "src/functions/create_uploaded_file.legacy.php", - "src/functions/marshal_headers_from_sapi.legacy.php", - "src/functions/marshal_method_from_sapi.legacy.php", - "src/functions/marshal_protocol_version_from_sapi.legacy.php", - "src/functions/marshal_uri_from_sapi.legacy.php", - "src/functions/normalize_server.legacy.php", - "src/functions/normalize_uploaded_files.legacy.php", - "src/functions/parse_cookie_header.legacy.php" - ], - "psr-4": { - "Laminas\\Diactoros\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "description": "PSR HTTP Message implementations", - "homepage": "https://laminas.dev", - "keywords": [ - "http", - "laminas", - "psr", - "psr-17", - "psr-7" - ], - "time": "2020-11-18T18:39:28+00:00" - }, - { - "name": "laminas/laminas-zendframework-bridge", - "version": "1.1.1", - "source": { - "type": "git", - "url": "https://github.com/laminas/laminas-zendframework-bridge.git", - "reference": "6ede70583e101030bcace4dcddd648f760ddf642" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/laminas/laminas-zendframework-bridge/zipball/6ede70583e101030bcace4dcddd648f760ddf642", - "reference": "6ede70583e101030bcace4dcddd648f760ddf642", - "shasum": "" - }, - "require": { - "php": "^5.6 || ^7.0 || ^8.0" - }, - "require-dev": { - "phpunit/phpunit": "^5.7 || ^6.5 || ^7.5 || ^8.1 || ^9.3", - "squizlabs/php_codesniffer": "^3.5" - }, - "type": "library", - "extra": { - "laminas": { - "module": "Laminas\\ZendFrameworkBridge" - } - }, - "autoload": { - "files": [ - "src/autoload.php" - ], - "psr-4": { - "Laminas\\ZendFrameworkBridge\\": "src//" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "description": "Alias legacy ZF class names to Laminas Project equivalents.", - "keywords": [ - "ZendFramework", - "autoloading", - "laminas", - "zf" - ], - "time": "2020-09-14T14:23:00+00:00" - }, - { - "name": "lcobucci/clock", - "version": "2.0.0", - "source": { - "type": "git", - "url": "https://github.com/lcobucci/clock.git", - "reference": "353d83fe2e6ae95745b16b3d911813df6a05bfb3" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/lcobucci/clock/zipball/353d83fe2e6ae95745b16b3d911813df6a05bfb3", - "reference": "353d83fe2e6ae95745b16b3d911813df6a05bfb3", - "shasum": "" - }, - "require": { - "php": "^7.4 || ^8.0" - }, - "require-dev": { - "infection/infection": "^0.17", - "lcobucci/coding-standard": "^6.0", - "phpstan/extension-installer": "^1.0", - "phpstan/phpstan": "^0.12", - "phpstan/phpstan-deprecation-rules": "^0.12", - "phpstan/phpstan-phpunit": "^0.12", - "phpstan/phpstan-strict-rules": "^0.12", - "phpunit/php-code-coverage": "9.1.4", - "phpunit/phpunit": "9.3.7" - }, - "type": "library", - "autoload": { - "psr-4": { - "Lcobucci\\Clock\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Luís Cobucci", - "email": "lcobucci@gmail.com" - } - ], - "description": "Yet another clock abstraction", - "time": "2020-08-27T18:56:02+00:00" - }, - { - "name": "lcobucci/jwt", - "version": "4.0.0", - "source": { - "type": "git", - "url": "https://github.com/lcobucci/jwt.git", - "reference": "6d8665ccd924dc076a9b65d1ea8abe21d68f6958" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/lcobucci/jwt/zipball/6d8665ccd924dc076a9b65d1ea8abe21d68f6958", - "reference": "6d8665ccd924dc076a9b65d1ea8abe21d68f6958", - "shasum": "" - }, - "require": { - "ext-mbstring": "*", - "ext-openssl": "*", - "lcobucci/clock": "^2.0", - "php": "^7.4 || ^8.0" - }, - "require-dev": { - "infection/infection": "^0.20", - "lcobucci/coding-standard": "^6.0", - "mikey179/vfsstream": "^1.6", - "phpbench/phpbench": "^0.17", - "phpstan/extension-installer": "^1.0", - "phpstan/phpstan": "^0.12", - "phpstan/phpstan-deprecation-rules": "^0.12", - "phpstan/phpstan-phpunit": "^0.12", - "phpstan/phpstan-strict-rules": "^0.12", - "phpunit/php-invoker": "^3.1", - "phpunit/phpunit": "^9.4" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "4.0-dev" - } - }, - "autoload": { - "psr-4": { - "Lcobucci\\JWT\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Luís Cobucci", - "email": "lcobucci@gmail.com", - "role": "Developer" - } - ], - "description": "A simple library to work with JSON Web Token and JSON Web Signature", - "keywords": [ - "JWS", - "jwt" - ], - "time": "2020-11-25T02:06:12+00:00" - }, - { - "name": "league/event", - "version": "2.2.0", - "source": { - "type": "git", - "url": "https://github.com/thephpleague/event.git", - "reference": "d2cc124cf9a3fab2bb4ff963307f60361ce4d119" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/thephpleague/event/zipball/d2cc124cf9a3fab2bb4ff963307f60361ce4d119", - "reference": "d2cc124cf9a3fab2bb4ff963307f60361ce4d119", - "shasum": "" - }, - "require": { - "php": ">=5.4.0" - }, - "require-dev": { - "henrikbjorn/phpspec-code-coverage": "~1.0.1", - "phpspec/phpspec": "^2.2" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.2-dev" - } - }, - "autoload": { - "psr-4": { - "League\\Event\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Frank de Jonge", - "email": "info@frenky.net" - } - ], - "description": "Event package", - "keywords": [ - "emitter", - "event", - "listener" - ], - "time": "2018-11-26T11:52:41+00:00" - }, - { - "name": "paragonie/random_compat", - "version": "v9.99.100", - "source": { - "type": "git", - "url": "https://github.com/paragonie/random_compat.git", - "reference": "996434e5492cb4c3edcb9168db6fbb1359ef965a" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/paragonie/random_compat/zipball/996434e5492cb4c3edcb9168db6fbb1359ef965a", - "reference": "996434e5492cb4c3edcb9168db6fbb1359ef965a", - "shasum": "" - }, - "require": { - "php": ">= 7" - }, - "require-dev": { - "phpunit/phpunit": "4.*|5.*", - "vimeo/psalm": "^1" - }, - "suggest": { - "ext-libsodium": "Provides a modern crypto API that can be used to generate random bytes." - }, - "type": "library", - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Paragon Initiative Enterprises", - "email": "security@paragonie.com", - "homepage": "https://paragonie.com" - } - ], - "description": "PHP 5.x polyfill for random_bytes() and random_int() from PHP 7", - "keywords": [ - "csprng", - "polyfill", - "pseudorandom", - "random" - ], - "time": "2020-10-15T08:29:30+00:00" - }, - { - "name": "psr/http-factory", - "version": "1.0.1", - "source": { - "type": "git", - "url": "https://github.com/php-fig/http-factory.git", - "reference": "12ac7fcd07e5b077433f5f2bee95b3a771bf61be" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/php-fig/http-factory/zipball/12ac7fcd07e5b077433f5f2bee95b3a771bf61be", - "reference": "12ac7fcd07e5b077433f5f2bee95b3a771bf61be", - "shasum": "" - }, - "require": { - "php": ">=7.0.0", - "psr/http-message": "^1.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "psr-4": { - "Psr\\Http\\Message\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "PHP-FIG", - "homepage": "http://www.php-fig.org/" - } - ], - "description": "Common interfaces for PSR-7 HTTP message factories", - "keywords": [ - "factory", - "http", - "message", - "psr", - "psr-17", - "psr-7", - "request", - "response" - ], - "time": "2019-04-30T12:38:16+00:00" - } - ], - "aliases": [], - "minimum-stability": "stable", - "stability-flags": [], - "prefer-stable": false, - "prefer-lowest": false, - "platform": [], - "platform-dev": [] -} diff --git a/examples/public/auth_code.php b/examples/public/auth_code.php index c082e3b3f..0b6161011 100644 --- a/examples/public/auth_code.php +++ b/examples/public/auth_code.php @@ -8,7 +8,11 @@ */ use Laminas\Diactoros\Stream; +use League\Event\EventDispatcher; use League\OAuth2\Server\AuthorizationServer; +use League\OAuth2\Server\Events\AccessTokenIssued; +use League\OAuth2\Server\Events\ClientAuthenticationFailed; +use League\OAuth2\Server\Events\RefreshTokenIssued; use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Grant\AuthCodeGrant; use OAuth2ServerExamples\Entities\UserEntity; @@ -46,6 +50,19 @@ 'lxZFUEsBCJ2Yb14IF2ygAHI5N4+ZAUXXaSeeJm6+twsUmIen' ); + // Setup EventDispatcher + $dispatcher = new EventDispatcher(); + $dispatcher->subscribeTo(ClientAuthenticationFailed::class, function (ClientAuthenticationFailed $event) { + // Handle client authentication failure + }); + $dispatcher->subscribeTo(AccessTokenIssued::class, function (AccessTokenIssued $event) { + // Handle access token issue + }); + $dispatcher->subscribeTo(RefreshTokenIssued::class, function (RefreshTokenIssued $event) { + // Handle refresh token issue + }); + $server->useEventDispatcher($dispatcher); + // Enable the authentication code grant on the server with a token TTL of 1 hour $server->enableGrantType( new AuthCodeGrant( diff --git a/examples/public/client_credentials.php b/examples/public/client_credentials.php index 51a1ca0b7..a59e37687 100644 --- a/examples/public/client_credentials.php +++ b/examples/public/client_credentials.php @@ -8,7 +8,9 @@ */ use Laminas\Diactoros\Stream; +use League\Event\EventDispatcher; use League\OAuth2\Server\AuthorizationServer; +use League\OAuth2\Server\Events\ClientAuthenticationFailed; use League\OAuth2\Server\Exception\OAuthServerException; use OAuth2ServerExamples\Repositories\AccessTokenRepository; use OAuth2ServerExamples\Repositories\ClientRepository; @@ -42,6 +44,13 @@ 'lxZFUEsBCJ2Yb14IF2ygAHI5N4+ZAUXXaSeeJm6+twsUmIen' ); + // Setup EventDispatcher + $dispatcher = new EventDispatcher(); + $dispatcher->subscribeTo(ClientAuthenticationFailed::class, function (ClientAuthenticationFailed $event) { + // Handle client authentication failure + }); + $server->useEventDispatcher($dispatcher); + // Enable the client credentials grant on the server $server->enableGrantType( new \League\OAuth2\Server\Grant\ClientCredentialsGrant(), diff --git a/examples/public/implicit.php b/examples/public/implicit.php index ac43f5dd1..cd8a5794d 100644 --- a/examples/public/implicit.php +++ b/examples/public/implicit.php @@ -8,7 +8,9 @@ */ use Laminas\Diactoros\Stream; +use League\Event\EventDispatcher; use League\OAuth2\Server\AuthorizationServer; +use League\OAuth2\Server\Events\ClientAuthenticationFailed; use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Grant\ImplicitGrant; use OAuth2ServerExamples\Entities\UserEntity; @@ -42,6 +44,13 @@ 'lxZFUEsBCJ2Yb14IF2ygAHI5N4+ZAUXXaSeeJm6+twsUmIen' ); + // Setup EventDispatcher + $dispatcher = new EventDispatcher(); + $dispatcher->subscribeTo(ClientAuthenticationFailed::class, function (ClientAuthenticationFailed $event) { + // Handle client authentication failure + }); + $server->useEventDispatcher($dispatcher); + // Enable the implicit grant on the server with a token TTL of 1 hour $server->enableGrantType(new ImplicitGrant(new \DateInterval('PT1H'))); diff --git a/examples/public/middleware_use.php b/examples/public/middleware_use.php index 9f958ed26..7c4f3a8a7 100644 --- a/examples/public/middleware_use.php +++ b/examples/public/middleware_use.php @@ -8,7 +8,12 @@ */ use Laminas\Diactoros\Stream; +use League\Event\EventDispatcher; use League\OAuth2\Server\AuthorizationServer; +use League\OAuth2\Server\Events\AccessTokenIssued; +use League\OAuth2\Server\Events\ClientAuthenticationFailed; +use League\OAuth2\Server\Events\RefreshTokenClientFailed; +use League\OAuth2\Server\Events\RefreshTokenIssued; use League\OAuth2\Server\Grant\AuthCodeGrant; use League\OAuth2\Server\Grant\RefreshTokenGrant; use League\OAuth2\Server\Middleware\AuthorizationServerMiddleware; @@ -48,6 +53,23 @@ 'lxZFUEsBCJ2Yb14IF2ygAHI5N4+ZAUXXaSeeJm6+twsUmIen' ); + // Setup EventDispatcher + $dispatcher = new EventDispatcher(); + $dispatcher->subscribeTo(ClientAuthenticationFailed::class, function (ClientAuthenticationFailed $event) { + // Handle client authentication failure + }); + $dispatcher->subscribeTo(RefreshTokenClientFailed::class, function (RefreshTokenClientFailed $event) { + // Handle refresh token client failure + }); + $dispatcher->subscribeTo(AccessTokenIssued::class, function (AccessTokenIssued $event) { + // Handle access token issue + }); + $dispatcher->subscribeTo(RefreshTokenIssued::class, function (RefreshTokenIssued $event) { + // Handle refresh token issue + }); + $server->useEventDispatcher($dispatcher); + + // Enable the authentication code grant on the server with a token TTL of 1 hour $server->enableGrantType( new AuthCodeGrant( diff --git a/examples/public/password.php b/examples/public/password.php index 6857e988a..e9441f55f 100644 --- a/examples/public/password.php +++ b/examples/public/password.php @@ -1,6 +1,10 @@ subscribeTo(UserAuthenticationFailed::class, function (UserAuthenticationFailed $event) { + // Handle user authentication failure + }); + $dispatcher->subscribeTo(AccessTokenIssued::class, function (AccessTokenIssued $event) { + // Handle access token issue + }); + $dispatcher->subscribeTo(RefreshTokenIssued::class, function (RefreshTokenIssued $event) { + // Handle refresh token issue + }); + $server->useEventDispatcher($dispatcher); + + // Enable the password grant on the server with a token TTL of 1 hour $grant = new PasswordGrant( new UserRepository(), // instance of UserRepositoryInterface new RefreshTokenRepository() // instance of RefreshTokenRepositoryInterface ); $grant->setRefreshTokenTTL(new \DateInterval('P1M')); // refresh tokens will expire after 1 month - // Enable the password grant on the server with a token TTL of 1 hour $server->enableGrantType( $grant, new \DateInterval('PT1H') // access tokens will expire after 1 hour diff --git a/examples/public/refresh_token.php b/examples/public/refresh_token.php index 39be08262..421349d75 100644 --- a/examples/public/refresh_token.php +++ b/examples/public/refresh_token.php @@ -7,7 +7,11 @@ * @link https://github.com/thephpleague/oauth2-server */ +use League\Event\EventDispatcher; use League\OAuth2\Server\AuthorizationServer; +use League\OAuth2\Server\Events\AccessTokenIssued; +use League\OAuth2\Server\Events\RefreshTokenClientFailed; +use League\OAuth2\Server\Events\RefreshTokenIssued; use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Grant\RefreshTokenGrant; use OAuth2ServerExamples\Repositories\AccessTokenRepository; @@ -42,6 +46,19 @@ 'lxZFUEsBCJ2Yb14IF2ygAHI5N4+ZAUXXaSeeJm6+twsUmIen' ); + // Setup EventDispatcher + $dispatcher = new EventDispatcher(); + $dispatcher->subscribeTo(RefreshTokenClientFailed::class, function (RefreshTokenClientFailed $event) { + // Handle refresh token client failure + }); + $dispatcher->subscribeTo(AccessTokenIssued::class, function (AccessTokenIssued $event) { + // Handle access token issue + }); + $dispatcher->subscribeTo(RefreshTokenIssued::class, function (RefreshTokenIssued $event) { + // Handle refresh token issue + }); + $server->useEventDispatcher($dispatcher); + // Enable the refresh token grant on the server $grant = new RefreshTokenGrant($refreshTokenRepository); $grant->setRefreshTokenTTL(new \DateInterval('P1M')); // The refresh token will expire in 1 month diff --git a/src/AuthorizationServer.php b/src/AuthorizationServer.php index a719656c6..8be7a7b0d 100644 --- a/src/AuthorizationServer.php +++ b/src/AuthorizationServer.php @@ -11,8 +11,8 @@ use DateInterval; use Defuse\Crypto\Key; -use League\Event\EmitterAwareInterface; -use League\Event\EmitterAwareTrait; +use League\OAuth2\Server\Events\EventDispatcherAwareInterface; +use League\OAuth2\Server\Events\EventDispatcherAwareTrait; use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Grant\GrantTypeInterface; use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; @@ -25,9 +25,9 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -class AuthorizationServer implements EmitterAwareInterface +class AuthorizationServer implements EventDispatcherAwareInterface { - use EmitterAwareTrait; + use EventDispatcherAwareTrait; /** * @var GrantTypeInterface[] @@ -139,7 +139,7 @@ public function enableGrantType(GrantTypeInterface $grantType, DateInterval $acc $grantType->setScopeRepository($this->scopeRepository); $grantType->setDefaultScope($this->defaultScope); $grantType->setPrivateKey($this->privateKey); - $grantType->setEmitter($this->getEmitter()); + $grantType->useEventDispatcher($this->eventDispatcher); $grantType->setEncryptionKey($this->encryptionKey); $grantType->revokeRefreshTokens($this->revokeRefreshTokens); diff --git a/src/Events/AbstractEvent.php b/src/Events/AbstractEvent.php new file mode 100644 index 000000000..f926c4a63 --- /dev/null +++ b/src/Events/AbstractEvent.php @@ -0,0 +1,39 @@ +name = static::class; + $this->request = $request; + $this->occuredOn = new \DateTimeImmutable(); + } + + public function getName(): string + { + return $this->name; + } + + public function getRequest(): ServerRequestInterface + { + return $this->request; + } + + public function getOccuredOn(): \DateTimeImmutable + { + return $this->occuredOn; + } +} diff --git a/src/Events/AccessTokenIssued.php b/src/Events/AccessTokenIssued.php new file mode 100644 index 000000000..951985144 --- /dev/null +++ b/src/Events/AccessTokenIssued.php @@ -0,0 +1,26 @@ +name = 'access_token.issued'; + $this->accessToken = $accessToken; + } + + public function getAccessToken(): AccessTokenEntityInterface + { + return $this->accessToken; + } +} diff --git a/src/Events/ClientAuthenticationFailed.php b/src/Events/ClientAuthenticationFailed.php new file mode 100644 index 000000000..f6cb73955 --- /dev/null +++ b/src/Events/ClientAuthenticationFailed.php @@ -0,0 +1,25 @@ +name = 'client.authentication.failed'; + $this->clientId = $clientId; + } + + public function getClientId(): ?string + { + return $this->clientId; + } +} diff --git a/src/Events/EventDispatcherAwareInterface.php b/src/Events/EventDispatcherAwareInterface.php new file mode 100644 index 000000000..93efa03fd --- /dev/null +++ b/src/Events/EventDispatcherAwareInterface.php @@ -0,0 +1,10 @@ +eventDispatcher = $eventDispatcher; + } + + protected function dispatchEvent(object $event): object + { + if ($this->eventDispatcher !== null) { + return $this->eventDispatcher->dispatch($event); + } + + return $event; + } +} diff --git a/src/Events/RefreshTokenClientFailed.php b/src/Events/RefreshTokenClientFailed.php new file mode 100644 index 000000000..0276c860c --- /dev/null +++ b/src/Events/RefreshTokenClientFailed.php @@ -0,0 +1,35 @@ +name = 'refresh_token.client.failed'; + $this->clientId = $clientId; + $this->refreshTokenData = $refreshTokenData; + } + + public function getClientId(): ?string + { + return $this->clientId; + } + + public function getRefreshTokenData(): array + { + return $this->refreshTokenData; + } +} diff --git a/src/Events/RefreshTokenIssued.php b/src/Events/RefreshTokenIssued.php new file mode 100644 index 000000000..97a27c09b --- /dev/null +++ b/src/Events/RefreshTokenIssued.php @@ -0,0 +1,26 @@ +name = 'refresh_token.issued'; + $this->refreshToken = $refreshToken; + } + + public function getRefreshToken(): RefreshTokenEntityInterface + { + return $this->refreshToken; + } +} diff --git a/src/Events/UserAuthenticationFailed.php b/src/Events/UserAuthenticationFailed.php new file mode 100644 index 000000000..65f50bd01 --- /dev/null +++ b/src/Events/UserAuthenticationFailed.php @@ -0,0 +1,25 @@ +name = 'user.authentication.failed'; + $this->username = $username; + } + + public function getUsername(): ?string + { + return $this->username; + } +} diff --git a/src/Grant/AbstractGrant.php b/src/Grant/AbstractGrant.php index 0a5b514fc..9936e003a 100644 --- a/src/Grant/AbstractGrant.php +++ b/src/Grant/AbstractGrant.php @@ -14,7 +14,6 @@ use DateTimeImmutable; use Error; use Exception; -use League\Event\EmitterAwareTrait; use League\OAuth2\Server\CryptKey; use League\OAuth2\Server\CryptTrait; use League\OAuth2\Server\Entities\AccessTokenEntityInterface; @@ -22,6 +21,8 @@ use League\OAuth2\Server\Entities\ClientEntityInterface; use League\OAuth2\Server\Entities\RefreshTokenEntityInterface; use League\OAuth2\Server\Entities\ScopeEntityInterface; +use League\OAuth2\Server\Events\ClientAuthenticationFailed; +use League\OAuth2\Server\Events\EventDispatcherAwareTrait; use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException; use League\OAuth2\Server\RedirectUriValidators\RedirectUriValidator; @@ -31,7 +32,6 @@ use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; use League\OAuth2\Server\Repositories\ScopeRepositoryInterface; use League\OAuth2\Server\Repositories\UserRepositoryInterface; -use League\OAuth2\Server\RequestEvent; use League\OAuth2\Server\RequestTypes\AuthorizationRequest; use LogicException; use Psr\Http\Message\ServerRequestInterface; @@ -42,7 +42,7 @@ */ abstract class AbstractGrant implements GrantTypeInterface { - use EmitterAwareTrait, CryptTrait; + use EventDispatcherAwareTrait, CryptTrait; const SCOPE_DELIMITER_STRING = ' '; @@ -194,7 +194,7 @@ protected function validateClient(ServerRequestInterface $request) [$clientId, $clientSecret] = $this->getClientCredentials($request); if ($this->clientRepository->validateClient($clientId, $clientSecret, $this->getIdentifier()) === false) { - $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); + $this->dispatchEvent(new ClientAuthenticationFailed($clientId, $request)); throw OAuthServerException::invalidClient($request); } @@ -235,7 +235,7 @@ protected function getClientEntityOrFail($clientId, ServerRequestInterface $requ $client = $this->clientRepository->getClientEntity($clientId); if ($client instanceof ClientEntityInterface === false) { - $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); + $this->dispatchEvent(new ClientAuthenticationFailed($clientId, $request)); throw OAuthServerException::invalidClient($request); } @@ -286,7 +286,7 @@ protected function validateRedirectUri( ) { $validator = new RedirectUriValidator($client->getRedirectUri()); if (!$validator->validateRedirectUri($redirectUri)) { - $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); + $this->dispatchEvent(new ClientAuthenticationFailed($client->getIdentifier(), $request)); throw OAuthServerException::invalidClient($request); } } diff --git a/src/Grant/AuthCodeGrant.php b/src/Grant/AuthCodeGrant.php index 3fac0344e..b7e59cfcc 100644 --- a/src/Grant/AuthCodeGrant.php +++ b/src/Grant/AuthCodeGrant.php @@ -17,12 +17,12 @@ use League\OAuth2\Server\CodeChallengeVerifiers\S256Verifier; use League\OAuth2\Server\Entities\ClientEntityInterface; use League\OAuth2\Server\Entities\UserEntityInterface; +use League\OAuth2\Server\Events\AccessTokenIssued; +use League\OAuth2\Server\Events\ClientAuthenticationFailed; +use League\OAuth2\Server\Events\RefreshTokenIssued; use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface; use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; -use League\OAuth2\Server\RequestAccessTokenEvent; -use League\OAuth2\Server\RequestEvent; -use League\OAuth2\Server\RequestRefreshTokenEvent; use League\OAuth2\Server\RequestTypes\AuthorizationRequest; use League\OAuth2\Server\ResponseTypes\RedirectResponse; use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; @@ -164,14 +164,14 @@ public function respondToAccessTokenRequest( // Issue and persist new access token $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $authCodePayload->user_id, $scopes); - $this->getEmitter()->emit(new RequestAccessTokenEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request, $accessToken)); + $this->dispatchEvent(new AccessTokenIssued($accessToken, $request)); $responseType->setAccessToken($accessToken); // Issue and persist new refresh token if given $refreshToken = $this->issueRefreshToken($accessToken); if ($refreshToken !== null) { - $this->getEmitter()->emit(new RequestRefreshTokenEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request, $refreshToken)); + $this->dispatchEvent(new RefreshTokenIssued($refreshToken, $request)); $responseType->setRefreshToken($refreshToken); } @@ -269,7 +269,7 @@ public function validateAuthorizationRequest(ServerRequestInterface $request) $this->validateRedirectUri($redirectUri, $client, $request); } elseif (empty($client->getRedirectUri()) || (\is_array($client->getRedirectUri()) && \count($client->getRedirectUri()) !== 1)) { - $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); + $this->dispatchEvent(new ClientAuthenticationFailed($clientId, $request)); throw OAuthServerException::invalidClient($request); } diff --git a/src/Grant/ClientCredentialsGrant.php b/src/Grant/ClientCredentialsGrant.php index d342b269f..9148b0b45 100644 --- a/src/Grant/ClientCredentialsGrant.php +++ b/src/Grant/ClientCredentialsGrant.php @@ -12,9 +12,9 @@ namespace League\OAuth2\Server\Grant; use DateInterval; +use League\OAuth2\Server\Events\AccessTokenIssued; +use League\OAuth2\Server\Events\ClientAuthenticationFailed; use League\OAuth2\Server\Exception\OAuthServerException; -use League\OAuth2\Server\RequestAccessTokenEvent; -use League\OAuth2\Server\RequestEvent; use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; use Psr\Http\Message\ServerRequestInterface; @@ -36,7 +36,7 @@ public function respondToAccessTokenRequest( $client = $this->getClientEntityOrFail($clientId, $request); if (!$client->isConfidential()) { - $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); + $this->dispatchEvent(new ClientAuthenticationFailed($clientId, $request)); throw OAuthServerException::invalidClient($request); } @@ -53,7 +53,7 @@ public function respondToAccessTokenRequest( $accessToken = $this->issueAccessToken($accessTokenTTL, $client, null, $finalizedScopes); // Send event to emitter - $this->getEmitter()->emit(new RequestAccessTokenEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request, $accessToken)); + $this->dispatchEvent(new AccessTokenIssued($accessToken, $request)); // Inject access token into response type $responseType->setAccessToken($accessToken); diff --git a/src/Grant/GrantTypeInterface.php b/src/Grant/GrantTypeInterface.php index 41ebeb5ff..10261d1f6 100644 --- a/src/Grant/GrantTypeInterface.php +++ b/src/Grant/GrantTypeInterface.php @@ -13,8 +13,8 @@ use DateInterval; use Defuse\Crypto\Key; -use League\Event\EmitterAwareInterface; use League\OAuth2\Server\CryptKey; +use League\OAuth2\Server\Events\EventDispatcherAwareInterface; use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; use League\OAuth2\Server\Repositories\ClientRepositoryInterface; use League\OAuth2\Server\Repositories\ScopeRepositoryInterface; @@ -25,7 +25,7 @@ /** * Grant type interface. */ -interface GrantTypeInterface extends EmitterAwareInterface +interface GrantTypeInterface extends EventDispatcherAwareInterface { /** * Set refresh token TTL. diff --git a/src/Grant/ImplicitGrant.php b/src/Grant/ImplicitGrant.php index 0bd91d5ac..226a67000 100644 --- a/src/Grant/ImplicitGrant.php +++ b/src/Grant/ImplicitGrant.php @@ -11,9 +11,9 @@ use DateInterval; use League\OAuth2\Server\Entities\UserEntityInterface; +use League\OAuth2\Server\Events\ClientAuthenticationFailed; use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; -use League\OAuth2\Server\RequestEvent; use League\OAuth2\Server\RequestTypes\AuthorizationRequest; use League\OAuth2\Server\ResponseTypes\RedirectResponse; use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; @@ -136,7 +136,7 @@ public function validateAuthorizationRequest(ServerRequestInterface $request) $this->validateRedirectUri($redirectUri, $client, $request); } elseif (\is_array($client->getRedirectUri()) && \count($client->getRedirectUri()) !== 1 || empty($client->getRedirectUri())) { - $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request)); + $this->dispatchEvent(new ClientAuthenticationFailed($client->getIdentifier(), $request)); throw OAuthServerException::invalidClient($request); } else { $redirectUri = \is_array($client->getRedirectUri()) diff --git a/src/Grant/PasswordGrant.php b/src/Grant/PasswordGrant.php index fd32d2688..272776302 100644 --- a/src/Grant/PasswordGrant.php +++ b/src/Grant/PasswordGrant.php @@ -14,12 +14,12 @@ use DateInterval; use League\OAuth2\Server\Entities\ClientEntityInterface; use League\OAuth2\Server\Entities\UserEntityInterface; +use League\OAuth2\Server\Events\AccessTokenIssued; +use League\OAuth2\Server\Events\RefreshTokenIssued; +use League\OAuth2\Server\Events\UserAuthenticationFailed; use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; use League\OAuth2\Server\Repositories\UserRepositoryInterface; -use League\OAuth2\Server\RequestAccessTokenEvent; -use League\OAuth2\Server\RequestEvent; -use League\OAuth2\Server\RequestRefreshTokenEvent; use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; use Psr\Http\Message\ServerRequestInterface; @@ -60,14 +60,14 @@ public function respondToAccessTokenRequest( // Issue and persist new access token $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $user->getIdentifier(), $finalizedScopes); - $this->getEmitter()->emit(new RequestAccessTokenEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request, $accessToken)); + $this->dispatchEvent(new AccessTokenIssued($accessToken, $request)); $responseType->setAccessToken($accessToken); // Issue and persist new refresh token if given $refreshToken = $this->issueRefreshToken($accessToken); if ($refreshToken !== null) { - $this->getEmitter()->emit(new RequestRefreshTokenEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request, $refreshToken)); + $this->dispatchEvent(new RefreshTokenIssued($refreshToken, $request)); $responseType->setRefreshToken($refreshToken); } @@ -104,7 +104,7 @@ protected function validateUser(ServerRequestInterface $request, ClientEntityInt ); if ($user instanceof UserEntityInterface === false) { - $this->getEmitter()->emit(new RequestEvent(RequestEvent::USER_AUTHENTICATION_FAILED, $request)); + $this->dispatchEvent(new UserAuthenticationFailed($username, $request)); throw OAuthServerException::invalidCredentials(); } diff --git a/src/Grant/RefreshTokenGrant.php b/src/Grant/RefreshTokenGrant.php index 2dedf15c3..324169175 100644 --- a/src/Grant/RefreshTokenGrant.php +++ b/src/Grant/RefreshTokenGrant.php @@ -13,11 +13,11 @@ use DateInterval; use Exception; +use League\OAuth2\Server\Events\AccessTokenIssued; +use League\OAuth2\Server\Events\RefreshTokenClientFailed; +use League\OAuth2\Server\Events\RefreshTokenIssued; use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; -use League\OAuth2\Server\RequestAccessTokenEvent; -use League\OAuth2\Server\RequestEvent; -use League\OAuth2\Server\RequestRefreshTokenEvent; use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; use Psr\Http\Message\ServerRequestInterface; @@ -71,7 +71,7 @@ public function respondToAccessTokenRequest( // Issue and persist new access token $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $oldRefreshToken['user_id'], $scopes); - $this->getEmitter()->emit(new RequestAccessTokenEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request, $accessToken)); + $this->dispatchEvent(new AccessTokenIssued($accessToken, $request)); $responseType->setAccessToken($accessToken); // Issue and persist new refresh token if given @@ -79,7 +79,7 @@ public function respondToAccessTokenRequest( $refreshToken = $this->issueRefreshToken($accessToken); if ($refreshToken !== null) { - $this->getEmitter()->emit(new RequestRefreshTokenEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request, $refreshToken)); + $this->dispatchEvent(new RefreshTokenIssued($refreshToken, $request)); $responseType->setRefreshToken($refreshToken); } } @@ -111,7 +111,7 @@ protected function validateOldRefreshToken(ServerRequestInterface $request, $cli $refreshTokenData = \json_decode($refreshToken, true); if ($refreshTokenData['client_id'] !== $clientId) { - $this->getEmitter()->emit(new RequestEvent(RequestEvent::REFRESH_TOKEN_CLIENT_FAILED, $request)); + $this->dispatchEvent(new RefreshTokenClientFailed($clientId, $refreshTokenData, $request)); throw OAuthServerException::invalidRefreshToken('Token is not linked to client'); } diff --git a/src/RequestEvent.php b/src/RequestEvent.php deleted file mode 100644 index b1ca3f6b8..000000000 --- a/src/RequestEvent.php +++ /dev/null @@ -1,49 +0,0 @@ - - * @copyright Copyright (c) Alex Bilbie - * @license http://mit-license.org/ - * - * @link https://github.com/thephpleague/oauth2-server - */ - -namespace League\OAuth2\Server; - -use League\Event\Event; -use Psr\Http\Message\ServerRequestInterface; - -class RequestEvent extends Event -{ - const CLIENT_AUTHENTICATION_FAILED = 'client.authentication.failed'; - const USER_AUTHENTICATION_FAILED = 'user.authentication.failed'; - const REFRESH_TOKEN_CLIENT_FAILED = 'refresh_token.client.failed'; - - const REFRESH_TOKEN_ISSUED = 'refresh_token.issued'; - const ACCESS_TOKEN_ISSUED = 'access_token.issued'; - - /** - * @var ServerRequestInterface - */ - private $request; - - /** - * RequestEvent constructor. - * - * @param string $name - * @param ServerRequestInterface $request - */ - public function __construct($name, ServerRequestInterface $request) - { - parent::__construct($name); - $this->request = $request; - } - - /** - * @return ServerRequestInterface - * @codeCoverageIgnore - */ - public function getRequest() - { - return $this->request; - } -}