@structured-id/proto 0.9.0 → 0.11.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/dist/totp.js ADDED
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Presentation of a TOTP enrollment. The server sends the raw shared secret
3
+ * and its parameters (`TotpEnrollmentChallenge`); an authenticator app takes
4
+ * the Base32 secret typed in, or the `otpauth://totp` URI behind a QR code.
5
+ */
6
+ import { TotpAlgorithm } from "./generated/sid/v1/authn/auth.js";
7
+ const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
8
+ /** `bytes` in Base32 (RFC 4648 §6), without padding. */
9
+ export function base32(bytes) {
10
+ let out = "";
11
+ let buffer = 0;
12
+ let bits = 0;
13
+ for (const byte of bytes) {
14
+ buffer = ((buffer << 8) | byte) & 0xffff;
15
+ bits += 8;
16
+ while (bits >= 5) {
17
+ bits -= 5;
18
+ out += ALPHABET[(buffer >> bits) & 0x1f];
19
+ }
20
+ }
21
+ if (bits > 0) {
22
+ out += ALPHABET[(buffer << (5 - bits)) & 0x1f];
23
+ }
24
+ return out;
25
+ }
26
+ /**
27
+ * `text` percent-encoded as a URI component: only RFC 3986 §2.3 unreserved
28
+ * characters stay, as the Rust client encodes it.
29
+ */
30
+ function component(text) {
31
+ return encodeURIComponent(text).replace(/[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
32
+ }
33
+ const ALGORITHMS = {
34
+ [TotpAlgorithm.SHA1]: "SHA1",
35
+ [TotpAlgorithm.SHA256]: "SHA256",
36
+ [TotpAlgorithm.SHA512]: "SHA512",
37
+ };
38
+ /**
39
+ * The `otpauth://totp/<issuer>:<account>` URI of `challenge` (Key URI
40
+ * Format), with issuer and account percent-encoded so neither can split the
41
+ * label or add a parameter. Throws when the challenge lacks the secret or a
42
+ * parameter the app needs (a guess would enroll codes the server refuses),
43
+ * or when issuer or account holds the colon the label separates them with.
44
+ */
45
+ export function otpauthUri(challenge) {
46
+ const algorithm = ALGORITHMS[challenge.algorithm];
47
+ if (algorithm === undefined ||
48
+ challenge.digits === 0 ||
49
+ challenge.periodSeconds === 0 ||
50
+ challenge.sharedSecret.length === 0) {
51
+ throw new Error("the TOTP enrollment names no secret, algorithm, digits or period");
52
+ }
53
+ // Key URI Format, Label: the colon separates issuer and account, literal
54
+ // or %3A, so neither may contain one.
55
+ if (challenge.issuer.includes(":") || challenge.accountName.includes(":")) {
56
+ throw new Error("a TOTP issuer or account cannot contain a colon");
57
+ }
58
+ const issuer = component(challenge.issuer);
59
+ const label = `${issuer}:${component(challenge.accountName)}`;
60
+ return (`otpauth://totp/${label}?secret=${base32(challenge.sharedSecret)}` +
61
+ `&issuer=${issuer}&algorithm=${algorithm}` +
62
+ `&digits=${challenge.digits}&period=${challenge.periodSeconds}`);
63
+ }
64
+ //# sourceMappingURL=totp.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"totp.js","sourceRoot":"","sources":["../src/totp.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,aAAa,EAAgC,MAAM,kCAAkC,CAAC;AAE/F,MAAM,QAAQ,GAAG,kCAAkC,CAAC;AAEpD,wDAAwD;AACxD,MAAM,UAAU,MAAM,CAAC,KAAiB;IACtC,IAAI,GAAG,GAAG,EAAE,CAAC;IACb,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,GAAG,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,MAAM,CAAC;QACzC,IAAI,IAAI,CAAC,CAAC;QACV,OAAO,IAAI,IAAI,CAAC,EAAE,CAAC;YACjB,IAAI,IAAI,CAAC,CAAC;YACV,GAAG,IAAI,QAAQ,CAAC,CAAC,MAAM,IAAI,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;QAC3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AAM,MAAM,GAAG,SAAS,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;IAC3C,MAAM,KAAK,GAAG,GAAG,MAAM,IAAI,SAAS,CAAC,SAAS,CAAC,WAAW,CAAC,EAAE,CAAC;IAC9D,OAAO,CACL,kBAAkB,KAAK,WAAW,MAAM,CAAC,SAAS,CAAC,YAAY,CAAC,EAAE;QAClE,WAAW,MAAM,cAAc,SAAS,EAAE;QAC1C,WAAW,SAAS,CAAC,MAAM,WAAW,SAAS,CAAC,aAAa,EAAE,CAChE,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Presentation of a TOTP enrollment. The server sends the raw shared secret\n * and its parameters (`TotpEnrollmentChallenge`); an authenticator app takes\n * the Base32 secret typed in, or the `otpauth://totp` URI behind a QR code.\n */\nimport { TotpAlgorithm, type TotpEnrollmentChallenge } from \"./generated/sid/v1/authn/auth.js\";\n\nconst ALPHABET = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567\";\n\n/** `bytes` in Base32 (RFC 4648 §6), without padding. */\nexport function base32(bytes: Uint8Array): string {\n let out = \"\";\n let buffer = 0;\n let bits = 0;\n for (const byte of bytes) {\n buffer = ((buffer << 8) | byte) & 0xffff;\n bits += 8;\n while (bits >= 5) {\n bits -= 5;\n out += ALPHABET[(buffer >> bits) & 0x1f];\n }\n }\n if (bits > 0) {\n out += ALPHABET[(buffer << (5 - bits)) & 0x1f];\n }\n return out;\n}\n\n/**\n * `text` percent-encoded as a URI component: only RFC 3986 §2.3 unreserved\n * characters stay, as the Rust client encodes it.\n */\nfunction component(text: string): string {\n return encodeURIComponent(text).replace(\n /[!'()*]/g,\n (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`,\n );\n}\n\nconst ALGORITHMS: Partial<Record<TotpAlgorithm, string>> = {\n [TotpAlgorithm.SHA1]: \"SHA1\",\n [TotpAlgorithm.SHA256]: \"SHA256\",\n [TotpAlgorithm.SHA512]: \"SHA512\",\n};\n\n/**\n * The `otpauth://totp/<issuer>:<account>` URI of `challenge` (Key URI\n * Format), with issuer and account percent-encoded so neither can split the\n * label or add a parameter. Throws when the challenge lacks the secret or a\n * parameter the app needs (a guess would enroll codes the server refuses),\n * or when issuer or account holds the colon the label separates them with.\n */\nexport function otpauthUri(challenge: TotpEnrollmentChallenge): string {\n const algorithm = ALGORITHMS[challenge.algorithm];\n if (\n algorithm === undefined ||\n challenge.digits === 0 ||\n challenge.periodSeconds === 0 ||\n challenge.sharedSecret.length === 0\n ) {\n throw new Error(\"the TOTP enrollment names no secret, algorithm, digits or period\");\n }\n // Key URI Format, Label: the colon separates issuer and account, literal\n // or %3A, so neither may contain one.\n if (challenge.issuer.includes(\":\") || challenge.accountName.includes(\":\")) {\n throw new Error(\"a TOTP issuer or account cannot contain a colon\");\n }\n const issuer = component(challenge.issuer);\n const label = `${issuer}:${component(challenge.accountName)}`;\n return (\n `otpauth://totp/${label}?secret=${base32(challenge.sharedSecret)}` +\n `&issuer=${issuer}&algorithm=${algorithm}` +\n `&digits=${challenge.digits}&period=${challenge.periodSeconds}`\n );\n}\n"]}
package/package.json CHANGED
@@ -1,9 +1,13 @@
1
1
  {
2
2
  "name": "@structured-id/proto",
3
- "version": "0.9.0",
3
+ "version": "0.11.0",
4
4
  "type": "module",
5
5
  "description": "StructuredID public sid.v1 API for TypeScript: generated messages and clients for gRPC and gRPC-web.",
6
6
  "exports": {
7
+ "./totp": {
8
+ "types": "./dist/totp.d.ts",
9
+ "default": "./dist/totp.js"
10
+ },
7
11
  "./sid/v1/*": {
8
12
  "types": "./dist/generated/sid/v1/*.d.ts",
9
13
  "default": "./dist/generated/sid/v1/*.js"
@@ -53,7 +57,7 @@
53
57
  "dependencies": {
54
58
  "@protobuf-ts/runtime": "^2.11.1",
55
59
  "@protobuf-ts/runtime-rpc": "^2.11.1",
56
- "@structured-id/ids": "^0.1.0"
60
+ "@structured-id/ids": "^0.3.0"
57
61
  },
58
62
  "devDependencies": {
59
63
  "@bufbuild/buf": "^1.75.0",