|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Saito - The Threaded Web Forum |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
|
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
|
10
|
|
|
* @license http://opensource.org/licenses/MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace App\Auth; |
|
14
|
|
|
|
|
15
|
|
|
use App\Auth\LegacyPasswordHasherSaltless; |
|
16
|
|
|
use App\Auth\Mlf2PasswordHasher; |
|
17
|
|
|
use Authentication\AuthenticationService; |
|
18
|
|
|
use Cake\Core\Configure; |
|
19
|
|
|
use Cake\Routing\Router; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Builds AuthenticationService consumed by Authentication middleware |
|
23
|
|
|
*/ |
|
24
|
|
|
class AuthenticationServiceFactory |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Build authentication service for JWT based API |
|
28
|
|
|
* |
|
29
|
|
|
* @return AuthenticationService |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function buildJwt(): AuthenticationService |
|
32
|
|
|
{ |
|
33
|
|
|
$service = new AuthenticationService(); |
|
34
|
|
|
|
|
35
|
|
|
$service->loadIdentifier('Authentication.JwtSubject'); |
|
36
|
|
|
$service->loadAuthenticator('Authentication.Jwt', [ |
|
37
|
|
|
'returnPayload' => false, |
|
38
|
|
|
'secretKey' => Configure::read('Security.cookieSalt'), |
|
39
|
|
|
]); |
|
40
|
|
|
|
|
41
|
|
|
return $service; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Build authentication service with Session, Cookie and Form |
|
46
|
|
|
* |
|
47
|
|
|
* @return AuthenticationService |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function buildApp(): AuthenticationService |
|
50
|
|
|
{ |
|
51
|
|
|
$service = new AuthenticationService(); |
|
52
|
|
|
|
|
53
|
|
|
$service->setConfig('queryParam', 'redirect'); |
|
54
|
|
|
$service->setConfig('unauthenticatedRedirect', '/login'); |
|
55
|
|
|
|
|
56
|
|
|
$service->loadIdentifier('Authentication.Password', [ |
|
57
|
|
|
'passwordHasher' => [ |
|
58
|
|
|
'className' => 'Authentication.Fallback', |
|
59
|
|
|
'hashers' => [ |
|
60
|
|
|
// Saito passwords (Cake default) |
|
61
|
|
|
['className' => 'Authentication.Default'], |
|
62
|
|
|
// Mylittleforum 2 legacy passwords |
|
63
|
|
|
['className' => Mlf2PasswordHasher::class], |
|
64
|
|
|
// Mylittleforum 1 legacy passwords |
|
65
|
|
|
['className' => LegacyPasswordHasherSaltless::class, 'hashType' => 'md5'], |
|
66
|
|
|
] |
|
67
|
|
|
] |
|
68
|
|
|
]); |
|
69
|
|
|
|
|
70
|
|
|
// Authenticators are checked in order of registration. |
|
71
|
|
|
// Leave Session first. |
|
72
|
|
|
$service->loadAuthenticator( |
|
73
|
|
|
'Authentication.Session', |
|
74
|
|
|
[ |
|
75
|
|
|
// Always check against DB. User-state (type, locked) might have |
|
76
|
|
|
// changed and must be reflected immediately. |
|
77
|
|
|
'identify' => true, |
|
78
|
|
|
] |
|
79
|
|
|
); |
|
80
|
|
|
$service->loadAuthenticator( |
|
81
|
|
|
'Authentication.Cookie', |
|
82
|
|
|
[ |
|
83
|
|
|
'cookie' => [ |
|
84
|
|
|
'expire' => new \DateTimeImmutable('+10 days'), |
|
85
|
|
|
'httpOnly' => true, |
|
86
|
|
|
'name' => Configure::read('Security.cookieAuthName'), |
|
87
|
|
|
'path' => Router::url('/', false), |
|
88
|
|
|
] |
|
89
|
|
|
] |
|
90
|
|
|
); |
|
91
|
|
|
$service->loadAuthenticator('Authentication.Form', ['loginUrl' => '/login']); |
|
92
|
|
|
|
|
93
|
|
|
return $service; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|