如何在云端请求中使用 boto3 python 3.8 提取不记名令牌并将其用于另一个查询

How to extract bearer token in using boto3 python 3.8 in cloudfront request and use it in another query

如何在传入的云端请求中提取不记名令牌并在另一个获取请求中使用它。

curl -X GET \
  https://domain/api/files/7d0ab8ef-9061-4458--de79a2c9e436 \
  -H 'Authorization: Bearer eTA' \
  -H 'Cache-Control: no-cache' \
  -H 'Postman-Token: token'

在以下请求中使用不记名令牌作为 jwt

 in phython domain.com/service/api/files/7d0ab8ef-9061-4458--de79a2c9e436

这给了我以下回应:

https://domain/file-service/api/files/7d0ab8ef-9061-4458-b97a-de79a2c9e436

{
    "id": "7d0ab8ef-9061-4458-b97a-de79a2c9e436",
    "uploadId": "-9b68-44bd-864a-cd9a40d601ba",
    "consumerId": "-97d1-11ea-bb37-0242ac130002",
    "metadata": {
        "fileName": "somefile.docx",
        "fileSize": 1000,
        "mimeType": "application/msword"
    },
    "objectKey": "2020-04-31/ju-28fc-4d7c-b086-66c15eb311e7.docx",
    "status": "PENDING"
}

我的 lambda 代码如下所示

import json

def lambda_handler(event, context):
    # TODO implement
    request = event['Records'][0]['cf']['request']
    print(request['headers'])
    print(response)

您可以将其添加为查看器请求事件的 Lambda@Edge 函数。

Lambda@Edge 如下所示

import json
import requests

def lambda_handler(event, context):

    request = event['Records'][0]['cf']['request']
    print (request)
    print(request['headers'])
    print(request['origin']['s3']['domainName'])
    token = request['headers']['cookie'][0]['value'].partition("=")[2]
    print (token)
    print(type(request['uri']))
    cosumer_id = request['uri'].rpartition('/')[-1]
    print (cosumer_id)

    #Take the token and send it somewhere
    token_response = requests.get(url = 'https://url/api/files/'  + cosumer_id, headers = {'Authorization': 'Bearer ' + token}) 

    print (token_response.request)
    print (token_response)
    print (token_response.text)
    data = token_response.json() 
    objectKey = data["objectKey"]
    print (objectKey)


    return request

假设您正在使用请求库,您将能够像这样检索响应

object = token_response.objectKey