badmfck-api-server 5.0.10 → 5.0.11

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.
@@ -42,6 +42,7 @@ export declare class MonitorAuth {
42
42
  private static base32Decode;
43
43
  private static hotp;
44
44
  static verifyTotp(login: string, secretBase32: string, code: string): string | null;
45
+ private static base32Encode;
45
46
  static generateTotpSecret(login: string, issuer?: string): {
46
47
  secret: string;
47
48
  uri: string;
@@ -191,12 +191,23 @@ class MonitorAuth {
191
191
  }
192
192
  return "TOTP code is invalid";
193
193
  }
194
- static generateTotpSecret(login, issuer = "Monitor") {
194
+ static base32Encode(buf) {
195
195
  const A = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
196
- const raw = crypto_1.default.randomBytes(20);
197
- let secret = "";
198
- for (let i = 0; i < raw.length; i++)
199
- secret += A[raw[i] % 32];
196
+ let bits = 0, value = 0, out = "";
197
+ for (const b of buf) {
198
+ value = (value << 8) | b;
199
+ bits += 8;
200
+ while (bits >= 5) {
201
+ out += A[(value >>> (bits - 5)) & 31];
202
+ bits -= 5;
203
+ }
204
+ }
205
+ if (bits > 0)
206
+ out += A[(value << (5 - bits)) & 31];
207
+ return out;
208
+ }
209
+ static generateTotpSecret(login, issuer = "Monitor") {
210
+ const secret = MonitorAuth.base32Encode(crypto_1.default.randomBytes(20));
200
211
  const safeIssuer = String(issuer || "").replace(/[:\u0000-\u001f]/g, " ").trim() || "Monitor";
201
212
  const label = encodeURIComponent(`${safeIssuer}:${login}`);
202
213
  const uri = `otpauth://totp/${label}?secret=${secret}&issuer=${encodeURIComponent(safeIssuer)}&algorithm=SHA1&digits=6&period=30`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "5.0.10",
3
+ "version": "5.0.11",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",