1
1
import { encodeURLEncodedBase64 , decodeURLEncodedBase64 } from '../utils.js' ;
2
- import { showElem , hideElem } from '../utils/dom.js' ;
2
+ import { showElem } from '../utils/dom.js' ;
3
3
4
4
const { appSubUrl, csrfToken} = window . config ;
5
5
6
6
export async function initUserAuthWebAuthn ( ) {
7
- hideElem ( '#webauthn-error' ) ;
8
-
9
7
const elPrompt = document . querySelector ( '.user.signin.webauthn-prompt' ) ;
10
8
if ( ! elPrompt ) {
11
9
return ;
@@ -25,21 +23,21 @@ export async function initUserAuthWebAuthn() {
25
23
for ( const cred of options . publicKey . allowCredentials ) {
26
24
cred . id = decodeURLEncodedBase64 ( cred . id ) ;
27
25
}
28
- const credential = await navigator . credentials . get ( {
29
- publicKey : options . publicKey
30
- } ) ;
31
26
try {
27
+ const credential = await navigator . credentials . get ( {
28
+ publicKey : options . publicKey
29
+ } ) ;
32
30
await verifyAssertion ( credential ) ;
33
31
} catch ( err ) {
34
32
if ( ! options . publicKey . extensions ?. appid ) {
35
33
webAuthnError ( 'general' , err . message ) ;
36
34
return ;
37
35
}
38
36
delete options . publicKey . extensions . appid ;
39
- const credential = await navigator . credentials . get ( {
40
- publicKey : options . publicKey
41
- } ) ;
42
37
try {
38
+ const credential = await navigator . credentials . get ( {
39
+ publicKey : options . publicKey
40
+ } ) ;
43
41
await verifyAssertion ( credential ) ;
44
42
} catch ( err ) {
45
43
webAuthnError ( 'general' , err . message ) ;
@@ -48,7 +46,7 @@ export async function initUserAuthWebAuthn() {
48
46
}
49
47
50
48
async function verifyAssertion ( assertedCredential ) {
51
- // Move data into Arrays incase it is super long
49
+ // Move data into Arrays in case it is super long
52
50
const authData = new Uint8Array ( assertedCredential . response . authenticatorData ) ;
53
51
const clientDataJSON = new Uint8Array ( assertedCredential . response . clientDataJSON ) ;
54
52
const rawId = new Uint8Array ( assertedCredential . rawId ) ;
@@ -137,15 +135,11 @@ function webAuthnError(errorType, message) {
137
135
138
136
function detectWebAuthnSupport ( ) {
139
137
if ( ! window . isSecureContext ) {
140
- document . getElementById ( 'register-button' ) . disabled = true ;
141
- document . getElementById ( 'login-button' ) . disabled = true ;
142
138
webAuthnError ( 'insecure' ) ;
143
139
return false ;
144
140
}
145
141
146
142
if ( typeof window . PublicKeyCredential !== 'function' ) {
147
- document . getElementById ( 'register-button' ) . disabled = true ;
148
- document . getElementById ( 'login-button' ) . disabled = true ;
149
143
webAuthnError ( 'browser' ) ;
150
144
return false ;
151
145
}
@@ -158,15 +152,13 @@ export function initUserAuthWebAuthnRegister() {
158
152
if ( ! elRegister ) {
159
153
return ;
160
154
}
161
-
162
- hideElem ( '#webauthn-error' ) ;
163
-
164
- elRegister . addEventListener ( 'click' , ( e ) => {
155
+ if ( ! detectWebAuthnSupport ( ) ) {
156
+ elRegister . disabled = true ;
157
+ return ;
158
+ }
159
+ elRegister . addEventListener ( 'click' , async ( e ) => {
165
160
e . preventDefault ( ) ;
166
- if ( ! detectWebAuthnSupport ( ) ) {
167
- return ;
168
- }
169
- webAuthnRegisterRequest ( ) ;
161
+ await webAuthnRegisterRequest ( ) ;
170
162
} ) ;
171
163
}
172
164
@@ -203,15 +195,12 @@ async function webAuthnRegisterRequest() {
203
195
}
204
196
}
205
197
206
- let credential ;
207
198
try {
208
- credential = await navigator . credentials . create ( {
199
+ const credential = await navigator . credentials . create ( {
209
200
publicKey : options . publicKey
210
201
} ) ;
202
+ await webauthnRegistered ( credential ) ;
211
203
} catch ( err ) {
212
204
webAuthnError ( 'unknown' , err ) ;
213
- return ;
214
205
}
215
-
216
- webauthnRegistered ( credential ) ;
217
206
}
0 commit comments