Completed
Push — master ( bab30e...5d9c47 )
by Stefan
18s queued 14s
created

Registration::setAuthSimple()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
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
        $authSimple = $this->authSimple;
116
        $as = new $authSimple($registrationAuthSource);
117
        $as->requireAuth();
118
        $attrs = $as->getAttributes();
119
120
        $state = [];
121
        $state['Attributes'] = $attrs;
122
123
        $stateData = new StateData();
124
        $stateData->requestTokenModel = $moduleConfig->getBoolean('request_tokenmodel', false);
125
        try {
126
            $stateData->store = Store::parseStoreConfig($moduleConfig->getArray('store'));
127
        } catch (Exception $e) {
128
            $this->logger::error(
129
                'webauthn: Could not create storage: ' . $e->getMessage()
130
            );
131
        }
132
133
        $stateData->scope = $moduleConfig->getString('scope', null);
134
        $baseurl = Utils\HTTP::getSelfHost();
135
        $hostname = parse_url($baseurl, PHP_URL_HOST);
136
        if ($hostname !== null) {
137
            $stateData->derivedScope = $hostname;
138
        }
139
        $stateData->usernameAttrib = $moduleConfig->getString('attrib_username');
140
        $stateData->displaynameAttrib = $moduleConfig->getString('attrib_displayname');
141
        $stateData->useInflowRegistration = true;
142
143
        StaticProcessHelper::prepareState($stateData, $state);
144
145
        $metadataHandler = MetaDataStorageHandler::getMetadataHandler();
146
        $metadata = $metadataHandler->getMetaDataCurrent('saml20-idp-hosted');
147
        $state['Source'] = $metadata;
148
        $state['IdPMetadata'] = $metadata;
149
        $state['Registration'] = true;
150
        $state['FIDO2AuthSuccessful'] = $state['FIDO2Tokens'][0][0];
151
        $state['FIDO2WantsRegister'] = true;
152
153
        return new RunnableResponse([StaticProcessHelper::class, 'saveStateAndRedirect'], [$state]);
154
    }
155
}
156