@seayoo-io/combo-sdk-node 1.4.5 → 1.5.1
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/README.md +6 -11
- package/dist/const.d.ts +1 -7
- package/dist/index.cjs +72 -36
- package/dist/index.js +72 -36
- package/dist/utils/gcm.d.ts +9 -0
- package/dist/verify/id.d.ts +4 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -108,10 +108,6 @@ interface OrderMetaData {
|
|
|
108
108
|
role_name?: string
|
|
109
109
|
/** 游戏角色的等级 */
|
|
110
110
|
role_level?: number
|
|
111
|
-
/** 微信小游戏的 App ID, 微信小游戏的 iOS 支付场景必须传入 */
|
|
112
|
-
weixin_appid?: string
|
|
113
|
-
/** 微信小游戏的玩家 OpenID, 微信小游戏的 iOS 支付场景必须传入 */
|
|
114
|
-
weixin_openid?: string
|
|
115
111
|
}
|
|
116
112
|
|
|
117
113
|
interface CreateOrderResponse {
|
|
@@ -298,9 +294,8 @@ const identityPayload = verifier.verifyIdentityToken(token)
|
|
|
298
294
|
if(identityPayload instanceof Error) {
|
|
299
295
|
console.error(result.message)
|
|
300
296
|
} else {
|
|
301
|
-
//
|
|
302
|
-
if(identityPayload.idp === IdP.
|
|
303
|
-
// 微信登录会提供 weixin_unionid
|
|
297
|
+
// WebGL 平台,包括微信、抖音等小游戏,以及 HTML5 网页游戏
|
|
298
|
+
if(identityPayload.idp === IdP.WebGL) {
|
|
304
299
|
// do something
|
|
305
300
|
}
|
|
306
301
|
// 游客登录判断
|
|
@@ -331,11 +326,11 @@ interface IdentityPayload {
|
|
|
331
326
|
/** external_name 是用户在外部 IdP 中的名称,通常是用户的昵称 */
|
|
332
327
|
external_name: string
|
|
333
328
|
/**
|
|
334
|
-
*
|
|
335
|
-
*
|
|
336
|
-
*
|
|
329
|
+
* weixin_session_key 是用户在微信小游戏登录时,从微信服务端获得的会话密钥 session_key。
|
|
330
|
+
* 该字段在 Identity Token 中以 AES-256-GCM 加密存储,SDK 会自动解密。
|
|
331
|
+
* weixin_session_key 只在 IdP 为 MinigameWeixin 时才会有值。
|
|
337
332
|
*/
|
|
338
|
-
|
|
333
|
+
weixin_session_key: string
|
|
339
334
|
/**
|
|
340
335
|
* device_id 是用户在登录时使用的设备的唯一 ID。
|
|
341
336
|
*/
|
package/dist/const.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** SDK 名称 */
|
|
2
2
|
export declare const SDKName = "combo-sdk-node";
|
|
3
3
|
/** SDK 版本,在编译前会被动态替换 */
|
|
4
|
-
export declare const SDKVersion = "1.
|
|
4
|
+
export declare const SDKVersion = "1.5.1";
|
|
5
5
|
/**
|
|
6
6
|
* 世游支持的服务端点列表
|
|
7
7
|
*/
|
|
@@ -21,12 +21,6 @@ export declare enum Platform {
|
|
|
21
21
|
Windows = "windows",
|
|
22
22
|
/** macOS 桌面平台 */
|
|
23
23
|
macOS = "macos",
|
|
24
|
-
/**
|
|
25
|
-
* 微信小游戏
|
|
26
|
-
*
|
|
27
|
-
* @deprecated 已被 Platform.WebGL 取代
|
|
28
|
-
*/
|
|
29
|
-
Weixin = "weixin",
|
|
30
24
|
/** WebGL 平台,包括微信、抖音等小游戏,以及 HTML5 网页游戏 */
|
|
31
25
|
WebGL = "webgl",
|
|
32
26
|
/** 华为的 HarmonyOS NEXT 鸿蒙应用开发平台 */
|
package/dist/index.cjs
CHANGED
|
@@ -12,7 +12,7 @@ var https__default = /*#__PURE__*/_interopDefaultLegacy(https);
|
|
|
12
12
|
/** SDK 名称 */
|
|
13
13
|
const SDKName = "combo-sdk-node";
|
|
14
14
|
/** SDK 版本,在编译前会被动态替换 */
|
|
15
|
-
const SDKVersion = "1.
|
|
15
|
+
const SDKVersion = "1.5.1";
|
|
16
16
|
/**
|
|
17
17
|
* 世游支持的服务端点列表
|
|
18
18
|
*/
|
|
@@ -34,12 +34,6 @@ exports.Platform = void 0;
|
|
|
34
34
|
Platform["Windows"] = "windows";
|
|
35
35
|
/** macOS 桌面平台 */
|
|
36
36
|
Platform["macOS"] = "macos";
|
|
37
|
-
/**
|
|
38
|
-
* 微信小游戏
|
|
39
|
-
*
|
|
40
|
-
* @deprecated 已被 Platform.WebGL 取代
|
|
41
|
-
*/
|
|
42
|
-
Platform["Weixin"] = "weixin";
|
|
43
37
|
/** WebGL 平台,包括微信、抖音等小游戏,以及 HTML5 网页游戏 */
|
|
44
38
|
Platform["WebGL"] = "webgl";
|
|
45
39
|
/** 华为的 HarmonyOS NEXT 鸿蒙应用开发平台 */
|
|
@@ -104,7 +98,7 @@ exports.IdP = void 0;
|
|
|
104
98
|
IdP["Huya"] = "huya";
|
|
105
99
|
})(exports.IdP || (exports.IdP = {}));
|
|
106
100
|
/** GM 错误类型枚举 */
|
|
107
|
-
|
|
101
|
+
exports.GMError = void 0;
|
|
108
102
|
(function (GMError) {
|
|
109
103
|
/** 请求中的 HTTP method 不正确,没有按照预期使用 POST。*/
|
|
110
104
|
GMError["InvalidHttpMethod"] = "invalid_http_method";
|
|
@@ -134,7 +128,7 @@ var GMError;
|
|
|
134
128
|
GMError["TimeoutError"] = "timeout_error";
|
|
135
129
|
/** 处理 GM 命令时内部出错。可作为兜底的通用错误类型。*/
|
|
136
130
|
GMError["InternalError"] = "internal_error";
|
|
137
|
-
})(GMError || (GMError = {}));
|
|
131
|
+
})(exports.GMError || (exports.GMError = {}));
|
|
138
132
|
|
|
139
133
|
function getUserAgent(game) {
|
|
140
134
|
return `${SDKName}/${SDKVersion} game/${game} node/${process.version} os/${os.platform()} arch/${os.arch()}`;
|
|
@@ -1174,6 +1168,47 @@ function isAdJwtPayload(payload) {
|
|
|
1174
1168
|
return isObject(payload) && "sub" in payload && "iss" in payload && "scope" in payload && "placement_id" in payload && "impression_id" in payload;
|
|
1175
1169
|
}
|
|
1176
1170
|
|
|
1171
|
+
/**
|
|
1172
|
+
* decryptAESGCM 使用 AES-256-GCM 解密 base64 编码的密文。
|
|
1173
|
+
* 密文格式为 base64(nonce || ciphertext || authTag),其中 nonce 为 12 字节,authTag 为 16 字节。
|
|
1174
|
+
*/
|
|
1175
|
+
function decryptAESGCM(sk, encoded) {
|
|
1176
|
+
let data;
|
|
1177
|
+
try {
|
|
1178
|
+
data = Buffer.from(encoded, "base64");
|
|
1179
|
+
} catch (err) {
|
|
1180
|
+
throw new Error(`base64 decode error: ${err}`);
|
|
1181
|
+
}
|
|
1182
|
+
const key = deriveAESKey(sk);
|
|
1183
|
+
const nonceSize = 12;
|
|
1184
|
+
const authTagSize = 16;
|
|
1185
|
+
if (data.length < nonceSize + authTagSize) {
|
|
1186
|
+
throw new Error("ciphertext too short");
|
|
1187
|
+
}
|
|
1188
|
+
const nonce = data.slice(0, nonceSize);
|
|
1189
|
+
// 密文格式: nonce (12) || ciphertext (variable) || authTag (16)
|
|
1190
|
+
const authTag = data.slice(data.length - authTagSize);
|
|
1191
|
+
const ciphertext = data.slice(nonceSize, data.length - authTagSize);
|
|
1192
|
+
const decipher = crypto.createDecipheriv("aes-256-gcm", key, nonce);
|
|
1193
|
+
decipher.setAuthTag(authTag);
|
|
1194
|
+
// Node.js 12 兼容:使用 update 处理所有数据,避免 final() 的问题
|
|
1195
|
+
let plaintext;
|
|
1196
|
+
try {
|
|
1197
|
+
plaintext = decipher.update(ciphertext);
|
|
1198
|
+
// Node.js 12 中 GCM 模式下 final() 通常返回空 Buffer,但会验证 authTag
|
|
1199
|
+
const finalBuffer = decipher.final();
|
|
1200
|
+
if (finalBuffer.length > 0) {
|
|
1201
|
+
plaintext = Buffer.concat([plaintext, finalBuffer]);
|
|
1202
|
+
}
|
|
1203
|
+
} catch (err) {
|
|
1204
|
+
throw new Error(`gcm decrypt error: ${err}`);
|
|
1205
|
+
}
|
|
1206
|
+
return plaintext.toString("utf8");
|
|
1207
|
+
}
|
|
1208
|
+
function deriveAESKey(sk) {
|
|
1209
|
+
return crypto.createHash("sha256").update(sk).digest();
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1177
1212
|
const IdentityTokenScope = "auth";
|
|
1178
1213
|
const AdTokenScope = "ads";
|
|
1179
1214
|
/**
|
|
@@ -1210,12 +1245,13 @@ class TokenVerifier {
|
|
|
1210
1245
|
if (payload.scope !== IdentityTokenScope) {
|
|
1211
1246
|
return new Error("verifyIdentityToken: 无效的 Scope");
|
|
1212
1247
|
}
|
|
1248
|
+
// spell-checker:ignore AESGCM
|
|
1213
1249
|
return {
|
|
1214
1250
|
combo_id: payload.sub,
|
|
1215
1251
|
idp: payload.idp,
|
|
1216
1252
|
external_id: payload.external_id,
|
|
1217
1253
|
external_name: payload.external_name,
|
|
1218
|
-
|
|
1254
|
+
weixin_session_key: payload.weixin_session_key ? decryptAESGCM(this.privateKey, payload.weixin_session_key) : "",
|
|
1219
1255
|
distro: payload.distro || "",
|
|
1220
1256
|
variant: payload.variant || "",
|
|
1221
1257
|
device_id: payload.device_id || "",
|
|
@@ -1477,20 +1513,20 @@ class RedisIdempotencyStore {
|
|
|
1477
1513
|
}
|
|
1478
1514
|
|
|
1479
1515
|
const error2httpStatus = {
|
|
1480
|
-
[
|
|
1481
|
-
[
|
|
1482
|
-
[
|
|
1483
|
-
[
|
|
1484
|
-
[
|
|
1485
|
-
[
|
|
1486
|
-
[
|
|
1487
|
-
[
|
|
1488
|
-
[
|
|
1489
|
-
[
|
|
1490
|
-
[
|
|
1491
|
-
[
|
|
1492
|
-
[
|
|
1493
|
-
[
|
|
1516
|
+
[exports.GMError.InvalidHttpMethod]: 405 /* HttpStatus.MethodNotAllowed */,
|
|
1517
|
+
[exports.GMError.InvalidContentType]: 415 /* HttpStatus.UnsupportedMediaType */,
|
|
1518
|
+
[exports.GMError.InvalidSignature]: 401 /* HttpStatus.Unauthorized */,
|
|
1519
|
+
[exports.GMError.InvalidRequest]: 400 /* HttpStatus.BadRequest */,
|
|
1520
|
+
[exports.GMError.InvalidCommand]: 400 /* HttpStatus.BadRequest */,
|
|
1521
|
+
[exports.GMError.InvalidArgs]: 400 /* HttpStatus.BadRequest */,
|
|
1522
|
+
[exports.GMError.NetworkError]: 500 /* HttpStatus.InternalServerError */,
|
|
1523
|
+
[exports.GMError.DatabaseError]: 500 /* HttpStatus.InternalServerError */,
|
|
1524
|
+
[exports.GMError.IdempotencyConflict]: 409 /* HttpStatus.Conflict */,
|
|
1525
|
+
[exports.GMError.IdempotencyMismatch]: 422 /* HttpStatus.UnprocessableEntity */,
|
|
1526
|
+
[exports.GMError.TimeoutError]: 500 /* HttpStatus.InternalServerError */,
|
|
1527
|
+
[exports.GMError.MaintenanceError]: 503 /* HttpStatus.ServiceUnavailable */,
|
|
1528
|
+
[exports.GMError.ThrottlingError]: 429 /* HttpStatus.TooManyRequests */,
|
|
1529
|
+
[exports.GMError.InternalError]: 500 /* HttpStatus.InternalServerError */
|
|
1494
1530
|
};
|
|
1495
1531
|
/**
|
|
1496
1532
|
* 获取一个处理函数用于响应 Seayoo Server 推送过来的 GM 执行通知,storeHelper 提供存储支持以实现幂等key(idempotencyKey)的自动化处理
|
|
@@ -1504,12 +1540,12 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1504
1540
|
verifyStoreHelper(storeHelper);
|
|
1505
1541
|
return async function (req, res, rawBodyString) {
|
|
1506
1542
|
if (req.method !== "POST") {
|
|
1507
|
-
responseError(res, 405 /* HttpStatus.MethodNotAllowed */,
|
|
1543
|
+
responseError(res, 405 /* HttpStatus.MethodNotAllowed */, exports.GMError.InvalidHttpMethod, `Expecting POST, got ${req.method}`);
|
|
1508
1544
|
return;
|
|
1509
1545
|
}
|
|
1510
1546
|
const contentType = `${req.headers["Content-Type"] || req.headers["content-type"]}`.toLowerCase();
|
|
1511
1547
|
if (!contentType || !contentType.startsWith("application/json")) {
|
|
1512
|
-
responseError(res, 415 /* HttpStatus.UnsupportedMediaType */,
|
|
1548
|
+
responseError(res, 415 /* HttpStatus.UnsupportedMediaType */, exports.GMError.InvalidContentType, `Expecting application/json, got ${contentType}`);
|
|
1513
1549
|
return;
|
|
1514
1550
|
}
|
|
1515
1551
|
const authString = req.headers[AuthorizationField] || req.headers[AuthorizationField.toLowerCase()];
|
|
@@ -1523,12 +1559,12 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1523
1559
|
url: req.url || "",
|
|
1524
1560
|
data: body
|
|
1525
1561
|
})) {
|
|
1526
|
-
responseError(res, 401 /* HttpStatus.Unauthorized */,
|
|
1562
|
+
responseError(res, 401 /* HttpStatus.Unauthorized */, exports.GMError.InvalidSignature, "Signature failed");
|
|
1527
1563
|
return;
|
|
1528
1564
|
}
|
|
1529
1565
|
const gmRequest = parseJSON(body, isGMRequestBody);
|
|
1530
1566
|
if (!gmRequest) {
|
|
1531
|
-
responseError(res, 400 /* HttpStatus.BadRequest */,
|
|
1567
|
+
responseError(res, 400 /* HttpStatus.BadRequest */, exports.GMError.InvalidRequest, `GM request body format error`);
|
|
1532
1568
|
return;
|
|
1533
1569
|
}
|
|
1534
1570
|
// 处理幂等key
|
|
@@ -1544,13 +1580,13 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1544
1580
|
const oldRecordStr = await storeHelper.setNX(gmRequest.idempotency_key, toString(currentRecord)).catch(e => e instanceof Error ? e : new Error(String(e)));
|
|
1545
1581
|
if (oldRecordStr instanceof Error) {
|
|
1546
1582
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) setNX error`, oldRecordStr);
|
|
1547
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1583
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, exports.GMError.InternalError, oldRecordStr.message);
|
|
1548
1584
|
return;
|
|
1549
1585
|
}
|
|
1550
1586
|
const oldRecord = oldRecordStr ? parseJSON(oldRecordStr, isIdempotencyRecord) : null;
|
|
1551
1587
|
if (!oldRecord && oldRecordStr) {
|
|
1552
1588
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) parse old idempotency record failed: ${oldRecordStr}`);
|
|
1553
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1589
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, exports.GMError.InternalError, "parse old idempotency record failed");
|
|
1554
1590
|
return;
|
|
1555
1591
|
}
|
|
1556
1592
|
// spell-checker:disable
|
|
@@ -1563,15 +1599,15 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1563
1599
|
const isFirstRequest = oldRecordStr === "" || (oldRecord == null ? void 0 : oldRecord.nonce) === currentRecord.nonce;
|
|
1564
1600
|
if (!isFirstRequest && oldRecord) {
|
|
1565
1601
|
if (!oldRecord.resp) {
|
|
1566
|
-
responseError(res, error2httpStatus[
|
|
1602
|
+
responseError(res, error2httpStatus[exports.GMError.IdempotencyConflict], exports.GMError.IdempotencyConflict, "previous request is not completed");
|
|
1567
1603
|
return;
|
|
1568
1604
|
}
|
|
1569
1605
|
if (oldRecord.cmd !== gmRequest.command) {
|
|
1570
|
-
responseError(res, error2httpStatus[
|
|
1606
|
+
responseError(res, error2httpStatus[exports.GMError.IdempotencyMismatch], exports.GMError.IdempotencyMismatch, `cmd mismatch, expecting ${oldRecord.cmd}, got ${gmRequest.command}`);
|
|
1571
1607
|
return;
|
|
1572
1608
|
}
|
|
1573
1609
|
if (oldRecord.args !== currentRecord.args) {
|
|
1574
|
-
responseError(res, error2httpStatus[
|
|
1610
|
+
responseError(res, error2httpStatus[exports.GMError.IdempotencyMismatch], exports.GMError.IdempotencyMismatch, `args mismatch, expecting ${oldRecord.args}, got ${currentRecord.args}`);
|
|
1575
1611
|
return;
|
|
1576
1612
|
}
|
|
1577
1613
|
oldResponse = oldRecord.resp;
|
|
@@ -1589,11 +1625,11 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1589
1625
|
}
|
|
1590
1626
|
// 游戏主动抛错不打印日志信息
|
|
1591
1627
|
if (result instanceof Error) {
|
|
1592
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1628
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, exports.GMError.InternalError, result.message);
|
|
1593
1629
|
return;
|
|
1594
1630
|
}
|
|
1595
1631
|
if (isGMErrorResponse(result)) {
|
|
1596
|
-
responseError(res, error2httpStatus[result.error] || 500 /* HttpStatus.InternalServerError */, result.error ||
|
|
1632
|
+
responseError(res, error2httpStatus[result.error] || 500 /* HttpStatus.InternalServerError */, result.error || exports.GMError.InternalError, result.message);
|
|
1597
1633
|
return;
|
|
1598
1634
|
}
|
|
1599
1635
|
// 对游戏处理函数进一步兼容处理,允许返回一个预定义的错误码
|
|
@@ -1609,7 +1645,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1609
1645
|
responseJson(res, result != null ? result : {});
|
|
1610
1646
|
} catch (e) {
|
|
1611
1647
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) exec error`, e);
|
|
1612
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1648
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, exports.GMError.InternalError, e instanceof Error ? e.message : String(e));
|
|
1613
1649
|
}
|
|
1614
1650
|
};
|
|
1615
1651
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { platform, arch } from 'os';
|
|
2
2
|
import http from 'http';
|
|
3
3
|
import https from 'https';
|
|
4
|
-
import { createHash, createHmac } from 'crypto';
|
|
4
|
+
import { createHash, createHmac, createDecipheriv } from 'crypto';
|
|
5
5
|
import { verify } from 'jsonwebtoken';
|
|
6
6
|
|
|
7
7
|
/** SDK 名称 */
|
|
8
8
|
const SDKName = "combo-sdk-node";
|
|
9
9
|
/** SDK 版本,在编译前会被动态替换 */
|
|
10
|
-
const SDKVersion = "1.
|
|
10
|
+
const SDKVersion = "1.5.1";
|
|
11
11
|
/**
|
|
12
12
|
* 世游支持的服务端点列表
|
|
13
13
|
*/
|
|
@@ -29,12 +29,6 @@ var Platform;
|
|
|
29
29
|
Platform["Windows"] = "windows";
|
|
30
30
|
/** macOS 桌面平台 */
|
|
31
31
|
Platform["macOS"] = "macos";
|
|
32
|
-
/**
|
|
33
|
-
* 微信小游戏
|
|
34
|
-
*
|
|
35
|
-
* @deprecated 已被 Platform.WebGL 取代
|
|
36
|
-
*/
|
|
37
|
-
Platform["Weixin"] = "weixin";
|
|
38
32
|
/** WebGL 平台,包括微信、抖音等小游戏,以及 HTML5 网页游戏 */
|
|
39
33
|
Platform["WebGL"] = "webgl";
|
|
40
34
|
/** 华为的 HarmonyOS NEXT 鸿蒙应用开发平台 */
|
|
@@ -1169,6 +1163,47 @@ function isAdJwtPayload(payload) {
|
|
|
1169
1163
|
return isObject(payload) && "sub" in payload && "iss" in payload && "scope" in payload && "placement_id" in payload && "impression_id" in payload;
|
|
1170
1164
|
}
|
|
1171
1165
|
|
|
1166
|
+
/**
|
|
1167
|
+
* decryptAESGCM 使用 AES-256-GCM 解密 base64 编码的密文。
|
|
1168
|
+
* 密文格式为 base64(nonce || ciphertext || authTag),其中 nonce 为 12 字节,authTag 为 16 字节。
|
|
1169
|
+
*/
|
|
1170
|
+
function decryptAESGCM(sk, encoded) {
|
|
1171
|
+
let data;
|
|
1172
|
+
try {
|
|
1173
|
+
data = Buffer.from(encoded, "base64");
|
|
1174
|
+
} catch (err) {
|
|
1175
|
+
throw new Error(`base64 decode error: ${err}`);
|
|
1176
|
+
}
|
|
1177
|
+
const key = deriveAESKey(sk);
|
|
1178
|
+
const nonceSize = 12;
|
|
1179
|
+
const authTagSize = 16;
|
|
1180
|
+
if (data.length < nonceSize + authTagSize) {
|
|
1181
|
+
throw new Error("ciphertext too short");
|
|
1182
|
+
}
|
|
1183
|
+
const nonce = data.slice(0, nonceSize);
|
|
1184
|
+
// 密文格式: nonce (12) || ciphertext (variable) || authTag (16)
|
|
1185
|
+
const authTag = data.slice(data.length - authTagSize);
|
|
1186
|
+
const ciphertext = data.slice(nonceSize, data.length - authTagSize);
|
|
1187
|
+
const decipher = createDecipheriv("aes-256-gcm", key, nonce);
|
|
1188
|
+
decipher.setAuthTag(authTag);
|
|
1189
|
+
// Node.js 12 兼容:使用 update 处理所有数据,避免 final() 的问题
|
|
1190
|
+
let plaintext;
|
|
1191
|
+
try {
|
|
1192
|
+
plaintext = decipher.update(ciphertext);
|
|
1193
|
+
// Node.js 12 中 GCM 模式下 final() 通常返回空 Buffer,但会验证 authTag
|
|
1194
|
+
const finalBuffer = decipher.final();
|
|
1195
|
+
if (finalBuffer.length > 0) {
|
|
1196
|
+
plaintext = Buffer.concat([plaintext, finalBuffer]);
|
|
1197
|
+
}
|
|
1198
|
+
} catch (err) {
|
|
1199
|
+
throw new Error(`gcm decrypt error: ${err}`);
|
|
1200
|
+
}
|
|
1201
|
+
return plaintext.toString("utf8");
|
|
1202
|
+
}
|
|
1203
|
+
function deriveAESKey(sk) {
|
|
1204
|
+
return createHash("sha256").update(sk).digest();
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1172
1207
|
const IdentityTokenScope = "auth";
|
|
1173
1208
|
const AdTokenScope = "ads";
|
|
1174
1209
|
/**
|
|
@@ -1205,12 +1240,13 @@ class TokenVerifier {
|
|
|
1205
1240
|
if (payload.scope !== IdentityTokenScope) {
|
|
1206
1241
|
return new Error("verifyIdentityToken: 无效的 Scope");
|
|
1207
1242
|
}
|
|
1243
|
+
// spell-checker:ignore AESGCM
|
|
1208
1244
|
return {
|
|
1209
1245
|
combo_id: payload.sub,
|
|
1210
1246
|
idp: payload.idp,
|
|
1211
1247
|
external_id: payload.external_id,
|
|
1212
1248
|
external_name: payload.external_name,
|
|
1213
|
-
|
|
1249
|
+
weixin_session_key: payload.weixin_session_key ? decryptAESGCM(this.privateKey, payload.weixin_session_key) : "",
|
|
1214
1250
|
distro: payload.distro || "",
|
|
1215
1251
|
variant: payload.variant || "",
|
|
1216
1252
|
device_id: payload.device_id || "",
|
|
@@ -1472,20 +1508,20 @@ class RedisIdempotencyStore {
|
|
|
1472
1508
|
}
|
|
1473
1509
|
|
|
1474
1510
|
const error2httpStatus = {
|
|
1475
|
-
[
|
|
1476
|
-
[
|
|
1477
|
-
[
|
|
1478
|
-
[
|
|
1479
|
-
[
|
|
1480
|
-
[
|
|
1481
|
-
[
|
|
1482
|
-
[
|
|
1483
|
-
[
|
|
1484
|
-
[
|
|
1485
|
-
[
|
|
1486
|
-
[
|
|
1487
|
-
[
|
|
1488
|
-
[
|
|
1511
|
+
[GMError.InvalidHttpMethod]: 405 /* HttpStatus.MethodNotAllowed */,
|
|
1512
|
+
[GMError.InvalidContentType]: 415 /* HttpStatus.UnsupportedMediaType */,
|
|
1513
|
+
[GMError.InvalidSignature]: 401 /* HttpStatus.Unauthorized */,
|
|
1514
|
+
[GMError.InvalidRequest]: 400 /* HttpStatus.BadRequest */,
|
|
1515
|
+
[GMError.InvalidCommand]: 400 /* HttpStatus.BadRequest */,
|
|
1516
|
+
[GMError.InvalidArgs]: 400 /* HttpStatus.BadRequest */,
|
|
1517
|
+
[GMError.NetworkError]: 500 /* HttpStatus.InternalServerError */,
|
|
1518
|
+
[GMError.DatabaseError]: 500 /* HttpStatus.InternalServerError */,
|
|
1519
|
+
[GMError.IdempotencyConflict]: 409 /* HttpStatus.Conflict */,
|
|
1520
|
+
[GMError.IdempotencyMismatch]: 422 /* HttpStatus.UnprocessableEntity */,
|
|
1521
|
+
[GMError.TimeoutError]: 500 /* HttpStatus.InternalServerError */,
|
|
1522
|
+
[GMError.MaintenanceError]: 503 /* HttpStatus.ServiceUnavailable */,
|
|
1523
|
+
[GMError.ThrottlingError]: 429 /* HttpStatus.TooManyRequests */,
|
|
1524
|
+
[GMError.InternalError]: 500 /* HttpStatus.InternalServerError */
|
|
1489
1525
|
};
|
|
1490
1526
|
/**
|
|
1491
1527
|
* 获取一个处理函数用于响应 Seayoo Server 推送过来的 GM 执行通知,storeHelper 提供存储支持以实现幂等key(idempotencyKey)的自动化处理
|
|
@@ -1499,12 +1535,12 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1499
1535
|
verifyStoreHelper(storeHelper);
|
|
1500
1536
|
return async function (req, res, rawBodyString) {
|
|
1501
1537
|
if (req.method !== "POST") {
|
|
1502
|
-
responseError(res, 405 /* HttpStatus.MethodNotAllowed */,
|
|
1538
|
+
responseError(res, 405 /* HttpStatus.MethodNotAllowed */, GMError.InvalidHttpMethod, `Expecting POST, got ${req.method}`);
|
|
1503
1539
|
return;
|
|
1504
1540
|
}
|
|
1505
1541
|
const contentType = `${req.headers["Content-Type"] || req.headers["content-type"]}`.toLowerCase();
|
|
1506
1542
|
if (!contentType || !contentType.startsWith("application/json")) {
|
|
1507
|
-
responseError(res, 415 /* HttpStatus.UnsupportedMediaType */,
|
|
1543
|
+
responseError(res, 415 /* HttpStatus.UnsupportedMediaType */, GMError.InvalidContentType, `Expecting application/json, got ${contentType}`);
|
|
1508
1544
|
return;
|
|
1509
1545
|
}
|
|
1510
1546
|
const authString = req.headers[AuthorizationField] || req.headers[AuthorizationField.toLowerCase()];
|
|
@@ -1518,12 +1554,12 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1518
1554
|
url: req.url || "",
|
|
1519
1555
|
data: body
|
|
1520
1556
|
})) {
|
|
1521
|
-
responseError(res, 401 /* HttpStatus.Unauthorized */,
|
|
1557
|
+
responseError(res, 401 /* HttpStatus.Unauthorized */, GMError.InvalidSignature, "Signature failed");
|
|
1522
1558
|
return;
|
|
1523
1559
|
}
|
|
1524
1560
|
const gmRequest = parseJSON(body, isGMRequestBody);
|
|
1525
1561
|
if (!gmRequest) {
|
|
1526
|
-
responseError(res, 400 /* HttpStatus.BadRequest */,
|
|
1562
|
+
responseError(res, 400 /* HttpStatus.BadRequest */, GMError.InvalidRequest, `GM request body format error`);
|
|
1527
1563
|
return;
|
|
1528
1564
|
}
|
|
1529
1565
|
// 处理幂等key
|
|
@@ -1539,13 +1575,13 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1539
1575
|
const oldRecordStr = await storeHelper.setNX(gmRequest.idempotency_key, toString(currentRecord)).catch(e => e instanceof Error ? e : new Error(String(e)));
|
|
1540
1576
|
if (oldRecordStr instanceof Error) {
|
|
1541
1577
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) setNX error`, oldRecordStr);
|
|
1542
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1578
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, GMError.InternalError, oldRecordStr.message);
|
|
1543
1579
|
return;
|
|
1544
1580
|
}
|
|
1545
1581
|
const oldRecord = oldRecordStr ? parseJSON(oldRecordStr, isIdempotencyRecord) : null;
|
|
1546
1582
|
if (!oldRecord && oldRecordStr) {
|
|
1547
1583
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) parse old idempotency record failed: ${oldRecordStr}`);
|
|
1548
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1584
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, GMError.InternalError, "parse old idempotency record failed");
|
|
1549
1585
|
return;
|
|
1550
1586
|
}
|
|
1551
1587
|
// spell-checker:disable
|
|
@@ -1558,15 +1594,15 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1558
1594
|
const isFirstRequest = oldRecordStr === "" || (oldRecord == null ? void 0 : oldRecord.nonce) === currentRecord.nonce;
|
|
1559
1595
|
if (!isFirstRequest && oldRecord) {
|
|
1560
1596
|
if (!oldRecord.resp) {
|
|
1561
|
-
responseError(res, error2httpStatus[
|
|
1597
|
+
responseError(res, error2httpStatus[GMError.IdempotencyConflict], GMError.IdempotencyConflict, "previous request is not completed");
|
|
1562
1598
|
return;
|
|
1563
1599
|
}
|
|
1564
1600
|
if (oldRecord.cmd !== gmRequest.command) {
|
|
1565
|
-
responseError(res, error2httpStatus[
|
|
1601
|
+
responseError(res, error2httpStatus[GMError.IdempotencyMismatch], GMError.IdempotencyMismatch, `cmd mismatch, expecting ${oldRecord.cmd}, got ${gmRequest.command}`);
|
|
1566
1602
|
return;
|
|
1567
1603
|
}
|
|
1568
1604
|
if (oldRecord.args !== currentRecord.args) {
|
|
1569
|
-
responseError(res, error2httpStatus[
|
|
1605
|
+
responseError(res, error2httpStatus[GMError.IdempotencyMismatch], GMError.IdempotencyMismatch, `args mismatch, expecting ${oldRecord.args}, got ${currentRecord.args}`);
|
|
1570
1606
|
return;
|
|
1571
1607
|
}
|
|
1572
1608
|
oldResponse = oldRecord.resp;
|
|
@@ -1584,11 +1620,11 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1584
1620
|
}
|
|
1585
1621
|
// 游戏主动抛错不打印日志信息
|
|
1586
1622
|
if (result instanceof Error) {
|
|
1587
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1623
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, GMError.InternalError, result.message);
|
|
1588
1624
|
return;
|
|
1589
1625
|
}
|
|
1590
1626
|
if (isGMErrorResponse(result)) {
|
|
1591
|
-
responseError(res, error2httpStatus[result.error] || 500 /* HttpStatus.InternalServerError */, result.error ||
|
|
1627
|
+
responseError(res, error2httpStatus[result.error] || 500 /* HttpStatus.InternalServerError */, result.error || GMError.InternalError, result.message);
|
|
1592
1628
|
return;
|
|
1593
1629
|
}
|
|
1594
1630
|
// 对游戏处理函数进一步兼容处理,允许返回一个预定义的错误码
|
|
@@ -1604,7 +1640,7 @@ function getGMCommandHandler(config, handler, storeHelper) {
|
|
|
1604
1640
|
responseJson(res, result != null ? result : {});
|
|
1605
1641
|
} catch (e) {
|
|
1606
1642
|
console.error(`gm command "${gmRequest.command}"(requestId: ${gmRequest.request_id}; idempotencyKey: ${gmRequest.idempotency_key || ""}) exec error`, e);
|
|
1607
|
-
responseError(res, 500 /* HttpStatus.InternalServerError */,
|
|
1643
|
+
responseError(res, 500 /* HttpStatus.InternalServerError */, GMError.InternalError, e instanceof Error ? e.message : String(e));
|
|
1608
1644
|
}
|
|
1609
1645
|
};
|
|
1610
1646
|
}
|
|
@@ -1671,4 +1707,4 @@ function getGMMiddlewareForKoa(paths, config, handler, storeHelper) {
|
|
|
1671
1707
|
};
|
|
1672
1708
|
}
|
|
1673
1709
|
|
|
1674
|
-
export { ApiClient, Endpoint, IdP, MemoryIdempotencyStore, Platform, RedisIdempotencyStore, TokenVerifier, calcAuthorizationHeader, checkHttpAuthInfo, genSessionID, getGMCommandHandler, getGMHandlerForExpress, getGMHandlerForKoa, getGMMiddlewareForExpress, getGMMiddlewareForKoa, getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, getNotificationMiddlewareForExpress, getNotificationMiddlewareForKoa, parseAuthorizationHeader };
|
|
1710
|
+
export { ApiClient, Endpoint, GMError, IdP, MemoryIdempotencyStore, Platform, RedisIdempotencyStore, TokenVerifier, calcAuthorizationHeader, checkHttpAuthInfo, genSessionID, getGMCommandHandler, getGMHandlerForExpress, getGMHandlerForKoa, getGMMiddlewareForExpress, getGMMiddlewareForKoa, getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, getNotificationMiddlewareForExpress, getNotificationMiddlewareForKoa, parseAuthorizationHeader };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
type SecretKey = string | Buffer;
|
|
4
|
+
/**
|
|
5
|
+
* decryptAESGCM 使用 AES-256-GCM 解密 base64 编码的密文。
|
|
6
|
+
* 密文格式为 base64(nonce || ciphertext || authTag),其中 nonce 为 12 字节,authTag 为 16 字节。
|
|
7
|
+
*/
|
|
8
|
+
export declare function decryptAESGCM(sk: SecretKey, encoded: string): string;
|
|
9
|
+
export {};
|
package/dist/verify/id.d.ts
CHANGED
|
@@ -30,12 +30,12 @@ export interface IdentityPayload {
|
|
|
30
30
|
*/
|
|
31
31
|
external_name: string;
|
|
32
32
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
33
|
+
* weixin_session_key 是用户在微信小游戏登录时,从微信服务端获得的会话密钥 session_key。
|
|
34
|
+
* 该字段在 Identity Token 中以 AES-256-GCM 加密存储,SDK 会自动解密。
|
|
35
35
|
*
|
|
36
|
-
*
|
|
36
|
+
* weixin_session_key 只在 IdP 为 MinigameWeixin 时才会有值。
|
|
37
37
|
*/
|
|
38
|
-
|
|
38
|
+
weixin_session_key: string;
|
|
39
39
|
/**
|
|
40
40
|
* DeviceId 是用户在登录时使用的设备的唯一 ID。
|
|
41
41
|
*/
|