import{i as e}from"./headers-CJ8zthei.js";import{env as t}from"cloudflare:workers";var n=`gapido_admin_session`;function r(){return t}function i(){return(r().ADMIN_PATH||`admin`).trim().replace(/^\/+|\/+$/g,``).replace(/[^a-zA-Z0-9_-]/g,``)}function a(){return r().ADMIN_USERNAME||``}function o(e){return Array.from(new Uint8Array(e)).map(e=>e.toString(16).padStart(2,`0`)).join(``)}async function s(e){return o(await crypto.subtle.digest(`SHA-256`,new TextEncoder().encode(e)))}function c(e){if(!/^[a-f0-9]+$/i.test(e)||e.length%2)throw Error(`Invalid hex`);return Uint8Array.from(e.match(/.{2}/g)||[],e=>parseInt(e,16))}async function l(e,t){let[n,r,i,a]=t.split(`:`),o=Number(r);if(n!==`pbkdf2-sha256`||!Number.isSafeInteger(o)||o<21e4||!i||!a)return!1;try{let t=c(a),n=await crypto.subtle.importKey(`raw`,new TextEncoder().encode(e),`PBKDF2`,!1,[`deriveBits`]),r=new Uint8Array(await crypto.subtle.deriveBits({name:`PBKDF2`,hash:`SHA-256`,salt:c(i),iterations:o},n,t.byteLength*8)),s=r.byteLength^t.byteLength;for(let e=0;e<r.byteLength;e++)s|=r[e]^t[e];return s===0}catch{return!1}}function u(){return r().ADMIN_SESSION_SECRET||``}async function d(e){let t=await crypto.subtle.importKey(`raw`,new TextEncoder().encode(u()),{name:`HMAC`,hash:`SHA-256`},!1,[`sign`]);return o(await crypto.subtle.sign(`HMAC`,t,new TextEncoder().encode(e)))}function f(e,t){if(e.length!==t.length)return!1;let n=0;for(let r=0;r<e.length;r++)n|=e.charCodeAt(r)^t.charCodeAt(r);return n===0}async function p(e,t){let n=a(),i=r().ADMIN_PASSWORD_HASH||``;return!n||!i||!u()?!1:f(await s(e),await s(n))&&await l(t,i)}async function m(){let t=a();if(!t||!u())throw Error(`Admin environment is incomplete`);let r=`${t}.${Date.now()+1e3*60*60*12}`,i=`${r}.${await d(r)}`;(await e()).set(n,i,{httpOnly:!0,secure:!0,sameSite:`strict`,path:`/`,maxAge:3600*12})}async function h(){(await e()).set(n,``,{httpOnly:!0,secure:!0,sameSite:`strict`,path:`/`,maxAge:0})}async function g(){let t=(await e()).get(n)?.value;if(!t)return!1;let r=t.split(`.`);if(r.length!==3)return!1;let[i,o,s]=r;return!u()||i!==a()||Number(o)<Date.now()?!1:f(s,await d(`${i}.${o}`))}export{g as a,a as i,m as n,p as o,i as r,h as t};