繁体   English   中英

Google Cloud CDN 以 bucket 作为后端签署了 cookies

[英]Google Cloud CDN signed cookies with bucket as backend

作为带有 url 前缀的签名 url的替代方案,我正在尝试让签名 cookies工作。 Google Cloud CDN 设置有后端存储桶,该存储桶已配置并适用于标准签名 url。

使用这些Go 示例,我在 nodejs(typescript) 中实现了一个 cookie 签名 function,当提供测试样本数据时会产生预期的结果。

export function signCookie(urlPrefix: any, keyName: string, key: any, experation: Date): string {
    // Base64url encode the url prefix
    const urlPrefixEncoded = Buffer.from(urlPrefix)
        .toString('base64')
        .replace(/\+/g, '-')
        .replace(/\//g, '_');

    // Input to be signed
    const input = `URLPrefix=${urlPrefixEncoded}:Expires=${experation.getTime()}:KeyName=${keyName}`;

    // Create bytes from given key string.
    const keyBytes = Buffer.from(key, 'base64');

    // Use key bytes and crypto.createHmac to produce a base64 encoded signature which is then escaped to be base64url encoded.
    const signature = createHmac('sha1', keyBytes)
        .update(input)
        .digest('base64').replace(/\+/g, '-')
        .replace(/\//g, '_');

    // Adding the signature on the end if the cookie value
    const signedValue = `${input}:Signature=${signature}`;

    return signedValue;
}

然后,当我使用相同的 function 为我的实际 cdn 实例生成签名的 cookie 值时,我得到以下信息(密钥名称和 url 前缀不是实际的):

URLPrefix=aHR0cHM6L-----------------HdhcmUuaW8v:Expires=1587585646437:KeyName=my-key-name:Signature=2mJbbtYVclycXBGIpKzsJWuLXEA=

使用 firefox 开发工具创建烹饪 我在附加 cookie 和未附加 cookie 时得到以下两个结果:

附上签名的 Cookie 没有签名的cookie

cookie“Cloud-CDN-Cookie”似乎只是通过 Cloud CDN 传递并直接传递到后端存储桶,在那里它被忽略,并给出标准响应访问被拒绝响应。

云平台日志显示没有cdn介入。

附带cookie 附有饼干 没有附加cookie 没有附加cookie

在我做错的 cookie 的签名实现或创建和使用中是否有什么问题?

我的 Google 项目尚未启用签名 cookie 功能。 另一位用户联系了支持人员,一旦他们解决了问题,我就解决了,没有更改代码并且可以正常工作。

这是我最终的 nodejs(typescript) 签名 cookie 实现。

function signCookie(urlPrefix: any, keyName: string, key: any, experation: Date): string {
    // Base64url encode the url prefix
    const urlPrefixEncoded = Buffer.from(urlPrefix)
        .toString('base64')
        .replace(/\+/g, '-')
        .replace(/\//g, '_');

    // Input to be signed
    const input = `URLPrefix=${urlPrefixEncoded}:Expires=${experation.getTime()}:KeyName=${keyName}`;

    // Create bytes from given key string.
    const keyBytes = Buffer.from(key, 'base64');

    // Use key bytes and crypto.createHmac to produce a base64 encoded signature which is then escaped to be base64url encoded.
    const signature = createHmac('sha1', keyBytes)
        .update(input)
        .digest('base64').replace(/\+/g, '-')
        .replace(/\//g, '_');

    // Adding the signature on the end if the cookie value
    const signedValue = `${input}:Signature=${signature}`;

    return signedValue;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM