/** * SSO / OAuth2 相关常量 * * 集中维护 client_id、state 存储 key 等跨文件复用的字符串,避免魔法字符串散落。 */ /** 本平台(物联运维)在芋道后端登记的 OAuth2 客户端编号 */ export const IOT_CLIENT_ID = 'iot'; /** 业务平台在芋道后端登记的 OAuth2 客户端编号 */ export const BIZ_CLIENT_ID = 'biz'; /** 跳业务平台发起 OAuth2 授权时,state 写入 sessionStorage 的 key */ export const SSO_STATE_STORAGE_KEY = 'sso:biz:state'; /** 本平台自身 SSO 回调页面路径 */ export const SSO_CALLBACK_PATH = '/sso-callback'; /** * 生成一个密码学安全的随机 state。 * 首选 crypto.randomUUID;低版本浏览器回退 getRandomValues。 */ export function generateOauthState(): string { if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') { return crypto.randomUUID(); } if (typeof crypto !== 'undefined' && crypto.getRandomValues) { const arr = new Uint8Array(16); crypto.getRandomValues(arr); return Array.from(arr, (b) => b.toString(16).padStart(2, '0')).join(''); } // 极端降级:不会加密但比 Math.random 多一层时间戳 return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`; }