如何使用 graphql 和护照设置身份验证,但仍使用 Playground

Dar*_*te1 2 express passport.js graphql apollo-server passport-azure-ad

将身份验证添加到后端 Graphql 服务器后,“Schema”和“Docs”在 Graphql Playground 中不再可见。在 Playground 中向“HTTP HEADERS”添加令牌时执行查询在经过身份验证时可以正常工作,而在用户未经身份验证时则不能正常工作,所以没关系。

我们禁用了 Apollo 服务器的内置 Playground,并使用中间件graphql-playground-middleware-express来使用不同的 URL 并绕过身份验证。我们现在可以浏览到 Playground 并使用它,但我们无法读取那里的“架构”或“文档”。

尝试启用introspection并没有解决此问题。passport.authenticate()打电话过来会更好Contextapollo-server?还有一个名为Passport-graphql 的工具,但它适用于本地策略,可能无法解决问题。我还尝试在调用 Playground 路由之前在标头中设置令牌,但这不起作用。

我们对此有点迷失。感谢您为我们提供的任何见解。

在此输入图像描述

相关代码:

// index/ts
import passport from 'passport'
import expressPlayground from 'graphql-playground-middleware-express'

const app = express()
app.use(cors({ origin: true }))
app.get('/playground', expressPlayground({ endpoint: '/graphql' }))
app.use(passport.initialize())
passport.use(bearerStrategy)

app.use(
  passport.authenticate('oauth-bearer', { session: false }),
  (req, _res, next) => { next() }
)
;(async () => {
    await createConnections()
    const server = await new ApolloServer({
      schema: await getSchema(),
      context: ({ req }) => ({ getUser: () => req.user, }),
      introspection: false,
      playground: false,
    })
    server.applyMiddleware({ app, cors: false })

    app.listen({ port: ENVIRONMENT.port }, () => { console.log(`Server ready`) })      
})()
Run Code Online (Sandbox Code Playgroud)
// passport.ts
import { IBearerStrategyOptionWithRequest, BearerStrategy, ITokenPayload } from passport-azure-ad'
import { Account } from '@it-portal/entity/Account'

export const bearerStrategy = new BearerStrategy( config,
  async (token: ITokenPayload, done: CallableFunction) => {
    try {
      if (!token.oid) throw 'token oid missing'

      const knownAccount = await Account.findOne({ accountIdentifier: token.oid })
      if (knownAccount) return done(null, knownAccount, token)

      const account = new Account()
      account.accountIdentifier = token.oid
      account.name = token.name
      account.userName = (token as any).preferred_username
      const newAccount = await account.save()
      return done(null, newAccount, token)
    } catch (error) {
      console.error(`Failed adding the user to the request object: ${error}`)
    }
  }
)
Run Code Online (Sandbox Code Playgroud)

Dar*_*te1 10

感谢这个答案,我明白了。关键是不要passport在 Express 上用作中间件,而是在 Graphql 中使用它Context

在下面的示例代码中,您可以看到getUser在 ApolloServer 中使用的 Promise ,它执行护照身份验证Context。这样,在模式下运行时,仍然可以访问 Playground,并且仍然可以访问“架构”端“文档” dev

根据 Apollo文档部分“将用户信息放在上下文中”,这也是首选方式。

// apollo.ts
passport.use(bearerStrategy)

const getUser = (req: Express.Request, res: Express.Response) =>
  new Promise((resolve, reject) => {
    passport.authenticate('oauth-bearer', { session: false }, (err, user) => {
      if (err) reject(err)
      resolve(user)
    })(req, res)
  })

const playgroundEnabled = ENVIRONMENT.mode !== 'production'

export const getApolloServer = async () => {
  return new ApolloServer({
    schema,
    context: async ({ req, res }) => {
      const user = await getUser(req, res)
      if (!user) throw new AuthenticationError('No user logged in')
      console.log('User found', user)

      return { user }
    },
    introspection: playgroundEnabled,
    playground: playgroundEnabled,
  })
}
Run Code Online (Sandbox Code Playgroud)

最好的事情是你只需要两个函数就可以工作:passport.use(BearerStrategy)passport.authenticate()。这是因为不使用会话,因此我们不需要将其添加为 Express 中间件。

// index/ts
const app = express()
app.use(cors({ origin: true }))

;(async () => {
    await createConnections()
    const server = await getApolloServer()
    server.applyMiddleware({ app, cors: false })

    app.listen({ port: ENVIRONMENT.port }, () => { console.log(`Server ready`) })      
})()
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助其他人解决同样的问题。