index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. queryUserInfo(query, opts) {
  72. const { uid, platform, platformId } = query;
  73. const params = {
  74. action: 'auth.getUserInfoForAdmin',
  75. uuid: uid,
  76. platform,
  77. platformId
  78. };
  79. return httpRequest_1.default({
  80. config: cloudbase.config,
  81. params,
  82. method: 'post',
  83. opts,
  84. headers: {
  85. 'content-type': 'application/json'
  86. }
  87. }).then(res => {
  88. if (res.code) {
  89. return res;
  90. }
  91. return {
  92. userInfo: Object.assign({}, res.data),
  93. requestId: res.requestId
  94. };
  95. });
  96. },
  97. async getAuthContext(context) {
  98. const { TCB_UUID, LOGINTYPE, QQ_OPENID, QQ_APPID } = cloudbase_1.CloudBase.getCloudbaseContext(context);
  99. const res = {
  100. uid: TCB_UUID,
  101. loginType: LOGINTYPE
  102. };
  103. if (LOGINTYPE === 'QQ-MINI') {
  104. res.appId = QQ_APPID;
  105. res.openId = QQ_OPENID;
  106. }
  107. return res;
  108. },
  109. getClientIP() {
  110. const { TCB_SOURCE_IP } = cloudbase_1.CloudBase.getCloudbaseContext();
  111. return TCB_SOURCE_IP || '';
  112. },
  113. createTicket: (uid, options = {}) => {
  114. validateUid(uid);
  115. const timestamp = new Date().getTime();
  116. const { TCB_ENV, SCF_NAMESPACE } = cloudbase_1.CloudBase.getCloudbaseContext();
  117. const { credentials } = cloudbase.config;
  118. const { env_id } = credentials;
  119. let { envName } = cloudbase.config;
  120. if (!envName) {
  121. throw utils_1.E(Object.assign({}, code_1.ERROR.INVALID_PARAM, { message: 'no env in config' }));
  122. }
  123. // 检查credentials 是否包含env
  124. if (!env_id) {
  125. throw utils_1.E(Object.assign({}, code_1.ERROR.INVALID_PARAM, { message: '当前私钥未包含env_id 信息, 请前往腾讯云云开发控制台,获取自定义登录最新私钥' }));
  126. }
  127. // 使用symbol时替换为环境变量内的env
  128. if (envName === symbol_1.SYMBOL_CURRENT_ENV) {
  129. envName = TCB_ENV || SCF_NAMESPACE;
  130. }
  131. // 检查 credentials env 和 init 指定env 是否一致
  132. if (env_id && env_id !== envName) {
  133. throw utils_1.E(Object.assign({}, code_1.ERROR.INVALID_PARAM, { message: '当前私钥所属环境与 init 指定环境不一致!' }));
  134. }
  135. const { refresh = 3600 * 1000, expire = timestamp + 7 * 24 * 60 * 60 * 1000 } = options;
  136. const token = jsonwebtoken_1.default.sign({
  137. alg: 'RS256',
  138. env: envName,
  139. iat: timestamp,
  140. exp: timestamp + 10 * 60 * 1000,
  141. uid,
  142. refresh,
  143. expire
  144. }, credentials.private_key, { algorithm: 'RS256' });
  145. return credentials.private_key_id + '/@@/' + token;
  146. }
  147. };
  148. }
  149. exports.auth = auth;