I’m currently working on an application that needs to get access token from user login through battlenet oauth flow. I’m using passport-bnet from npm, but have some issues getting it to work (im using fastify instead of express). I wanted to locate the error so I cloned the example from passport-bnet repository. Of course I added the required env values (id and secret), but the authentication always fails with InternalOAuthError: Failed to fetch the user id error (the redirect to /bnet/callback is always pending for several seconds and then it returns the error).
The entire code looks like this
var express = require('express');
var passport = require('passport');
var util = require('util');
var fs = require('fs');
var https = require('https');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var BnetStrategy = require('passport-bnet').Strategy;
var BNET_ID = '...';
var BNET_SECRET = '...';
var privateKey = fs.readFileSync('server.key', 'utf8');
var certificate = fs.readFileSync('server.cert', 'utf8');
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
// Use the BnetStrategy within Passport.
passport.use(
new BnetStrategy(
{ clientID: BNET_ID,
clientSecret: BNET_SECRET,
scope: "wow.profile",
callbackURL: "https://localhost:3000/auth/bnet/callback" },
function(accessToken, refreshToken, profile, done) {
process.nextTick(function () {
return done(null, profile);
});
})
);
var app = express();
// configure Express
app.use(cookieParser());
app.use(session({ secret: 'blizzard',
saveUninitialized: true,
resave: true }));
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());
app.get('/auth/bnet',
passport.authenticate('bnet'));
app.get('/auth/bnet/callback',
passport.authenticate('bnet', { failureRedirect: '/' }),
function(req, res){
res.redirect('/');
});
app.get('/', function(req, res) {
if(req.isAuthenticated()) {
var output = '<h1>Express OAuth Test</h1>' + req.user.id + '<br>';
if(req.user.battletag) {
output += req.user.battletag + '<br>';
}
output += '<a href="/logout">Logout</a>';
res.send(output);
} else {
res.send('<h1>Express OAuth Test</h1>' +
'<a href="/auth/github">Login with Github</a><br>' +
'<a href="/auth/bnet">Login with Bnet</a>');
}
});
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
var credentials = {key: privateKey, cert: certificate};
var httpsServer = https.createServer(credentials, app);
var server = httpsServer.listen(3000, function() {
console.log('Listening on port %d', server.address().port);
});
I even tried adding a self-signed cert and run express with https enabled. Don’t really know if I’m doing something wrong, I have yet to find a simillar issue to this that is solved. Help is appreciated, thanks.