'use strict';Object.defineProperty(exports,'__esModule',{value:!0});conste=require('crypto');constt=require('path');functions(e){returne&&typeofe==='object'&&'default'ine?e:{default:e}}require('fs');consto=s(e);constn=s(t);consti='uni-cloud-s2s';constr={code:5e4,message:'Config error'};constc={code:51e3,message:'Access denied'};classaextendsError{constructor(e){super(e.message),this.errMsg=e.message||'',this.code=this.errCode=e.code,this.errSubject=e.subject,this.forceReturn=e.forceReturn||!1,this.cause=e.cause,Object.defineProperties(this,{message:{get(){returnthis.errMsg},set(e){this.errMsg=e}}})}toJSON(e=0){if(!(e>=10))returne++,{errCode:this.errCode,errMsg:this.errMsg,errSubject:this.errSubject,cause:this.cause&&this.cause.toJSON?this.cause.toJSON(e):this.cause}}}constd=Object.prototype.toString;consth=50002;constu=Object.create(null);['string','boolean','number','null'].forEach(e=>{u[e]=function(t,s){if((function(e){returnd.call(e).slice(8,-1).toLowerCase()}(t))!==e)return{code:h,message:`${s} is invalid`}}});constf='Unicloud-S2s-Authorization';classg{constructor(e){const{config:t}=e||{};this.config=t;const{connectCode:s}=t||{};if(this.connectCode=s,!s||typeofs!=='string')thrownewa({subject:i,code:r.code,message:'Invalid connectCode in config'})}getHeadersValue(e={},t,s){consto=Object.keys(e||{}).find(e=>e.toLowerCase()===t.toLowerCase());returno?e[o]:s}verifyHttpInfo(e){constt=this.getHeadersValue(e.headers,f,'');const[s='',o='']=t.split('');if(s.toLowerCase()==='CONNECTCODE'.toLowerCase()&&o===this.config.connectCode)return!0;thrownewa({subject:i,code:c.code,message:`Invalid CONNECTCODE in headers['${f}']`})}getSecureHeaders(e){return{[f]:`CONNECTCODE ${this.config.connectCode}`}}}functionl(e){returnfunction(t){const{content:s,signKey:n}=t||{};returno.default.createHash(e).update(s+'\n'+n).digest('hex')}}constp={md5:l('md5'),sha1:l('sha1'),sha256:l('md5'),'hmac-sha256':function(e){const{content:t,signKey:s}=e||{};returno.default.createHmac('sha256',s).update(t).digest('hex')}};functionm(e){const{timestamp:t,data:s={},signKey:o,hashMethod:n='hmac-sha256'}=e||{};consti=p[n];constr=['number','string','boolean'];constc=Object.keys(s).sort();consta=[];for(lete=0;e<c.length;e++){constt=c[e];consto=s[t];constn=typeofo;r.includes(n)&&a.push(`${t}=${o}`)}returni({content:`${t}\n${a.join('&')}`,signKey:o})}classw{constructor(e){const{config:t}=e||{};this.config=t;const{signKey:s,hashMethod:o='hmac-sha256',timeDiffTolerance:n=60}=t;if(!p[o])thrownewa({subject:i,code:r.code,message:`Invalid hashMethod in config, expected "md5", "sha1", "sha256" or "hmac-sha256", got "${o}"`});if(!s||typeofs!=='string')thrownewa({subject:i,code:r.code,message:'Invalid signKey in config'});this.signKey=s,this.hashMethod=o,this.timeDiffTolerance=n}getHttpHeaders(e){returne.headers||{}}getHeadersValue(e,t,s){consto=Object.keys(e||{}).find(e=>e.toLowerCase()===t.toLowerCase());returno?e[o]:s}getHttpData(e){constt=e.httpMethod.toLowerCase();consts=this.getHttpHeaders(e);consto=this.getHeadersValue(s,'Content-Type','');if(t==='get')returne.queryStringParameters;if(t!=='post')thrownewa({subject:i,code:c.code,message:`Invalid http method, expected "POST" or "get", got "${t}"`});if(o.indexOf('application/json')===0)returnJSON.parse(e.body);if(o.indexOf('application/x-www-form-urlencoded')===0)returnrequire('querystring').parse(e.body);thrownewa({subject:i,code:c.code,message:`Invalid content type of POST method, expected "application/json" or "application/x-www-form-urlencoded", got "${o}"`})}verifyHttpInfo(e){constt=e.headers||{};consts=this.getHeadersValue(t,'Unicloud-S2s-Timestamp','0');let[o,n]=this.getHeadersValue(t,'Unicloud-S2s-Signature','').split('');if(o=o.toLowerCase(),o!==this.hashMethod)thrownewa({subject:i,code:c.code,message:`Invalid hash method, expected "${this.hashMethod}", got "${o}"`});constr=parseInt(s);constd=Date.now();if(Math.abs(d-r)>1e3*this.timeDiffTolerance)thrownewa({subject:i,code:c.code,message:`Invalid timestamp, server timestamp is ${d}, ${r} exceed max timeDiffTolerance(${this.timeDiffTolerance} seconds)`});returnm({timestamp:r,data:this.getHttpData(e),signKey:this.signKey,hashMethod:this.hashMethod})===n}getSecureHeaders(e){const{data:t}=e||{};consts=Date.now();consto=m({timestamp:s,data:t,signKey:this.signKey,hashMethod:this.hashMethod});return{'Unicloud-S2s-Timestamp':s+'','Unicloud-S2s-Signature':this.hashMethod+''+o}}}consty=require('uni-config-center')({pluginId:i});classb{constructor(){this.config=y.config();conste=n.default.resolve(require.resolve('uni-config-center'),i,'config.json');if(!this.config)thrownewa({subject:i,code:r.code,message:`${i} config required, please check your config file: ${e}`});if(this.config.type==='connectCode')this.verifier=newg({config:this.config});else{if(!(function(e){returne.type==='sign'}(this.config)))thrownewa({subject:i,code:r.code,message:`Invalid ${i} config, expected policy is "code" or "sign", got ${this.config.policy}`});this.verifier=neww({config:this.config})}}verifyHttpInfo(e){if(!e)thrownewa({subject:i,code:c.code,message:'Access denied, httpInfo required'});returnthis.verifier.verifyHttpInfo(e)}getSecureHeaders(e){returnthis.verifier.getSecureHeaders(e)}}exports.getSecureHeaders=function(e){return(newb()).getSecureHeaders(e)},exports.verifyHttpInfo=function(e){constt=(newb()).verifyHttpInfo(e);if(!t)thrownewa({subject:i,code:c.code,message:c.message});returnt}
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});vare=require("crypto"),t=require("path");functions(e){returne&&"object"==typeofe&&"default"ine?e:{default:e}}require("fs");varo=s(e),n=s(t);consti="uni-cloud-s2s",r={code:5e4,message:"Config error"},c={code:51e3,message:"Access denied"};classaextendsError{constructor(e){super(e.message),this.errMsg=e.message||"",this.code=this.errCode=e.code,this.errSubject=e.subject,this.forceReturn=e.forceReturn||!1,this.cause=e.cause,Object.defineProperties(this,{message:{get(){returnthis.errMsg},set(e){this.errMsg=e}}})}toJSON(e=0){if(!(e>=10))returne++,{errCode:this.errCode,errMsg:this.errMsg,errSubject:this.errSubject,cause:this.cause&&this.cause.toJSON?this.cause.toJSON(e):this.cause}}}constd=Object.prototype.toString;consth=50002,u=Object.create(null);["string","boolean","number","null"].forEach((e=>{u[e]=function(t,s){if(function(e){returnd.call(e).slice(8,-1).toLowerCase()}(t)!==e)return{code:h,message:`${s} is invalid`}}}));constf="Unicloud-S2s-Authorization";classg{constructor(e){const{config:t}=e||{};this.config=t;const{connectCode:s}=t||{};if(this.connectCode=s,!s||"string"!=typeofs)thrownewa({subject:i,code:r.code,message:"Invalid connectCode in config"})}getHeadersValue(e={},t,s){consto=Object.keys(e||{}).find((e=>e.toLowerCase()===t.toLowerCase()));returno?e[o]:s}verifyHttpInfo(e){constt=this.getHeadersValue(e.headers,f,""),[s="",o=""]=t.split("");if(s.toLowerCase()==="CONNECTCODE".toLowerCase()&&o===this.config.connectCode)return!0;thrownewa({subject:i,code:c.code,message:`Invalid CONNECTCODE in headers['${f}']`})}getSecureHeaders(e){return{[f]:`CONNECTCODE ${this.config.connectCode}`}}}functionl(e){returnfunction(t){const{content:s,signKey:n}=t||{};returno.default.createHash(e).update(s+"\n"+n).digest("hex")}}constp={md5:l("md5"),sha1:l("sha1"),sha256:l("md5"),"hmac-sha256":function(e){const{content:t,signKey:s}=e||{};returno.default.createHmac("sha256",s).update(t).digest("hex")}};functionm(e){const{timestamp:t,data:s={},signKey:o,hashMethod:n="hmac-sha256"}=e||{},i=p[n],r=["number","string","boolean"],c=Object.keys(s).sort(),a=[];for(lete=0;e<c.length;e++){constt=c[e],o=s[t],n=typeofo;r.includes(n)&&a.push(`${t}=${o}`)}returni({content:`${t}\n${a.join("&")}`,signKey:o})}classw{constructor(e){const{config:t}=e||{};this.config=t;const{signKey:s,hashMethod:o="hmac-sha256",timeDiffTolerance:n=60}=t;if(!p[o])thrownewa({subject:i,code:r.code,message:`Invalid hashMethod in config, expected "md5", "sha1", "sha256" or "hmac-sha256", got "${o}"`});if(!s||"string"!=typeofs)thrownewa({subject:i,code:r.code,message:"Invalid signKey in config"});this.signKey=s,this.hashMethod=o,this.timeDiffTolerance=n}getHttpHeaders(e){returne.headers||{}}getHeadersValue(e,t,s){consto=Object.keys(e||{}).find((e=>e.toLowerCase()===t.toLowerCase()));returno?e[o]:s}getHttpData(e){constt=e.httpMethod.toLowerCase(),s=this.getHttpHeaders(e),o=this.getHeadersValue(s,"Content-Type","");if("get"===t)returne.queryStringParameters;if("post"!==t)thrownewa({subject:i,code:c.code,message:`Invalid http method, expected "POST" or "get", got "${t}"`});if(0===o.indexOf("application/json"))returnJSON.parse(e.body);if(0===o.indexOf("application/x-www-form-urlencoded"))returnrequire("querystring").parse(e.body);thrownewa({subject:i,code:c.code,message:`Invalid content type of POST method, expected "application/json" or "application/x-www-form-urlencoded", got "${o}"`})}verifyHttpInfo(e){constt=e.headers||{},s=this.getHeadersValue(t,"Unicloud-S2s-Timestamp","0");let[o,n]=this.getHeadersValue(t,"Unicloud-S2s-Signature","").split("");if(o=o.toLowerCase(),o!==this.hashMethod)thrownewa({subject:i,code:c.code,message:`Invalid hash method, expected "${this.hashMethod}", got "${o}"`});constr=parseInt(s),d=Date.now();if(Math.abs(d-r)>1e3*this.timeDiffTolerance)thrownewa({subject:i,code:c.code,message:`Invalid timestamp, server timestamp is ${d}, ${r} exceed max timeDiffTolerance(${this.timeDiffTolerance} seconds)`});returnm({timestamp:r,data:this.getHttpData(e),signKey:this.signKey,hashMethod:this.hashMethod})===n}getSecureHeaders(e){const{data:t}=e||{},s=Date.now(),o=m({timestamp:s,data:t,signKey:this.signKey,hashMethod:this.hashMethod});return{"Unicloud-S2s-Timestamp":s+"","Unicloud-S2s-Signature":this.hashMethod+""+o}}}consty=require("uni-config-center")({pluginId:i});classb{constructor(){this.config=y.config();conste=n.default.resolve(require.resolve("uni-config-center"),i,"config.json");if(!this.config)thrownewa({subject:i,code:r.code,message:`${i} config required, please check your config file: ${e}`});if("connectCode"===this.config.type)this.verifier=newg({config:this.config});else{if(!function(e){return"sign"===e.type}(this.config))thrownewa({subject:i,code:r.code,message:`Invalid ${i} config, expected policy is "code" or "sign", got ${this.config.policy}`});this.verifier=neww({config:this.config})}}verifyHttpInfo(e){if(!e)thrownewa({subject:i,code:c.code,message:"Access denied, httpInfo required"});returnthis.verifier.verifyHttpInfo(e)}getSecureHeaders(e){returnthis.verifier.getSecureHeaders(e)}}exports.getSecureHeaders=function(e){return(newb).getSecureHeaders(e)},exports.verifyHttpInfo=function(e){constt=(newb).verifyHttpInfo(e);if(!t)thrownewa({subject:i,code:c.code,message:c.message});returnt};