配置URL鉴权

本文为您介绍边缘安全加速 ESA边缘函数的函数模板中的三种鉴权方式的实现规范,涵盖签名算法、URL格式、字段定义及服务端校验逻辑,适用于开发者在文件访问、API请求等场景中集成安全鉴权能力。文档通过代码示例、参数说明,系统化指导鉴权流程的配置与调试。

鉴权效果

阻止鉴权失败的URL访问您的资源,保护您的源站资源。

鉴权方式

A、B、C三种鉴权方式通过MD5对不同字符串进行加密后,对客户端和边缘节点存储计算的MD5值进行对比,结果一致则返回请求资源,结果不一致则拒绝访问。

鉴权方式A

  • 鉴权URL格式http://DomainName/Filename?auth_key={timestamp}-{rand}-{uid}-{md5hash},组成说明参考字段说明

  • 示例代码

    import { createHash } from "node:crypto";
    
    async function handleRequest(request) {
      const url = new URL(request.url);
      const path = url.pathname;
      const delta = 3600;
    
      const authKeyTypeA = url.searchParams.get('auth_key');
    
      const privateKey = 'your_secret_key'
      const currentTimestamp = Math.floor(Date.now() / 1000);
    
      if (!authKeyTypeA) {
        return new Response('Unauthorized', { status: 401 });
      }
    
      const [timestamp, rand, uid, signature] = authKeyTypeA.split('-');
    
      if (currentTimestamp > parseInt(timestamp)+ delta) {
        return new Response('Link expired', { status: 403 });
      }
    
      const signString = [path, timestamp, rand, uid, privateKey].join('-');
    
      const md5 = createHash('md5').update(signString).digest('hex');
    
      if (md5 !== signature) {
        return new Response('Unauthorized', { status: 401 });
      }
    
      // 如果资源在其他域名,则请求后返回
      // const yourUrl = `https://your-dcdn-domain.com${path}${url.search}`
      // const cdnResponse = await fetch(yourUrl, request)
      // return new Response(cdnResponse.body, cdnResponse)
    
      // 大多情况下回源即可
      return fetch(request.url)
    }
    
    export default {
      async fetch(request) {
        return handleRequest(request)
      }
    }
  • 测试与验证

    • 通过命令行工具,测试符合可鉴权通过的测试URL:curl -I http://esa.aliyun.top/video/test.mp4?auth_key=1743388566-61b20a42d14f403ba3790d1b82502027-1-ee1c2c463a41ef4d72fcff5987f1e08c 。返回结果HTTP响应码为200,表示鉴权成功。

      image

    • 通过命令行工具,测试不符合可鉴权通过的测试URL:curl -I http://esa.aliyun.top/test.mp4。返回结果HTTP响应码为401,表示鉴权失败。

      image

鉴权方式B

  • 鉴权URL格式http://DomainName/{timestamp}/{md5hash}/FileName,组成说明参考字段说明

  • 示例代码

    import { createHash } from "node:crypto";
    
    function handleRequest(request) {
      const url = new URL(request.url);
      const path = url.pathname;
      const parts = path.split('/');
      const delta = 3600;
      const privateKey = 'your_secret_key'
      const currentTimestamp = Math.floor(Date.now() / 1000);
      const timestamp = parts[1];
      const signature = parts[2];
    
      if (!timestamp || !signature) {
        return new Response('Unauthorized', { status: 401 });
      }
    
      const filePath = '/' + parts.slice(3).join('/');
      const signString = [privateKey, timestamp, filePath].join('');
      const md5 = createHash('md5').update(signString).digest('hex');
    
      if (md5 !== signature) {
        return new Response('Unauthorized', { status: 403 });
      }
    
      if (currentTimestamp > parseInt(timestamp)+ delta) {
        return new Response('Link expired', { status: 403 });
      }
    
      // 如果资源在其他域名,则请求后返回
      // const yourUrl = `https://your-dcdn-domain.com${path}${url.search}`
      // const cdnResponse = await fetch(yourUrl, request)
      // return new Response(cdnResponse.body, cdnResponse)
    
      // B鉴权的内容在path中,需要重新拼接
      return fetch(url.origin + filePath)
    }
    
    export default {
      async fetch(request) {
        return handleRequest(request)
      }
    }
  • 测试与验证

    • 通过命令行工具,测试符合可鉴权通过的测试URL:http://esa.aliyun.top/1743391454/f1415e282ae5ae650455e7b525231eff/test.mp4。返回结果HTTP响应码为200,表示鉴权成功。

      image

    • 通过命令行工具,测试不符合可鉴权通过的测试URL:http://esa.aliyun.top/test.mp4。返回结果HTTP响应码为401,表示鉴权失败。

      image

鉴权方式C

  • 鉴权URL格式http://DomainName/{md5hash}/{timestamp}/FileName,组成说明参考字段说明

  • 示例代码

    import { createHash } from "node:crypto";
    
    function handleRequest(request) {
      const url = new URL(request.url);
      const path = url.pathname;
      const parts = path.split('/');
      const delta = 3600;
      const privateKey = 'your_secret_key'
      const currentTimestamp = Math.floor(Date.now() / 1000);
      const signature = parts[1];
      const hexTime = parts[2];
      const timestamp = parseInt(hexTime, 16);
    
      if (!hexTime || !signature) {
        return new Response('Unauthorized', { status: 401 });
      }
    
      const filePath = '/' + parts.slice(3).join('/');
      const signString = [privateKey, filePath, hexTime].join('-');
      const md5 = createHash('md5').update(signString).digest('hex');
    
      if (md5 !== signature) {
        return new Response('Unauthorized', { status: 403 });
      }
    
      if (currentTimestamp > parseInt(timestamp)+ delta) {
        return new Response('Link expired', { status: 403 });
      }
    
      // 如果资源在其他域名,则请求后返回
      // const yourUrl = `https://your-dcdn-domain.com${path}${url.search}`
      // const cdnResponse = await fetch(yourUrl, request)
      // return new Response(cdnResponse.body, cdnResponse)
    
      // C鉴权的内容在path中,需要重新拼接
      return fetch(url.origin + filePath)
    }
    
    export default {
      async fetch(request) {
        return handleRequest(request)
      }
    }
  • 测试与验证

    • 通过命令行工具,测试符合可鉴权通过的测试URL:http://esa.aliyun.top/e59c904c85f19a48413b6283fc9d2f5a/1743400480/test.mp4。返回结果HTTP响应码为200,表示鉴权成功。

      image

    • 通过命令行工具,测试不符合可鉴权通过的测试URL:http://esa.aliyun.top/test.mp4。返回结果HTTP响应码为401,表示鉴权失败。

      image

字段说明

  • DomainName:ESA站点的DNS记录。

  • timestamp:签算服务器生成鉴权URL的时间,与鉴权URL有效时长共同控制鉴权URL的失效时间。时间点取自签算服务器的“UTC+8”时间,格式为:YYYYMMDDHHMM。

    说明

    多数情况下,鉴权URL的有效时长为ESA配置有效时长。有时在签算增加鉴权URL的有效时长的,此时,timestamp=Unix时间戳+增加的时长;鉴权URL实际有效时长=timestamp+ESA配置的时长。

  • FileName:实际回源访问的文件路径名,鉴权时FileName需以/开头。

  • md5hash:通过md5算法计算出的验证串,由数字0~9和小写英文字母a~z混合组成,固定长度32。

    md5hash值的计算方式:

    • A鉴权方式:getMd5(FileName + "-" + timestamp + "-" + rand + "-" + uid + "-" + your_secret_key)

    • B鉴权方式:getMd5(your_secret_key + timestamp + FileName)

    • C鉴权方式:getMd5(your_secret_key + "-" + FileName + "-" + timestamp)

    说明

    上面的getMd5函数需要自己实现。

  • rand:随机数。建议使用UUID,不能包含中划线-,例如:477b3bbc253f467b8def6711128c7bec

  • uid:用户ID,根据业务设置。

  • auth_key:本次请求的鉴权信息,由timestamp、rand、uidmd5hash组成。