【问题标题】:Same passport js strategy with different configuration (SAML)具有不同配置的相同护照 js 策略 (SAML)
【发布时间】:2020-04-02 14:16:25
【问题描述】:

我正在尝试使用 Okta 作为身份提供者和 passport-saml 库在我的 Nest.js 应用程序中创建 SSO。我阅读了 Nest authentication 和 passport-saml 的文档。我对示例的理解没有问题,但我确实需要使用具有不同配置的 SAML 策略,这取决于 POST api/auth/saml 的请求正文值。换句话说,我有一个策略,但我的自定义 LoginSSOStrategy 类 具有不同的 entryPoint, issuer, cert 参数,它扩展了 Nest 的 PassportStrategy 类 .js。任何想法我该如何处理?

【问题讨论】:

  • 嗨,你有什么更新吗?我也面临同样的问题

标签: javascript node.js passport.js nestjs passport-saml


【解决方案1】:

我认为这个问题类似于GitHub issue。由于 Passport.js 的全球性以及 Nest 无法确定哪些路由使用 Passport 护照策略这一事实,因此无法使用 @Injectable({ scope: Scope.REQUEST }) 创建请求范围的 Passport 策略。

最近,我不得不根据传入请求中的一些数据使用动态重定向 URL 实现 Azure Active Directory 登录。根据您使用的策略,您可以在调用 Passport 策略的 authenticate 方法时使用(未记录的)extraAuthReqQueryParams 属性覆盖某些选项。 了解您是否能够覆盖某些选项的一种方法是检查文档,如果您感到幸运,您可以查看您正在使用的 Passport 策略的源代码。在阅读了undocumented feature 并在source code of the Azure AD Passport strategy 中看到这些行(特别是#1355 和#1374 行)后,我能够使用redirect_uri 属性更改我之前在redirectUrl 属性中指定的值(注意这里的细微差别)。

@Injectable()
export class AzureOIDCStrategy extends PassportStrategy(OIDCStrategy,'AzureOIDC') {
  constructor() {
    super({
      // Even though it is overwritten in the 'authenticate' method the Passport Strategy expects this to be set to a valid URL.
      redirectUrl: `https://your-backend-domain.com/auth/azure/callback`,
      // This ensures we have access to the request in the `authenticate` method
      passReqToCallback: true,
    });
  }

  authenticate(req: Request, options: Record<string, any>): void {
    return super.authenticate(req, {
      // `options` may contain more options for the `authenticate` method in Passport.
      ...options,
      extraAuthReqQueryParams: {
        // This overwrites the `redirectUrl` specified in the constructor
        redirect_uri: `https://${req.headers.host}/auth/callback`,
      },
    });
  }
}

我希望您能够应用此“策略”来更新entryPointissuercert 参数。

在 Express 应用中,您可以执行以下操作:

app.get('/login', 
  (req, res, next) =>
    passport.authenticate('azure-ad', {
      extraAuthReqQueryParams: {
        redirect_uri: `https://${req.headers.host}/auth/callback`,
      },
  })(req, res, next)
);

【讨论】:

    【解决方案2】:

    我不太确定这是否是一个好方法,但是如果您愿意,您可以将类请求限定为范围并通过构造函数注入请求,然后可以访问请求对象并能够使用每个请求的护照策略的新实例。您可以使用请求将req.whatever 传递给super() 类的构造函数。

    @Injectable({ scope: Scope.REQUEST })
    export class LoginSSOStrategy exends PassportStrategy(Strategy) {
    
      constructor(@Inject(REQUEST) request: Request, ...) {
        super({/* options matching to request.field */});
      }
    
      validate(/* validate params*/) {
        /* validate functionality */
      }
    }
    

    这似乎是您需要进行大量测试并确保它适用于并发请求的事情,但总的来说它至少在理论上是可行的。

    【讨论】:

      猜你喜欢
      • 2016-10-17
      • 2021-06-20
      • 1970-01-01
      • 2016-05-29
      • 2023-03-29
      • 2020-01-03
      • 2021-09-02
      • 2018-01-09
      • 2020-11-27
      相关资源
      最近更新 更多