0

我正在尝试使用护照和快递通过 fusionauth 授权我的节点应用程序,并且在登录到 fusionauth 后,我从 fusionauth 回调“无法获取访问令牌”中收到节点错误。我不确定为什么 fusionauth 响应不包含令牌?

fusionauth 使用回调授权链接 fusion_auth_server:9011/oauth2/authorize?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Foauth2%2Fcallback&client_id=42a5####-####-####-### #-########

 name: 'InternalOAuthError',
  message: 'Failed to obtain access token',
  oauthError:
   { Error: connect EHOSTUNREACH 0.0.35.51:80 - Local (192.168.1.46:62475)
       at internalConnect (net.js:872:16)
       at defaultTriggerAsyncIdScope (internal/async_hooks.js:294:19)
       at GetAddrInfoReqWrap.emitLookup [as callback] (net.js:1019:9)
       at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:61:10)
     errno: 'EHOSTUNREACH',
     code: 'EHOSTUNREACH',
     syscall: 'connect',
     address: '0.0.35.51',
     port: 80 } }

```
app.get('/oauth2/authorize', oauth2.authorize);
app.get('/oauth2/callback', oauth2.callback);
app.get('/oauth2/logout', oauth2.logout);

```

```
const passport = require('passport');
const OAuth2Strategy = require('passport-oauth').OAuth2Strategy;
const http = require('http');
const config = {
    "apiKey": "63353861-####-####-####-##########",
    "callbackURL": "http://localhost:3000/oauth2/callback",
    "clientID": "42a5bc23-####-####-####-#####",
    "clientSecret": "WI2Y04lkozWonBeRz_####################",
    "host": "fusion_auth_server", 
    "port": "9011"
    };

passport.use(
  'fusionauth',
  new OAuth2Strategy(
    {
      authorizationURL: `${config.host}:${config.port}/oauth2/authorize`,
      tokenURL: `${config.host}:${config.port}/oauth2/token`,
      clientID: config.clientID,
      clientSecret: config.clientSecret,
      callbackURL: config.callbackURL
    },
    function(accessToken, refreshToken, profile, done) {
      // verify accessToken was provided`enter code here`
      if (!accessToken) {
        done(null, false);
      }

      // verify token and get user info
      const options = {
        host: config.host,
        port: config.port,
        path: '/oauth2/userinfo',
        method: 'GET',
        headers: {
          'Content-Type': 'application/json',
          Authorization: `Bearer ${accessToken}`
        }
      };
      const userInfoRequest = http.get(options, res => {
        var chunks = '';
        res.on('data', data => {
          chunks += data;
        });
        res.on('end', () => {
          if (res.statusCode === 200) {
            const result = JSON.parse(chunks);
            const user = {
              ...result,
              accessToken
            };

            // todo: persist user

            done(null, user);
          } else {
            done(null, false);
          }
        });
      });
      userInfoRequest.end();
    }
  )
);

const callback = (req, res, next) => {
    //console.log("callback",res)
  passport.authenticate('fusionauth', (err, user) => {
     console.log("Authenticating",err)
    if (err) {
      return next(err);
    }
    if (!user) {
      return res.redirect('http://localhost:4200/login');
    }
   // console.log(user);
    res.cookie('accessToken', user.accessToken, { httpOnly: true });
    res.redirect('http://localhost:4200');
  })(req, res, next);
};

module.exports = {

  authorize: passport.authenticate('fusionauth', {
    session: false
  }),
  callback,
  logout: (req, res) => {
    req.logout();
    res.redirect('http://localhost:4200/');
  }
};


```
4

2 回答 2

1

看起来像它前面的令牌http://解析为正确的 IP

更改了以下行:从:

tokenURL: `${config.host}:${config.port}/oauth2/token`,

至:

tokenURL: `http://${config.host}:${config.port}/oauth2/token`,
于 2020-04-24T17:11:51.953 回答
0

看起来像是某种网络错误,这是我的第一个猜测。Error: connect EHOSTUNREACH 0.0.35.51:80. 这是一个奇怪的IP地址。

我看到配置主机是fusion_auth_server. 这是否解析为该 IP 地址?

于 2020-04-24T16:05:29.700 回答