-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added autologin option in querystring to login with access token
If a user is allready login, we simply redirect without reauthenticate Added doc of autologin Fixed, autologin when no token is aviaible
- Loading branch information
1 parent
2fc6cde
commit 88ea553
Showing
12 changed files
with
311 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
namespace Ant\Bundle\ChateaSecureBundle\DependencyInjection\Factory; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\DependencyInjection\DefinitionDecorator; | ||
use Symfony\Component\Config\Definition\Builder\NodeDefinition; | ||
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface; | ||
|
||
class AutologinSecurityFactory implements SecurityFactoryInterface | ||
{ | ||
public function getKey() | ||
{ | ||
return 'antwebs_chateasecure_login'; | ||
} | ||
|
||
protected function getListenerId() | ||
{ | ||
return 'security.authentication.listener.autologin'; | ||
} | ||
|
||
public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint) | ||
{ | ||
$providerId = 'ecurity.authentication_provider.antwebs_chateasecure.'.$id; | ||
$container | ||
->setDefinition($providerId, new DefinitionDecorator('security.authentication_provider.antwebs_chateasecure')) | ||
->replaceArgument(0, new Reference($userProvider)) | ||
; | ||
|
||
$listenerId = 'ant_bundle.chateasecurebundle.security.firewall.autologinlistener.'.$id; | ||
$listener = $container->setDefinition($listenerId, new DefinitionDecorator('ant_bundle.chateasecurebundle.security.firewall.autologinlistener')); | ||
|
||
return array($providerId, $listenerId, $defaultEntryPoint); | ||
} | ||
|
||
/** | ||
* Defines the position at which the provider is called. | ||
* Possible values: pre_auth, form, http, and remember_me. | ||
* | ||
* @return string | ||
*/ | ||
public function getPosition() | ||
{ | ||
return 'pre_auth'; | ||
} | ||
|
||
public function addConfiguration(NodeDefinition $builder) | ||
{ | ||
} | ||
} |
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
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,77 @@ | ||
<?php | ||
|
||
namespace Ant\Bundle\ChateaSecureBundle\Security\Firewall; | ||
|
||
use Ant\Bundle\ChateaSecureBundle\Security\Token\AccessTokenToken; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Security\Core\SecurityContextInterface; | ||
use Symfony\Component\Security\Http\Firewall\ListenerInterface; | ||
|
||
class AutologinListener implements ListenerInterface | ||
{ | ||
protected $securityContext; | ||
protected $authenticationManager; | ||
|
||
/** | ||
* @param SecurityContextInterface $securityContext | ||
* @param AuthenticationManagerInterface $authenticationManager | ||
*/ | ||
public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager) | ||
{ | ||
$this->securityContext = $securityContext; | ||
$this->authenticationManager = $authenticationManager; | ||
} | ||
|
||
/** | ||
* This interface must be implemented by firewall listeners. | ||
* | ||
* @param GetResponseEvent $event | ||
*/ | ||
public function handle(GetResponseEvent $event) | ||
{ | ||
$request = $event->getRequest(); | ||
|
||
if($request->query->has('autologin')){ | ||
$token = new AccessTokenToken($request->query->get('autologin')); | ||
|
||
try{ | ||
$this->authenticateIfUserIsNotLoggedIn($token); | ||
$this->setRedirectResponse($event); | ||
}catch(\Exception $failed) { | ||
$this->setRedirectResponse($event); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param GetResponseEvent $event | ||
* @param $request | ||
*/ | ||
private function setRedirectResponse(GetResponseEvent $event) | ||
{ | ||
$request = $event->getRequest(); | ||
|
||
$request->query->remove('autologin'); | ||
$request->overrideGlobals(); | ||
|
||
$redirectResponse = new RedirectResponse($request->getUri()); | ||
$event->setResponse($redirectResponse); | ||
} | ||
|
||
/** | ||
* @param $token | ||
*/ | ||
private function authenticateIfUserIsNotLoggedIn($token) | ||
{ | ||
if($this->securityContext->getToken() !== null && $this->securityContext->isGranted('IS_AUTHENTICATED_FULLY')){ | ||
return; | ||
} | ||
|
||
$authToken = $this->authenticationManager->authenticate($token); | ||
|
||
$this->securityContext->setToken($authToken); | ||
} | ||
} |
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,23 @@ | ||
<?php | ||
|
||
namespace Ant\Bundle\ChateaSecureBundle\Security\Token; | ||
|
||
|
||
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | ||
|
||
class AccessTokenToken extends UsernamePasswordToken | ||
{ | ||
public function __construct($accessToken, $roles = array()) | ||
{ | ||
$this->accessToken = $accessToken; | ||
parent::__construct('', $accessToken, 'access-token', $roles); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getAccessToken() | ||
{ | ||
return $this->accessToken; | ||
} | ||
} |
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
Oops, something went wrong.