forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthenticationServiceLoader.php
More file actions
117 lines (104 loc) · 3.58 KB
/
Copy pathAuthenticationServiceLoader.php
File metadata and controls
117 lines (104 loc) · 3.58 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
declare(strict_types=1);
/**
* Copyright 2010 - 2019, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2018, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Loader;
use Cake\Core\Configure;
use CakeDC\Auth\Authentication\AuthenticationService;
use Psr\Http\Message\ServerRequestInterface;
/**
* Class AuthenticationServiceLoader
*
* @package CakeDC\Users\Loader
*/
class AuthenticationServiceLoader
{
/**
* Load the authentication service with authenticators from config Auth.Authenticators,
* and identifiers from config Auth.Identifiers.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request.
* @return \CakeDC\Auth\Authentication\AuthenticationService
*/
public function __invoke(ServerRequestInterface $request)
{
$service = new AuthenticationService();
$this->loadIdentifiers($service);
$this->loadAuthenticators($service);
$this->loadTwoFactorAuthenticator($service);
return $service;
}
/**
* Load the indentifiers defined at config Auth.Identifiers
*
* @param \CakeDC\Auth\Authentication\AuthenticationService $service Authentication service to load identifiers
* @return void
*/
protected function loadIdentifiers($service)
{
$identifiers = Configure::read('Auth.Identifiers');
foreach ($identifiers as $key => $item) {
[$identifier, $options] = $this->_getItemLoadData($item, $key);
$service->loadIdentifier($identifier, $options);
}
}
/**
* Load the authenticators defined at config Auth.Authenticators
*
* @param \CakeDC\Auth\Authentication\AuthenticationService $service Authentication service to load identifiers
* @return void
*/
protected function loadAuthenticators($service)
{
$authenticators = Configure::read('Auth.Authenticators');
foreach ($authenticators as $key => $item) {
[$authenticator, $options] = $this->_getItemLoadData($item, $key);
$service->loadAuthenticator($authenticator, $options);
}
}
/**
* Load the CakeDC/Auth.TwoFactor based on config OneTimePasswordAuthenticator.login
*
* @param \CakeDC\Auth\Authentication\AuthenticationService $service Authentication service to load identifiers
* @return void
*/
protected function loadTwoFactorAuthenticator($service)
{
if (
Configure::read('OneTimePasswordAuthenticator.login') !== false
|| Configure::read('U2f.enabled') !== false
) {
$service->loadAuthenticator('CakeDC/Auth.TwoFactor', [
'skipTwoFactorVerify' => true,
]);
}
}
/**
* @param mixed $item Item configuration or className
* @param string $key Item array key.
* @return array
*/
protected function _getItemLoadData($item, $key)
{
$options = [];
if (!is_array($item)) {
return [$item, $options];
}
$options = $item;
if (!isset($options['className'])) {
throw new \InvalidArgumentException(
__d('cake_d_c/users', 'Property {0}.className should be defined', $key)
);
}
$className = $options['className'];
unset($options['className']);
return [$className, $options];
}
}