Skip to content

Instantly share code, notes, and snippets.

@diolektor
Created January 19, 2017 13:02
Show Gist options
  • Save diolektor/d17904347e11ad516831b01dd622dad9 to your computer and use it in GitHub Desktop.
Save diolektor/d17904347e11ad516831b01dd622dad9 to your computer and use it in GitHub Desktop.
<?php
require __DIR__ . '/vendor/autoload.php';
# security-core
//"symfony/event-dispatcher": "~2.8|~3.0",
//"symfony/expression-language": "~2.8|~3.0",
//"symfony/http-foundation": "~2.8|~3.0",
//"symfony/ldap": "~3.1",
//"symfony/validator": "~2.8|~3.0",
//"psr/log": "~1.0"
# security-csrf
//"symfony/polyfill-php56": "~1.0",
//"symfony/polyfill-php70": "~1.0",
//"symfony/security-core": "~2.8|~3.0"
#----- need for auth in symfony -----#
# security-guard
//"symfony/security-core": "~2.8|~3.0",
//"symfony/security-http": "~3.1"
# security-http
//"symfony/security-core": "~3.2",
//"symfony/event-dispatcher": "~2.8|~3.0",
//"symfony/http-foundation": "~2.8|~3.0",
//"symfony/http-kernel": "~2.8|~3.0",
//"symfony/polyfill-php56": "~1.0",
//"symfony/polyfill-php70": "~1.0",
//"symfony/property-access": "~2.8|~3.0"
$user = 'Diolektor';
$secret = 'secret-word';
$providerKey = 'provider-key-wtf';
$roles = ['ROLE'];// for PreAuthenticatedToken|UsernamePasswordToken
/*** SOURCE USERS ***/
/** @var \Symfony\Component\Security\Core\User\UserProviderInterface[] $userProviders */
$userProviders = [];
$userProviders[0] = new \Symfony\Component\Security\Core\User\InMemoryUserProvider([$user => ['password' => $secret, 'roles' => $roles]]);
//$userProviders[1] = new \Symfony\Component\Security\Core\User\LdapUserProvider();
/** @var \Symfony\Component\Security\Core\User\UserProviderInterface $userProvider */
$userProvider = new \Symfony\Component\Security\Core\User\ChainUserProvider($userProviders); // для нескольких провайдеров (источников пользователей)
/********************/
/** @var \Symfony\Component\Security\Core\User\UserCheckerInterface $userChecker */
$userChecker = new \Symfony\Component\Security\Core\User\UserChecker();
/*** INPUT DATA ***/
/** @var \Symfony\Component\Security\Core\Authentication\Token\TokenInterface $tokens */
$tokens = [];
$tokens[1] = new \Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken($user, $secret, $providerKey);
$tokens[2] = new \Symfony\Component\Security\Core\Authentication\Token\RememberMeToken($userProviders[0]->loadUserByUsername('Diolektor'), $providerKey, $secret);
$tokens[3] = new \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken($user, $secret, $providerKey);
$tokens[4] = new \Symfony\Component\Security\Core\Authentication\Token\AnonymousToken($secret, $user);
/******************/
/**** ENCODERS ****/
/** @var \Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface[] $encoders */
$encoders = [];
//$encoders[0] = new \Symfony\Component\Security\Core\Encoder\Pbkdf2PasswordEncoder();
//$encoders[1] = new \Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder();
$encoders[\Symfony\Component\Security\Core\User\User::class] = new \Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder();
//$encoders[3] = new \Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder();
$encoderFactory = new \Symfony\Component\Security\Core\Encoder\EncoderFactory($encoders);
/******************/
/**** AUTH PROVIDER ****/
/** @var \Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface[] $authenticationProviders */
$authenticationProviders = [];
$authenticationProviders[0] = new \Symfony\Component\Security\Core\Authentication\Provider\PreAuthenticatedAuthenticationProvider($userProvider, $userChecker, $providerKey); // $tokens[1]
$authenticationProviders[1] = new \Symfony\Component\Security\Core\Authentication\Provider\RememberMeAuthenticationProvider($userChecker, $secret, $providerKey); // $tokens[2]
//$authenticationProviders[2] = new \Symfony\Component\Security\Core\Authentication\Provider\SimpleAuthenticationProvider();
$authenticationProviders[3] = new \Symfony\Component\Security\Core\Authentication\Provider\AnonymousAuthenticationProvider($secret); // $tokens[4]
//$authenticationProviders[5] = new \Symfony\Component\Security\Core\Authentication\Provider\LdapBindAuthenticationProvider(); // $tokens[3]
$authenticationProviders[6] = new \Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider($userProvider, $userChecker, $providerKey, $encoderFactory); // $tokens[3]
$authenticationProviderManager = new \Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager($authenticationProviders);
/***********************/
foreach ($tokens as $token) {
try {
/** @var \Symfony\Component\Security\Core\Authentication\Token\TokenInterface $token */
$token = $authenticationProviderManager->authenticate($token);
var_dump(get_class($token), $token->getUsername(), $token->isAuthenticated());
} catch (\Symfony\Component\Security\Core\Exception\AuthenticationException $e) {
var_dump('error', $e->getMessage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment