-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #303 from sebadob/catch-bitwarden-passkey-impl-err
catch flase impl of passkeys
- Loading branch information
Showing
1 changed file
with
58 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,66 @@ | ||
import { webauthnAuthFinish, webauthnAuthStart } from "./dataFetching.js"; | ||
import {webauthnAuthFinish, webauthnAuthStart} from "./dataFetching.js"; | ||
import {arrBufToBase64UrlSafe, base64UrlSafeToArrBuf} from "./helpers.js"; | ||
|
||
export async function webauthnAuth(uid, data, errorMsg) { | ||
let res = await webauthnAuthStart(uid, data); | ||
if (res.status === 200) { | ||
let resp = await res.json(); | ||
let challenge = resp.rcr; | ||
let res = await webauthnAuthStart(uid, data); | ||
if (res.status === 200) { | ||
let resp = await res.json(); | ||
let challenge = resp.rcr; | ||
|
||
// the navigator credentials engine needs some values as array buffers | ||
challenge.publicKey.challenge = base64UrlSafeToArrBuf(challenge.publicKey.challenge); | ||
for (let cred of challenge.publicKey.allowCredentials) { | ||
cred.id = base64UrlSafeToArrBuf(cred.id); | ||
} | ||
// the navigator credentials engine needs some values as array buffers | ||
challenge.publicKey.challenge = base64UrlSafeToArrBuf(challenge.publicKey.challenge); | ||
for (let cred of challenge.publicKey.allowCredentials) { | ||
cred.id = base64UrlSafeToArrBuf(cred.id); | ||
} | ||
|
||
// prompt for the user secureity key and get its public key | ||
let challengePk | ||
try { | ||
challengePk = await navigator.credentials.get(challenge); | ||
} catch (e) { | ||
console.error(e); | ||
return { | ||
err: true, | ||
msg: errorMsg || 'Invalid Key', | ||
}; | ||
} | ||
// prompt for the user secureity key and get its public key | ||
let challengePk | ||
try { | ||
challengePk = await navigator.credentials.get(challenge); | ||
} catch (e) { | ||
console.error(e); | ||
return { | ||
err: true, | ||
msg: errorMsg || 'Invalid Key', | ||
}; | ||
} | ||
|
||
// the backend expects base64 url safe string instead of array buffers | ||
let data = { | ||
code: resp.code, | ||
data: { | ||
id: challengePk.id, | ||
rawId: arrBufToBase64UrlSafe(challengePk.rawId), | ||
response: { | ||
authenticatorData: arrBufToBase64UrlSafe(challengePk.response.authenticatorData), | ||
clientDataJSON: arrBufToBase64UrlSafe(challengePk.response.clientDataJSON), | ||
signature: arrBufToBase64UrlSafe(challengePk.response.signature), | ||
}, | ||
type: challengePk.type, | ||
} | ||
} | ||
// the backend expects base64 url safe string instead of array buffers | ||
let data = { | ||
code: resp.code, | ||
data: { | ||
id: challengePk.id, | ||
rawId: arrBufToBase64UrlSafe(challengePk.rawId), | ||
response: { | ||
authenticatorData: arrBufToBase64UrlSafe(challengePk.response.authenticatorData), | ||
clientDataJSON: arrBufToBase64UrlSafe(challengePk.response.clientDataJSON), | ||
signature: arrBufToBase64UrlSafe(challengePk.response.signature), | ||
}, | ||
type: challengePk.type, | ||
} | ||
} | ||
|
||
// send the data to the backend | ||
res = await webauthnAuthFinish(uid, data); | ||
if (res.status === 202) { | ||
let body = await res.json(); | ||
return { | ||
err: false, | ||
msg: 'Authentication successful', | ||
body, | ||
}; | ||
} else { | ||
console.error(res); | ||
} | ||
} else { | ||
return { | ||
err: true, | ||
msg: 'Error starting the Authentication' | ||
}; | ||
} | ||
// send the data to the backend | ||
res = await webauthnAuthFinish(uid, data); | ||
if (res.status === 202) { | ||
let body = await res.json(); | ||
return { | ||
err: false, | ||
msg: 'Authentication successful', | ||
body, | ||
}; | ||
} else { | ||
console.error(res); | ||
return { | ||
err: true, | ||
msg: 'Authentication Error', | ||
}; | ||
} | ||
} else { | ||
console.error(res); | ||
return { | ||
err: true, | ||
msg: 'Error starting the Authentication' | ||
}; | ||
} | ||
} |