@oneuptime/common 12.0.21 → 12.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/Models/DatabaseModels/Index.ts +4 -0
  2. package/Models/DatabaseModels/NetworkDevice.ts +44 -0
  3. package/Models/DatabaseModels/NetworkDeviceAutoImportRule.ts +564 -0
  4. package/Models/DatabaseModels/NetworkDeviceDiscoveryScan.ts +38 -0
  5. package/Models/DatabaseModels/UserTwoFactorBackupCode.ts +262 -0
  6. package/Server/API/UserTwoFactorBackupCodeAPI.ts +258 -0
  7. package/Server/Infrastructure/Postgres/SchemaMigrations/1789100000000-AddNetworkDeviceAutoImportRule.ts +60 -0
  8. package/Server/Infrastructure/Postgres/SchemaMigrations/1789100000000-AddUserTwoFactorBackupCode.ts +63 -0
  9. package/Server/Infrastructure/Postgres/SchemaMigrations/1789200000000-AddSysObjectIdPatternToAutoImportRule.ts +26 -0
  10. package/Server/Infrastructure/Postgres/SchemaMigrations/1789300000000-AddAutoApplyVendorHealthTemplate.ts +26 -0
  11. package/Server/Infrastructure/Postgres/SchemaMigrations/Index.ts +8 -0
  12. package/Server/Services/Index.ts +4 -0
  13. package/Server/Services/NetworkDeviceAutoImportRuleEngineService.ts +917 -0
  14. package/Server/Services/NetworkDeviceAutoImportRuleService.ts +181 -0
  15. package/Server/Services/UserService.ts +42 -0
  16. package/Server/Services/UserTwoFactorBackupCodeService.ts +355 -0
  17. package/Server/Utils/Monitor/NetworkInventoryUtil.ts +39 -1
  18. package/Server/Utils/TwoFactorBackupCode.ts +266 -0
  19. package/Tests/Server/API/UserAuthenticationAPI.test.ts +16 -3
  20. package/Tests/Server/API/UserTwoFactorAuthAdminAPI.test.ts +18 -2
  21. package/Tests/Server/API/UserTwoFactorBackupCodeAPI.test.ts +1390 -0
  22. package/Tests/Server/Services/AutoImportScanCredentialSelect.test.ts +205 -0
  23. package/Tests/Server/Services/NetworkDeviceAutoImportRuleEngineService.test.ts +933 -0
  24. package/Tests/Server/Services/UserAuthenticationService.test.ts +23 -1
  25. package/Tests/Server/Services/UserTwoFactorAuthAdmin.test.ts +21 -0
  26. package/Tests/Server/Services/UserTwoFactorBackupCodeAdminSurface.test.ts +919 -0
  27. package/Tests/Server/Services/UserTwoFactorBackupCodeService.test.ts +862 -0
  28. package/Tests/Server/Utils/Monitor/NetworkInventoryUtil.test.ts +97 -1
  29. package/Tests/Server/Utils/TwoFactorBackupCode.test.ts +475 -0
  30. package/Tests/Utils/NetworkDiscovery/AutoImportRuleMatcher.test.ts +516 -0
  31. package/Tests/Utils/NetworkDiscovery/DiscoveredDeviceBuilder.test.ts +272 -0
  32. package/Tests/Utils/NetworkDiscovery/ScanTargetUtil.test.ts +164 -0
  33. package/Types/Email/EmailTemplateType.ts +2 -0
  34. package/Types/NetworkAutomation/RuleRunResult.ts +84 -0
  35. package/Types/Permission.ts +47 -0
  36. package/Types/UserAuthenticationStatus.ts +16 -0
  37. package/Utils/NetworkDiscovery/AutoImportRuleMatcher.ts +257 -0
  38. package/Utils/NetworkDiscovery/DiscoveredDeviceBuilder.ts +183 -0
  39. package/Utils/NetworkDiscovery/DiscoveredHostUtil.ts +95 -0
  40. package/Utils/NetworkDiscovery/DiscoveryImportEligibility.ts +66 -0
  41. package/Utils/NetworkDiscovery/ScanTargetUtil.ts +82 -0
  42. package/build/dist/Models/DatabaseModels/Index.js +4 -0
  43. package/build/dist/Models/DatabaseModels/Index.js.map +1 -1
  44. package/build/dist/Models/DatabaseModels/NetworkDevice.js +45 -0
  45. package/build/dist/Models/DatabaseModels/NetworkDevice.js.map +1 -1
  46. package/build/dist/Models/DatabaseModels/NetworkDeviceAutoImportRule.js +589 -0
  47. package/build/dist/Models/DatabaseModels/NetworkDeviceAutoImportRule.js.map +1 -0
  48. package/build/dist/Models/DatabaseModels/NetworkDeviceDiscoveryScan.js +28 -0
  49. package/build/dist/Models/DatabaseModels/NetworkDeviceDiscoveryScan.js.map +1 -1
  50. package/build/dist/Models/DatabaseModels/UserTwoFactorBackupCode.js +277 -0
  51. package/build/dist/Models/DatabaseModels/UserTwoFactorBackupCode.js.map +1 -0
  52. package/build/dist/Server/API/UserTwoFactorBackupCodeAPI.js +201 -0
  53. package/build/dist/Server/API/UserTwoFactorBackupCodeAPI.js.map +1 -0
  54. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1789100000000-AddNetworkDeviceAutoImportRule.js +35 -0
  55. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1789100000000-AddNetworkDeviceAutoImportRule.js.map +1 -0
  56. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1789100000000-AddUserTwoFactorBackupCode.js +46 -0
  57. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1789100000000-AddUserTwoFactorBackupCode.js.map +1 -0
  58. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1789200000000-AddSysObjectIdPatternToAutoImportRule.js +19 -0
  59. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1789200000000-AddSysObjectIdPatternToAutoImportRule.js.map +1 -0
  60. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1789300000000-AddAutoApplyVendorHealthTemplate.js +19 -0
  61. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1789300000000-AddAutoApplyVendorHealthTemplate.js.map +1 -0
  62. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/Index.js +8 -0
  63. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/Index.js.map +1 -1
  64. package/build/dist/Server/Services/Index.js +4 -0
  65. package/build/dist/Server/Services/Index.js.map +1 -1
  66. package/build/dist/Server/Services/NetworkDeviceAutoImportRuleEngineService.js +711 -0
  67. package/build/dist/Server/Services/NetworkDeviceAutoImportRuleEngineService.js.map +1 -0
  68. package/build/dist/Server/Services/NetworkDeviceAutoImportRuleService.js +155 -0
  69. package/build/dist/Server/Services/NetworkDeviceAutoImportRuleService.js.map +1 -0
  70. package/build/dist/Server/Services/UserService.js +39 -0
  71. package/build/dist/Server/Services/UserService.js.map +1 -1
  72. package/build/dist/Server/Services/UserTwoFactorBackupCodeService.js +327 -0
  73. package/build/dist/Server/Services/UserTwoFactorBackupCodeService.js.map +1 -0
  74. package/build/dist/Server/Utils/Monitor/NetworkInventoryUtil.js +26 -0
  75. package/build/dist/Server/Utils/Monitor/NetworkInventoryUtil.js.map +1 -1
  76. package/build/dist/Server/Utils/TwoFactorBackupCode.js +269 -0
  77. package/build/dist/Server/Utils/TwoFactorBackupCode.js.map +1 -0
  78. package/build/dist/Types/Email/EmailTemplateType.js +2 -0
  79. package/build/dist/Types/Email/EmailTemplateType.js.map +1 -1
  80. package/build/dist/Types/NetworkAutomation/RuleRunResult.js +24 -0
  81. package/build/dist/Types/NetworkAutomation/RuleRunResult.js.map +1 -1
  82. package/build/dist/Types/Permission.js +41 -0
  83. package/build/dist/Types/Permission.js.map +1 -1
  84. package/build/dist/Utils/NetworkDiscovery/AutoImportRuleMatcher.js +149 -0
  85. package/build/dist/Utils/NetworkDiscovery/AutoImportRuleMatcher.js.map +1 -0
  86. package/build/dist/Utils/NetworkDiscovery/DiscoveredDeviceBuilder.js +121 -0
  87. package/build/dist/Utils/NetworkDiscovery/DiscoveredDeviceBuilder.js.map +1 -0
  88. package/build/dist/Utils/NetworkDiscovery/DiscoveredHostUtil.js +75 -0
  89. package/build/dist/Utils/NetworkDiscovery/DiscoveredHostUtil.js.map +1 -0
  90. package/build/dist/Utils/NetworkDiscovery/DiscoveryImportEligibility.js +59 -0
  91. package/build/dist/Utils/NetworkDiscovery/DiscoveryImportEligibility.js.map +1 -0
  92. package/build/dist/Utils/NetworkDiscovery/ScanTargetUtil.js +60 -0
  93. package/build/dist/Utils/NetworkDiscovery/ScanTargetUtil.js.map +1 -1
  94. package/package.json +1 -1
@@ -0,0 +1,266 @@
1
+ import { EncryptionSecret } from "../EnvironmentConfig";
2
+ import ObjectID from "../../Types/ObjectID";
3
+ import CaptureSpan from "./Telemetry/CaptureSpan";
4
+ import crypto from "crypto";
5
+
6
+ /*
7
+ * Single-use recovery codes for an account whose second factor is gone --
8
+ * the phone that held the authenticator app, or the security key that is now
9
+ * in a taxi somewhere.
10
+ *
11
+ * WHY THIS IS SERVER-ONLY
12
+ *
13
+ * Nothing in this file falls back to Math.random. Common/Utils/UUID does, on
14
+ * purpose, because it is bundled into the dashboard and has to keep producing
15
+ * well-formed ids in a browser with no Web Crypto -- which is exactly why
16
+ * ObjectID.generate() must never mint a code here. A backup code is a
17
+ * password-equivalent credential; if the platform cannot produce secure
18
+ * randomness, generation must throw rather than quietly emit something
19
+ * predictable. Common/Server/Utils/VerificationCode.ts makes the same call for
20
+ * the same reason.
21
+ *
22
+ * THE CODE SPACE
23
+ *
24
+ * Ten characters drawn uniformly from a 32 symbol alphabet is 2^50 codes, and
25
+ * a user holds ten of them at once -- so a blind guess lands with probability
26
+ * ~10/2^50, about one in 10^14. That is far beyond anything the rate limiter
27
+ * needs to defend, which matters because it settles the design question a TOTP
28
+ * code cannot settle: the six digit space is small enough that the limiter IS
29
+ * the control, whereas here the code itself is.
30
+ *
31
+ * THE HASH
32
+ *
33
+ * Codes are stored as HMAC-SHA256 keyed by the instance's EncryptionSecret,
34
+ * never in the clear, and the fast keyed-digest lane is the RIGHT one here
35
+ * rather than scrypt. scrypt exists to make guessing a low-entropy,
36
+ * human-chosen secret expensive; a code minted above has no low-entropy
37
+ * structure to guess, so the cost would buy nothing and would be paid on every
38
+ * verification. Keying with the EncryptionSecret is what a bare SHA-256 would
39
+ * miss: it lives in configuration rather than in Postgres, so a database dump
40
+ * on its own cannot be run through a dictionary of every possible code.
41
+ *
42
+ * The digest is deterministic given (userId, code), which is deliberate and is
43
+ * what makes single-use consumption a single conditional UPDATE rather than a
44
+ * read of every one of the user's rows followed by a comparison of each. A
45
+ * per-row salt would forfeit that for no gain -- see the note on domain
46
+ * separation below for the property it would have been bought for, which the
47
+ * userId already provides.
48
+ */
49
+
50
+ /**
51
+ * Crockford's Base32 alphabet: the digits and the uppercase letters, minus
52
+ * I, L, O and U.
53
+ *
54
+ * These codes get read off a screen and typed back in months later, possibly
55
+ * from a piece of paper in a drawer, so the alphabet is chosen for the eye
56
+ * rather than for density. I/1, L/1 and O/0 are the pairs people transcribe
57
+ * wrongly; U is dropped by Crockford so that no code can spell an obscenity
58
+ * at a user who did nothing to deserve one.
59
+ *
60
+ * Exactly 32 symbols, so each character carries a clean five bits and the
61
+ * entropy arithmetic in the header comment is exact rather than approximate.
62
+ */
63
+ export const BackupCodeAlphabet: string = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
64
+
65
+ /** Characters per code. Ten symbols over a 32 symbol alphabet is 2^50. */
66
+ export const BackupCodeLength: number = 10;
67
+
68
+ /**
69
+ * How many codes a user is issued at once.
70
+ *
71
+ * Ten is enough that losing a phone does not become an emergency after the
72
+ * second sign-in, and small enough that the printed list is one short block a
73
+ * person will actually keep.
74
+ */
75
+ export const BackupCodeSetSize: number = 10;
76
+
77
+ /** Characters per group in the displayed form, e.g. `AB3D5-9XZQ2`. */
78
+ const DISPLAY_GROUP_LENGTH: number = 5;
79
+
80
+ /*
81
+ * Prefixed into every digest so a stored hash is bound to the scheme that
82
+ * produced it. If the construction below ever has to change, old rows keep
83
+ * verifying under the version they were written with instead of silently
84
+ * failing to match and locking a user out of their own recovery codes.
85
+ */
86
+ const HASH_SCHEME_VERSION: string = "v1";
87
+
88
+ /*
89
+ * Characters a person plausibly types in place of a symbol that is not in the
90
+ * alphabet. Applied before the strip below, so `O` becomes `0` rather than
91
+ * being deleted -- deleting it would shorten the code and guarantee a
92
+ * mismatch, which is the confusing failure this map exists to avoid.
93
+ */
94
+ const AMBIGUOUS_CHARACTER_MAP: Record<string, string> = {
95
+ I: "1",
96
+ L: "1",
97
+ O: "0",
98
+ };
99
+
100
+ export default class TwoFactorBackupCode {
101
+ /**
102
+ * One code, drawn uniformly from the alphabet above.
103
+ *
104
+ * `crypto.randomInt` per character rather than `randomBytes(n) % 32`. The
105
+ * modulo version happens to be uniform for this alphabet only because 32
106
+ * divides 256, and it stops being uniform the moment somebody edits the
107
+ * alphabet -- silently, with no test that would notice. `randomInt` rejects
108
+ * out-of-range draws internally for whatever bound it is given, so the
109
+ * uniformity does not depend on a coincidence nobody wrote down.
110
+ */
111
+ @CaptureSpan()
112
+ public static generateCode(): string {
113
+ let code: string = "";
114
+
115
+ for (let index: number = 0; index < BackupCodeLength; index++) {
116
+ code += BackupCodeAlphabet.charAt(
117
+ crypto.randomInt(0, BackupCodeAlphabet.length),
118
+ );
119
+ }
120
+
121
+ return code;
122
+ }
123
+
124
+ /**
125
+ * A full set of distinct codes.
126
+ *
127
+ * Duplicates are not a security problem -- at 2^50 the birthday odds across
128
+ * ten draws are around 4e-14 -- but a duplicate WOULD be a correctness
129
+ * problem downstream: two rows sharing a digest means consuming one leaves a
130
+ * second, identical, still-valid code behind, so a "single-use" code would
131
+ * work twice. Cheaper to rule out here than to reason about there.
132
+ */
133
+ @CaptureSpan()
134
+ public static generateCodeSet(
135
+ count: number = BackupCodeSetSize,
136
+ ): Array<string> {
137
+ const codes: Set<string> = new Set<string>();
138
+
139
+ while (codes.size < count) {
140
+ codes.add(TwoFactorBackupCode.generateCode());
141
+ }
142
+
143
+ return Array.from(codes);
144
+ }
145
+
146
+ /**
147
+ * The form shown to the user: one hyphen in the middle, for the same reason
148
+ * the alphabet drops ambiguous letters -- a ten character run is hard to
149
+ * read back without losing your place.
150
+ *
151
+ * Purely cosmetic. `normalizeCode` strips the hyphen straight back out, so
152
+ * a user may type the code with it, without it, or with the spaces their
153
+ * password manager pasted in.
154
+ */
155
+ @CaptureSpan()
156
+ public static formatForDisplay(code: string): string {
157
+ const groups: Array<string> = [];
158
+
159
+ for (
160
+ let index: number = 0;
161
+ index < code.length;
162
+ index += DISPLAY_GROUP_LENGTH
163
+ ) {
164
+ groups.push(code.substring(index, index + DISPLAY_GROUP_LENGTH));
165
+ }
166
+
167
+ return groups.join("-");
168
+ }
169
+
170
+ /**
171
+ * Reduce whatever the user typed to the canonical form the digest is
172
+ * computed over.
173
+ *
174
+ * Everything here is about not rejecting somebody who supplied exactly the
175
+ * right secret material: the display hyphen, the spaces a clipboard adds,
176
+ * lowercase from a phone keyboard, and the three transcription confusions
177
+ * the alphabet was chosen to make survivable. Anything still outside the
178
+ * alphabet after that is dropped rather than rejected -- this is a
179
+ * canonicaliser, not a validator; the digest comparison is what decides
180
+ * whether a code is real.
181
+ *
182
+ * @param rawCode - The code exactly as submitted.
183
+ * @returns The code reduced to alphabet symbols, uppercase.
184
+ */
185
+ @CaptureSpan()
186
+ public static normalizeCode(rawCode: string): string {
187
+ /*
188
+ * The code arrives straight off a JSON body, so it is only a string
189
+ * because the client chose to send one. A number or an object here must
190
+ * fail verification, not throw out of `.toUpperCase` and surface as a 500.
191
+ */
192
+ if (typeof rawCode !== "string" || !rawCode) {
193
+ return "";
194
+ }
195
+
196
+ let normalized: string = "";
197
+
198
+ for (const character of rawCode.toUpperCase()) {
199
+ const mapped: string = AMBIGUOUS_CHARACTER_MAP[character] || character;
200
+
201
+ if (BackupCodeAlphabet.includes(mapped)) {
202
+ normalized += mapped;
203
+ }
204
+ }
205
+
206
+ return normalized;
207
+ }
208
+
209
+ /**
210
+ * The digest stored for one of `userId`'s codes.
211
+ *
212
+ * Domain separated by the owning user, which is what stops one precomputed
213
+ * table from inverting every account's codes at once and stops two users who
214
+ * happen to be issued the same code from being visibly linked by a matching
215
+ * row. The user id is known at verification time -- the password has already
216
+ * been accepted by then -- so binding to it costs nothing.
217
+ *
218
+ * Both parts are LENGTH-PREFIXED into the message rather than merely
219
+ * concatenated, so no pair of (userId, code) values can be rearranged into
220
+ * the same byte string as another pair. Concatenation alone is a real
221
+ * ambiguity, not a theoretical one: without prefixes, ("ab", "cd") and
222
+ * ("abc", "d") hash identically.
223
+ */
224
+ @CaptureSpan()
225
+ public static hashCode(data: { code: string; userId: ObjectID }): string {
226
+ const message: string = [
227
+ HASH_SCHEME_VERSION,
228
+ data.userId.toString(),
229
+ TwoFactorBackupCode.normalizeCode(data.code),
230
+ ]
231
+ .map((part: string) => {
232
+ return `${part.length}:${part}`;
233
+ })
234
+ .join("");
235
+
236
+ return crypto
237
+ .createHmac("sha256", EncryptionSecret.toString())
238
+ .update(message)
239
+ .digest("hex");
240
+ }
241
+
242
+ /**
243
+ * Compare two digests without leaking, through timing, how many leading
244
+ * characters matched.
245
+ *
246
+ * The consume path compares digests in Postgres rather than here, so this is
247
+ * for callers that already hold both -- but it exists so that nobody writing
248
+ * one reaches for `===` and hands an attacker a way to recover a stored hash
249
+ * one character at a time.
250
+ *
251
+ * Lengths are compared first because `crypto.timingSafeEqual` THROWS on a
252
+ * length mismatch. Digests here are always 64 hex characters, so an early
253
+ * exit only happens on malformed input, where the length is not the secret.
254
+ */
255
+ @CaptureSpan()
256
+ public static isHashEqual(a: string, b: string): boolean {
257
+ if (!a || !b || a.length !== b.length) {
258
+ return false;
259
+ }
260
+
261
+ return crypto.timingSafeEqual(
262
+ Buffer.from(a, "utf8"),
263
+ Buffer.from(b, "utf8"),
264
+ );
265
+ }
266
+ }
@@ -96,6 +96,14 @@ const AUTHENTICATION_STATUS: UserAuthenticationStatus = {
96
96
  */
97
97
  twoFactorAuthStatus: TwoFactorAuthStatus.EnabledPendingSetup,
98
98
  verifiedTwoFactorAuthMethodCount: 0,
99
+
100
+ /*
101
+ * Reported alongside the method count, never folded into it -- backup codes
102
+ * are the way back in to an account whose factor is unreachable, not a
103
+ * factor. Zero here keeps this fixture the pure "mandated, nothing set up"
104
+ * case it exists to be.
105
+ */
106
+ unusedTwoFactorBackupCodeCount: 0,
99
107
  hasPendingPasswordResetLink: false,
100
108
  };
101
109
 
@@ -201,9 +209,13 @@ describe("GET /user/:userId/authentication-status", () => {
201
209
 
202
210
  test("sends exactly what the service reported", async () => {
203
211
  /*
204
- * The page renders these four booleans directly. A field dropped or
205
- * renamed on the way out reads as `undefined`, which the UI shows as "no
206
- * password" for an account that has one.
212
+ * The page renders these fields directly. One dropped or renamed on the
213
+ * way out reads as `undefined`, which the UI shows as "no password" for an
214
+ * account that has one.
215
+ *
216
+ * Asserted as an EXACT object rather than field by field, so a new field
217
+ * added to the DTO has to be added here too -- which is how this test has
218
+ * done its job twice now, most recently for the backup code count.
207
219
  */
208
220
  await callRoute({
209
221
  method: "GET",
@@ -217,6 +229,7 @@ describe("GET /user/:userId/authentication-status", () => {
217
229
  isTwoFactorAuthEnabled: true,
218
230
  twoFactorAuthStatus: TwoFactorAuthStatus.EnabledPendingSetup,
219
231
  verifiedTwoFactorAuthMethodCount: 0,
232
+ unusedTwoFactorBackupCodeCount: 0,
220
233
  hasPendingPasswordResetLink: false,
221
234
  });
222
235
  });
@@ -792,6 +792,7 @@ describe("GET /user/:userId/authentication-status two factor auth fields", () =>
792
792
  isTwoFactorAuthEnabled: true,
793
793
  twoFactorAuthStatus: TwoFactorAuthStatus.EnabledPendingSetup,
794
794
  verifiedTwoFactorAuthMethodCount: 0,
795
+ unusedTwoFactorBackupCodeCount: 0,
795
796
  hasPendingPasswordResetLink: false,
796
797
  };
797
798
 
@@ -826,6 +827,7 @@ describe("GET /user/:userId/authentication-status two factor auth fields", () =>
826
827
  isTwoFactorAuthEnabled: true,
827
828
  twoFactorAuthStatus: TwoFactorAuthStatus.EnabledPendingSetup,
828
829
  verifiedTwoFactorAuthMethodCount: 0,
830
+ unusedTwoFactorBackupCodeCount: 0,
829
831
  hasPendingPasswordResetLink: false,
830
832
  });
831
833
  });
@@ -863,6 +865,9 @@ describe("GET /user/:userId/authentication-status two factor auth fields", () =>
863
865
 
864
866
  expect(Object.keys(payload)).toContain("verifiedTwoFactorAuthMethodCount");
865
867
  expect(payload["verifiedTwoFactorAuthMethodCount"]).toBe(0);
868
+
869
+ expect(Object.keys(payload)).toContain("unusedTwoFactorBackupCodeCount");
870
+ expect(payload["unusedTwoFactorBackupCodeCount"]).toBe(0);
866
871
  });
867
872
 
868
873
  test("reports a configured account with its real method count", async () => {
@@ -871,14 +876,17 @@ describe("GET /user/:userId/authentication-status two factor auth fields", () =>
871
876
  * operator would reset a user who was perfectly fine and lock them out
872
877
  * themselves.
873
878
  */
874
- statusSpy.mockResolvedValue({
879
+ const configuredStatus: UserAuthenticationStatus = {
875
880
  hasPassword: false,
876
881
  isEmailVerified: true,
877
882
  isTwoFactorAuthEnabled: true,
878
883
  twoFactorAuthStatus: TwoFactorAuthStatus.EnabledConfigured,
879
884
  verifiedTwoFactorAuthMethodCount: 2,
885
+ unusedTwoFactorBackupCodeCount: 5,
880
886
  hasPendingPasswordResetLink: false,
881
- } as UserAuthenticationStatus);
887
+ };
888
+
889
+ statusSpy.mockResolvedValue(configuredStatus);
882
890
 
883
891
  await callRoute({
884
892
  method: "GET",
@@ -892,6 +900,14 @@ describe("GET /user/:userId/authentication-status two factor auth fields", () =>
892
900
  TwoFactorAuthStatus.EnabledConfigured,
893
901
  );
894
902
  expect(payload["verifiedTwoFactorAuthMethodCount"]).toBe(2);
903
+
904
+ /*
905
+ * Carried alongside the method count, never folded into it. This is the
906
+ * number that tells an operator whether the user can get back in without
907
+ * them -- so a reset that would sign the user out everywhere is only the
908
+ * right button when it is zero.
909
+ */
910
+ expect(payload["unusedTwoFactorBackupCodeCount"]).toBe(5);
895
911
  });
896
912
 
897
913
  test("sets the no-cache headers before the payload goes out", async () => {