Last active
January 2, 2016 09:09
-
-
Save mathewka/8281158 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| <?php | |
| namespace Api\Factory; | |
| use Zend\ServiceManager\FactoryInterface; | |
| use Zend\ServiceManager\ServiceLocatorInterface; | |
| use Api\Controller\DbResolver; | |
| use Zend\Authentication\Adapter\DbTable as AuthAdapter; | |
| class AuthAdapterFactory implements FactoryInterface | |
| { | |
| public function createService(ServiceLocatorInterface $sl) | |
| { | |
| $db = $sl->get('Zend\Db\Adapter\Adapter'); | |
| $adapter = new AuthAdapter($db, | |
| 'users', | |
| 'user_login', | |
| 'user_password', | |
| 'MD5(?)' | |
| ); | |
| return $adapter; | |
| } | |
| } |
This file contains hidden or 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
| <?php | |
| /** | |
| * Zend Framework (http://framework.zend.com/) | |
| * | |
| * @link http://github.com/zendframework/zf2 for the canonical source repository | |
| * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | |
| * @license http://framework.zend.com/license/new-bsd New BSD License | |
| */ | |
| namespace Api\Controller; | |
| use Zend\Stdlib\ErrorHandler; | |
| use Zend\Authentication\Adapter\Http\ResolverInterface; | |
| use Zend\Authentication\Result as AuthResult; | |
| use Zend\Authentication\Adapter\DbTable as AuthAdapter; | |
| /** | |
| * HTTP Authentication File Resolver | |
| */ | |
| class DbResolver implements ResolverInterface | |
| { | |
| protected $adapter; | |
| function __construct(AuthAdapter $adapter){ | |
| $this->adapter = $adapter; | |
| } | |
| /** | |
| * Resolve credentials | |
| * | |
| * Only the first matching username/realm combination in the file is | |
| * returned. If the file contains credentials for Digest authentication, | |
| * the returned string is the password hash, or h(a1) from RFC 2617. The | |
| * returned string is the plain-text password for Basic authentication. | |
| * | |
| * The expected format of the file is: | |
| * username:realm:sharedSecret | |
| * | |
| * That is, each line consists of the user's username, the applicable | |
| * authentication realm, and the password or hash, each delimited by | |
| * colons. | |
| * | |
| * @param string $username Username | |
| * @param string $realm Authentication Realm | |
| * @return string|false User's shared secret, if the user is found in the | |
| * realm, false otherwise. | |
| * @throws Exception\ExceptionInterface | |
| */ | |
| public function resolve($username, $realm, $password = null) | |
| { | |
| if (empty($username)) { | |
| throw new Exception\InvalidArgumentException('Username is required'); | |
| } elseif (!ctype_print($username) || strpos($username, ':') !== false) { | |
| throw new Exception\InvalidArgumentException('Username must consist only of printable characters, ' | |
| . 'excluding the colon'); | |
| } | |
| if (empty($realm)) { | |
| throw new Exception\InvalidArgumentException('Realm is required'); | |
| } elseif (!ctype_print($realm) || strpos($realm, ':') !== false) { | |
| throw new Exception\InvalidArgumentException('Realm must consist only of printable characters, ' | |
| . 'excluding the colon.'); | |
| } | |
| // Open file, read through looking for matching credentials | |
| ErrorHandler::start(E_WARNING); | |
| $this->adapter->setIdentity($username)->setCredential($password); | |
| $select = $this->adapter->getDbSelect(); | |
| $result = $this->adapter->authenticate(); | |
| return $result; | |
| } | |
| } |
This file contains hidden or 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
| <?php | |
| namespace Api\Factory; | |
| use Zend\ServiceManager\FactoryInterface; | |
| use Zend\ServiceManager\ServiceLocatorInterface; | |
| use Api\Controller\DbResolver; | |
| class DbResolverFactory implements FactoryInterface | |
| { | |
| public function createService(ServiceLocatorInterface $sl) | |
| { | |
| $authAdapter = $sl->get('AuthAdapter'); | |
| $resolver = new DbResolver($authAdapter); | |
| return $resolver; | |
| } | |
| } |
This file contains hidden or 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
| 'db' => array( | |
| 'driver' => 'Pdo', | |
| 'dsn' => 'mysql:dbname=dbname;host=localhost', | |
| ), | |
| 'service_manager' => array( | |
| 'factories' => array( | |
| 'AuthAdapter' => 'Api\Factory\AuthAdapterFactory', | |
| 'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory', | |
| 'DbResolver' => 'Api\Factory\DbResolverFactory', | |
| ), | |
| ), |
This file contains hidden or 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
| $adapter = new Http($conf); | |
| $setRequest = $e->getRequest(); | |
| $setResponse = $e->getResponse(); | |
| $sm = $e->getApplication()->getServiceManager(); | |
| $resolver = $sm->get('DbResolver'); | |
| $adapter->setBasicResolver($resolver); | |
| $adapter->setRequest($setRequest); | |
| $adapter->setResponse($setResponse); | |
| // $adapter->setBasicResolver(""); | |
| $result = $adapter->authenticate(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Factory for Resolver:
Then register it as a service:
Then in your module.php: