如何在 NestJS 中实现 Passport.js Azure AD 承载策略 (OpenID)

Ter*_*rry 7 azure-active-directory passport.js nestjs

根据 nestjs 和护照的文档,我有以下实现。

我从默认的 nest cli 设置开始,nest new auth-test 然后我在 src 文件夹下添加了一个 auth 文件夹,下面是 aad-auth.gaurd、aad.strategy 和 auth.module。

然后我将新的守卫添加到 app.controller.ts 中的默认路由

我通过将其成功用作 C# Web API 来确认 Azure 应用注册设置,因此 Azure 端设置正确。

我不需要发出 JWT,因为这是 Azure AD 前端的问题,该不记名令牌被传递给标头中的 API。没有有用的错误,只是一个 500 内部错误。我注意到很多 Github 请求提供有关使用 Nest 实现 Azure AD 的文档,以及任何 OAuth 流提供程序(facebook、google),但该请求仍处于开放状态。

不知道什么是错误的,任何关于修复以下代码的指导或建议将不胜感激。

文档:NestJS:https ://docs.nestjs.com/techniques/authentication 护照:http : //www.passportjs.org/packages/passport-azure-ad/

//auth/aad.strategy.ts

import { BearerStrategy } from 'passport-azure-ad';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, ValidationPipe } from '@nestjs/common';

@Injectable() 
export class AADStrategy extends PassportStrategy(BearerStrategy) {
    constructor () {
        super({
            identityMetadata: "https://login.microsoftonline.com/<tenant>.onmicrosoft.com/v2.0/.well-known/openid-configuration",
            clientID: "<clientid>",
            issuer: null,
            audience: null,
            loggingLevel: "info",
            passReqToCallback: false
        })
    }

    async validate(payload: any){
        console.log(payload);
        return(payload);    
    }
}
Run Code Online (Sandbox Code Playgroud)

//auth/aad-auth.gaurd

import { Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class AADAuthGaurd extends AuthGuard('aad') {}
Run Code Online (Sandbox Code Playgroud)

//auth/auth.module.ts

import { Module } from '@nestjs/common';
import { AADStrategy } from './aad.strategy'

@Module({
    imports: [
        AADStrategy,
    ],
    providers: [
        AADStrategy,
    ]
})
export class AuthModule {}
Run Code Online (Sandbox Code Playgroud)

//app.controller.ts

import { Controller, Get, UseGuards } from '@nestjs/common';
import { AppService } from './app.service';
import { AADAuthGaurd } from './auth/aad-auth.gaurd';


@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @UseGuards(AADAuthGaurd)
  @Get()
  getHello(): string {
    return this.appService.getHello();
  }
}
Run Code Online (Sandbox Code Playgroud)

Ter*_*rry 6

我解决了这个问题,这里是压缩到一个文件的工作代码。

//app.controller.ts

import { Controller, Get, UseGuards } from '@nestjs/common';
import { AppService } from './app.service';
import { Injectable } from "@nestjs/common";
import { PassportStrategy } from "@nestjs/passport";
import { BearerStrategy } from 'passport-azure-ad'
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class AzureADStrategy extends PassportStrategy(BearerStrategy, 'oauth-bearer')
{
  constructor()
  {
    super({
      identityMetadata: `https://login.microsoftonline.com/<tenant>.onmicrosoft.com/.well-known/openid-configuration`,
      clientID: 'client id from azure app',
    })
  }

  async validate(response: any)
  {
    const { unique_name }: {unique_name: string} = response;
    if (unique_name) return unique_name;
    else return null;
  }
}

@Controller()
export class AppController {
  constructor() {}

  @Get('unprotected')
  unprotected() : string {
    return 'Unprotected';
  }

  @UseGuards(AuthGuard('oauth-bearer'))
  @Get('protected')
  protected() : string {
    return 'Protected';
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 你好,我有一个类似的问题,但我无法使它工作,你有一个简单的状态器项目,具有最小的配置以使其正常工作吗?我正在尝试实现 B2C,这使得它变得更加复杂。任何形式的帮助将非常感激! (2认同)