@tma.js/init-data-node 0.0.13 → 0.0.15
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/dts/validation.d.ts +1 -1
- package/dist/{index.js → index.mjs} +1 -1
- package/dist/index.mjs.map +1 -0
- package/package.json +10 -9
- package/dist/index.js.map +0 -1
package/dist/dts/validation.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/validation.ts"],"sourcesContent":["import { createHmac } from 'crypto';\nimport { URLSearchParams } from 'url';\n\nexport interface ValidateOptions {\n /**\n * Time in seconds which states, how long from creation time is init data\n * considered valid.\n *\n * In other words, in case, when authDate + expiresIn is before current\n * time, init data recognized as expired.\n *\n * In case, this value is equal to 0, function does not check init data\n * expiration.\n * @default 86400 (1 day)\n */\n expiresIn?: number;\n}\n\n/**\n * Validates passed init data presented as search params converted to string,\n * or its object presentation.\n *\n * @param sp - search parameters.\n * @param token - Telegram bot secret token.\n * @param options - validation options.\n * @see toSearchParams\n * @throws {TypeError} \"hash\" should be string.\n * @throws {Error} \"hash\" is empty or not found.\n * @throws {TypeError} \"auth_date\" should be string.\n * @throws {TypeError} \"auth_date\" does not represent integer.\n * @throws {Error} \"auth_date\" is empty or not found.\n * @throws {Error} Init data expired.\n * @throws {Error} Sign invalid.\n */\nexport function validate(\n sp: string | URLSearchParams,\n token: string,\n options: ValidateOptions = {},\n): void {\n const searchParams = typeof sp === 'string' ? new URLSearchParams(sp) : sp;\n\n // Init data creation time.\n let authDate = new Date(0);\n\n // Init data sign.\n let hash = '';\n\n // All search params pairs presented as `k=v`.\n const pairs: string[] = [];\n\n // Iterate over all key-value pairs of parsed parameters and find required\n // parameters.\n searchParams.forEach((value, key) => {\n if (key === 'hash') {\n hash = value;\n return;\n }\n\n if (key === 'auth_date') {\n const authDateNum = parseInt(value, 10);\n\n if (Number.isNaN(authDateNum)) {\n throw new TypeError('\"auth_date\" should present integer');\n }\n authDate = new Date(authDateNum * 1000);\n }\n\n // Append new pair.\n pairs.push(`${key}=${value}`);\n });\n\n // Hash and auth date always required.\n if (hash.length === 0) {\n throw new Error('\"hash\" is empty or not found');\n }\n\n if (authDate.getTime() === 0) {\n throw new Error('\"auth_date\" is empty or not found');\n }\n\n // In case, expiration time passed, we do additional parameters check.\n const { expiresIn = 86400 } = options;\n\n if (expiresIn > 0) {\n // Check if init data expired.\n if (authDate.getTime() + expiresIn * 1000 < new Date().getTime()) {\n throw new Error('Init data expired');\n }\n }\n\n // According to docs, we sort all the pairs in alphabetical order.\n pairs.sort();\n\n // Compute sign.\n const computedHash = createHmac(\n 'sha256',\n createHmac('sha256', 'WebAppData').update(token).digest(),\n )\n .update(pairs.join('\\n'))\n .digest()\n .toString('hex');\n\n // In case, our sign is not equal to found one, we should throw an error.\n if (computedHash !== hash) {\n throw new Error('Signature is invalid');\n }\n}\n"],"names":["validate","sp","token","options","searchParams","URLSearchParams","authDate","hash","pairs","value","key","authDateNum","expiresIn","createHmac"],"mappings":";;;AAkCO,SAASA,EACdC,GACAC,GACAC,IAA2B,CAAA,GACrB;AACN,QAAMC,IAAe,OAAOH,KAAO,WAAW,IAAII,EAAgBJ,CAAE,IAAIA;AAGpE,MAAAK,IAAe,oBAAA,KAAK,CAAC,GAGrBC,IAAO;AAGX,QAAMC,IAAkB,CAAA;AAwBpB,MApBSJ,EAAA,QAAQ,CAACK,GAAOC,MAAQ;AACnC,QAAIA,MAAQ,QAAQ;AACX,MAAAH,IAAAE;AACP;AAAA,IACF;AAEA,QAAIC,MAAQ,aAAa;AACjB,YAAAC,IAAc,SAASF,GAAO,EAAE;AAElC,UAAA,OAAO,MAAME,CAAW;AACpB,cAAA,IAAI,UAAU,oCAAoC;AAE/C,MAAAL,IAAA,IAAI,KAAKK,IAAc,GAAI;AAAA,IACxC;AAGA,IAAAH,EAAM,KAAK,GAAGE,CAAG,IAAID,CAAK,EAAE;AAAA,EAAA,CAC7B,GAGGF,EAAK,WAAW;AACZ,UAAA,IAAI,MAAM,8BAA8B;AAG5C,MAAAD,EAAS,QAAQ,MAAM;AACnB,UAAA,IAAI,MAAM,mCAAmC;AAI/C,QAAA,EAAE,WAAAM,IAAY,MAAU,IAAAT;AAE9B,MAAIS,IAAY,KAEVN,EAAS,YAAYM,IAAY,OAAW,oBAAA,QAAO;AAC/C,UAAA,IAAI,MAAM,mBAAmB;AAiBvC,MAZAJ,EAAM,KAAK,GAGUK;AAAA,IACnB;AAAA,IACAA,EAAW,UAAU,YAAY,EAAE,OAAOX,CAAK,EAAE,OAAO;AAAA,EAAA,EAEvD,OAAOM,EAAM,KAAK;AAAA,CAAI,CAAC,EACvB,OACA,EAAA,SAAS,KAAK,MAGID;AACb,UAAA,IAAI,MAAM,sBAAsB;AAE1C;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tma.js/init-data-node",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.15",
|
|
4
4
|
"description": "TypeScript Node library to operate with Telegram init data.",
|
|
5
5
|
"author": "Vladislav Kibenko <wolfram.deus@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/Telegram-Mini-Apps/tma.js#readme",
|
|
@@ -26,32 +26,33 @@
|
|
|
26
26
|
"src"
|
|
27
27
|
],
|
|
28
28
|
"main": "./dist/index.cjs",
|
|
29
|
-
"module": "./dist/index.
|
|
29
|
+
"module": "./dist/index.mjs",
|
|
30
30
|
"types": "./dist/dts/index.d.ts",
|
|
31
31
|
"exports": {
|
|
32
32
|
".": {
|
|
33
33
|
"types": "./dist/dts/index.d.ts",
|
|
34
|
-
"import": "./dist/index.
|
|
34
|
+
"import": "./dist/index.mjs",
|
|
35
35
|
"require": "./dist/index.cjs",
|
|
36
36
|
"default": "./dist/index.cjs"
|
|
37
37
|
}
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@tma.js/init-data": "0.2.
|
|
40
|
+
"@tma.js/init-data": "0.2.17"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@types/node": "^16.0.0",
|
|
44
44
|
"tsconfig": "0.0.2",
|
|
45
45
|
"eslint-config-custom": "0.1.0",
|
|
46
|
-
"
|
|
47
|
-
"build-utils": "0.0.0"
|
|
46
|
+
"build-utils": "0.0.1"
|
|
48
47
|
},
|
|
49
48
|
"publishConfig": {
|
|
50
49
|
"access": "public"
|
|
51
50
|
},
|
|
52
51
|
"scripts": {
|
|
53
|
-
"test": "
|
|
54
|
-
"lint": "eslint -c .eslintrc.cjs src/**/*
|
|
55
|
-
"
|
|
52
|
+
"test": "vitest",
|
|
53
|
+
"lint": "eslint -c .eslintrc.cjs src/**/* tests/**/*",
|
|
54
|
+
"typecheck": "tsc --noEmit -p tsconfig.build.json",
|
|
55
|
+
"build": "vite build",
|
|
56
|
+
"rollup": "pnpm run typecheck && pnpm run build"
|
|
56
57
|
}
|
|
57
58
|
}
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/validation.ts"],"sourcesContent":["import { createHmac } from 'crypto';\nimport { URLSearchParams } from 'url';\n\nexport interface ValidateOptions {\n /**\n * Time in seconds which states, how long from creation time is init data\n * considered valid.\n *\n * In other words, in case, when authDate + expiresIn is before current\n * time, init data recognized as expired.\n *\n * In case, this value is equal to 0, function does not check init data\n * expiration.\n * @default 86400 (1 day)\n */\n expiresIn?: number;\n}\n\n/**\n * Validates passed init data presented as search params converted to string,\n * or its object presentation.\n *\n * @param sp - search parameters.\n * @param token - Telegram bot secret token.\n * @param options - validation options.\n * @see toSearchParams\n * @throws {TypeError} \"hash\" should be string.\n * @throws {Error} \"hash\" is empty or not found.\n * @throws {TypeError} \"auth_date\" should be string.\n * @throws {TypeError} \"auth_date\" does not represent integer.\n * @throws {Error} \"auth_date\" is empty or not found.\n * @throws {Error} Init data expired.\n * @throws {Error} Sign invalid.\n */\nexport function validate(\n sp: string | URLSearchParams,\n token: string,\n options: ValidateOptions = {},\n): void {\n const searchParams = typeof sp === 'string' ? new URLSearchParams(sp) : sp;\n\n // Init data creation time.\n let authDate = new Date(0);\n\n // Init data sign.\n let hash = '';\n\n // All search params pairs presented as `k=v`.\n const pairs: string[] = [];\n\n // Iterate over all key-value pairs of parsed parameters and find required\n // parameters.\n searchParams.forEach((value, key) => {\n if (key === 'hash') {\n hash = value;\n return;\n }\n\n if (key === 'auth_date') {\n const authDateNum = parseInt(value, 10);\n\n if (Number.isNaN(authDateNum)) {\n throw new TypeError('\"auth_date\" should present integer');\n }\n authDate = new Date(authDateNum * 1000);\n }\n\n // Append new pair.\n pairs.push(`${key}=${value}`);\n });\n\n // Hash and auth date always required.\n if (hash.length === 0) {\n throw new Error('\"hash\" is empty or not found');\n }\n\n if (authDate.getTime() === 0) {\n throw new Error('\"auth_date\" is empty or not found');\n }\n\n // In case, expiration time passed, we do additional parameters check.\n const { expiresIn = 86400 } = options;\n\n if (expiresIn > 0) {\n // Check if init data expired.\n if (authDate.getTime() + expiresIn * 1000 < new Date().getTime()) {\n throw new Error('Init data expired');\n }\n }\n\n // According to docs, we sort all the pairs in alphabetical order.\n pairs.sort();\n\n // Compute sign.\n const computedHash = createHmac(\n 'sha256',\n createHmac('sha256', 'WebAppData').update(token).digest(),\n )\n .update(pairs.join('\\n'))\n .digest()\n .toString('hex');\n\n // In case, our sign is not equal to found one, we should throw an error.\n if (computedHash !== hash) {\n throw new Error('Signature is invalid');\n }\n}\n"],"names":["validate","sp","token","options","searchParams","URLSearchParams","authDate","hash","pairs","value","key","authDateNum","expiresIn","createHmac"],"mappings":";;;AAkCO,SAASA,EACdC,GACAC,GACAC,IAA2B,CAAA,GACrB;AACN,QAAMC,IAAe,OAAOH,KAAO,WAAW,IAAII,EAAgBJ,CAAE,IAAIA;AAGpE,MAAAK,IAAe,oBAAA,KAAK,CAAC,GAGrBC,IAAO;AAGX,QAAMC,IAAkB,CAAA;AAwBpB,MApBSJ,EAAA,QAAQ,CAACK,GAAOC,MAAQ;AACnC,QAAIA,MAAQ,QAAQ;AACX,MAAAH,IAAAE;AACP;AAAA,IACF;AAEA,QAAIC,MAAQ,aAAa;AACjB,YAAAC,IAAc,SAASF,GAAO,EAAE;AAElC,UAAA,OAAO,MAAME,CAAW;AACpB,cAAA,IAAI,UAAU,oCAAoC;AAE/C,MAAAL,IAAA,IAAI,KAAKK,IAAc,GAAI;AAAA,IACxC;AAGA,IAAAH,EAAM,KAAK,GAAGE,CAAG,IAAID,CAAK,EAAE;AAAA,EAAA,CAC7B,GAGGF,EAAK,WAAW;AACZ,UAAA,IAAI,MAAM,8BAA8B;AAG5C,MAAAD,EAAS,QAAQ,MAAM;AACnB,UAAA,IAAI,MAAM,mCAAmC;AAI/C,QAAA,EAAE,WAAAM,IAAY,MAAU,IAAAT;AAE9B,MAAIS,IAAY,KAEVN,EAAS,YAAYM,IAAY,OAAW,oBAAA,QAAO;AAC/C,UAAA,IAAI,MAAM,mBAAmB;AAiBvC,MAZAJ,EAAM,KAAK,GAGUK;AAAA,IACnB;AAAA,IACAA,EAAW,UAAU,YAAY,EAAE,OAAOX,CAAK,EAAE,OAAO;AAAA,EAAA,EAEvD,OAAOM,EAAM,KAAK;AAAA,CAAI,CAAC,EACvB,OACA,EAAA,SAAS,KAAK,MAGID;AACb,UAAA,IAAI,MAAM,sBAAsB;AAE1C;"}
|