【问题标题】:how to implement Auth0 strategy in nestjs based on GraphQL如何基于GraphQL在nestjs中实现Auth0策略
【发布时间】:2019-12-17 17:20:36
【问题描述】:

我正在尝试在 Nestjs 应用程序中实现 passport-auth0 策略,并且我正在使用 GraphQl for api,最后我最终得到了任何一个

TypeError: res.setHeader is not a function at Auth0 Strategy.strategy.redirect 要么 OAuth 2.0 身份验证在使用状态时需要会话支持。您忘记使用快速会话中间件了吗?

我按照nestjs文档中的说明仍然是同样的问题,我检查了github存储库也没有成功

    import { use, serializeUser, deserializeUser } from 'passport';
    import { Strategy } from 'passport-auth0';
    import { Injectable } from '@nestjs/common';
    import { environment } from '../../environments/environment';
    import { PassportStrategy } from '@nestjs/passport';

    @Injectable()
    export class Auth0Strategy extends PassportStrategy(Strategy) {
    constructor() {
    super(
      {
        domain: environment.auth0.domain,
        clientID: environment.auth0.clientID,
        clientSecret: environment.auth0.clientSecret,
        callbackURL: environment.auth0.callbackURL,
        state: false // or true
      },
      async (accessToken, refreshToken, extraParams, profile, done) => {
        return done(null, profile);
      }
    );
    use(this);

    serializeUser((user, done) => {
      done(null, user);
    });

    deserializeUser((user, done) => {
      done(null, user);
    });
    }
    }

    import { Injectable, ExecutionContext } from '@nestjs/common';

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

    import { GqlExecutionContext } from '@nestjs/graphql';

    @Injectable()
    export class Auth0Guard extends AuthGuard('auth0') {
    getRequest(context: ExecutionContext) {
    const ctx = GqlExecutionContext.create(context);
    return ctx.getContext().req;
    }
    }

【问题讨论】:

  • 您的main.ts 中是否导入并配置了表达式?
  • 不,我该怎么做?
  • 如果您计划使用会话,您可能需要阅读如何使用会话实现护照,或者如果您计划使用 JWT 之类的东西,如何禁用会话。

标签: graphql auth0 nestjs


【解决方案1】:

我设法通过 auth0 使用 passport-jwtjwks-rsa 完成了身份验证。

import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy, ExtractJwt } from 'passport-jwt';
import { passportJwtSecret } from 'jwks-rsa';

import { JwtPayload } from './interfaces/jwt-payload.interface';

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor() {
    super({
      secretOrKeyProvider: passportJwtSecret({
        cache: true,
        rateLimit: true,
        jwksRequestsPerMinute: 5,
        jwksUri: `https://${process.env.AUTH0_DOMAIN}/.well-known/jwks.json`,
      }),

      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      audience: process.env.AUTH0_AUDIENCE,
      issuer: `https://${process.env.AUTH0_DOMAIN}`,
    });
  }

  validate(payload: JwtPayload): JwtPayload {
    const minimumScope = ['openid', 'profile', 'email'];

    if (
      payload.scope.split(' ').filter(scope => minimumScope.indexOf(scope) > -1)
        .length !== 3
    ) {
      throw new UnauthorizedException(
        'JWT does not possess the requires scope (`openid profile email`).',
      );
    }

    return payload;
  }
}

可以在https://github.com/jajaperson/nestjs-auth0找到一个完整的模板存储库

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-28
    • 2020-10-21
    • 2020-08-10
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    相关资源
    最近更新 更多