简体   繁体   English

有什么方法可以将Auth0集成到Loopback 4?

[英]Is there any way to integrate Auth0 to Loopback 4?

I'm working in a project with Loopback version 4, a Node.js framework. 我正在使用Loopback版本4(Node.js框架)进行项目。 I need to implement authentication with Auth0. 我需要使用Auth0实施身份验证。 However, it seems Auth0 is not compatible with version 4 of Loopback. 但是,似乎Auth0与Loopback的版本4不兼容。 I couldn't find any documentation, or even a tutorial, about this subject. 我找不到有关此主题的任何文档,甚至没有教程。 Anybody there has ever implement authentication with Auth0 in Loopback 4? 有人在Loopback 4中使用Auth0实施身份验证吗?

Hello from the LoopBack team 👋 LoopBack团队您好👋

Authentication and authorization are features that we are actively working on right now (as of June 2019). 身份验证和授权是我们目前(截至2019年6月)正在积极致力于的功能。 As far as I can tell from Auth0 docs (see Server Client + API: Node.js Implementation for the API ), they are using JWT tokens. 据我从Auth0文档得知(请参阅服务器客户端+ API:API的Node.js实现 ),他们正在使用JWT令牌。

Fortunately, we already have an example application demonstrating JWT-based authentication - see https://github.com/strongloop/loopback4-example-shopping 幸运的是,我们已经有一个示例应用程序演示了基于JWT的身份验证-请参阅https://github.com/strongloop/loopback4-example-shopping

Take a look at JWTService class, it's the place where the client-provided tokens are parsed and verified. 看一下JWTService类,它是解析和验证客户端提供的令牌的地方。

The Express example provided by Auth0 uses express-jwt package that wraps jsonwebtoken into an Express middleware. Auth0提供的Express示例使用express-jwt包,该包将jsonwebtoken包装到Express中间件中。

In LoopBack, we call jsonwebtoken directly. 在LoopBack中,我们直接调用jsonwebtoken

To adapt the Express based example provided by Auth0, it should be enough to figure out how to pass relevant bits of express-jwt configuration to jsonwebtoken library. 为了适应Auth0提供的基于Express的示例,应该足以弄清楚如何将express-jwt配置的相关位传递给jsonwebtoken库。

Here is the configuration copied from Auth0 docs: 这是从Auth0文档复制的配置:

// Create middleware for checking the JWT
const checkJwt = jwt({
  // Dynamically provide a signing key based on the kid in the header and the singing keys provided by the JWKS endpoint.
  secret: jwksRsa.expressJwtSecret({
    cache: true,
    rateLimit: true,
    jwksRequestsPerMinute: 5,
    jwksUri: `https://YOUR_DOMAIN/.well-known/jwks.json`
  }),

  // Validate the audience and the issuer.
  audience: process.env.AUTH0_AUDIENCE,
  issuer: `https://YOUR_DOMAIN/`,
  algorithms: ['RS256']
});

Here is how LoopBack's JWTService calls jsonwebtoken library: 这是LoopBack的JWTService调用jsonwebtoken库的方式:

https://github.com/strongloop/loopback4-example-shopping/blob/5f36ae289f50d67bcdc33637c0323daa1f10e02b/packages/shopping/src/services/jwt-service.ts#L35 https://github.com/strongloop/loopback4-example-shopping/blob/5f36ae289f50d67bcdc33637c0323daa1f10e02b/packages/shopping/src/services/jwt-service.ts#L35

const decryptedToken = await verifyAsync(token, this.jwtSecret);

Note that jwtSecret is injected into the service, we are configuring it here: 请注意, jwtSecret已注入服务中,我们在此处进行配置:

https://github.com/strongloop/loopback4-example-shopping/blob/2d8978d0d72150caf95573c362402491a92757e7/packages/shopping/src/application.ts#L76-L78 https://github.com/strongloop/loopback4-example-shopping/blob/2d8978d0d72150caf95573c362402491a92757e7/packages/shopping/src/application.ts#L76-L78

    this.bind(TokenServiceBindings.TOKEN_SECRET).to(
      TokenServiceConstants.TOKEN_SECRET_VALUE,
    );

I believe you need to replace TokenServiceConstants.TOKEN_SECRET_VALUE with the value returned by jwksRsa . 我相信您需要用TokenServiceConstants.TOKEN_SECRET_VALUE返回的值替换jwksRsa

this.bind(TokenServiceBindings.TOKEN_SECRET).to(
  jwksRsa.expressJwtSecret({
    cache: true,
    rateLimit: true,
    jwksRequestsPerMinute: 5,
    jwksUri: `https://YOUR_DOMAIN/.well-known/jwks.json`
  }),
})

Remaining things to figure out: 剩下的事情要弄清楚:

  • How to apply audience , issuer and algorithms options. 如何应用audienceissuer人和algorithms选项。

  • I am not sure if jsonwebtoken supports dynamic secrets out of the box. 我不确定jsonwebtoken支持开箱即用的动态机密。 If the proposal above does not work, then you may need to look into express-jwt sources to find out how they are handling dynamic secrets. 如果以上建议不起作用,那么您可能需要研究express-jwt源,以了解它们如何处理动态机密。

I hope my answer provides enough pointers that will allow you to figure the missing details yourself. 我希望我的答案能提供足够的指针,让您自己找出缺失的细节。 It would be great if you could post the full working solution if you manage to work it out. 如果可以解决问题,可以发布完整的工作解决方案,那就太好了。

One of the LoopBack team members has recently implemented a proof-of-concept application showing how to integrate with Auth0. LoopBack团队成员之一最近实现了概念验证应用程序,该应用程序显示了如何与Auth0集成。 You can find the repository here: 您可以在此处找到存储库:

https://github.com/raymondfeng/loopback4-example-auth0 https://github.com/raymondfeng/loopback4-example-auth0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM