angular-oauth2-oidc - 读取用户声明

angular-oauth2-oidc - Read user claims

我在 Angular 应用程序中使用 angular-oauth2-oidc 的代码流。一切正常,但我无法阅读用户声明。

我尝试使用 this.oAuthService.getIdToken()this.oAuthService.getAccessToken()this.oauthService.getUserInfo(),但我似乎没有得到任何可以使用常规方法解码的有效 JWT。

在我的后端 API (.NET Core) 中,我使用 access_token 查询 TokenIntrospection 端点,我可以正确地看到所有声明。

相关信息:

export const oAuthConfig: AuthConfig = {
  issuer: environment.oauth.issuer,
  // URL of the SPA to be redirected to after login
  redirectUri: environment.oauth.redirectUri,
  clientId: environment.oauth.clientId,
  dummyClientSecret: environment.oauth.clientSecret,
  responseType: 'code',
  scope: 'openid profile email',
  showDebugInformation: true,
  oidc: true,
  requireHttps: false,
};
async login() {
  await this.oAuthService.loadDiscoveryDocumentAndTryLogin();
  await this.oAuthService.initCodeFlow();

  this.router.navigate(['/']);
}

有什么建议吗?感谢您的帮助。

有一个 getIdentityClaims 方法可以为您提供 id_token 作为对象的声明。首先,您需要检查 HTTP 请求以验证 id_token 是否确实被 returned,并且它包含您期望的声明。

索赔 ISSUING 到令牌

在授权服务器中,您可以指定向何处发出声明,并且向两个令牌发出不同的声明是非常标准的:

  • 在您的情况下,ID 令牌供 UI 客户端使用,并且可能包含显示名称等详细信息
  • 访问令牌由 API 客户端使用,可能包含用于授权的角色或用户 ID。

为了更好地理解这个概念,请参阅 Curity Identity Server 使用的 Claims Mapper 概念。

我怀疑在你的情况下,系统未配置为(或不支持)向 id 令牌发出一些声明。

我的偏好

实际上我认为您在处理访问令牌方面走在正确的轨道上。 UI 获取用户信息的另一种方法是将访问令牌发送到它的 API,路径如 /api/userinfo/current,然后API 然后可以 return 一个 JSON 对象包含:

  • 来自访问令牌的声明
  • 可能对 UI
  • 有用的任何其他域特定数据

此设计具有以下优点:

  • 可扩展性:很容易向 API 负载添加额外数据,这可能与 OAuth
  • 无关
  • 隐私:通常不建议向 id_token 添加过多的用户数据,因为它随后会在持续整个用户会话的可见 JWT 中可用