【问题标题】:Google OAuth2 authentication in Swagger for a Node,js applicationSwagger 中针对 Node、js 应用程序的 Google OAuth2 身份验证
【发布时间】:2022-08-17 04:53:32
【问题描述】:

我创建了一个简单的 NodeJS 应用程序,它使用 Passport 来处理使用 Google 的 OAuth2 身份验证。我在 Google API 中创建了凭据,配置了 Google 策略...

const passport = require(\'passport\');
const GoogleStrategy = require(\'passport-google-oauth\').OAuth2Strategy;

passport.serializeUser((user , cb) => {
    cb(null , user);
})
passport.deserializeUser(function(user, cb) {
    cb(null, user);
});
  
passport.use(new GoogleStrategy({
    clientID: process.env.CLIENTID,
    clientSecret: process.env.CLIENTSECRET,
    callbackURL: process.env.CALLBACK,
    passReqToCallback: true
  },
  function(request, accessToken, refreshToken, profile, cb) {
    return cb(null, profile);
  }
));

app.use(passport.initialize());
app.use(passport.session());

该应用程序运行良好,凭据允许我控制对应用程序的访问。

我还配置了 Swagger 以提供一种测试应用程序提供的 REST API 的方法。

var options = {
    validatorUrl : null,
    oauth: {
        clientId: process.env.CLIENTID,
        clientSecret: process.env.CLIENTSECRET,
        appName: \"MyApp\",
  }
};

var swaggerUi = require(\'swagger-ui-express\');
swaggerDocument = require(\'./swagger.json\');
app.use(
  \'/api-docs\',
  swaggerUi.serve, 
  swaggerUi.setup(swaggerDocument,false,options)
);

Swagger 也可以正常工作,但有些 API 需要身份验证,所以我还需要让 OAUth2 身份验证与 Google 一起使用。

在我的 swagger.json (Swagger 2.0) 中,我按照我找到的一些示例配置了 securityDefinitions 部分:

\"securityDefinitions\": {
    \"google_oauth\": { 
      \"type\": \"oauth2\",
      \"flow\": \"implicit\",
      \"authorizationUrl\": \"https://accounts.google.com/o/oauth2/v2/auth\",
      \"tokenUrl\": \"https://www.googleapis.com/oauth2/v4/token\",
      \"scopes\": {
        \"https://www.googleapis.com/auth/userinfo.profile\": \"All user operations requiring authentication.\"
      }
    }
  },

笔记:我已经尝试过使用流的 \'implicit\' 和 \'authorizationCode\' 值。

我已经为那些需要执行凭据的 API 添加了安全配置。

\"/favourites/User/{user}/City/{city}\": {
  \"post\": {
    \"summary\": \"Adds the selected city as a new favourite for that user.\",
    \"tags\": [\"Favourites\"],
    \"security\": [
      {\"google_oauth\": [\"https://www.googleapis.com/auth/userinfo.email\"]}
    ],

现在在 Swagger 中显示了 Authorize 按钮,单击后我被重定向到 Google(在新选项卡中)。

我提供了我的凭据,然后我返回到原始的 Swagger 选项卡。

但是现在,如果我尝试执行受保护的 API,我的 REST 代码将收到一个不记名令牌(我还没有配置我的应用程序来处理它)。

我认为我可能为 Swagger 使用了错误的配置,因此我使用我的应用程序的 auth URL 更改了 swagger.json 文件中的安全定义(在使用应用程序时调用它而不是 swagger)。

  \"securityDefinitions\": {
    \"google_oauth\": { 
      \"type\": \"oauth2\",
      \"flow\": \"implicit\",
      \"authorizationUrl\": \"http://www.myapp.es/auth\",
      \"scopes\": {
        \"favourites\": \"All user operations requiring authentication.\"
      }
    }
  },

但这也不起作用。

有任何想法吗?我想我很接近,只有一些属性缺少让 OAuth2 在 Swagger 中工作。

提前致谢。

    标签: node.js oauth-2.0 swagger google-authentication


    【解决方案1】:

    请问是怎么做到这一切的?我有一个使用此护照策略的 nodejs express 应用程序,我正在尝试使用 swagger 为我的 google 配置文件端点和我的用户配置文件端点配置 api。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-04
      • 2018-09-01
      • 2019-06-03
      • 1970-01-01
      • 1970-01-01
      • 2017-07-16
      • 2017-10-08
      • 2012-06-10
      相关资源
      最近更新 更多