@kabmo-labs/kabmo-auth 0.0.0-stage → 1.0.0
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.
- package/package.json +22 -4
- package/src/password.ts +72 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kabmo-labs/kabmo-auth",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Generated UI password and its environment names.",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/password.ts"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"src"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/kabmo-labs/kabmo.git",
|
|
16
|
+
"directory": "packages/kabmo-auth"
|
|
17
|
+
},
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"bun": ">=1.4.0"
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/password.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The generated UI password, shared by the CLI (which mints one for
|
|
8
|
+
* `--ui-password` with no value) and anything else that needs to offer one.
|
|
9
|
+
*
|
|
10
|
+
* The alphabet deliberately omits the characters a person confuses when reading
|
|
11
|
+
* a password off one screen and typing it into another: `I`/`O` (against `1`/`0`)
|
|
12
|
+
* and `i`/`l`/`o` (against each other). What is left is 55 unambiguous
|
|
13
|
+
* characters, which is what makes the value safe to transcribe from a terminal
|
|
14
|
+
* onto a phone.
|
|
15
|
+
*
|
|
16
|
+
* `crypto.getRandomValues` rather than a `node:crypto` import: it is a global in
|
|
17
|
+
* both Bun and the browser, so this module stays importable from either side.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** Every character a generated password may contain (55 of them). */
|
|
21
|
+
export const PASSWORD_ALPHABET = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The environment variable that configures the UI password.
|
|
25
|
+
*
|
|
26
|
+
* Lives here, in the dependency-free module, rather than beside the code that
|
|
27
|
+
* applies it: the guard needs to name it for the boot banner, and the guard is
|
|
28
|
+
* imported by that same module — naming it here is what keeps the two from
|
|
29
|
+
* needing a circular import.
|
|
30
|
+
*/
|
|
31
|
+
export const UI_PASSWORD_ENV = 'KABMO_UI_PASSWORD';
|
|
32
|
+
|
|
33
|
+
/** The pre-rename spelling, still read so an exported password keeps working. */
|
|
34
|
+
export const LEGACY_UI_PASSWORD_ENV = 'OMPCHAMBER_UI_PASSWORD';
|
|
35
|
+
|
|
36
|
+
/** Length of a generated password — 16 characters is ~92 bits over this alphabet. */
|
|
37
|
+
export const GENERATED_PASSWORD_LENGTH = 16;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Random bytes with the modulo bias removed.
|
|
41
|
+
*
|
|
42
|
+
* `bytes[i] % alphabet.length` alone would over-represent the first
|
|
43
|
+
* `256 % 55 = 36` characters, since 256 is not a multiple of 55. Bytes at or
|
|
44
|
+
* above the largest multiple of the alphabet size are discarded and redrawn
|
|
45
|
+
* instead, which makes every character equally likely.
|
|
46
|
+
*/
|
|
47
|
+
function unbiasedBytes(length: number, alphabetLength: number): Uint8Array {
|
|
48
|
+
const limit = 256 - (256 % alphabetLength);
|
|
49
|
+
const out = new Uint8Array(length);
|
|
50
|
+
const buffer = new Uint8Array(length * 2);
|
|
51
|
+
let filled = 0;
|
|
52
|
+
|
|
53
|
+
while (filled < length) {
|
|
54
|
+
crypto.getRandomValues(buffer);
|
|
55
|
+
for (const byte of buffer) {
|
|
56
|
+
if (byte >= limit) continue;
|
|
57
|
+
out[filled] = byte % alphabetLength;
|
|
58
|
+
filled += 1;
|
|
59
|
+
if (filled === length) break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A fresh password from `PASSWORD_ALPHABET`, `length` characters long. */
|
|
66
|
+
export function generatePassword(length: number = GENERATED_PASSWORD_LENGTH): string {
|
|
67
|
+
const size = Number.isFinite(length) && length > 0 ? Math.floor(length) : GENERATED_PASSWORD_LENGTH;
|
|
68
|
+
const indices = unbiasedBytes(size, PASSWORD_ALPHABET.length);
|
|
69
|
+
let password = '';
|
|
70
|
+
for (const index of indices) password += PASSWORD_ALPHABET[index];
|
|
71
|
+
return password;
|
|
72
|
+
}
|
package/README.md
DELETED