Completed
Push — master ( 75babc...67a09a )
by Paul
03:02
created

index.php ➔ fixupArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 9.4285
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 72 and the first side effect is on line 37.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Copyright (c) 2014 Yubico AB
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are
8
 * met:
9
 *
10
 *   * Redistributions of source code must retain the above copyright
11
 *     notice, this list of conditions and the following disclaimer.
12
 *
13
 *   * Redistributions in binary form must reproduce the above
14
 *     copyright notice, this list of conditions and the following
15
 *     disclaimer in the documentation and/or other materials provided
16
 *     with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 */
30
31
/**
32
 * This is a minimal example of U2F registration and authentication.
33
 * The data that has to be stored between registration and authentication
34
 * is stored in browser localStorage, so there's nothing real-world
35
 * about this.
36
 */
37
require_once('../../src/u2flib_server/U2F.php');
38
$scheme = isset($_SERVER['HTTPS']) ? "https://" : "http://";
39
$u2f = new u2flib_server\U2F($scheme . $_SERVER['HTTP_HOST']);
40
?>
41
<html>
42
<head>
43
    <title>PHP U2F Demo</title>
44
45
    <script src="../assets/u2f-api.js"></script>
46
47
    <script>
48
        function addRegistration(reg) {
49
            var existing = localStorage.getItem('u2fregistration');
50
            var regobj = JSON.parse(reg);
51
            var data = null;
52
            if(existing) {
53
                data = JSON.parse(existing);
54
                if(Array.isArray(data)) {
55
                    for (var i = 0; i < data.length; i++) {
56
                        if(data[i].keyHandle === regobj.keyHandle) {
57
                            data.splice(i,1);
58
                            break;
59
                        }
60
                    }
61
                    data.push(regobj);
62
                } else {
63
                    data = null;
64
                }
65
            }
66
            if(data == null) {
67
                data = [regobj];
68
            }
69
            localStorage.setItem('u2fregistration', JSON.stringify(data));
70
        }
71
        <?php
72
        function fixupArray($data) {
73
            $ret = array();
74
            $decoded = json_decode($data);
75
            foreach ($decoded as $d) {
76
                $ret[] = json_encode($d);
77
            }
78
            return $ret;
79
        }
80
        if($_SERVER['REQUEST_METHOD'] === 'POST') {
81
            if(isset($_POST['startRegister'])) {
82
                $regs = json_decode($_POST['registrations']) ? : array();
83
                list($data, $reqs) = $u2f->getRegisterData($regs);
84
                echo "var request = " . json_encode($data) . ";\n";
85
                echo "var signs = " . json_encode($reqs) . ";\n";
86
        ?>
87
        setTimeout(function() {
88
            console.log("Register: ", request);
89
            u2f.register([request], signs, function(data) {
90
                var form = document.getElementById('form');
91
                var reg = document.getElementById('doRegister');
92
                var req = document.getElementById('request');
93
                console.log("Register callback", data);
94
                if(data.errorCode) {
95
                    alert("registration failed with errror: " + data.errorCode);
96
                    return;
97
                }
98
                reg.value=JSON.stringify(data);
99
                req.value=JSON.stringify(request);
100
                form.submit();
101
            });
102
        }, 1000);
103
        <?php
104
            } else if($_POST['doRegister']) {
105
                try {
106
                    $data = $u2f->doRegister(json_decode($_POST['request']), json_decode($_POST['doRegister']));
107
                    echo "var registration = '" . json_encode($data) . "';\n";
108
        ?>
109
        addRegistration(registration);
110
        alert("registration successful!");
111
        <?php
112
                } catch(u2flib_server\Error $e) {
0 ignored issues
show
Bug introduced by
The class u2flib_server\Error does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
113
                    echo "alert('error:" . $e->getMessage() . "');\n";
114
                }
115
            } else if(isset($_POST['startAuthenticate'])) {
116
                $regs = json_decode($_POST['registrations']);
117
                $data = $u2f->getAuthenticateData($regs);
118
                echo "var registrations = " . $_POST['registrations'] . ";\n";
119
                echo "var request = " . json_encode($data) . ";\n";
120
        ?>
121
        setTimeout(function() {
122
            console.log("sign: ", request);
123
            u2f.sign(request, function(data) {
124
                var form = document.getElementById('form');
125
                var reg = document.getElementById('doAuthenticate');
126
                var req = document.getElementById('request');
127
                var regs = document.getElementById('registrations');
128
                console.log("Authenticate callback", data);
129
                reg.value=JSON.stringify(data);
130
                req.value=JSON.stringify(request);
131
                regs.value=JSON.stringify(registrations);
132
                form.submit();
133
            });
134
        }, 1000);
135
        <?php
136
            } else if($_POST['doAuthenticate']) {
137
                $reqs = json_decode($_POST['request']);
138
                $regs = json_decode($_POST['registrations']);
139
                try {
140
                    $data = $u2f->doAuthenticate($reqs, $regs, json_decode($_POST['doAuthenticate']));
141
                    echo "var registration = '" . json_encode($data) . "';\n";
142
                    echo "addRegistration(registration);\n";
143
                    echo "alert('Authentication successful, counter:" . $data->counter . "');\n";
144
                } catch(u2flib_server\Error $e) {
0 ignored issues
show
Bug introduced by
The class u2flib_server\Error does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
145
                    echo "alert('error:" . $e->getMessage() . "');\n";
146
                }
147
            }
148
        }
149
        ?>
150
    </script>
151
152
</head>
153
<body>
154
<form method="POST" id="form">
155
    <button name="startRegister" type="submit">Register</button>
156
    <input type="hidden" name="doRegister" id="doRegister"/>
157
    <button name="startAuthenticate" type="submit" id="startAuthenticate">Authenticate</button>
158
    <input type="hidden" name="doAuthenticate" id="doAuthenticate"/>
159
    <input type="hidden" name="request" id="request"/>
160
    <input type="hidden" name="registrations" id="registrations"/>
161
</form>
162
163
<p>
164
    <span id="registered">0</span> Authenticators currently registered.
165
</p>
166
167
<script>
168
    var reg = localStorage.getItem('u2fregistration');
169
    var auth = document.getElementById('startAuthenticate');
170
    if(reg == null) {
171
        auth.disabled = true;
172
    } else {
173
        var regs = document.getElementById('registrations');
174
        decoded = JSON.parse(reg);
175
        if(!Array.isArray(decoded)) {
176
            auth.disabled = true;
177
        } else {
178
            regs.value = reg;
179
            console.log("set the registrations to : ", reg);
180
            var regged = document.getElementById('registered');
181
            regged.innerHTML = decoded.length;
182
        }
183
    }
184
</script>
185
</body>
186
</html>
187