php - Symfony FOS OAuth with custom User -
how possible implement oauth server based on fosoauthserverbundle without using fosuserbundle?
my user class starts his:
<?php namespace appbundle\entity; use doctrine\orm\mapping orm; use symfony\component\security\core\user\userinterface; /** * * @orm\entity * @orm\table(name="user") * @orm\entity(repositoryclass="appbundle\entity\userrepository") */ class user implements userinterface my user repository class starts this:
<?php namespace appbundle\entity; use symfony\bridge\doctrine\security\user\userloaderinterface; use symfony\component\security\core\user\userproviderinterface; use symfony\component\security\core\user\userinterface; use symfony\component\security\core\exception\unsupporteduserexception; use doctrine\orm\entityrepository; use doctrine\orm\noresultexception; class userrepository extends entityrepository implements userloaderinterface, userproviderinterface in security.yml included following:
providers: user_db: entity: class: appbundle\entity\user property: username and config.yml:
fos_oauth_server: db_driver: orm client_class: appbundle\entity\oauthclient access_token_class: appbundle\entity\oauthaccesstoken refresh_token_class: appbundle\entity\oauthrefreshtoken auth_code_class: appbundle\entity\oauthauthcode service: options: access_token_lifetime: 3600 user_provider: user_db #user_provider: appbundle\entity\userrepository #user_provider: appbundle\entity\user right throws exception:
[1] symfony\component\debug\exception\fatalthrowableerror: call member function loaduserbyusername() on null @ n/a in /home/wanderson/api/vendor/friendsofsymfony/oauth-server-bundle/storage/oauthstorage.php line 161 @ fos\oauthserverbundle\storage\oauthstorage->checkusercredentials(object(oauthclient), 'admin', 'admin') in /home/wanderson/api/vendor/friendsofsymfony/oauth2-php/lib/oauth2.php line 929 @ oauth2\oauth2->grantaccesstokenusercredentials(object(oauthclient), array('grant_type' => 'password', 'scope' => null, 'code' => null, 'redirect_uri' => null, 'username' => 'admin', 'password' => 'admin', 'refresh_token' => null)) in /home/wanderson/api/vendor/friendsofsymfony/oauth2-php/lib/oauth2.php line 815 @ oauth2\oauth2->grantaccesstoken(object(request)) in /home/wanderson/api/vendor/friendsofsymfony/oauth-server-bundle/controller/tokencontroller.php line 42 @ fos\oauthserverbundle\controller\tokencontroller->tokenaction(object(request)) in line @ call_user_func_array(array(object(tokencontroller), 'tokenaction'), array(object(request))) in /home/wanderson/api/vendor/symfony/symfony/src/symfony/component/httpkernel/httpkernel.php line 153 @ symfony\component\httpkernel\httpkernel->handleraw(object(request), '1') in /home/wanderson/api/vendor/symfony/symfony/src/symfony/component/httpkernel/httpkernel.php line 68 @ symfony\component\httpkernel\httpkernel->handle(object(request), '1', true) in /home/wanderson/api/vendor/symfony/symfony/src/symfony/component/httpkernel/kernel.php line 169 @ symfony\component\httpkernel\kernel->handle(object(request)) in /home/wanderson/api/web/app_dev.php line 30 @ require('/home/wanderson/api/web/app_dev.php') in /home/wanderson/api/vendor/symfony/symfony/src/symfony/bundle/frameworkbundle/resources/config/router_dev.php line 40
create user provider, or if want use repository class read 'how define repository service' (i don't know working if @ newest version or not):
https://stackoverflow.com/a/17230333/6848076 https://stackoverflow.com/a/31807608/6848076
but not recommended way
<?php namespace appbundle\security\provider\userprovider; use doctrine\common\persistence\objectrepository; use doctrine\orm\noresultexception; use symfony\component\security\core\exception\usernamenotfoundexception; use symfony\component\security\core\user\userinterface; use symfony\component\security\core\user\userproviderinterface; use symfony\component\serializer\exception\unsupportedexception; use doctrine\orm\entitymanager; class userprovider implements userproviderinterface { protected $class; protected $userrepository; public function __construct(entitymanager $entitymanager, $class) { $this->class = $class; $this->userrepository = $entitymanager->getrepository($class); } public function loaduserbyusername($username) { $user = $this->userrepository->findoneby(array('username' => $username)); if (null === $user) { $message = sprintf( 'unable find active user object identified "%s"', $username ); throw new usernamenotfoundexception($message, 0, $failed); } return $user; } public function refreshuser(userinterface $user) { $class = get_class($user); if (false == $this->supportsclass($class)) { throw new unsupportedexception( sprintf( 'instances of "%s" not supported', $class ) ); } return $this->userrepository->find($user->getid()); } public function supportsclass($class) { return $this->class === $class || is_subclass_of($class, $this->class); } } about services can read: http://symfony.com/doc/current/service_container.html define provider service @ {project_directory}/src/appbundle/resources/config/service.yml
parameters: user.class: appbundle\entity\user user.provider.class: appbundle\security\provider\userprovider services: user.provider: class: %user.provider.class% arguments: [@doctrine.orm.entity_manager, %user.class%] change fosoauth configuration @ config.yml:
fos_oauth_server: db_driver: orm client_class: appbundle\entity\oauthclient access_token_class: appbundle\entity\oauthaccesstoken refresh_token_class: appbundle\entity\oauthrefreshtoken auth_code_class: appbundle\entity\oauthauthcode service: options: access_token_lifetime: 3600 user_provider: user.provider about security.yml can read: http://symfony.com/doc/current/security.html
Comments
Post a Comment