diff --git a/src/Identifier/CallbackIdentifier.php b/src/Identifier/CallbackIdentifier.php index ef23f416..05b0f2e6 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,11 @@ 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; } 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()); + } }