使用 angular-oauth2-oidc 库从 Auth0 获取完整访问令牌

Jer*_*oen 2 auth0 angular angular-oauth2-oidc

angular-oauth2-oidc图书馆设置为使用与Auth0。但是,Auth0 不断向我发送一个非常短的访问令牌,例如mSNhEfdDHK6t-kT5QweRtgec-FPGAsdfEw9,而不是完整的 JWT 令牌。以下是重现问题的方法:

  1. 在 Auth0 中创建一个您希望作为资源访问的示例“API”。
  2. 在 Auth0 中为隐式流创建一个 SPA 应用程序。
  3. 按照快速入门angular-oauth2-oidc或克隆我的示例 repo
  4. 按照以下方式配置它:

    export const authConfig: AuthConfig = {
      issuer: 'https://your-tenant-name.eu.auth0.com/',
      clientId: 'your-spa-client-id-here',
      redirectUri: window.location.origin + '/index.html',
      scope: 'openid profile email',
    };
    
    Run Code Online (Sandbox Code Playgroud)
  5. initImplicitFlow()通过单击应用程序上的登录按钮触发调用。

当你这样做时:

  • 结果:完整的 JWT IdToken,但较短的 AccessToken。
  • 预期:两个令牌都是完整的 JWT 令牌。

Auth0 社区论坛上有一个线程,它解释了为什么您会为访问令牌获得这样一个“不透明字符串”。最重要的、被接受的答案提到了我已经在做的事情(sope就像我所做的那样,打电话/authorize等)。但是,在该线程的较低部分提到设置audience调用时/authorize是解决方案,无论如何这似乎是一件好事。

但是你如何发送audience?有上没有这样的属性AuthConfig类型来设置它,看initImplicitFlow()它只是直线上升的变化源location.href,所以没有拦截有两种。

Jer*_*oen 6

你快到了。虽然它可能是不错的audience作为对一个特定的属性AuthConfig类型,已经有配置它的方式:使用customQueryParams这个:

export const authConfig: AuthConfig = {
  issuer: 'https://your-tenant-name.eu.auth0.com/',
  clientId: 'your-spa-client-id-here',
  redirectUri: window.location.origin + '/index.html',
  scope: 'openid profile email',
  customQueryParams: {
    audience: 'https://your-api-audience-id.example.com',
  },
};
Run Code Online (Sandbox Code Playgroud)

audience是你在Auth0配置的标识符。这是管理界面的截图:

标识符是受众参数