-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from gepo/extendability
Allows user to use custom user provider
- Loading branch information
Showing
8 changed files
with
106 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
use Symfony\Component\Security\Core\User\UserProviderInterface; | ||
use FOS\UserBundle\Model\UserInterface; | ||
use Uecode\Bundle\ApiKeyBundle\Security\Authentication\Token\ApiKeyUserToken; | ||
use Uecode\Bundle\ApiKeyBundle\Security\Authentication\Provider\ApiKeyUserProviderInterface; | ||
|
||
/** | ||
* @author Aaron Scherer <[email protected]> | ||
|
@@ -58,9 +59,9 @@ public function authenticate(TokenInterface $token) | |
* @return bool|ApiKeyUserToken | ||
* @throws AuthenticationException | ||
*/ | ||
protected function doAuth(UserProviderInterface $provider, TokenInterface $token) | ||
protected function doAuth($provider, TokenInterface $token) | ||
{ | ||
if (!method_exists($provider, 'loadUserByApiKey')) { | ||
if (! $provider instanceof ApiKeyUserProviderInterface) { | ||
return false; | ||
} | ||
|
||
|
15 changes: 15 additions & 0 deletions
15
...code/Bundle/ApiKeyBundle/Security/Authentication/Provider/ApiKeyUserProviderInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
namespace Uecode\Bundle\ApiKeyBundle\Security\Authentication\Provider; | ||
|
||
/** | ||
* @author Gennady Telegin <[email protected]> | ||
*/ | ||
interface ApiKeyUserProviderInterface | ||
{ | ||
/** | ||
* @param string $apiKey | ||
* | ||
* @return UserInterface | ||
*/ | ||
public function loadUserByApiKey($apiKey); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,17 +8,15 @@ | |
/** | ||
* @author Aaron Scherer <[email protected]> | ||
*/ | ||
class UserProvider extends FOSUserProvider | ||
class UserProvider extends FOSUserProvider implements ApiKeyUserProviderInterface | ||
{ | ||
/** | ||
* @var bool Stateless Authentication? | ||
*/ | ||
private $stateless = false; | ||
|
||
/** | ||
* @param $apiKey | ||
* | ||
* @return UserInterface | ||
* {@inheritdoc} | ||
*/ | ||
public function loadUserByApiKey($apiKey) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Uecode\Bundle\ApiKeyBundle\Util; | ||
|
||
/** | ||
* @author Gennady Telegin <[email protected]> | ||
*/ | ||
class ApiKeyGenerator implements ApiKeyGeneratorInterface | ||
{ | ||
private $characterSet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | ||
private $apiKeyLength = 64; | ||
|
||
public function generateApiKey() | ||
{ | ||
return self::generate($this->characterSet, $this->apiKeyLength); | ||
} | ||
|
||
public static function generate($characterSet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', $apiKeyLength = 64) | ||
{ | ||
$characterSetLength = strlen($characterSet); | ||
|
||
$apikey = ''; | ||
for ($i = 0; $i < $apiKeyLength; ++$i) { | ||
$apikey .= $characterSet[rand(0, $characterSetLength - 1)]; | ||
} | ||
|
||
return rtrim(base64_encode(sha1(uniqid('ue' . rand(rand(), rand())) . $apikey)), '='); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Uecode/Bundle/ApiKeyBundle/Util/ApiKeyGeneratorInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Uecode\Bundle\ApiKeyBundle\Util; | ||
|
||
/** | ||
* @author Gennady Telegin <[email protected]> | ||
*/ | ||
interface ApiKeyGeneratorInterface | ||
{ | ||
public function generateApiKey(); | ||
} |