Passed
Push — master ( 47e107...d19bb5 )
by Tim
02:03
created

Registration::setAuthSimple()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
nc 1
nop 1
cc 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\webauthn\Controller;
6
7
use Exception;
8
use SimpleSAML\Auth;
9
use SimpleSAML\Configuration;
10
use SimpleSAML\HTTP\RunnableResponse;
11
use SimpleSAML\Logger;
12
use SimpleSAML\Metadata\MetaDataStorageHandler;
13
use SimpleSAML\Module;
14
use SimpleSAML\Module\webauthn\WebAuthn\StateData;
15
use SimpleSAML\Module\webauthn\WebAuthn\StaticProcessHelper;
16
use SimpleSAML\Module\webauthn\Store;
17
use SimpleSAML\Session;
18
use SimpleSAML\Utils;
19
use SimpleSAML\XHTML\Template;
20
use Symfony\Component\HttpFoundation\Request;
21
22
/**
23
 * Controller class for the webauthn module.
24
 *
25
 * This class serves the different views available in the module.
26
 *
27
 * @package SimpleSAML\Module\webauthn
28
 */
29
class Registration
30
{
31
    /** @var \SimpleSAML\Configuration */
32
    protected Configuration $config;
33
34
    /** @var \SimpleSAML\Session */
35
    protected Session $session;
36
37
    /**
38
     * @var \SimpleSAML\Auth\State|string
39
     * @psalm-var \SimpleSAML\Auth\State|class-string
40
     */
41
    protected $authState = Auth\State::class;
42
43
    /**
44
     * @var \SimpleSAML\Auth\Simple|string
45
     * @psalm-var \SimpleSAML\Auth\Simple|class-string
46
     */
47
    protected $authSimple = Auth\Simple::class;
48
49
    /**
50
     * @var \SimpleSAML\Logger|string
51
     * @psalm-var \SimpleSAML\Logger|class-string
52
     */
53
    protected $logger = Logger::class;
54
55
56
    /**
57
     * Controller constructor.
58
     *
59
     * It initializes the global configuration and session for the controllers implemented here.
60
     *
61
     * @param \SimpleSAML\Configuration              $config The configuration to use by the controllers.
62
     * @param \SimpleSAML\Session                    $session The session to use by the controllers.
63
     *
64
     * @throws \Exception
65
     */
66
    public function __construct(
67
        Configuration $config,
68
        Session $session
69
    ) {
70
        $this->config = $config;
71
        $this->session = $session;
72
    }
73
74
75
    /**
76
     * Inject the \SimpleSAML\Auth\State dependency.
77
     *
78
     * @param \SimpleSAML\Auth\State $authState
79
     */
80
    public function setAuthState(Auth\State $authState): void
81
    {
82
        $this->authState = $authState;
83
    }
84
85
86
    /**
87
     * Inject the \SimpleSAML\Auth\Simple dependency.
88
     *
89
     * @param \SimpleSAML\Auth\Simple $authSimple
90
     */
91
    public function setAuthSimple(Auth\Simple $authSimple): void
92
    {
93
        $this->authSimple = $authSimple;
94
    }
95
96
97
    /**
98
     * Inject the \SimpleSAML\Logger dependency.
99
     *
100
     * @param \SimpleSAML\Logger $logger
101
     */
102
    public function setLogger(Logger $logger): void
103
    {
104
        $this->logger = $logger;
105
    }
106
107
108
    /**
109
     * @param \Symfony\Component\HttpFoundation\Request $request
110
     * @return \SimpleSAML\HTTP\RunnableResponse  A Symfony Response-object.
111
     */
112
    public function main(/** @scrutinizer ignore-unused */ Request $request): RunnableResponse
113
    {
114
        $moduleConfig = Configuration::getOptionalConfig('module_webauthn.php');
115
        $registrationConfig = $moduleConfig->getArray('registration');
116
        $registrationAuthSource = $registrationConfig['auth_source'] ?? 'default-sp';
117
118
        /** @psalm-var class-string $authSimple */
119
        $authSimple = $this->authSimple;
120
        $as = new $authSimple($registrationAuthSource);
121
        $as->requireAuth();
122
        $attrs = $as->getAttributes();
123
124
        $state = [];
125
        $state['Attributes'] = $attrs;
126
127
        $stateData = new StateData();
128
        $stateData->requestTokenModel = ($registrationConfig['policy_2fa']['minimum_certification_level'] == "0" ? false : true);
129
        $stateData->minCertLevel2FA = $registrationConfig['policy_2fa']['minimum_certification_level'];
130
        $stateData->aaguidWhitelist2FA = $registrationConfig['policy_2fa']['aaguid_whitelist'];
131
        $stateData->attFmtWhitelist2FA = $registrationConfig['policy_2fa']['attestation_format_whitelist'];
132
        $stateData->minCertLevelPasswordless = $registrationConfig['policy_passwordless']['minimum_certification_level'];
133
        $stateData->aaguidWhitelistPasswordless = $registrationConfig['policy_passwordless']['aaguid_whitelist'];
134
        $stateData->attFmtWhitelistPasswordless = $registrationConfig['policy_passwordless']['attestation_format_whitelist'];
135
        $stateData->requestTokenModel = ($registrationConfig['minimum_certification_level'] == "0" ? false : true);
136
        $stateData->minCertLevel = $registrationConfig['minimum_certification_level'];
137
        $stateData->aaguidWhitelist = $registrationConfig['aaguid_whitelist'];
138
        $stateData->attFmtWhitelist = $registrationConfig['attestation_format_whitelist'];
139
140
>>>>>>> 9acf80d (Fix codesniffer issues)
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_SR on line 140 at column 0
Loading history...
141
        try {
142
            $stateData->store = Store::parseStoreConfig($moduleConfig->getArray('store'));
143
        } catch (Exception $e) {
144
            $this->logger::error(
145
                'webauthn: Could not create storage: ' . $e->getMessage()
146
            );
147
        }
148
149
        $stateData->scope = $moduleConfig->getOptionalString('scope', null);
150
        $httpUtils = new Utils\HTTP();
151
        $baseurl = $httpUtils->getSelfHost();
152
        $hostname = parse_url($baseurl, PHP_URL_HOST);
153
        if ($hostname !== null) {
154
            $stateData->derivedScope = $hostname;
155
        }
156
        $stateData->usernameAttrib = $moduleConfig->getString('identifyingAttribute');
157
        $stateData->displaynameAttrib = $moduleConfig->getString('attrib_displayname');
158
159
        StaticProcessHelper::prepareState($stateData, $state);
160
161
        $metadataHandler = MetaDataStorageHandler::getMetadataHandler();
162
        $metadata = $metadataHandler->getMetaDataCurrent('saml20-idp-hosted');
163
        $state['Source'] = $metadata;
164
        $state['IdPMetadata'] = $metadata;
165
        // inflow users are not allowed to enter the Registration page. If they
166
        // did, kill the session
167
        $moduleConfig = Configuration::getOptionalConfig('module_webauthn.php')->toArray();
168
169
        if ($moduleConfig['registration']['use_inflow_registration']) {
170
            throw new Exception("Attempt to access the stand-alone registration page in inflow mode!");
171
        }
172
173
        $state['Registration'] = true;
174
        $state['FIDO2WantsRegister'] = true;
175
176
        return new RunnableResponse([StaticProcessHelper::class, 'saveStateAndRedirect'], [&$state]);
177
    }
178
}
179