oauth-2.0 在swagger用户界面nestjs oauth2中设置客户端ID

wz8daaqr  于 2022-10-31  发布在  其他
关注(0)|答案(1)|浏览(199)

最终目标(如果提供了clientId并单击了作用域,则此目标有效):使用Swagger UI获取azure Auth,以接收accessToken用于进一步请求。
由于client_id和scopes是静态的,我希望通过预先设置client_id和scopes来绕过弹出窗口,并立即触发单击Authorize按钮时发生的情况,由于我在那里找不到任何内容,我至少尝试预先填写表单,这样用户只需在我的组织中单击“授权”。
我尝试过但没有成功的:

  • swagger 选项initOAuth
  • DocumentBuilder.components.requestBodies

nestjs的main.ts中的代码:

// Swagger
const config = new DocumentBuilder()
  .setTitle('Auth Backend')
  .setDescription('Azure PoC backend')
  .setVersion('0.1')
  .addTag('auth')
  .addOAuth2({
    type: "oauth2",
    description: "description",
    name: "AzureAD",
    flows: {
      implicit: {
        scopes: { "User.Read": "Read user profile" },
        authorizationUrl: `https://login.microsoftonline.com/${process.env.TENANT_ID}/oauth2/v2.0/authorize`,
      }
    }
  }, "AzureAD")
  .build()

const document = SwaggerModule.createDocument(app, config)
SwaggerModule.setup('swagger', app, document, {initOAuth: {clientId: process.env.CLIENT_ID, clientSecret: process.env.CLIENT_SECRET}});
nue99wik

nue99wik1#

请尝试在SwaggerModule.setup中包含**swaggerOptions**,这样可以将swaggerOptions传递到swaggerUi中。generateHTML

SwaggerModule.setup('api', app, document, {   customSiteTitle: 'Your API name',   swaggerOptions: {
    oauth: {
      clientId:  clientid",
      clientSecret: "clientsecret",
      realm: "your-realms",
      appName: " ",
      scopeSeparator: " ",
      scopes: ["User.Read", "profile",”offline_access”],
    …. },
    persistAuthorization: true,   }, });

对于***最新版本***:(由Access swagger-ui after setup to initialize oauth2 -nest.js· Issue · GitHub中的@julianklumpers给出)

SwaggerModule.setup('api', app, document, {
    customSiteTitle: 'API',
    swaggerOptions: {
      persistAuthorization: true,
      oauth2RedirectUrl: 'https://…….’,
      initOAuth: {
        ClientId,
        ClientSecret,
        scopes: ["User.Read", "profile",”offline_access”],
        appName: ‘name of the app',
      },
    },
  });

参考swagger-ui oauth2 · GitHub

相关问题