Node with Fusionauth passport-oauth OAuth2Strategy Error: Failed to obtain access token
Node with Fusionauth passport-oauth OAuth2Strategy Error: Failed to obtain access token
我正在尝试通过 fusionauth 使用 passport 和 express 授权我的节点应用程序,
登录 fusionauth 后,我从 fusionauth 回调 "Failed to obtain access token" 收到节点错误。我不确定为什么 fusionauth 响应不包含令牌?
fusionauth 授权 link 回调
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/');
}
};
```
我的第一个猜测可能是某种网络错误。 Error: connect EHOSTUNREACH 0.0.35.51:80
。这是一个奇怪的 IP 地址。
我看到配置主机是 fusion_auth_server
。是否解析为该 IP 地址?
看起来它前面的令牌 http:// 解析为正确的 IP
更改了以下行:
来自:
tokenURL: `${config.host}:${config.port}/oauth2/token`,
收件人:
tokenURL: `http://${config.host}:${config.port}/oauth2/token`,
我正在尝试通过 fusionauth 使用 passport 和 express 授权我的节点应用程序, 登录 fusionauth 后,我从 fusionauth 回调 "Failed to obtain access token" 收到节点错误。我不确定为什么 fusionauth 响应不包含令牌?
fusionauth 授权 link 回调 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/');
}
};
```
我的第一个猜测可能是某种网络错误。 Error: connect EHOSTUNREACH 0.0.35.51:80
。这是一个奇怪的 IP 地址。
我看到配置主机是 fusion_auth_server
。是否解析为该 IP 地址?
看起来它前面的令牌 http:// 解析为正确的 IP
更改了以下行: 来自:
tokenURL: `${config.host}:${config.port}/oauth2/token`,
收件人:
tokenURL: `http://${config.host}:${config.port}/oauth2/token`,