提交 c3365ae6 编写于 作者: DCloud_JSON's avatar DCloud_JSON
## 1.0.2(2022-06-23)
- 对齐旧版本uni-id默认配置
## 1.0.1(2022-06-22)
- 补充对uni-config-center的依赖
## 1.0.0(2022-06-21)
- 提供uni-id token创建、校验、刷新接口,简化旧版uni-id公共模块
{
"id": "uni-id-common",
"displayName": "uni-id-common",
"version": "1.0.0",
"description": "uni-id公共模块,仅包含公用基础能力,建议搭配uni-id-pages使用",
"keywords": [
"uni-id-common",
"uni-id",
"uniCloud用户中心",
"token生成/校验"
],
"repository": "",
"engines": {
"HBuilderX": "^3.1.0"
},
"dcloudext": {
"category": [
"id": "uni-id-common",
"displayName": "uni-id-common",
"version": "1.0.2",
"description": "包含uni-id token生成、校验、刷新功能的云函数公共模块",
"keywords": [
"uni-id-common",
"uniCloud",
"云函数模板"
"token",
"权限"
],
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "",
"data": "",
"permissions": ""
},
"npmurl": ""
},
"uni_modules": {
"dependencies": [],
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "u",
"aliyun": "u"
},
"client": {
"Vue": {
"vue2": "u",
"vue3": "u"
},
"App": {
"app-vue": "u",
"app-nvue": "u"
},
"H5-mobile": {
"Safari": "u",
"Android Browser": "u",
"微信浏览器(Android)": "u",
"QQ浏览器(Android)": "u"
},
"H5-pc": {
"Chrome": "u",
"IE": "u",
"Edge": "u",
"Firefox": "u",
"Safari": "u"
},
"小程序": {
"微信": "u",
"阿里": "u",
"百度": "u",
"字节跳动": "u",
"QQ": "u",
"钉钉": "u",
"快手": "u",
"飞书": "u",
"京东": "u",
"小红书": "u"
},
"快应用": {
"华为": "u",
"联盟": "u"
}
}
}
}
}
\ No newline at end of file
"repository": "https://gitcode.net/dcloud/uni-id-common",
"engines": {
"HBuilderX": "^3.1.0"
},
"dcloudext": {
"category": [
"uniCloud",
"云函数模板"
],
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "无",
"data": "无",
"permissions": "无"
},
"npmurl": ""
},
"uni_modules": {
"dependencies": ["uni-config-center"],
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "y",
"aliyun": "y"
},
"client": {
"Vue": {
"vue2": "u",
"vue3": "u"
},
"App": {
"app-vue": "u",
"app-nvue": "u"
},
"H5-mobile": {
"Safari": "u",
"Android Browser": "u",
"微信浏览器(Android)": "u",
"QQ浏览器(Android)": "u"
},
"H5-pc": {
"Chrome": "u",
"IE": "u",
"Edge": "u",
"Firefox": "u",
"Safari": "u"
},
"小程序": {
"微信": "u",
"阿里": "u",
"百度": "u",
"字节跳动": "u",
"QQ": "u",
"钉钉": "u",
"快手": "u",
"飞书": "u",
"京东": "u",
"小红书": "u"
},
"快应用": {
"华为": "u",
"联盟": "u"
}
}
}
}
}
# uni-id-common
\ No newline at end of file
# uni-id-common
文档请参考:[uni-id-common](https://uniapp.dcloud.net.cn/uniCloud/uni-id-common.html)
\ No newline at end of file
"use strict";var e,n=(e=require("crypto"))&&"object"==typeof e&&"default"in e?e.default:e;const t={TOKEN_EXPIRED:"uni-id-token-expired",CHECK_TOKEN_FAILED:"uni-id-check-token-failed",PARAM_REQUIRED:"uni-id-param-required",ACCOUNT_EXISTS:"uni-id-account-exists",ACCOUNT_NOT_EXISTS:"uni-id-account-not-exists",ACCOUNT_CONFLICT:"uni-id-account-conflict",ACCOUNT_BANNED:"uni-id-account-banned",ACCOUNT_AUDITING:"uni-id-account-auditing",ACCOUNT_AUDIT_FAILED:"uni-id-account-audit-failed",ACCOUNT_CLOSED:"uni-id-account-closed"};function i(e){return!!e&&("object"==typeof e||"function"==typeof e)&&"function"==typeof e.then}const o={"uni-id-token-expired":30203,"uni-id-check-token-failed":30202};function r(e){const{errCode:n,errMsgValue:t}=e;e.errMsg=this._t(n,t),n in o&&(e.code=o[n])}function c(e){return"object"===(i=e,Object.prototype.toString.call(i).slice(8,-1).toLowerCase())&&e.errCode&&(n=e.errCode,Object.values(t).includes(n))&&!!e.errCode;var n,i}let s={"zh-Hans":{"uni-id-token-expired":"登陆状态失效,token已过期","uni-id-check-token-failed":"token校验未通过","uni-id-param-required":"缺少参数: {param}","uni-id-account-exists":"此账号已注册","uni-id-account-not-exists":"此账号未注册","uni-id-account-conflict":"用户账号冲突","uni-id-account-banned":"从账号已封禁","uni-id-account-auditing":"此账号正在审核中","uni-id-account-audit-failed":"此账号审核失败","uni-id-account-closed":"此账号已注销"},en:{"uni-id-token-expired":"The login status is invalid, token has expired","uni-id-check-token-failed":"Check token failed","uni-id-param-required":"Parameter required: {param}","uni-id-account-exists":"Account exists","uni-id-account-not-exists":"Account does not exists","uni-id-account-conflict":"User account conflict","uni-id-account-banned":"Account has been banned","uni-id-account-auditing":"Account audit in progress","uni-id-account-audit-failed":"Account audit failed","uni-id-account-closed":"Account has been closed"}};try{const e=require.resolve("uni-config-center/uni-id/lang/index.js");s=function(e,n){const t=Object.keys(e);t.push(...Object.keys(n));const i={};for(let o=0;o<t.length;o++){const r=t[o];i[r]=Object.assign({},e[r],n[r])}return i}(s,require(e))}catch(e){}var a=s;function u(e){return e.replace(/=/g,"").replace(/\+/g,"-").replace(/\//g,"_")}function d(e){return JSON.parse((n=function(e){var n=4-(e=e.toString()).length%4;if(4!==n)for(var t=0;t<n;++t)e+="=";return e.replace(/-/g,"+").replace(/_/g,"/")}(e),Buffer.from(n,"base64").toString("utf-8")));var n}function l(e){return u((n=JSON.stringify(e),Buffer.from(n,"utf-8").toString("base64")));var n}function f(e,t){return u(n.createHmac("sha256",t).update(e).digest("base64"))}const p=function(e,n){if("string"!=typeof e)throw new Error("Invalid token");const t=e.split(".");if(3!==t.length)throw new Error("Invalid token");const[i,o,r]=t;if(f(i+"."+o,n)!==r)throw new Error("Invalid token");const c=d(i);if("HS256"!==c.alg||"JWT"!==c.typ)throw new Error("Invalid token");const s=d(o);if(1e3*s.exp<Date.now()){const e=new Error("Token expired");throw e.name="TokenExpiredError",e}return s},h=function(e,n,t={}){const{expiresIn:i}=t;if(!i)throw new Error("expiresIn is required");const o=parseInt(Date.now()/1e3),r={...e,iat:o,exp:o+t.expiresIn},c=l({alg:"HS256",typ:"JWT"})+"."+l(r);return c+"."+f(c,n)},g=uniCloud.database(),C=g.command,E=g.collection("uni-id-users"),_=g.collection("uni-id-roles");async function I(e){const{tokenSecret:n,tokenExpiresIn:i,tokenExpiresThreshold:o}=this._getConfig();if(o>i)throw new Error("Config error, tokenExpiresThreshold should be less than tokenExpiresIn");let r;try{r=p(e,n)}catch(e){if("TokenExpiredError"===e.name)throw{errCode:t.TOKEN_EXPIRED};throw{errCode:t.CHECK_TOKEN_FAILED}}return r}async function k(e){const n=e.uid,i=(await E.doc(n).get()).data[0];if(!i)throw{errCode:t.CHECK_TOKEN_FAILED};if(i.valid_token_date&&i.valid_token_date>1e3*e.iat)throw{errCode:t.TOKEN_EXPIRED};const{role:o,permission:r}=await w(i);return T.call(this,{uid:n,role:o,permission:r})}async function w(e){if(!e)throw{errCode:t.ACCOUNT_NOT_EXISTS};!function(e){switch(e.status){case void 0:case 0:break;case 1:throw{errCode:t.ACCOUNT_BANNED};case 2:throw{errCode:t.ACCOUNT_AUDITING};case 3:throw{errCode:t.ACCOUNT_AUDIT_FAILED};case 4:throw{errCode:t.ACCOUNT_CLOSED}}}(e);const n=e.role||[];if(0===n.length)return{role:[],permission:[]};if(n.includes("admin"))return{role:["admin"],permission:[]};const i=await _.where({role_id:C.in(n)});var o;return{role:n,permission:(o=i.data.reduce((e,n)=>{n.permission&&e.push(...n.permission)},[]),Array.from(new Set(o)))}}async function m({uid:e,signContent:n,tokenSecret:t,tokenExpiresIn:i}={}){const o=Date.now(),r=h(n,t,{expiresIn:i});return await E.doc(e).update({last_login_ip:this._clientInfo.clientIP,last_login_date:o}),{token:r,tokenExpired:o+1e3*i}}async function T({uid:e,role:n,permission:i}={}){const{tokenSecret:o,tokenExpiresIn:r,tokenExpiresThreshold:c}=this._getConfig();if(!e)throw{errCode:t.PARAM_REQUIRED,errMsgValue:{param:"uid"}};if(c>r)throw new Error("Config error, tokenExpiresThreshold should be less than tokenExpiresIn");if(!n||!i){const t=(await E.doc(e).get()).data[0],o=await w(t);n=o.role,i=o.permission}if(!this.interceptorMap.has("customToken"))return m.call(this,{uid:e,signContent:{uid:e,role:n,permission:i},tokenSecret:o,tokenExpiresIn:r});const s=this.interceptorMap.get("customToken");if("function"==typeof s)throw new Error("Invalid custom token file");const a=await s({uid:e,role:n,permission:i});return m.call(this,{uid:e,signContent:a,tokenSecret:o,tokenExpiresIn:r})}var A=Object.freeze({__proto__:null,checkToken:async function(e,{autoRefresh:n=!0}={}){const t=await I.call(this,e),{tokenExpiresThreshold:i}=this._getConfig();if(!i||!n)return{code:0,errCode:0,...t};const o=Date.now();let r={};return 1e3*t.exp-o<1e3*i&&(r=await k.call(this,t)),{code:0,errCode:0,...t,...r}},createToken:async function({uid:e,role:n,permission:t}={}){return T.call(this,{uid:e,role:n,permission:t})},refreshToken:async function({token:e}={}){const n=await I.call(this,e);return{errCode:0,...await k.call(this,n)}}});const x=require("uni-config-center")({pluginId:"uni-id"});class O{constructor({context:e,clientInfo:n,config:t}={}){this._clientInfo=e?function(e){return{appId:e.APPID,platform:e.PLATFORM,locale:e.LOCALE,clientIP:e.CLIENTIP,deviceId:e.DEVICEID}}(e):n,this.config=t||this._getOriginConfig(),this.interceptorMap=new Map,x.hasFile("custom-token.js")&&this.setInterceptor("customToken",require(x.resolve("custom-token.js"))),this._i18n=uniCloud.initI18n({locale:this._clientInfo.locale,fallbackLocale:"zh-Hans",messages:a})}_t(...e){return this._i18n.t(...e)}_getOriginConfig(){if(x.hasFile("config.json")){let e;try{e=x.config()}catch(e){throw new Error("Invalid uni-id config file\n"+e.messages)}return Array.isArray(e)?e:e[0]?Object.values(e):e}throw new Error("Invalid uni-id config file")}_getAppConfig(){const e=this._getOriginConfig();return Array.isArray(e)?e.find(e=>e.dcloudAppid===this.clientInfo.appId)||e.find(e=>e.isDefaultConfig):e}_getPlatformConfig(){const e=this._getAppConfig();"app-plus"===this._clientInfo.platform&&(this._clientInfo.platform="app"),"h5"===this._clientInfo.platform&&(this._clientInfo.platform="web");const n=Object.assign({},e,e[this._clientInfo.platform]);return["tokenSecret","tokenExpiresIn"].forEach(e=>{if(!n||!n[e])throw new Error(`Config parameter missing, ${e} is required`)}),n}_getConfig(){return this._getPlatformConfig()}}for(const e in A)O.prototype[e]=A[e];function y(e){const n=new O(e);return new Proxy(n,{get(e,n){if(n in e&&0!==n.indexOf("_")){if("function"==typeof e[n])return(t=e[n],function(){let e;try{e=t.apply(this,arguments)}catch(e){if(c(e))return r.call(this,e),e;throw e}return i(e)?e.then(e=>(c(e)&&r.call(this,e),e),e=>{if(c(e))return r.call(this,e),e;throw e}):(c(e)&&r.call(this,e),e)}).bind(e);if("context"!==n&&"config"!==n)return e[n]}var t}})}O.prototype.createInstance=y;const N={createInstance:y};module.exports=N;
"use strict";var e,n=(e=require("crypto"))&&"object"==typeof e&&"default"in e?e.default:e;const t={TOKEN_EXPIRED:"uni-id-token-expired",CHECK_TOKEN_FAILED:"uni-id-check-token-failed",PARAM_REQUIRED:"uni-id-param-required",ACCOUNT_EXISTS:"uni-id-account-exists",ACCOUNT_NOT_EXISTS:"uni-id-account-not-exists",ACCOUNT_CONFLICT:"uni-id-account-conflict",ACCOUNT_BANNED:"uni-id-account-banned",ACCOUNT_AUDITING:"uni-id-account-auditing",ACCOUNT_AUDIT_FAILED:"uni-id-account-audit-failed",ACCOUNT_CLOSED:"uni-id-account-closed"};function i(e){return!!e&&("object"==typeof e||"function"==typeof e)&&"function"==typeof e.then}const r={"uni-id-token-expired":30203,"uni-id-check-token-failed":30202};function o(e){const{errCode:n,errMsgValue:t}=e;e.errMsg=this._t(n,t),n in r&&(e.code=r[n])}function c(e){return"object"===(i=e,Object.prototype.toString.call(i).slice(8,-1).toLowerCase())&&e.errCode&&(n=e.errCode,Object.values(t).includes(n))&&!!e.errCode;var n,i}let s={"zh-Hans":{"uni-id-token-expired":"登陆状态失效,token已过期","uni-id-check-token-failed":"token校验未通过","uni-id-param-required":"缺少参数: {param}","uni-id-account-exists":"此账号已注册","uni-id-account-not-exists":"此账号未注册","uni-id-account-conflict":"用户账号冲突","uni-id-account-banned":"从账号已封禁","uni-id-account-auditing":"此账号正在审核中","uni-id-account-audit-failed":"此账号审核失败","uni-id-account-closed":"此账号已注销"},en:{"uni-id-token-expired":"The login status is invalid, token has expired","uni-id-check-token-failed":"Check token failed","uni-id-param-required":"Parameter required: {param}","uni-id-account-exists":"Account exists","uni-id-account-not-exists":"Account does not exists","uni-id-account-conflict":"User account conflict","uni-id-account-banned":"Account has been banned","uni-id-account-auditing":"Account audit in progress","uni-id-account-audit-failed":"Account audit failed","uni-id-account-closed":"Account has been closed"}};try{const e=require.resolve("uni-config-center/uni-id/lang/index.js");s=function(e,n){const t=Object.keys(e);t.push(...Object.keys(n));const i={};for(let r=0;r<t.length;r++){const o=t[r];i[o]=Object.assign({},e[o],n[o])}return i}(s,require(e))}catch(e){}var a=s;function u(e){return e.replace(/=/g,"").replace(/\+/g,"-").replace(/\//g,"_")}function d(e){return JSON.parse((n=function(e){var n=4-(e=e.toString()).length%4;if(4!==n)for(var t=0;t<n;++t)e+="=";return e.replace(/-/g,"+").replace(/_/g,"/")}(e),Buffer.from(n,"base64").toString("utf-8")));var n}function l(e){return u((n=JSON.stringify(e),Buffer.from(n,"utf-8").toString("base64")));var n}function f(e,t){return u(n.createHmac("sha256",t).update(e).digest("base64"))}const p=function(e,n){if("string"!=typeof e)throw new Error("Invalid token");const t=e.split(".");if(3!==t.length)throw new Error("Invalid token");const[i,r,o]=t;if(f(i+"."+r,n)!==o)throw new Error("Invalid token");const c=d(i);if("HS256"!==c.alg||"JWT"!==c.typ)throw new Error("Invalid token");const s=d(r);if(1e3*s.exp<Date.now()){const e=new Error("Token expired");throw e.name="TokenExpiredError",e}return s},h=function(e,n,t={}){const{expiresIn:i}=t;if(!i)throw new Error("expiresIn is required");const r=parseInt(Date.now()/1e3),o={...e,iat:r,exp:r+t.expiresIn},c=l({alg:"HS256",typ:"JWT"})+"."+l(o);return c+"."+f(c,n)},g=uniCloud.database(),C=g.command,E=g.collection("uni-id-users"),I=g.collection("uni-id-roles");async function _(e){const{tokenSecret:n,tokenExpiresIn:i,tokenExpiresThreshold:r}=this._getConfig();if(r>i)throw new Error("Config error, tokenExpiresThreshold should be less than tokenExpiresIn");let o;try{o=p(e,n)}catch(e){if("TokenExpiredError"===e.name)throw{errCode:t.TOKEN_EXPIRED};throw{errCode:t.CHECK_TOKEN_FAILED}}return o}async function k(e){const n=e.uid,i=(await E.doc(n).get()).data[0];if(!i)throw{errCode:t.CHECK_TOKEN_FAILED};if(i.valid_token_date&&i.valid_token_date>1e3*e.iat)throw{errCode:t.TOKEN_EXPIRED};const{role:r,permission:o}=await w(i);return T.call(this,{uid:n,role:r,permission:o})}async function w(e){if(!e)throw{errCode:t.ACCOUNT_NOT_EXISTS};!function(e){switch(e.status){case void 0:case 0:break;case 1:throw{errCode:t.ACCOUNT_BANNED};case 2:throw{errCode:t.ACCOUNT_AUDITING};case 3:throw{errCode:t.ACCOUNT_AUDIT_FAILED};case 4:throw{errCode:t.ACCOUNT_CLOSED}}}(e);const n=e.role||[];if(0===n.length)return{role:[],permission:[]};if(n.includes("admin"))return{role:["admin"],permission:[]};const i=await I.where({role_id:C.in(n)});var r;return{role:n,permission:(r=i.data.reduce((e,n)=>{n.permission&&e.push(...n.permission)},[]),Array.from(new Set(r)))}}async function m({uid:e,signContent:n,tokenSecret:t,tokenExpiresIn:i}={}){const r=Date.now(),o=h(n,t,{expiresIn:i});return await E.doc(e).update({last_login_ip:this._clientInfo.clientIP,last_login_date:r}),{token:o,tokenExpired:r+1e3*i}}async function T({uid:e,role:n,permission:i}={}){const{tokenSecret:r,tokenExpiresIn:o,tokenExpiresThreshold:c}=this._getConfig();if(!e)throw{errCode:t.PARAM_REQUIRED,errMsgValue:{param:"uid"}};if(c>o)throw new Error("Config error, tokenExpiresThreshold should be less than tokenExpiresIn");if(!n||!i){const t=(await E.doc(e).get()).data[0],r=await w(t);n=r.role,i=r.permission}if(!this.interceptorMap.has("customToken"))return m.call(this,{uid:e,signContent:{uid:e,role:n,permission:i},tokenSecret:r,tokenExpiresIn:o});const s=this.interceptorMap.get("customToken");if("function"==typeof s)throw new Error("Invalid custom token file");const a=await s({uid:e,role:n,permission:i});return m.call(this,{uid:e,signContent:a,tokenSecret:r,tokenExpiresIn:o})}var x=Object.freeze({__proto__:null,checkToken:async function(e,{autoRefresh:n=!0}={}){const t=await _.call(this,e),{tokenExpiresThreshold:i}=this._getConfig();if(!i||!n)return{code:0,errCode:0,...t};const r=Date.now();let o={};return 1e3*t.exp-r<1e3*i&&(o=await k.call(this,t)),{code:0,errCode:0,...t,...o}},createToken:async function({uid:e,role:n,permission:t}={}){return T.call(this,{uid:e,role:n,permission:t})},refreshToken:async function({token:e}={}){const n=await _.call(this,e);return{errCode:0,...await k.call(this,n)}}});const A=require("uni-config-center")({pluginId:"uni-id"});class O{constructor({context:e,clientInfo:n,config:t}={}){this._clientInfo=e?function(e){return{appId:e.APPID,platform:e.PLATFORM,locale:e.LOCALE,clientIP:e.CLIENTIP,deviceId:e.DEVICEID}}(e):n,this.config=t||this._getOriginConfig(),this.interceptorMap=new Map,A.hasFile("custom-token.js")&&this.setInterceptor("customToken",require(A.resolve("custom-token.js"))),this._i18n=uniCloud.initI18n({locale:this._clientInfo.locale,fallbackLocale:"zh-Hans",messages:a})}_t(...e){return this._i18n.t(...e)}_getOriginConfig(){if(A.hasFile("config.json")){let e;try{e=A.config()}catch(e){throw new Error("Invalid uni-id config file\n"+e.messages)}return Array.isArray(e)?e:e[0]?Object.values(e):e}throw new Error("Invalid uni-id config file")}_getAppConfig(){const e=this._getOriginConfig();return Array.isArray(e)?e.find(e=>e.dcloudAppid===this.clientInfo.appId)||e.find(e=>e.isDefaultConfig):e}_getPlatformConfig(){const e=this._getAppConfig();"app-plus"===this._clientInfo.platform&&(this._clientInfo.platform="app"),"h5"===this._clientInfo.platform&&(this._clientInfo.platform="web");const n=Object.assign({tokenExpiresIn:7200,tokenExpiresThreshold:1200,passwordErrorLimit:6,passwordErrorRetryTime:3600},e,e[this._clientInfo.platform]);return["tokenSecret","tokenExpiresIn"].forEach(e=>{if(!n||!n[e])throw new Error(`Config parameter missing, ${e} is required`)}),n}_getConfig(){return this._getPlatformConfig()}}for(const e in x)O.prototype[e]=x[e];function y(e){const n=new O(e);return new Proxy(n,{get(e,n){if(n in e&&0!==n.indexOf("_")){if("function"==typeof e[n])return(t=e[n],function(){let e;try{e=t.apply(this,arguments)}catch(e){if(c(e))return o.call(this,e),e;throw e}return i(e)?e.then(e=>(c(e)&&o.call(this,e),e),e=>{if(c(e))return o.call(this,e),e;throw e}):(c(e)&&o.call(this,e),e)}).bind(e);if("context"!==n&&"config"!==n)return e[n]}var t}})}O.prototype.createInstance=y;const N={createInstance:y};module.exports=N;
{
"name": "uni-id-common",
"version": "1.0.0",
"version": "1.0.2",
"description": "uni-id token生成、校验、刷新",
"main": "index.js",
"homepage": "https://uniapp.dcloud.io/uniCloud/uni-id-common.html",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册