我的目标是创建一个cookie来存储jwt`s的id标记,并将其传递回客户端。此逻辑在aws lambda中运行:
def lambda_handler(event, context):
.....
.....
cookie_name="my_cookie"
cookie = gen_cookie(domain, expiration, cookie_name,jwt):
return {"statusCode": 302,
"headers": {
"Location": "different-url/logged-in",
"Set-Cookie": cookie}
}
def gen_cookie(domain, expiration, cookie_name,jwt):
cookie = SimpleCookie()
cookie[cookie_name] = "test"
cookie[cookie_name]['httponly'] = "yes"
cookie[cookie_name]['domain'] = domain
cookie[cookie_name]['expires'] = expiration
cookie[cookie_name]['path'] = "/"
cookie[cookie_name]['id_token'] = jwt['id_token']
print(cookie)
return cookie我得到一个异常,id_token不是一个有效的属性。
[ERROR] CookieError: Invalid attribute 'id_token'
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 41, in lambda_handler
cookie[cookie_name]['id_token'] = response_content_dict['id_token']
File "/var/lang/lib/python3.7/http/cookies.py", line 311, in __setitem__
raise CookieError("Invalid attribute %r" % (K,))我检查过了,simpleCookie中唯一有效的值是:
_reserved = {
"expires" : "expires",
"path" : "Path",
"comment" : "Comment",
"domain" : "Domain",
"max-age" : "Max-Age",
"secure" : "Secure",
"httponly" : "HttpOnly",
"version" : "Version",
}所以我的问题是,如何创建包含jwt的cookie并将其返回给客户端?
发布于 2020-09-07 18:58:48
我以如下方式将jwt保存在cookie中:
def gen_cookie(domain, expiration, cookie_name,jwt):
cookie = SimpleCookie()
cookie[cookie_name] = jwt['id_token']
cookie[cookie_name]['httponly'] = "yes"
cookie[cookie_name]['domain'] = domain
cookie[cookie_name]['expires'] = expiration
cookie[cookie_name]['path'] = "/"
print(cookie)
return cookie注意,cookie的值是jwt`s的标记。
https://stackoverflow.com/questions/63774453
复制相似问题