Passed
Pull Request — master (#39)
by Tim
03:06
created

Registration::setAuthState()   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
namespace SimpleSAML\Module\webauthn\Controller;
4
5
use Exception;
6
use SimpleSAML\Auth;
7
use SimpleSAML\Configuration;
8
use SimpleSAML\HTTP\RunnableResponse;
9
use SimpleSAML\Logger;
10
use SimpleSAML\Metadata\MetaDataStorageHandler;
11
use SimpleSAML\Module;
12
use SimpleSAML\Module\webauthn\WebAuthn\StateData;
13
use SimpleSAML\Module\webauthn\WebAuthn\StaticProcessHelper;
14
use SimpleSAML\Module\webauthn\Store;
15
use SimpleSAML\Session;
16
use SimpleSAML\Utils;
17
use SimpleSAML\XHTML\Template;
18
use Symfony\Component\HttpFoundation\Request;
19
20
/**
21
 * Controller class for the webauthn module.
22
 *
23
 * This class serves the different views available in the module.
24
 *
25
 * @package SimpleSAML\Module\webauthn
26
 */
27
class Registration
28
{
29
    /** @var \SimpleSAML\Configuration */
30
    protected $config;
31
32
    /** @var \SimpleSAML\Session */
33
    protected $session;
34
35
    /**
36
     * @var \SimpleSAML\Auth\State|string
37
     * @psalm-var \SimpleSAML\Auth\State|class-string
38
     */
39
    protected $authState = Auth\State::class;
40
41
    /**
42
     * @var \SimpleSAML\Auth\Simple|string
43
     * @psalm-var \SimpleSAML\Auth\Simple|class-string
44
     */
45
    protected $authSimple = Auth\Simple::class;
46
47
    /**
48
     * @var \SimpleSAML\Logger|string
49
     * @psalm-var \SimpleSAML\Logger|class-string
50
     */
51
    protected $logger = Logger::class;
52
53
54
    /**
55
     * Controller constructor.
56
     *
57
     * It initializes the global configuration and session for the controllers implemented here.
58
     *
59
     * @param \SimpleSAML\Configuration              $config The configuration to use by the controllers.
60
     * @param \SimpleSAML\Session                    $session The session to use by the controllers.
61
     *
62
     * @throws \Exception
63
     */
64
    public function __construct(
65
        Configuration $config,
66
        Session $session
67
    ) {
68
        $this->config = $config;
69
        $this->session = $session;
70
    }
71
72
73
    /**
74
     * Inject the \SimpleSAML\Auth\State dependency.
75
     *
76
     * @param \SimpleSAML\Auth\State $authState
77
     */
78
    public function setAuthState(Auth\State $authState): void
79
    {
80
        $this->authState = $authState;
81
    }
82
83
84
    /**
85
     * Inject the \SimpleSAML\Auth\Simple dependency.
86
     *
87
     * @param \SimpleSAML\Auth\Simple $authSimple
88
     */
89
    public function setAuthSimple(Auth\Simple $authSimple): void
90
    {
91
        $this->authSimple = $authSimple;
92
    }
93
94
95
    /**
96
     * Inject the \SimpleSAML\Logger dependency.
97
     *
98
     * @param \SimpleSAML\Logger $logger
99
     */
100
    public function setLogger(Logger $logger): void
101
    {
102
        $this->logger = $logger;
103
    }
104
105
106
    /**
107
     * @param \Symfony\Component\HttpFoundation\Request $request
108
     * @return \SimpleSAML\HTTP\RunnableResponse  A Symfony Response-object.
109
     */
110
    public function main(/** @scrutinizer ignore-unused */ Request $request): RunnableResponse
111
    {
112
        $moduleConfig = Configuration::getOptionalConfig('module_webauthn.php');
113
        $registrationAuthSource = $moduleConfig->getString('registration_auth_source', 'default-sp');
114
115
        /** @psalm-var class-string $authSimple */
116
        $authSimple = $this->authSimple;
117
        $as = new $authSimple($registrationAuthSource);
118
        $as->requireAuth();
119
        $attrs = $as->getAttributes();
120
121
        $state = [];
122
        $state['Attributes'] = $attrs;
123
124
        $stateData = new StateData();
125
        $stateData->requestTokenModel = $moduleConfig->getBoolean('request_tokenmodel', false);
126
        try {
127
            $stateData->store = Store::parseStoreConfig($moduleConfig->getArray('store'));
128
        } catch (Exception $e) {
129
            $this->logger::error(
130
                'webauthn: Could not create storage: ' . $e->getMessage()
131
            );
132
        }
133
134
        $stateData->scope = $moduleConfig->getString('scope', null);
135
        $baseurl = Utils\HTTP::getSelfHost();
136
        $hostname = parse_url($baseurl, PHP_URL_HOST);
137
        if ($hostname !== null) {
138
            $stateData->derivedScope = $hostname;
139
        }
140
        $stateData->usernameAttrib = $moduleConfig->getString('attrib_username');
141
        $stateData->displaynameAttrib = $moduleConfig->getString('attrib_displayname');
142
        $stateData->useInflowRegistration = true;
143
144
        StaticProcessHelper::prepareState($stateData, $state);
145
146
        $metadataHandler = MetaDataStorageHandler::getMetadataHandler();
147
        $metadata = $metadataHandler->getMetaDataCurrent('saml20-idp-hosted');
148
        $state['Source'] = $metadata;
149
        $state['IdPMetadata'] = $metadata;
150
        $state['Registration'] = true;
151
        $state['FIDO2AuthSuccessful'] = $state['FIDO2Tokens'][0][0] ?? false;
152
        $state['FIDO2WantsRegister'] = true;
153
154
        return new RunnableResponse([StaticProcessHelper::class, 'saveStateAndRedirect'], [&$state]);
155
    }
156
}
157