Google 以存储桶为后端的 Cloud CDN 签名 cookie

Google Cloud CDN signed cookies with bucket as backend

作为 signed urls with a url prefix, I'm trying to get signed cookies 工作的替代方法。 Google Cloud CDN 设置有后端存储桶,该存储桶已配置并适用于标准签名 urls。

使用这些 Go examples I've implemented a cookie signing function in nodejs(typescript) that when provied with the test sample data 产生预期的结果。

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;
}

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

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

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

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

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

附有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;
}