forked from mvndaai/webauthn_demo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
173 lines (165 loc) · 9.24 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html>
<head>
<title>WebAuthn Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="mobile-web-app-capable" content="yes">
<script type="text/javascript">
(function(global, undefined) {
function strToBin(str) {
return Uint8Array.from(
atob(str),
function(c){ return c.charCodeAt(0); }
);
}
function binToStr(bin){
return btoa(new Uint8Array(bin).reduce(
function(s, byte){ return s + String.fromCharCode(byte); }
, ''
));
}
function parseClientDataJSON(clientDataJSON) {
return JSON.parse(atob(binToStr(r.response.clientDataJSON)));
}
// See CBOR.io or https://github.com/paroga/cbor-jsa
function parseAttestationObject(attestationObject) {
return CBOR.decode(attestationObject);
}
function unwrapPublicKeyCredential(cred) {
let r = {response: {}};
if ('id' in cred) r.id = cred.id
if ('type' in cred) r.type = cred.type;
if ('rawId' in cred) r.rawId = binToStr(cred.rawId);
if ('response' in cred) {
// Used in registration
if ('clientDataJSON' in cred.response)
r.response.clientDataJSON = binToStr(cred.response.clientDataJSON);
if ('attestationObject' in cred.response)
r.response.attestationObject = binToStr(cred.response.attestationObject);
// Used in authentication
if ('authenticatorData' in cred.response)
r.response.authenticatorData = binToStr(cred.response.authenticatorData);
if ('signature' in cred.response)
r.response.signature = binToStr(cred.response.signature);
if ('userHandle' in cred.response)
r.response.userHandle = binToStr(cred.response.userHandle);
}
return r;
}
var obj = { strToBin, binToStr, parseClientDataJSON, parseAttestationObject, unwrapPublicKeyCredential };
if (typeof define === 'function' && define.amd) define('webauthnHelper', obj);
else if (typeof module !== 'undefined' && module.exports) module.exports = obj;
else if (!global.webauthnHelper) global.webauthnHelper = obj;
})(this);
</script>
<script src='//unpkg.com/mithril/mithril.js'></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<style type="text/css">
.notification{position:fixed;margin:0 auto;left:0;right:0;top:30px;max-width:80vw;z-index:100}html,body{height:100%}body{display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center;padding-top:40px;padding-bottom:40px;background-color:#f5f5f5}.form-signin{width:100%;max-width:530px;padding:15px;margin:auto}.form-signin .checkbox{font-weight:400}.form-signin .form-control{position:relative;box-sizing:border-box;height:auto;padding:10px;font-size:16px}.form-signin .form-control:focus{z-index:2}.form-signin input[type="email"]{margin-bottom:-1px;border-bottom-right-radius:0;border-bottom-left-radius:0}.form-signin input[type="password"]{margin-bottom:10px;border-top-left-radius:0;border-top-right-radius:0}
</style>
</head>
<body>
<form class="form-signin">
<div class="alert alert-success" role="alert" id="loginin" style="display:none">
<h4 class="alert-heading">Well done!</h4>
<hr>
<p class="mb-0">Login in.</p>
</div>
<h1>Hello, world!</h1>
<h4>WebAuth Example</h4>
<ul class="nav nav-tabs">
<li class="nav-item active">
<a data-toggle="tab" class="nav-link active" href="#menu1">Register</a>
</li>
<li class="nav-item">
<a data-toggle="tab" class="nav-link" href="#menu2">Login</a>
</li>
</ul>
<div class="tab-content">
<div id="menu1" class="tab-pane active" style="padding:20px; background: #fff">
<label for="username" class="sr-only">Username</label>
<input type="text" id="username" class="form-control" placeholder="Username" required autofocus>
<br />
<button class="btn btn-lg btn-success btn-block" onsubmit="return false;" onclick="register();" type="button">Register</button>
</div>
<div id="menu2" class="tab-pane fade" style="padding:20px; background: #fff">
<button class="btn btn-lg btn-success btn-block" onsubmit="return false;" onclick="sign();" type="button">Login</button>
</div>
</div>
<hr />
<p class="mt-5 mb-3 text-center">
<a href="https://github.com/hto" target="_blank">@github</a>
<a href="https://medium.com/@hto" target="_blank">@medium</a>
<a href="https://tr.linkedin.com/in/haliltugcanozaktas" target="_blank">@linkedin</a>
</p>
</form>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script>
function register(username, displayName, deviceName) {
username = displayName = deviceName = $("#username").val();
let data = {
deviceName: deviceName,
origin: window.location.origin,
user: { name: username, displayName: displayName },
}
let user = { name: username, displayName: displayName, origin: window.location.origin}
m.request({ method: "POST", url: "/registration/start", body: data })
.then(r => {
r.publicKey.challenge = webauthnHelper.strToBin(r.publicKey.challenge);
r.publicKey.user.id = webauthnHelper.strToBin(r.publicKey.user.id);
return navigator.credentials.create(r);
})
.then(PublicKeyCredential => {
// https://w3c.github.io/webauthn/#iface-pkcredential
// https://developer.mozilla.org/en-US/docs/Web/API/PublicKeyCredential
let body = webauthnHelper.unwrapPublicKeyCredential(PublicKeyCredential);
body.user = user; // This is for the finding the user in the db
body.deviceName = deviceName;
return m.request({ method: 'POST', url: '/registration/finish', body: body });
})
.then(r => {
localStorage.setItem('username', $("#username").val());
alert($("#username").val() + ", register is successful.");
})
.catch(e => {
console.error(e)
alert(e);
});
}
function sign(username, deviceName){
username = deviceName = localStorage.getItem('username');
let data = { deviceName, user:{name: username}};
m.request({ method: "POST", url: "/authentication/start", body: data })
.then(o => {
console.log(o);
let getCredentialArgs = {
publicKey: {
timeout: 50000,
challenge: webauthnHelper.strToBin(o.challenge),
allowCredentials: [{
id: webauthnHelper.strToBin(o.credentialId),
transports: ["usb", "nfc", "ble", "internal"],
type: "public-key"
}],
},
};
return navigator.credentials.get(getCredentialArgs)
})
.then((assertion) => {
let body = webauthnHelper.unwrapPublicKeyCredential(assertion)
body.user = {name: username}; // ODO probably remove this
body.deviceName = deviceName;
return m.request({ method: "POST", url: "/authentication/finish", body: body });
})
.then(r => {
$("#loginin").show();
})
.catch((e) => {
console.error(e);
alert(e);
})
};
</script>
</body>
</html>