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

问题描述 投票:0回答:1

我在 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(['/']);
}

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

angular oauth-2.0 oauth angular-oauth2-oidc
1个回答
1
投票

有一个

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

发行代币的索赔

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

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

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

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

我的偏好

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

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

此设计有以下好处:

  • 可扩展性:很容易向 API 负载添加额外的数据,这可能与 OAuth 无关
  • 隐私:通常不建议向 id_token 添加过多的用户数据,因为它可以在持续整个用户会话的可查看 JWT 中使用
© www.soinside.com 2019 - 2024. All rights reserved.