我为我的项目配置了一个网关,我在路由中添加了安全选项,它的工作原理:
我的函数生成 jwt 令牌:
def generate_jwt():
payload = {"iat": iat, "exp": exp, "iss": iss, "aud": aud, "sub": iss, "email": iss, "company": company}
signer = google.auth.crypt.RSASigner.from_service_account_file(sa_keyfile)
jwt = google.auth.jwt.encode(signer, payload)
return jwt
.yaml 文件:
- 安全:
securityDefinitions:
apikey:
type: "apiKey"
name: "key"
in: "header"
bearer:
authorizationUrl: ""
flow: "implicit"
type: "oauth2"
x-google-issuer: "mygserviceaccount"
x-google-jwks_uri: "mygserviceaccount.com"
x-google-audiences: "aud"
x-google-jwt-locations:
- header: "Authorization"
value_prefix: "Bearer "
- 我配置了 jwt 的路线:
/MyRoute:
post:
description: "Route"
operationId: "Route"
x-google-backend:
address: routeadress
deadline: 360
security:
- bearer: []
responses:
200:
description: "Success."
400:
description: "Bad Request."
401:
description: "Unauthorized."
使用此配置,我需要在 Header 中发送 jwt 令牌,如果我不发送,网关返回错误,否则如果 jwt 有效,则调用我的函数。所以这行得通!
但我的问题是,我如何在 MyRoute 中恢复 api 网关生成的有效负载?
有效载荷应该可供我使用,还是我需要调用另一个 google api 来解码进来的 jwt req.headers.authorization
?
@John Hanley 所说的答案是使用标题 x-apigateway-api-userinfo:
const userInfo = req.headers['x-apigateway-api-userinfo']
const data = Buffer.from(userInfo, 'base64').toString('utf-8')
我的数据有我被告知的有效载荷。