summaryrefslogtreecommitdiffhomepage
path: root/packages/server/src/helpers/parseBackupFlags.ts
diff options
context:
space:
mode:
authorMatthew Miller <matthew@millerti.me>2022-05-16 22:52:53 -0700
committerGitHub <noreply@github.com>2022-05-16 22:52:53 -0700
commit39a41ae7e86a4782017c2f489f7c66a9effdcaf3 (patch)
treeccc0cb0708cce781f7d6e189528b37bbd9a1a503 /packages/server/src/helpers/parseBackupFlags.ts
parent5c189bca1dc480e919bb82077b51829f29123375 (diff)
parent190a746b89e2cf3238c0070c08f8458e7751c5ea (diff)
Merge pull request #195 from MasterKale/feat/backup-state
feat/backup-state
Diffstat (limited to 'packages/server/src/helpers/parseBackupFlags.ts')
-rw-r--r--packages/server/src/helpers/parseBackupFlags.ts36
1 files changed, 36 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..c0a1e99
--- /dev/null
+++ b/packages/server/src/helpers/parseBackupFlags.ts
@@ -0,0 +1,36 @@
+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 InvalidBackupFlags(
+ 'Single-device credential indicated that it was backed up, which should be impossible.'
+ )
+ }
+
+ return { credentialDeviceType, credentialBackedUp };
+}
+
+class InvalidBackupFlags extends Error {
+ constructor(message: string) {
+ super(message);
+ this.name = 'InvalidBackupFlags';
+ }
+}