PageSourceSearch

https://qkdreyer.dev/scripts/synocredential.js/synocredential.min.js?v=1712744176

js qkdreyer.dev collected 2026-09-25 22:34:54 UTC 14,817 bytes, 1 lines download raw bytes

1var synocredential=function(e){"use strict";e=e&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e;var t=null;function i(){return new Promise(e=>{synowebapi.promises.request({api:"SYNO.Remote.Credential.Challenge",method:"get",version:1}).then(t=>t&&t.challenge?e([void 0,t.challenge]):e([t]))})}class o{constructor(e={}){this.opt=e}makeid(e){for(var t="",i="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",o=i.length,s=0;s<e;s++)t+=i.charAt(Math.floor(Math.random()*o));return t}async execute(e){return this.login(Object.assign({challenge_getter:i},e))}async login(e={}){Object.assign(this.opt,e);let i=e=>{this.opt.callback(e,this.opt)},o=this.opt.url,s=this.opt.scope,n=this.opt.audience,r=this.opt.redirect_uri||window.origin,a=this.opt.client_id||"webui",c=this.opt.session||"webui",h=this.opt.forceLogin,l={capture:!1};if(!o||""===o)return i({message:"no request url."}),this.cancel();if(null!=t&&!t.closed)return void t.focus();if(!(t=window.open("about:blank","childWindow","width=600,height=800,toolbar=no,menubar=no")))return i({message:"open child window failed."}),this.cancel();const[u,d]=await this.opt.challenge_getter();if(u||!d||""===d)return i({message:"challenge_getter execute failed."}),this.cancel();var g,f;f=e=>{let t,s,n;try{t=new URL(o),s=new URL(r),n=new URL(e.origin)}catch{return void i({code:1002,mssage:"some of url, redirect_uri, event.origin  is invalid URL."})}n.hostname===s.hostname||n.hostname===t.hostname?e.target.origin.toUpperCase()===window.origin.toUpperCase()&&(clearInterval(g),window.removeEventListener("message",f,l),e.data&&e.data.code?e.data.rs?i({code:e.data.code,challenge:d,rs:e.data.rs}):i({code:e.data.code,challenge:d}):i({message:"request code failed."})):SYNO.Debug("PKCEClient","hostname not match",t.hostname,s.hostname,originUrl.hostname)},o+="?forceDesktop=1",o+="&client_id="+a,o+="&code_challenge="+d,o+="&code_challenge_method=S256",o+="&response_type=code",o+="&opener="+encodeURIComponent(window.origin),o+="&redirect_uri="+encodeURIComponent(r),o+="&state="+this.makeid(10),s&&(o+="&scope="+encodeURIComponent(s)),n&&(o+="&audience="+encodeURIComponent(n)),c&&(o+="&session="+c),h&&(o+="&force_login="+h),window.addEventListener("message",f,l),t.location=o,g=setInterval(()=>{t&&t.closed&&(clearInterval(g),window.removeEventListener("message",f,l),i({closed:!0}))},500)}logout(e){let t=this.opt.end_session_url+"?post_logout_redirect_uri="+this.opt.logout_redirect_uri+"&id_token_hint="+e;window.open(t,"childWindow","width=600,height=800,toolbar=no,menubar=no")}cancel(){t&&!t.closed&&t.close()}}const{createHash:s,randomBytes:n}=e;function r(e){return function(e,t){let i="";const o=n(e),s=Math.pow(2,8),r=s/Math.min(t.length,s);for(let s=0;s<e;s++){i+=t[Math.floor(o[s]/r)]}return i}(e,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~")}function a(e){const t=s("sha256").update(e).digest("base64");return t.replace(/=/g,"").replace(/\+/g,"-").replace(/\//g,"_")}var c=function(e){if(e||(e=43),e<43||e>128)throw`Expected a length between 43 and 128. Received ${e}.`;const t=r(e);return{code_challenge:a(t),code_verifier:t}};var h=function(e,t){return a(e)===t};c.verifyChallenge=h;const l="function"==typeof atob,u="function"==typeof btoa,d="function"==typeof Buffer,g=("function"==typeof TextDecoder&&new TextDecoder,"function"==typeof TextEncoder?new TextEncoder:void 0),f=[..."ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="],p=(e=>{let t={};return e.forEach((e,i)=>t[e]=i),t})(f),m=/^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/,_=String.fromCharCode.bind(String),k="function"==typeof Uint8Array.from?Uint8Array.from.bind(Uint8Array):(e,t=(e=>e))=>new Uint8Array(Array.prototype.slice.call(e,0).map(t)),w=e=>e.replace(/[+\/]/g,e=>"+"==e?"-":"_").replace(/=+$/m,""),y=e=>e.replace(/[^A-Za-z0-9\+\/]/g,""),b=e=>{let t,i,o,s,n="";const r=e.length%3;for(let r=0;r<e.length;){if((i=e.charCodeAt(r++))>255||(o=e.charCodeAt(r++))>255||(s=e.charCodeAt(r++))>255)throw new TypeError("invalid character found");t=i<<16|o<<8|s,n+=f[t>>18&63]+f[t>>12&63]+f[t>>6&63]+f[63&t]}return r?n.slice(0,r-3)+"===".substring(r):n},S=u?e=>btoa(e):d?e=>Buffer.from(e,"binary").toString("base64"):b,v=d?e=>
1Buffer.from(e).toString("base64"):e=>{let t=[];for(let i=0,o=e.length;i<o;i+=4096)t.push(_.apply(null,e.subarray(i,i+4096)));return S(t.join(""))},O=(e,t=!1)=>t?w(v(e)):v(e),A=e=>{if(e.length<2)return(t=e.charCodeAt(0))<128?e:t<2048?_(192|t>>>6)+_(128|63&t):_(224|t>>>12&15)+_(128|t>>>6&63)+_(128|63&t);var t=65536+1024*(e.charCodeAt(0)-55296)+(e.charCodeAt(1)-56320);return _(240|t>>>18&7)+_(128|t>>>12&63)+_(128|t>>>6&63)+_(128|63&t)},R=/[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g,C=e=>e.replace(R,A),N=d?e=>Buffer.from(e,"utf8").toString("base64"):g?e=>v(g.encode(e)):e=>S(C(e)),P=(e,t=!1)=>t?w(N(e)):N(e),I=e=>{if(e=e.replace(/\s+/g,""),!m.test(e))throw new TypeError("malformed base64.");e+="==".slice(2-(3&e.length));let t,i,o,s="";for(let n=0;n<e.length;)t=p[e.charAt(n++)]<<18|p[e.charAt(n++)]<<12|(i=p[e.charAt(n++)])<<6|(o=p[e.charAt(n++)]),s+=64===i?_(t>>16&255):64===o?_(t>>16&255,t>>8&255):_(t>>16&255,t>>8&255,255&t);return s},E=l?e=>atob(y(e)):d?e=>Buffer.from(e,"base64").toString("binary"):I,T=d?e=>k(Buffer.from(e,"base64")):e=>k(E(e),e=>e.charCodeAt(0)),U=e=>T(L(e)),L=e=>y(e.replace(/[-_]/g,e=>"-"==e?"+":"/")),D=P,x=O,M=U;class H{constructor(e){return this.__noise_c=e,this}init(e,t,i,o){var s=Array();if("initiator"===t&&!i)return this.key_pair=s,!1;let n=this.__noise_c;if(o)s.push(o);else{let t;if("Noise_IK_25519_ChaChaPoly_BLAKE2b"===e&&(t=n.CreateKeyPair(n.constants.NOISE_DH_CURVE25519)),!t)return!1;s=t}return t="initiator"===t?n.constants.NOISE_ROLE_INITIATOR:n.constants.NOISE_ROLE_RESPONDER,this._handshake_state=n.HandshakeState(e,t),this._handshake_state.Initialize(null,s[0],i,null),this.key_pair=s,!0}writeMessage(e=""){var t=this._handshake_state;if(t)return t.WriteMessage(e)}readMessage(e){var t=this._handshake_state;if(t)return t.ReadMessage(e,!0),t.GetRemotePublicKey()}split(){var e=this._handshake_state;if(e){var t=e.Split();return t[0].n=0,t[1].n=0,this.__cipher_states=t,t}}encrypt(e,t){var i=this.__cipher_states;if(i)return i[0].EncryptWithAd(e,t)}getNonceNum(){var e=this.__cipher_states;if(e)return e[0].n++}}class Y{constructor(e,t={}){return Y._instance||(this.__noise_c=e,this.running=!1,this.opt={},this.info=Object.assign({headers:{},hsh:"",account:"",lhstime:Date.now(),authtok:""},t),Y._instance=this),Y._instance}issue(e={}){var t=e.callback;if("function"!=typeof t)return t({code:1e3,message:"no callback"});var i=e.url;return"string"!=typeof i?t({code:1e3,message:"no url"}):this.running?t({code:1e3,message:"already running."}):(this.running=!0,Object.assign(this.opt,e),this.runner=new o,void this.runner.login({url:i,challenge_getter:e.challenge_getter,forceLogin:"yes",callback:this.onLoginCallback.bind(this)}))}onLoginCallback(e,t){let i=this.opt.callback;this.running=!1,e&&e.challenge&&e.code&&e.rs?i({url:t.url,challenge:e.challenge,code:e.code,rs:e.rs}):e&&e.message?i({code:1002,mssage:e.message}):e&&e.closed?i({code:1001}):i({code:1002,message:"Unknow"})}CreateChallenge(){return this._challenge=c(),this._challenge.code_challenge}GetLoginParams(e,t){var i=Object.assign({api:"SYNO.API.Auth",version:6,method:"login",session:"webui"},e);if(!this.__noise_c)return i;if(this.noise=new H(this.__noise_c),!this.info.authtok)try{this.info.authtok=JSON.parse(localStorage.getItem("lastHS")).authtok}catch{}let o=""!==this.info.authtok?M(this.info.authtok):void 0,s=M(t);if(!this.noise.init("Noise_IK_25519_ChaChaPoly_BLAKE2b","initiator",s,o))return i;let n=JSON.stringify({time:Math.floor(Date.now()/1e3)});return i.ik_message=x(this.noise.writeMessage(n),!0),i.version=7,e.code&&(i=Object.assign({type:"code",grant_type:"authorzation_code",code_verifier:this._challenge?this._challenge.code_verifier:"",client_id:""},i)),i}SetLoginResult(e){return this.info.cookie&&(e.sid&&(this.info.cookie.id=e.sid),e.device_id&&(this.info.cookie.did=e.device_id)),e.synotoken&&this.UpdateToken(e.synotoken),this.noise&&e.account&&e.ik_message&&this.noise.readMessage(M(e.ik_message))&&(this.info.hsh=x(this.noise._handshake_state.GetHandshakeHash(),!0),this.noise.split(),localStorage.setItem("lastHS",JSON.stringify({authtok:this.info.authtok=x(this.noise.key_pair[0],!0),account:this.info.account=e.account,lhstime:this.info.lhstime}))),this.info}GetLogoutParams(e){return Object.assign({api:"SYNO.API.Auth",method:"logout",version:7},e)}SetLogoutResult(){return delete this.noise,this.info.cookie&&delete this.info.cookie.id,delete this.info.headers["X-SYNO-TOKEN"],this.info.account="",this.info.authtok="",this.info}GetSuspendParams(e){return Object.assign({api:"SYNO.API.Auth",method:"suspend",version:7},e)}SetSuspendResult(){return delete this.noise,this.info.cookie&&delete this.info.cookie.id,delete this.info.headers["X-SYNO-TOKEN"],this.info}GetResumeParams(e,t){if(!this.__noise_c||!t)return;if(!this.info.account||!this.info.authtok)return;var i=localStorage.getItem("lastHS");if(i){var o=JSON.parse(i);o.account===this.info.account&&o.authtok!==this.info.authtok&&o.lhstime>this.info.lhstime&&(this.info.authtok=o.authtok,this.info.lhstime=o.lhstime)}let s=M(this.info.authtok),n=M(t);if(this.noise=new H(this.__noise_c),!this.noise.init("Noise_KK_25519_ChaChaPoly_BLAKE2b","initiator",n,s))return;let r=JSON.stringify({time:Math.floor(Date.now()/1e3)});return Object.assign({api:"SYNO.API.Auth",version:7,method:"resume",session:"webui",account:this.info.account,kk_message:x(this.noise.writeMessage(r),!0)},e)}SetResumeResult(e){return this.info.cookie&&e.sid&&(this.info.cookie.id=e.sid),e.synotoken&&this.UpdateToken(e.synotoken),this.noise&&e.kk_message&&this.noise.readMessage(M(e.kk_message))&&(this.info.hsh=x(this.noise._handshake_state.GetHandshakeHash(),!0),this.noise.split(),this.info.authtok=x(this.noise.key_pair[0],!0)),this.info}UpdateToken(e){const t=encodeURIComponent(e);this.info.headers["X-SYNO-TOKEN"]=t,SYNO.SDS.Session.SynoToken=t,synowebapi&&synowebapi.env.setSynoToken(SYNO.SDS.Session.SynoToken)}
1GetRequestParams(){return{}}GetRequestHeaders(e={}){if(e=Object.assign(e,this.info.headers),this.info.cookie){let t=this.info.cookie;e.Cookie=Object.keys(t).map(e=>e+"="+t[e]).join("; ")}let t=this.GetRequestHash();return t&&(e["X-SYNO-HASH"]=t),e}SetAccount(e){this.info.account=e}GetAccount(){return this.inf.account}SetAuthtok(e){this.info.authtok=e}GetAuthtok(){return this.info.authtok}GetRequestHash(){if(this.noise){var e=this.noise.encrypt(new Uint8Array,new Uint8Array),t=this.noise.getNonceNum(),i=this.info.hsh.slice(0,8);return e&&void 0!==t&&i?i+x(e,!0)+"."+D(""+t,!0):""}return""}WaitForReady(){return new Promise(e=>{this.resumePromise?this.resumePromise.then(e).catch(e):e()})}GetRemoteKey(){let e=document.cookie.match(/_SSID=[^;]+/);if(e)return e[0].split("=")[1]}Resume(){return this.resumePromise?this.resumePromise:(this.resumePromise=new Promise((e,t)=>{let i=this.GetRemoteKey();if(!i)return void t({code:119,message:"make resume parameters failed."});let o=this.GetResumeParams({enable_syno_token:"yes"},i);if(!i||!o)return void t({code:119,message:"make resume parameters failed."});let s=this;synowebapi.create().request({url:"webapi/entry.cgi?api=SYNO.API.Auth",requestFormat:"raw",responseFormat:"raw",method:"POST",params:o,callback:function(i,o){i&&o.data&&o.data.kk_message?(s.SetResumeResult(o.data),e()):t({code:119,message:"resume noise failed."})}})}),this.resumePromise.finally(()=>this.resumePromise=void 0))}}class G{async check(e){let t=e.forceLegacyMode||!1,i=e.url,o=e.appWin;return!0===t||!await this.isRemoteSupport(i)||!await this.isRemoteVisible(i,o)}issue(e){let t=e.appWin,i=e.callback,o=!1;SYNO.SDS.JSLoad("SYNO.SDS.Utils.AccountPasswordDialog",()=>{let e=SYNO.SDS.Utils.AccountPasswordDialog.openDialog(t,(e,t)=>{if(t&&e&&(""===e.account||""===e.passwd))return void t(!1,{text:_T("login","error_cantlogin"),clear:!0});let s;t||e||(o=!0),t&&(s=(e,i)=>{e&&(o=!0),t(e,i)}),i(e,s)});e.mon(e,"beforeclose",()=>{o||i()})},this)}isRemoteSupport(e){return new Promise((t,i)=>{synowebapi.request({api:"SYNO.Remote.Credential.Info",method:"get",version:1,params:{url:e},callback:(e,o)=>{e&&o?t(o.support_code_login):i(1002)}})})}isRemoteVisible(e,t){return new Promise((function(i,o){var s=new XMLHttpRequest;s.open("GET",e+"/webman/pingpong.cgi?action=cors"),s.onload=()=>{200==s.status?i(!0):i(!1,1002)},s.timeout=2e3,s.onerror=()=>{if(e.startsWith("https")){var o=String.format(_T("login","remote_network_error"),e),s=_T("login","btn_use_weblogin"),n=_T("login","btn_use_passwd");t&&t._isVue?(async()=>{i("confirm"===await t.getMsgBox().confirm(null,o,{confirm:{text:s},cancel:{text:n}}))})():t?t.getMsgBox().confirm("",o,e=>{i("yes"===e)},this,{yes:{text:s},no:{text:n}}):i(!0)}else i(!1)},s.ontimeout=s.onerror,s.send()}))}}return Y.PKCEClient=o,Y.Issue=async function(e){let t=new G,o=e.callback,s=e.url,n={};if(s)try{n=new URL(s)}catch{return void o({code:1002,mssage:"url is invalid."})}else if(!e.mustHaveCredential)return void o({code:1002,mssage:"url is empty"});this.options=Object.assign({},e,{callback:((i,s)=>{if(i&&(i.challenge&&i.code&&i.rs||i.account&&i.passwd)){let r={protocol:"http:"===n.protocol?"http":"https",host:n.hostname||"",port:n.port||("http:"===n.protocol?"80":"443"),application:e.session};i.code?(r.code=i.code,r.rs=i.rs,r.challenge=i.challenge):(r.account=i.account,r.passwd=i.passwd,r.otp=i.otp),synowebapi.request({api:"SYNO.Remote.Credential",method:"set",version:1,params:r,callback:(e,i)=>{if(e&&i&&i.credential)s&&s(!0),o(i.credential);else if(1004===i.code&&s)i.errors&&403===i.errors.code?s(!1,{status:"need_otp"}):i.errors&&404===i.errors.code?s(!1,{text:_T("login","error_otp_failed"),clear:!0}):s(!1,{text:_T("login","error_cantlogin"),clear:!0});else if(1004===i.code){r.code&&i&&i.errors&&i.errors.code&&400===i.errors.code&&this.options.appWin||o({code:1004,auth_error:i.errors});let e=this,s=this.options.appWin;new Promise((e,t)=>{let i=_T("login","is_weblogin_failback_password");s._isVue?(async()=>{"confirm"===await s.getMsgBox().confirm(null,i)?e():t()})():s.getMsgBox().confirm("",i,i=>{"yes"===i?e():t()})}).then(()=>{e.running=!0,t.issue(e.options)},()=>{o({code:1004,auth_error:i.errors})})}else s?s(!1,{text:_T("login","error_cantlogin"),clear:!0}):o({code:1003,success:e,data:i})}})}else i&&i.message?(s&&s(!0),o({code:1002,mssage:i.message})):(s&&s(!0),o({code:1001}))}).bind(this)});let r=!0;try{r=await t.check(this.options)}catch(t){if(s="",n={},1002===t&&!e.mustHaveCredential)return void o({code:1002,mssage:"server connect failed."})}if(r)this.running=!0,t.issue(this.options);else{let e=new Y;this.options.challenge_getter=i,e.issue(this.options)}},Y}(crypto);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.