简体   繁体   English

python在SimpleCookie中保存jwt令牌

[英]python saving jwt token in SimpleCookie

My goal is to create a cookie that stores the jwt`s id token and pass it back to the client side.我的目标是创建一个 cookie 来存储 jwt 的 id 令牌并将其传递回客户端。 This logic is running in an aws lambda :此逻辑在 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

I'm getting an exception that the id_token isnt a valid attribute.我收到一个异常,即 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,))

I checked and the only valid values in the simpleCookie are :我查了一下,simpleCookie 中唯一的有效值是:

  _reserved = {
        "expires"  : "expires",
        "path"     : "Path",
        "comment"  : "Comment",
        "domain"   : "Domain",
        "max-age"  : "Max-Age",
        "secure"   : "Secure",
        "httponly" : "HttpOnly",
        "version"  : "Version",
    }

So my question is, how can I create a cookie that contains the jwt and return it to the client side ?所以我的问题是,如何创建一个包含 jwt 的 cookie 并将其返回给客户端?

I saved the jwt in the cookie in the following way :我通过以下方式将 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

Notice that the value of the cookie is the jwt`s token.请注意,cookie 的值是 jwt 的令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM