Skip to content

Commit

Permalink
Merge pull request #373 from lilHermit/feature/callback-result-support
Browse files Browse the repository at this point in the history
Feature/callback Result support
  • Loading branch information
markstory authored May 9, 2020
2 parents 26f552a + e359dd0 commit b5a1779
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Identifier/CallbackIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace Authentication\Identifier;

use ArrayAccess;
use Authentication\Authenticator\Result;
use InvalidArgumentException;
use RuntimeException;

Expand Down Expand Up @@ -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;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/TestCase/Identifier/CallbackIdentifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit b5a1779

Please sign in to comment.