Skip to content

Commit

Permalink
utils.randomChallenge => server.randomChallenge
Browse files Browse the repository at this point in the history
  • Loading branch information
dagnelies committed Aug 7, 2024
1 parent c72d17c commit 78ad12f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 20 deletions.
13 changes: 8 additions & 5 deletions docs/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ sequenceDiagram
4. The server parses and verifies the authentication payload


1️⃣ Requesting challenge
------------------------
1️⃣ Requesting a challenge from the server
-----------------------------------------

The challenge is basically a [nonce](https://en.wikipedia.org/wiki/nonce) to avoid replay attacks. It must be a byte array encoded as *base64url* string.
The challenge is basically a [nonce](https://en.wikipedia.org/wiki/nonce) to avoid replay attacks.
It must be a truly random and non-deterministic byte buffer encoded as *byte64url*.

```
const challenge = /* request it from server */
```js
import { server } from '@passwordless-id/webauthn'

const challenge = server.randomChallenge()
```

Remember it on the server side during a certain amount of time and "consume" it once used.
Expand Down
4 changes: 2 additions & 2 deletions docs/demos/js/debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const app = new Vue({
registration: {
options: {
user: "Arnaud",
challenge: webauthn.utils.randomChallenge(),
challenge: webauthn.server.randomChallenge(),
hints: [],
userVerification: 'preferred',
discoverable: 'preferred',
Expand All @@ -20,7 +20,7 @@ const app = new Vue({
authentication: {
credentialId: null,
options: {
challenge: webauthn.utils.randomChallenge(),
challenge: webauthn.server.randomChallenge(),
hints: [],
authenticatorType: 'auto',
userVerification: 'required',
Expand Down
4 changes: 2 additions & 2 deletions docs/demos/js/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const app = new Vue({
registration: {
options: {
user: "Arnaud",
challenge: webauthn.utils.randomChallenge(),
challenge: webauthn.server.randomChallenge(),
hints: [],
userVerification: 'preferred',
discoverable: 'preferred',
Expand All @@ -41,7 +41,7 @@ const app = new Vue({
authentication: {
credentialId: null,
options: {
challenge: webauthn.utils.randomChallenge(),
challenge: webauthn.server.randomChallenge(),
hints: [],
authenticatorType: 'auto',
userVerification: 'required',
Expand Down
14 changes: 8 additions & 6 deletions docs/registration.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ sequenceDiagram
```


1️⃣ Requesting the challenge from the server
-------------------------------------------
1️⃣ Requesting a challenge from the server
-----------------------------------------

The challenge is basically a [nonce](https://en.wikipedia.org/wiki/nonce) to avoid replay attacks.
This challenge should truly random and not deterministic.
It must be a truly random and non-deterministic byte buffer encoded as *byte64url*.

```
const challenge = /* request it from server */
```js
import { server } from '@passwordless-id/webauthn'

const challenge = server.randomChallenge()
```

Remember the request on the server side during a certain amount of time and "consume" it once used.
Remember the challenge on the server side during a certain amount of time and "consume" it once used.

> There are two ways to deal with remembering the challenge. Either store it in a global cache containing all challenges, or by creating a (cookie based) session directly and storing it as part of the session data.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@passwordless-id/webauthn",
"version": "2.0.2",
"version": "2.0.3",
"description": "A small wrapper around the webauthn protocol to make one's life easier.",

"type": "module",
Expand Down
8 changes: 8 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import { parseAuthenticator, parseClient, toAuthenticationInfo } from "./parsers
import { AuthenticationJSON, NamedAlgo, RegistrationJSON, RegistrationInfo, AuthenticationInfo, Base64URLString, CollectedClientData, UserInfo, CredentialInfo, AuthenticatorInfo, AuthenticatorParsed } from "./types";
import * as utils from './utils'


export async function randomChallenge() {
const buffer = crypto.getRandomValues(new Uint8Array(16)); // 128 bits
return utils.toBase64url(buffer);
}



async function isValid(validator :any, value :any) :Promise<boolean> {
if(typeof validator === 'function') {
const res = validator(value)
Expand Down
4 changes: 0 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

import { Base64URLString } from "./types"

export function randomChallenge() {
return crypto.randomUUID()
}


export function toBuffer(txt :string) :ArrayBuffer {
return Uint8Array.from(txt, c => c.charCodeAt(0)).buffer
Expand Down

0 comments on commit 78ad12f

Please sign in to comment.