【问题标题】:Loopback 4 authentication metadata options undefinedLoopback 4 身份验证元数据选项未定义
【发布时间】:2021-10-12 09:28:39
【问题描述】:

我创建了一个简单的 jwt 身份验证应用程序,其显示方式与此处显示的方式相同:https://github.com/raymondfeng/loopback4-example-auth0

身份验证部分工作正常,但授权未按预期工作。

我用以下功能装饰了我的控制器并添加了一个范围。

@authenticate({strategy: 'auth0-jwt', options: {scopes: ['greet']}})

在我的身份验证策略中,我通过 AuthenticationMetadata 类检查范围。

import {AuthenticationBindings, AuthenticationMetadata, AuthenticationStrategy} from '@loopback/authentication';
import {inject} from '@loopback/core';
import {ExpressRequestHandler, Request, Response, RestBindings} from '@loopback/rest';
import {UserProfile} from '@loopback/security';
import {JWT_SERVICE} from './types';

const jwtAuthz = require('express-jwt-authz');

export class JWTAuthenticationStrategy implements AuthenticationStrategy {
  name = 'auth0-jwt';

  constructor(
    @inject(RestBindings.Http.RESPONSE)
    private response: Response,
    @inject(AuthenticationBindings.METADATA)
    private metadata: AuthenticationMetadata,
    @inject(JWT_SERVICE)
    private jwtCheck: ExpressRequestHandler,
  ) {}

  async authenticate(request: Request): Promise<UserProfile | undefined> {
    return new Promise<UserProfile | undefined>((resolve, reject) => {
      this.jwtCheck(request, this.response, (err: unknown) => {
        if (err) {
          console.error(err);
          reject(err);
          return;
        }
        console.log(this.metadata.options);
        // If the `@authenticate` requires `scopes` check
        if (this.metadata.options?.scopes) {
          jwtAuthz(this.metadata.options!.scopes, {failWithError: true})(request, this.response, (err2?: Error) => {
            if (err2) {
              console.error(err2);
              reject(err2);
              return;
            }
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            resolve((request as any).user);
          });
        } else {
          // eslint-disable-next-line @typescript-eslint/no-explicit-any
          resolve((request as any).user);
        }
      });
    });
  }
}

尝试访问时

this.metadata.options

我总是得到一个不确定的回报。

我怎样才能获得元数据之外的选项和范围?

谢谢

【问题讨论】:

    标签: node.js loopback


    【解决方案1】:

    对于环回授权,您的类需要实现Provider&lt;Authorizer&gt; 接口。在该接口中,它定义了您需要实现的 2 个功能

    @injectable({scope: BindingScope.TRANSIENT})
    class AuthorizationService implements Provider<Authorizer>{
    
      value (): Authorizer {
        return this.authorize.bind(this);  
      }
    
      async authorize (
        context: AuthorizationContext,
        metadata: AuthorizationMetadata,
      ) {
         // TODO implement authorization
      }
    
    }
    

    在你绑定AuthorizationTags.Authorizer后,授权元数据将通过回送自动注入到该函数中

    如果您在实施Authentication 时遇到问题,请阅读我的step by step guide,了解我们如何使用 Firebase 实施环回身份验证。这应该能够帮助您了解让Authentication 运行的核心思想。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-19
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 2014-03-30
      • 2019-05-14
      • 2016-09-07
      • 2018-03-16
      相关资源
      最近更新 更多