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', Router::url(['_name' => 'login'], false)); |
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
|
|
|
'resolver' => [ |
69
|
|
|
'className' => 'Authentication.Orm', |
70
|
|
|
'finder' => 'profile', |
71
|
|
|
], |
72
|
|
|
]); |
73
|
|
|
|
74
|
|
|
// Authenticators are checked in order of registration. |
75
|
|
|
// Leave Session first. |
76
|
|
|
$service->loadAuthenticator( |
77
|
|
|
'Authentication.Session', |
78
|
|
|
[ |
79
|
|
|
// Always check against DB. User-state (type, locked) might have |
80
|
|
|
// changed and must be reflected immediately. |
81
|
|
|
'identify' => true, |
82
|
|
|
] |
83
|
|
|
); |
84
|
|
|
$service->loadAuthenticator( |
85
|
|
|
'Authentication.Cookie', |
86
|
|
|
[ |
87
|
|
|
'cookie' => [ |
88
|
|
|
'expire' => new \DateTimeImmutable('+10 days'), |
89
|
|
|
'httpOnly' => true, |
90
|
|
|
'name' => Configure::read('Security.cookieAuthName'), |
91
|
|
|
'path' => Router::url('/', false), |
92
|
|
|
], |
93
|
|
|
] |
94
|
|
|
); |
95
|
|
|
$service->loadAuthenticator( |
96
|
|
|
'Authentication.Form', |
97
|
|
|
['loginUrl' => Router::url(['_name' => 'login'])] |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
return $service; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|