From 042cbd587f781575886992f1780e82122525b6bb Mon Sep 17 00:00:00 2001 From: lilHermit Date: Mon, 27 Apr 2020 13:18:01 +0100 Subject: [PATCH 1/3] Added support for CallbackIdentifier to return Result --- src/Identifier/CallbackIdentifier.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Identifier/CallbackIdentifier.php b/src/Identifier/CallbackIdentifier.php index ef23f416..2b817367 100644 --- a/src/Identifier/CallbackIdentifier.php +++ b/src/Identifier/CallbackIdentifier.php @@ -17,6 +17,7 @@ namespace Authentication\Identifier; use ArrayAccess; +use Authentication\Authenticator\Result; use InvalidArgumentException; use RuntimeException; @@ -70,6 +71,10 @@ public function identify(array $data) $callback = $this->getConfig('callback'); $result = $callback($data); + if ($result instanceof Result) { + $this->_errors = $result->getErrors(); + return $result->getData(); + } if ($result === null || $result instanceof ArrayAccess) { return $result; } From 1ed7a4905bc4d75dd637215bfb9dc5ddb9a5f751 Mon Sep 17 00:00:00 2001 From: lilHermit Date: Fri, 8 May 2020 16:30:59 +0100 Subject: [PATCH 2/3] Added tests --- .../Identifier/CallbackIdentifierTest.php | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/TestCase/Identifier/CallbackIdentifierTest.php b/tests/TestCase/Identifier/CallbackIdentifierTest.php index d110dd24..e3bb4eed 100644 --- a/tests/TestCase/Identifier/CallbackIdentifierTest.php +++ b/tests/TestCase/Identifier/CallbackIdentifierTest.php @@ -17,6 +17,7 @@ namespace Authentication\Test\TestCase\Identifier; use ArrayAccess; +use Authentication\Authenticator\Result; use Authentication\Identifier\CallbackIdentifier; use Authentication\Test\TestCase\AuthenticationTestCase as TestCase; use Cake\ORM\Entity; @@ -113,4 +114,31 @@ public function testInvalidReturnValue() ]); $identifier->identify([]); } + + /** + * testResultReturn + * + * @return void + */ + public function testResultReturn() + { + $identifier = new CallbackIdentifier([ + 'callback' => function ($data) { + if (isset($data['username']) && $data['username'] === 'florian') { + return new Result(new Entity($data), Result::SUCCESS); + } + + return new Result(null, Result::FAILURE_OTHER, ['message' => 'Access denied by 3rd party API']); + }, + ]); + $result = $identifier->identify(['username' => 'florian']); + + $this->assertInstanceOf(Entity::class, $result); + $this->assertEquals('florian', $result->username); + + $result = $identifier->identify(['username' => 'larry']); + + $this->assertNull($result); + $this->assertEquals(['message' => 'Access denied by 3rd party API'], $identifier->getErrors()); + } } From e359dd0e99b6756c7b648e37281d65c8ce173735 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Fri, 8 May 2020 15:32:56 +0000 Subject: [PATCH 3/3] Fixing style errors. --- src/Identifier/CallbackIdentifier.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Identifier/CallbackIdentifier.php b/src/Identifier/CallbackIdentifier.php index 2b817367..05b0f2e6 100644 --- a/src/Identifier/CallbackIdentifier.php +++ b/src/Identifier/CallbackIdentifier.php @@ -73,6 +73,7 @@ public function identify(array $data) $result = $callback($data); if ($result instanceof Result) { $this->_errors = $result->getErrors(); + return $result->getData(); } if ($result === null || $result instanceof ArrayAccess) {