使用 ORY Hydra 进行 OpenID 会话管理

问题描述

我将 Ory Hydra 作为 OAuth 2.0 / OpenID 提供者运行。

我想制定一个解决方案,以便中继方可以检查最终用户的 OpenID 会话状态。他们是否登录退出等。OpenID 规范有一个使用 iframe 指定的解决方here。我在此解决方案中遇到的主要问题在 OpenID Connect 会话管理规范 here 的第 5.1 节中描述:

Note that at the time of this writing,some User Agents (browsers) are starting to block access to third-party content by default to block some mechanisms used to track the End-User's activity across sites. 
Specifically,the third-party content being blocked is website content with an origin different that the origin of the focused User Agent window. 
Site data includes cookies and any web storage APIs (sessionStorage,localStorage,etc.).

This can prevent the ability for notifications from the OP at the RP from being able to access the RP's User Agent state to implement local logout actions. 
In particular,cookies and web storage APIs may not be available in the OP frame loaded in the RP context. The side effect here is that,depending on the used mechanism (cookies or web storage),the data needed to recalculate session_state might not be available. 
Cookie based implementations might then return changed for every single call,resulting in infinite loops of re-authentications. 
Therefore,deployments of this specification are recommended to include defensive code to detect this situation,and if possible,notify the End-User that the requested RP logouts Could not be performed. 
The details of the defensive code needed are beyond the scope of this specification; it may vary per User Agent and may vary over time,as the User Agent tracking prevention situation is fluid and continues to evolve.

是否有另一种方法可以在不使用 iframe 实现的情况下查看最终用户是否具有 OpenID 会话?

解决方法

您始终可以使用访问令牌询问令牌自省端点,以查看访问令牌是否仍然有效。使用这种方法可以避免使用 iframe。作为替代方案,您可以使用短期访问令牌并使用刷新令牌来获取新的访问令牌。对于大多数情况,这可能是一个合适的折衷方案。

请参阅令牌自省规范 here 并且此 tutorial 也是一个很好的起点

在调用此端点的响应中,您应该找到一个活动字段,如果用户仍处于登录状态,该字段应设置为 true。

规范说:

活跃 必需的。是否提供令牌的布尔指标 当前处于活动状态。令牌“活动”状态的细节 将根据授权的实施而有所不同 服务器及其保留的有关其令牌的信息,但“真实” “活动”属性的值返回通常会表明 此授权服务器已发出给定令牌, 尚未被资源所有者撤销,并且在其范围内 给定的有效时间窗口(例如,在其发布时间和 在其到期时间之前)。