diff options
author | Matthew Miller <matthew@millerti.me> | 2022-04-11 23:08:17 -0700 |
---|---|---|
committer | Matthew Miller <matthew@millerti.me> | 2022-05-15 21:49:33 -0700 |
commit | 5993fd43f579315217c9585dbc9825719640b7f2 (patch) | |
tree | 0825a332167835cdc8767eba2b546ea6d620f740 /packages/server/src | |
parent | fc6351302525dc7c96677cf9c50138b6e4bde3b8 (diff) |
Create a helper to contain backup state nuances
Diffstat (limited to 'packages/server/src')
-rw-r--r-- | packages/server/src/helpers/parseBackupFlags.ts | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/packages/server/src/helpers/parseBackupFlags.ts b/packages/server/src/helpers/parseBackupFlags.ts new file mode 100644 index 0000000..0ebddb9 --- /dev/null +++ b/packages/server/src/helpers/parseBackupFlags.ts @@ -0,0 +1,29 @@ +import { CredentialDeviceType } from '@simplewebauthn/typescript-types'; + +/** + * Make sense of Bits 3 and 4 in authenticator indicating: + * + * - Whether the credential can be used on multiple devices + * - Whether the credential is backed up or not + * + * Invalid configurations will raise an `Error` + */ +export function parseBackupFlags({ be, bs }: { be: boolean, bs: boolean }): { + credentialDeviceType: CredentialDeviceType, + credentialBackedUp: boolean, +} { + const credentialBackedUp = bs; + let credentialDeviceType: CredentialDeviceType = 'singleDevice'; + + if (be) { + credentialDeviceType = 'multiDevice'; + } + + if (credentialDeviceType === 'singleDevice' && credentialBackedUp) { + throw new Error( + 'Single-device credential indicated that it was backed up, which should be impossible.' + ) + } + + return { credentialDeviceType, credentialBackedUp }; +} |