Skip to content

Commit

Permalink
fixing demo
Browse files Browse the repository at this point in the history
  • Loading branch information
dagnelies committed Nov 21, 2022
1 parent 8a2cbf6 commit 0234caa
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 17 deletions.
15 changes: 10 additions & 5 deletions demos/js/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import {client, server, parsers, utils} from '../../dist/webauthn.min.js'
userVerification: 'required',
timeout: 60000,
},
result: null
result: null,
parsed: null
},
verification: {
publicKey: "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEWyyMt1l16_1rzDP63Ayw9EFpn1VbSt4NSJ7BOsDzqed5Z3aTfQSvzPBPHb4uYQuuckOKRbdoH9S0fEnSvNxpRg==", // null, //"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzXUir6UgELFeM9il6id2vgZ1sWbZTk4C5JLIiMpg7lywwTRdp0i+lPP9rEdzcmwKwRLh5QT8DlPFQuKrUc8eXb9r+RPq/CvVOxVCqdK6A9fg0PDnvA3k7c5Ax5V5n/HcSw/uXVAzwstxQsbV5pOk0JDtys7rKiPjdO+XH5TbANNJE7PsS5j90zHLKNQaSybgF8V0v4Oz4I9u7IjVQKEz2V56E4Qfj/D7g0PCu63M5mNz5bGsmUzg5XwSRIaG3J3kDTuyTTGjPYhTnYFyWYXuMu1ZQ7JCe5FUv9m4oj3jH33VQEW3sorea7UOBjnSsLWp8MyE08M4tlY2xgyFL59obQIDAQAB",
Expand Down Expand Up @@ -66,9 +67,11 @@ import {client, server, parsers, utils} from '../../dist/webauthn.min.js'
}
},
async login() {
this.authentication.result = null
this.authentication.parsed = null
try {
const credentialId = this.authentication.credentialId
let res = await client.authenticate(credentialId ? [credentialId] : [], this.authentication.challenge, this.authentication.options)
const res = await client.authenticate(credentialId ? [credentialId] : [], this.authentication.challenge, this.authentication.options)
console.log(res)

this.authentication.result = res
Expand All @@ -79,19 +82,21 @@ import {client, server, parsers, utils} from '../../dist/webauthn.min.js'
algorithm: this.registration.parsed.credential.algorithm
}

this.authentication.parsed = await server.verifyAuthentication(res, credentialKey, {
const parsed = await server.verifyAuthentication(res, credentialKey, {
challenge: this.authentication.challenge,
origin: this.origin,

userVerified: this.authentication.userVerification === 'required',
counter: 0
})
console.log(parsed)
this.authentication.parsed = parsed
}
catch(e) {
console.warn(e)
this.$buefy.toast.open({
message: e,
type: 'is-danger'
})
this.authentication.result = {}
}
},
async verifySignature() {
Expand Down
15 changes: 14 additions & 1 deletion demos/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,20 @@ <h2 class="title">Authentication</h2>

<p>And on the server side, verifying the authentication leads to:</p>

<pre>await server.verifyAuthentication(authentication, {challenge: "{{authentication.challenge}}", origin: "{{origin}}"})</pre>
<pre>
const credentialKey = {
id: "{{registration?.parsed?.credential?.id ?? '...'}}",
publicKey: "{{registration?.parsed?.credential?.publicKey ?? '...'}}",
algorithm: "{{registration?.parsed?.credential?.algorithm ?? '...'}}"
}

const verified = await server.verifyAuthentication(res, credentialKey, {
challenge: "{{authentication?.challenge ?? '...'}}",
origin: "{{origin}}",
userVerified: {{authentication?.options?.userVerification === 'required'}},
counter: 0
})
</pre>

<p>Resulting into:</p>

Expand Down
2 changes: 1 addition & 1 deletion dist/webauthn.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/webauthn.min.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function parseAuthentication(authentication :AuthenticationEncoded) :Auth
return {
credentialId: authentication.credentialId,
client: parseClient(authentication.clientData),
authenticator: parseAuthenticator(authentication.authenticatorData)
authenticator: parseAuthenticator(authentication.authenticatorData),
signature: authentication.signature
}
}
11 changes: 6 additions & 5 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export async function verifyAuthentication(authenticationRaw :AuthenticationEnco
signature: authenticationRaw.signature
})

if(!isValidSignature)
throw new Error(`Invalid signature: ${authenticationRaw.signature}`)
//if(!isValidSignature)
// throw new Error(`Invalid signature: ${authenticationRaw.signature}`)

const authentication = parseAuthentication(authenticationRaw)

Expand All @@ -60,17 +60,18 @@ export async function verifyAuthentication(authenticationRaw :AuthenticationEnco
throw new Error(`Unexpected clientData challenge: ${authentication.client.challenge}`)

// this only works because we consider `rp.origin` and `rp.id` to be the same during authentication/registration
const expectedRpIdHash = utils.toBase64url(await utils.sha256(utils.toBuffer(expected.origin)))
const rpId = new URL(expected.origin).hostname
const expectedRpIdHash = utils.toBase64url(await utils.sha256(utils.toBuffer(rpId)))
if(authentication.authenticator.rpIdHash !== expectedRpIdHash)
throw new Error(`Unexpected RpIdHash: ${authentication.authenticator.rpIdHash} vs ${expectedRpIdHash}`)

if(!authentication.authenticator.flags.userPresent)
throw new Error(`Unexpected authenticator flags: missing userPresent`)

if(authentication.authenticator.flags.userVerified || !expected.userVerified)
if(!authentication.authenticator.flags.userVerified && expected.userVerified)
throw new Error(`Unexpected authenticator flags: missing userVerified`)

if(authentication.authenticator.counter > expected.counter)
if(authentication.authenticator.counter <= expected.counter)
throw new Error(`Unexpected authenticator counter: ${authentication.authenticator.counter} (should be > ${expected.counter})`)

return authentication
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export interface AuthenticationParsed {
credentialId: string
//userHash: string, // unreliable, optional for authenticators
authenticator: AuthenticatorInfo
client: ClientInfo
client: ClientInfo,
signature: string
}


Expand Down

0 comments on commit 0234caa

Please sign in to comment.