summaryrefslogtreecommitdiffhomepage
path: root/packages/server/src/helpers/parseBackupFlags.ts
blob: ea3a93f23c4ee63a7179b7636a3ea054d2aab01a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import type { CredentialDeviceType } from '../deps.ts';

/**
 * 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 };
}

export class InvalidBackupFlags extends Error {
  constructor(message: string) {
    super(message);
    this.name = 'InvalidBackupFlags';
  }
}