如何在Cloudfront请求中使用boto3 python 3.8提取承载令牌并在另一个查询中使用它 [英] How to extract bearer token in using boto3 python 3.8 in cloudfront request and use it in another query

查看:178
本文介绍了如何在Cloudfront请求中使用boto3 python 3.8提取承载令牌并在另一个查询中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在传入的Cloudfront请求中提取承载令牌,并在另一个get请求中使用它.

How can I extract the bearer token in incoming cloudfront request and use it in another get request.

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

use the bearer token as jwt in the following request

 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代码如下所示

My lambda code looks like this

import json

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

推荐答案

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

You would add this as a Lambda@Edge function for the viewer request event.

Lambda @ Edge如下所示

The Lambda@Edge would look like the following

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

假设您正在使用请求库,则可以像这样检索响应

Assuming you're using the requests library you would be able to retrieve the response like this

object = token_response.objectKey

这篇关于如何在Cloudfront请求中使用boto3 python 3.8提取承载令牌并在另一个查询中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆