forked from asbiin/laravel-webauthn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webauthn.js
220 lines (199 loc) · 6.34 KB
/
webauthn.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* WebAuthn client.
*
* This file is part of asbiin/laravel-webauthn project.
*
* @copyright Alexis SAETTLER © 2019
* @license MIT
*/
'use strict';
/**
* Create a new instance of WebAuthn.
*
* @param {function(string, bool)} notifyCallback
* @constructor
*/
function WebAuthn(notifyCallback = null) {
if (notifyCallback) {
this.setNotify(notifyCallback);
}
};
/**
* Register a new key.
*
* @param {PublicKeyCredentialCreationOptions} publicKey - see https://www.w3.org/TR/webauthn/#dictdef-publickeycredentialcreationoptions
* @param {function(PublicKeyCredential)} callback User callback
*/
WebAuthn.prototype.register = function(publicKey, callback) {
let publicKeyCredential = Object.assign({}, publicKey);
publicKeyCredential.user.id = this._bufferDecode(publicKey.user.id);
publicKeyCredential.challenge = this._bufferDecode(this._base64Decode(publicKey.challenge));
if (publicKey.excludeCredentials) {
publicKeyCredential.excludeCredentials = this._credentialDecode(publicKey.excludeCredentials);
}
var self = this;
navigator.credentials.create({
publicKey: publicKeyCredential
}).then((data) => {
self._registerCallback(data, callback);
}, (error) => {
self._notify(error.name, error.message, false);
});
}
/**
* Register callback on register key.
*
* @param {PublicKeyCredential} publicKey @see https://www.w3.org/TR/webauthn/#publickeycredential
* @param {function(PublicKeyCredential)} callback User callback
*/
WebAuthn.prototype._registerCallback = function(publicKey, callback) {
let publicKeyCredential = {
id: publicKey.id,
type: publicKey.type,
rawId: this._bufferEncode(publicKey.rawId),
response: {
/** @see https://www.w3.org/TR/webauthn/#authenticatorattestationresponse */
clientDataJSON: this._bufferEncode(publicKey.response.clientDataJSON).replace(/=/g, ''),
attestationObject: this._bufferEncode(publicKey.response.attestationObject)
}
};
callback(publicKeyCredential);
}
/**
* Authenticate a user.
*
* @param {PublicKeyCredentialRequestOptions} publicKey - see https://www.w3.org/TR/webauthn/#dictdef-publickeycredentialrequestoptions
* @param {function(PublicKeyCredential)} callback User callback
*/
WebAuthn.prototype.sign = function(publicKey, callback) {
let publicKeyCredential = Object.assign({}, publicKey);
publicKeyCredential.challenge = this._bufferDecode(this._base64Decode(publicKey.challenge));
if (publicKey.allowCredentials) {
publicKeyCredential.allowCredentials = this._credentialDecode(publicKey.allowCredentials);
}
var self = this;
navigator.credentials.get({
publicKey: publicKeyCredential
}).then((data) => {
self._signCallback(data, callback);
}, (error) => {
self._notify(error.name, error.message, false);
}
);
}
/**
* Sign callback on authenticate.
*
* @param {PublicKeyCredential} publicKey @see https://www.w3.org/TR/webauthn/#publickeycredential
* @param {function(PublicKeyCredential)} callback User callback
*/
WebAuthn.prototype._signCallback = function(publicKey, callback) {
let publicKeyCredential = {
id: publicKey.id,
type: publicKey.type,
rawId: this._bufferEncode(publicKey.rawId),
response: {
/** @see https://www.w3.org/TR/webauthn/#iface-authenticatorassertionresponse */
authenticatorData: this._bufferEncode(publicKey.response.authenticatorData).replace(/=/g, ''),
clientDataJSON: this._bufferEncode(publicKey.response.clientDataJSON).replace(/=/g, ''),
signature: this._bufferEncode(publicKey.response.signature),
userHandle: (publicKey.response.userHandle ? this._bufferEncode(publicKey.response.userHandle) : null),
}
};
callback(publicKeyCredential);
}
/**
* Buffer encode.
*
* @param {ArrayBuffer} value
* @return {string}
*/
WebAuthn.prototype._bufferEncode = function(value) {
return window.btoa(String.fromCharCode.apply(null, new Uint8Array(value)));
}
/**
* Buffer decode.
*
* @param {ArrayBuffer} value
* @return {string}
*/
WebAuthn.prototype._bufferDecode = function(value) {
var t = window.atob(value);
return Uint8Array.from(t, c => c.charCodeAt(0));
}
/**
* Convert a base64url to a base64 string.
*
* @param {string} input
* @return {string}
*/
WebAuthn.prototype._base64Decode = function(input) {
// Replace non-url compatible chars with base64 standard chars
input = input.replace(/-/g, '+').replace(/_/g, '/');
// Pad out with standard base64 required padding characters
const pad = input.length % 4;
if (pad) {
if (pad === 1) {
throw new Error('InvalidLengthError: Input base64url string is the wrong length to determine padding');
}
input += new Array(5-pad).join('=');
}
return input;
}
/**
* Credential decode.
*
* @param {PublicKeyCredentialDescriptor} credentials
* @return {PublicKeyCredentialDescriptor}
*/
WebAuthn.prototype._credentialDecode = function(credentials) {
var self = this;
return credentials.map(function(data) {
return {
id: self._bufferDecode(self._base64Decode(data.id)),
type: data.type,
transports: data.transports,
};
});
}
/**
* Test is WebAuthn is supported by this navigator.
*
* @return {bool}
*/
WebAuthn.prototype.webAuthnSupport = function() {
return ! (window.PublicKeyCredential === undefined ||
typeof window.PublicKeyCredential !== 'function' ||
typeof window.PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable !== 'function');
}
/**
* Get the message in case WebAuthn is not supported.
*
* @return {string}
*/
WebAuthn.prototype.notSupportedMessage = function() {
if (! window.isSecureContext && window.location.hostname !== 'localhost' && window.location.hostname !== '127.0.0.1') {
return 'not_secured';
}
return 'not_supported';
}
/**
* Call the notify callback.
*
* @param {string} message
* @param {bool} isError
*/
WebAuthn.prototype._notify = function(message, isError) {
if (this._notifyCallback) {
this._notifyCallback(message, isError);
}
}
/**
* Set the notify callback.
*
* @param {function(name: string, message: string, isError: bool)} callback
*/
WebAuthn.prototype.setNotify = function(callback) {
this._notifyCallback = callback;
}
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t:"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.WebAuthn=t:e.WebAuthn=t}(this, WebAuthn);