Sign google cloud storage blob using access token

余生长醉 提交于 2019-12-24 10:04:52

问题


Goal: Generate Signed-URL Using OAuth2.0 Access Token

The examples and source codes I find for signing Google Cloud Storage blobs all require service account credentials file (the private key to be specific). For instance:

https://cloud.google.com/storage/docs/access-control/signing-urls-with-helpers#storage-signed-url-get-object-python

However, since I follow the authorization flow discussed here, I only have OAuth2.0 access token (and I do NOT have the credentials file and private key of a service account with access to GCS bucket/object). Hence, I was wondering how I can sign blobs using OAuth2.0 access tokens.

The Code Used:

I use the following to sign blob:


# First, get access token:
service_account = "<email address of a service account>"
access_token = build(
    serviceName='iamcredentials',
    version='v1',
    http=http
).projects().serviceAccounts().generateAccessToken(
    name="projects/{}/serviceAccounts/{}".format(
        "-",
        service_account),
    body=body
).execute()["accessToken"]

credentials = AccessTokenCredentials(access_token, "MyAgent/1.0", None)

# Second, use the access token to sign a blob
url = "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/{}:signBlob".format(service_account)
encoded = base64.b64encode(blob)
sign_blob_request_body = {"payload": encoded}

response = requests.post(url,
                         data=json.dumps(sign_blob_request_body),
                         headers={
                             'Content-Type': 'application/json',
                             'Authorization': 'Bearer {}'.format(credentials.access_token)})
signature = response.json()["signedBlob"]

# Third, use the signature to create signed URL:
encoded_signature = base64.b64encode(signature)
signed_url = "https://storage.googleapis.com/<BUCKET>/<OBJECT>?" \
             "GoogleAccessId={}&" \
             "Expires={}&" \
             "Signature={}".format(service_account, 
                                   expiration, 
                                   encoded_signature)

The Error Message Received:

<Error>
    <Code>SignatureDoesNotMatch</Code>
    <Message>
        The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.
    </Message>
    <StringToSign>GET 1561832204 /<BUCKET>/<OBJECT></StringToSign>
</Error>

回答1:


In case you do NOT want to use API secret key, follow procedure described in this sample that is using iamcredentials.signBlob() API signing URL 'remotely' for a service account with no need to distribute API secret key.

Signature string (that has to be signed) has this format:

signature_string = ('{verb}\n'
                    '{content_md5}\n'
                    '{content_type}\n'
                    '{expiration}\n'
                    '{resource}')


来源:https://stackoverflow.com/questions/56801281/sign-google-cloud-storage-blob-using-access-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!