Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
require __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
use Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider;
use Symfony\Component\Security\Core\User\UserChecker;
use Symfony\Component\Security\Core\Encoder\EncoderFactory;
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
use Symfony\Component\Security\Core\User\User;
$app = new Silex\Application();
$app['debug'] = true;
$userProvider = new InMemoryUserProvider(
array(
'admin' => array(
// password is "foo"
'password' => '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg==',
'roles' => array('ROLE_ADMIN'),
),
)
);
$app->get('/', function () use ($userProvider) {
$providers = [new \BiiG\SecurityTest\CustomAuthenticationProvider()];
$authenticationManager = new AuthenticationProviderManager($providers);
// for some extra checks: is account enabled, locked, expired, etc.
$userChecker = new \Symfony\Component\Security\Core\User\UserChecker();
try {
$authenticatedToken = $authenticationManager
->authenticate($unauthenticatedToken);
// for some extra checks: is account enabled, locked, expired, etc.
$userChecker = new UserChecker();
// an array of password encoders (see below)
$encoderFactory = new EncoderFactory([
User::class => new MessageDigestPasswordEncoder('sha512', true, 5000),
]);
$provider = new DaoAuthenticationProvider(
$userProvider,
$userChecker,
'secured_area',
$encoderFactory
);
$provider->authenticate($unauthenticatedToken);
} catch (AuthenticationException $failed) {
// authentication failed
}
return '<h1>Hello world</h1>';
});
$app->run();