Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
dagnelies committed Oct 6, 2022
1 parent bf22989 commit 898beaa
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 42 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ Parsing data

**`clientData`**
```js
webauthn.parseAuthenticatorData('eyJ0eXBlIjoid2...')
webauthn.parseAuthenticator('eyJ0eXBlIjoid2...')
```

```json
Expand All @@ -160,7 +160,7 @@ webauthn.parseAuthenticatorData('eyJ0eXBlIjoid2...')
**`authenticatorData`**

```js
webauthn.parseAuthenticatorData('SZYN5YgOjGh0NB...')
webauthn.parseAuthenticator('SZYN5YgOjGh0NB...')
```

```json
Expand Down
2 changes: 1 addition & 1 deletion demos/js/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import * as webauthn from '../../dist/webauthn.min.js'
},
async login() {
let credentialId = window.localStorage.getItem(this.username)
let res = await webauthn.login([credentialId], window.crypto.randomUUID(), {isExternal: this.isExternal})
let res = await webauthn.login(credentialId ? [credentialId] : [], window.crypto.randomUUID(), {isExternal: this.isExternal})
console.log(res)

this.isAuthenticated = true;
Expand Down
7 changes: 4 additions & 3 deletions demos/js/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ import * as webauthn from '../../dist/webauthn.min.js'
},
async login() {
try {
let res = await webauthn.login([this.authentication.credentialId], this.authentication.challenge, this.authentication.options)
const credentialId = this.authentication.credentialId
let res = await webauthn.login(credentialId ? [credentialId] : [], this.authentication.challenge, this.authentication.options)
console.log(res)
this.authentication.result = res
}
Expand All @@ -86,10 +87,10 @@ import * as webauthn from '../../dist/webauthn.min.js'
}
},
parseAuthData(authData) {
return webauthn.parseAuthenticatorBase64(authData)
return webauthn.parseAuthenticator(authData)
},
parseClientData(clientData) {
return webauthn.parseClientBase64(clientData)
return webauthn.parseClient(clientData)
}
}
})
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.

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": "0.0.9",
"version": "0.0.10",
"description": "A small wrapper around the webauthn protocol to make one's life easier.",
"main": "src/index.ts",
"scripts": {
Expand Down
23 changes: 12 additions & 11 deletions src/parsers.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import * as authenticators from './authenticators'
import * as utils from './utils'
import { AuthenticatorInfo, ClientInfo } from './types'

const utf8Decoder = new TextDecoder('utf-8')

export function parseClientBuffer(buffer :ArrayBuffer) {
return JSON.parse(utf8Decoder.decode(buffer))
export function parseClient(data :string|ArrayBuffer) :ClientInfo {
if(typeof data == 'string')
data = utils.parseBase64url(data)
return JSON.parse(utf8Decoder.decode(data))
}


export function parseAuthenticatorBuffer(buffer :ArrayBuffer) {
return authenticators.parseAuthBuffer(buffer)
export function parseAuthenticator(data :string|ArrayBuffer) :AuthenticatorInfo {
if(typeof data == 'string')
data = utils.parseBase64url(data)
return authenticators.parseAuthBuffer(data)
}


export function parseAttestationBuffer(buffer :ArrayBuffer) {
export function parseAttestation(data :string|ArrayBuffer) :unknown {
if(typeof data == 'string')
data = utils.parseBase64url(data)
return 'Really complex to parse. Good luck with that one!'
}

export function parseClientBase64(txt :string) {
return parseClientBuffer( utils.parseBase64url(txt) )
}


export function parseAuthenticatorBase64(txt :string) {
return parseAuthenticatorBuffer( utils.parseBase64url(txt) )
}
57 changes: 42 additions & 15 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ export type NamedAlgo = 'RS256' | 'ES256'


export interface LoginOptions {
userVerification ?:UserVerificationRequirement,
authenticatorType ?:AuthType,
timeout ?:number,
userVerification ?:UserVerificationRequirement
authenticatorType ?:AuthType
timeout ?:number
debug ?:boolean
}


export interface LoginResult {
credentialId: string,
credentialId: string
//userHash: string, // unreliable, optional for authenticators
authenticatorData: string,
clientData: string,
signature: string,
authenticatorData: string
clientData: string
signature: string
debug?: object
}

Expand All @@ -32,14 +32,41 @@ export interface RegisterOptions extends LoginOptions {


export interface RegisterResult {
username: string,
username: string
credential: {
id: string,
publicKey: string,
id: string
publicKey: string
algorithm: 'RS256' | 'ES256'
},
authenticatorData: string,
clientData: string,
attestationData?: string,
}
authenticatorData: string
clientData: string
attestationData?: string
debug?: object
}
}

export interface ClientInfo {
type: "webauthn.create" | "webauthn.get"
challenge: string
origin: string
crossOrigin: boolean
tokenBindingId?: {
id: string
status: string
}
// extensions?
}

export interface AuthenticatorInfo {
rpIdHash: string,
flags: {
userPresent: boolean
userVerified: boolean
backupEligibility: boolean
backupState: boolean
attestedData: boolean
extensionsIncluded: boolean
}
counter: number
aaguid: string
name: string
}
10 changes: 5 additions & 5 deletions src/webauthn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ export async function register(username :string, challenge :string, options? :Re

if(options.debug) {
registrationResponse.debug = {
client: parsers.parseClientBuffer(response.clientDataJSON),
authenticator: parsers.parseAuthenticatorBuffer(response.getAuthenticatorData()),
attestation: parsers.parseAttestationBuffer(response.attestationObject)
client: parsers.parseClient(response.clientDataJSON),
authenticator: parsers.parseAuthenticator(response.getAuthenticatorData()),
attestation: parsers.parseAttestation(response.attestationObject)
}
}

Expand Down Expand Up @@ -204,8 +204,8 @@ export async function login(credentialIds :string[], challenge :string, options?

if(options.debug) {
loginResult.debug = {
client: parsers.parseClientBuffer(response.clientDataJSON),
authenticator: parsers.parseAuthenticatorBuffer(response.authenticatorData),
client: parsers.parseClient(response.clientDataJSON),
authenticator: parsers.parseAuthenticator(response.authenticatorData),
}
}

Expand Down

0 comments on commit 898beaa

Please sign in to comment.