贝宝 id_token 验证

PayPal id_token verification

我正在实现 "Log In with PayPal" 功能。应该相当简单,但不幸的是 PayPal 缺少文档(只描述了一些基本的东西)。

我使用 "Log In with PayPal" 小部件获得 authorization_code。然后我向 https://api.sandbox.paypal.com/v1/identity/openidconnect/tokenservice 发出请求并使用 access_token、refresh_token 和 id_token 获得 JSON。我想获得唯一的用户标识符,所以我解码 id_token,但我无法验证我的 id_token 的签名。 Id_token header 包含 {"alg": "HS256", "typ": "JWT"}.

我应该使用什么秘密?尝试了我的应用程序的秘密(与我用来访问令牌服务的秘密相同),但它没有用(使用 https://jwt.io/ 调试器)。

PayPal 不兼容 OpenID 连接。 回答说明原因。

PayPal 公开配置端点 - https://www.paypalobjects.com/.well-known/openid-configuration。它仅支持 HS256,不支持或提及 RS256。这可能是由于 ID 令牌的专有验证。例如用作不记名令牌的 id 令牌。

替代解决方案是调用 userinfo 端点,如 document 所述。可以使用 access token 调用此端点,文档说它将 return user_id

user_id - identifier for the end-user at the issuer.

尽管在用户信息中找到的 user_id 似乎与可​​以从 id_token 中提取的 sub 不同。

如果签名算法是 RS256,如何找到 public 密钥

ID 令牌是 JWT。正如您所发现的,它包含一个 JWS 签名,充当 MAC.

此签名是使用私钥签名的。 id 令牌的接收者可以使用 public 密钥验证令牌。要查找 public 键,openid conenct 指定一个发现文件。您可以从 specification

阅读更多相关信息

在发现文档中,您将获得一个特殊端点来推断配置 .well-known/openid-configuration。配置响应必须具有的元数据之一是 jwk_url

jwks_uri

REQUIRED. URL of the OP's JSON Web Key Set [JWK] document. This contains the signing key(s) the RP uses to validate signatures from the OP. The JWK Set MAY also contain the Server's encryption key(s), which are used by RPs to encrypt requests to the Server. When both signing and encryption keys are made available, a use (Key Use) parameter value is REQUIRED for all keys in the referenced JWK Set to indicate each key's intended usage. Although some algorithms allow the same key to be used for both signatures and encryption, doing so is NOT RECOMMENDED, as it is less secure. The JWK x5c parameter MAY be used to provide X.509 representations of keys provided. When used, the bare key values MUST still be present and MUST match those in the certificate.