/
index.html
128 lines (123 loc) · 5.15 KB
/
index.html
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
118
119
120
121
122
123
124
125
126
127
128
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>U2F Demo</title>
<script src="js/u2f-api-1.1.js" type="application/javascript"></script>
<script type="application/javascript">
function FetchErrorHandler(err) {
console.log('Fetch Error :-S', err);
}
function DoRegistration() {
fetch("./auth/register/begin")
.then(
function (response) {
if (response.status !== 200) {
console.log(response);
return;
}
response.json().then(
function (registrationData) {
challenge = registrationData.challenge;
console.log("registration challenge" + challenge);
u2f.register(registrationData.appId,
[registrationData],
[],
function(data) {
if(data.errorCode) {
document.getElementById("result").innerText = "U2F failed with error: " + data.errorCode;
return;
}
fetch("./auth/register/complete", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
}).then(function () {
alert("key registered");
}
).catch(
FetchErrorHandler
);
});
}
);
}
)
.catch(
FetchErrorHandler
);
}
function DoSignRequest() {
let user = document.getElementById("user").value;
let pass = document.getElementById("pass").value;
if (user.length === 0 || pass.length === 0) {
alert("Please enter the username and password first!");
return;
}
let credentials = {
user: user,
password: pass,
};
fetch("./auth/authenticate/begin", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(credentials),
}).then(
function (response) {
response.json().then(
function (SignRequestData) {
u2f.sign(SignRequestData.appId,
SignRequestData.challenge,
[SignRequestData],
function (signResponseData) {
if(signResponseData.errorCode) {
return;
}
fetch("./auth/authenticate/complete", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(signResponseData),
}).then(
function (authResponseData) {
console.log(authResponseData)
if (authResponseData.status === 200) {
alert("auth complete");
} else {
alert("auth failed");
}
}
).catch(
FetchErrorHandler
);
}
)
}
);
}
).catch(
FetchErrorHandler
);
}
</script>
</head>
<body>
<h1>U2F Demo</h1>
<p>
Demo page for U2F stuff.
</p>
<form>
<label for="user" style="margin-right: 10px; font-family: monospace;">Username: </label>
<input id="user" type="text"><br>
<label for="pass" style="margin-right: 10px; font-family: monospace;">Password: </label>
<input id="pass" type="password"><br><br>
<button type="button" onclick="DoRegistration()">Register</button>
<button type="button" onclick="DoSignRequest()">Sign</button>
</form>
</body>
</html>