index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
  7. const utils_1 = require("../utils/utils");
  8. const code_1 = require("../const/code");
  9. const cloudbase_1 = require("../cloudbase");
  10. const symbol_1 = require("../const/symbol");
  11. const httpRequest_1 = __importDefault(require("../utils/httpRequest"));
  12. const checkCustomUserIdRegex = /^[a-zA-Z0-9_\-#@~=*(){}[\]:.,<>+]{4,32}$/;
  13. function validateUid(uid) {
  14. if (typeof uid !== 'string') {
  15. // console.log('debug:', { ...ERROR.INVALID_PARAM, message: 'uid must be a string' })
  16. throw utils_1.E(Object.assign({}, code_1.ERROR.INVALID_PARAM, { message: 'uid must be a string' }));
  17. }
  18. if (!checkCustomUserIdRegex.test(uid)) {
  19. throw utils_1.E(Object.assign({}, code_1.ERROR.INVALID_PARAM, { message: `Invalid uid: "${uid}"` }));
  20. }
  21. }
  22. function auth(cloudbase) {
  23. return {
  24. getUserInfo() {
  25. const { WX_OPENID, WX_APPID, TCB_UUID, TCB_CUSTOM_USER_ID, TCB_ISANONYMOUS_USER } = cloudbase_1.CloudBase.getCloudbaseContext();
  26. return {
  27. openId: WX_OPENID || '',
  28. appId: WX_APPID || '',
  29. uid: TCB_UUID || '',
  30. customUserId: TCB_CUSTOM_USER_ID || '',
  31. isAnonymous: TCB_ISANONYMOUS_USER === 'true' ? true : false
  32. };
  33. },
  34. getEndUserInfo(uid, opts) {
  35. const { WX_OPENID, WX_APPID, TCB_UUID, TCB_CUSTOM_USER_ID, TCB_ISANONYMOUS_USER } = cloudbase_1.CloudBase.getCloudbaseContext();
  36. const defaultUserInfo = {
  37. openId: WX_OPENID || '',
  38. appId: WX_APPID || '',
  39. uid: TCB_UUID || '',
  40. customUserId: TCB_CUSTOM_USER_ID || '',
  41. isAnonymous: TCB_ISANONYMOUS_USER === 'true' ? true : false
  42. };
  43. if (uid === undefined) {
  44. return {
  45. userInfo: defaultUserInfo
  46. };
  47. }
  48. validateUid(uid);
  49. const params = {
  50. action: 'auth.getUserInfoForAdmin',
  51. uuid: uid
  52. };
  53. return httpRequest_1.default({
  54. config: cloudbase.config,
  55. params,
  56. method: 'post',
  57. opts,
  58. headers: {
  59. 'content-type': 'application/json'
  60. }
  61. }).then(res => {
  62. if (res.code) {
  63. return res;
  64. }
  65. return {
  66. userInfo: Object.assign({}, defaultUserInfo, res.data),
  67. requestId: res.requestId
  68. };
  69. });
  70. },
  71. async getAuthContext(context) {
  72. const { TCB_UUID, LOGINTYPE, QQ_OPENID, QQ_APPID } = cloudbase_1.CloudBase.getCloudbaseContext(context);
  73. const res = {
  74. uid: TCB_UUID,
  75. loginType: LOGINTYPE
  76. };
  77. if (LOGINTYPE === 'QQ-MINI') {
  78. res.appId = QQ_APPID;
  79. res.openId = QQ_OPENID;
  80. }
  81. return res;
  82. },
  83. getClientIP() {
  84. const { TCB_SOURCE_IP } = cloudbase_1.CloudBase.getCloudbaseContext();
  85. return TCB_SOURCE_IP || '';
  86. },
  87. createTicket: (uid, options = {}) => {
  88. validateUid(uid);
  89. const timestamp = new Date().getTime();
  90. const { TCB_ENV, SCF_NAMESPACE } = cloudbase_1.CloudBase.getCloudbaseContext();
  91. const { credentials } = cloudbase.config;
  92. const { env_id } = credentials;
  93. let { envName } = cloudbase.config;
  94. if (!envName) {
  95. throw utils_1.E(Object.assign({}, code_1.ERROR.INVALID_PARAM, { message: 'no env in config' }));
  96. }
  97. // 检查credentials 是否包含env
  98. if (!env_id) {
  99. throw utils_1.E(Object.assign({}, code_1.ERROR.INVALID_PARAM, { message: '当前私钥未包含env_id 信息, 请前往腾讯云云开发控制台,获取自定义登录最新私钥' }));
  100. }
  101. // 使用symbol时替换为环境变量内的env
  102. if (envName === symbol_1.SYMBOL_CURRENT_ENV) {
  103. envName = TCB_ENV || SCF_NAMESPACE;
  104. }
  105. // 检查 credentials env 和 init 指定env 是否一致
  106. if (env_id && env_id !== envName) {
  107. throw utils_1.E(Object.assign({}, code_1.ERROR.INVALID_PARAM, { message: '当前私钥所属环境与 init 指定环境不一致!' }));
  108. }
  109. const { refresh = 3600 * 1000, expire = timestamp + 7 * 24 * 60 * 60 * 1000 } = options;
  110. const token = jsonwebtoken_1.default.sign({
  111. alg: 'RS256',
  112. env: envName,
  113. iat: timestamp,
  114. exp: timestamp + 10 * 60 * 1000,
  115. uid,
  116. refresh,
  117. expire
  118. }, credentials.private_key, { algorithm: 'RS256' });
  119. return credentials.private_key_id + '/@@/' + token;
  120. }
  121. };
  122. }
  123. exports.auth = auth;