Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APAC Region Doesn't Appear to Work #19

Open
ryant26 opened this issue Feb 5, 2018 · 4 comments
Open

APAC Region Doesn't Appear to Work #19

ryant26 opened this issue Feb 5, 2018 · 4 comments

Comments

@ryant26
Copy link

ryant26 commented Feb 5, 2018

I'll include some code although, it's much more complex than what is necessary to reproduce this issue. (The code is from my application)

Essentially I set up a strategy to handle each of the login regions. us and eu work fine, apac doesn't appear to work. Not sure if this is an issue with this library since the getHost() method that resolves the regional OAuth endpoint is quite simple...

If this is the wrong place to open this issue, please let me know.

const bnetRegions = ['us', 'eu', 'apac'];
const port = 3003;
const domainName = `localhost:${port}`;

let generateAuthHandler = function(region) {
    return function (accessToken, refreshToken, profile, done) {
        done(null, {platformDisplayName: profile.battletag, region, platform: 'pc'});
    };
};

// Initialize bnet regional OAuth server strategies
bnetRegions.forEach((region) => {
    passport.use(`bnet-${region}`, new BnetStrategy({
        clientID: config.get('bnet.id'),
        clientSecret: config.get('bnet.secret'),
        callbackURL: `https://${domainName}/auth/bnet/callback?region=${region}`,
        region: `${region}`
    }, generateAuthHandler(region)));
});

router.get('/bnet', function (req, res, next) {
    passport.authenticate(`bnet-${req.query.region}`)(req, res, next);
});

router.get('/bnet/callback', function (req, res, next) {
    passport.authenticate(`bnet-${req.query.region}`, {
        failureRedirect: localAuthenticator.failureRedirect,
        session: false
    })(req, res, next);
}, authenticationService.serializeUser, authenticationService.generateToken, authenticationService.respond);

and the error:

{
    "message": "Failed to fetch the user id",
    "error": {
        "name": "InternalOAuthError",
        "message": "Failed to fetch the user id",
        "oauthError": {
            "code": "ENOTFOUND",
            "errno": "ENOTFOUND",
            "syscall": "getaddrinfo",
            "hostname": "apac.api.battle.net",
            "host": "apac.api.battle.net",
            "port": 443
        }
    },
    "title": "error"
}
@ryant26
Copy link
Author

ryant26 commented Feb 7, 2018

Issue logged on blizzard dev forums:
https://us.battle.net/forums/en/bnet/topic/20761716790#1

Update, created a more minimal code example

const app = require('express')();                                           // <------- NPM install
const passport = require('passport');                                       // <------- NPM install
const BnetStrategy = require('passport-bnet').Strategy;                     // <------- NPM install
const fs = require('fs');
const https = require('https');

const port = 3000;
const BNET_ID = 'myID';                                                     // <------- Change me
const BNET_SECRET = 'mySecret';                                             // <------- Change me
const region = 'apac';                                                      // <------- us works, apac doesn't

const privateKey  = fs.readFileSync('../certs/key.pem', 'utf8');            // <------- Change me
const certificate = fs.readFileSync('../certs/cert.pem', 'utf8');           // <------- Change me
const credentials = {key: privateKey, cert: certificate};

app.use(passport.initialize());

// Use the BnetStrategy within Passport.
passport.use(new BnetStrategy({
    clientID: BNET_ID,
    clientSecret: BNET_SECRET,
    callbackURL: `https://localhost:${port}/auth/bnet/callback`,
    region: region
}, function(accessToken, refreshToken, profile, done) {
    return done(null, profile);
}));

app.get('/auth/bnet',
    passport.authenticate('bnet'));

app.get('/auth/bnet/callback',
    passport.authenticate('bnet', {
        failureRedirect: '/?successful=false',
        session: false
    }),
    function(req, res){
        console.log('SUCCESSFUL LOGIN');
        res.redirect('/?successful=true');
    });

https.createServer(credentials, app).listen(port, () => {
    console.log(`Server listening on port: ${port}`);
});

@ryant26
Copy link
Author

ryant26 commented Feb 26, 2018

The problem

Finally found some time to look into this. The problem is when you retrieve the profile:
https://github.com/Blizzard/passport-bnet/blob/master/lib/strategy.js#L100

this._profileUrl = options.userURL || 'https://' + getMasheryHost(options.region) + '/account/user'

This does not work with the apac region because the blizzard api doesn't have any community endpoints that use the apac region.

The workaround

The workaround is clearly to pass in a valid url with options.userURL.

Solutions

Documentation: Document that this module does not work out of the box with the apac region

Map apac to a region supported by the community API:
When the region is set to apac map this to another region during profile retrieval.

I'd open a PR for this myself but my guess is that someone at Blizzard needs to decide the best course of action for this.

@bweston92
Copy link

I get the error at the same point, it just hangs then after a minute I get TokenError: Invalid authorization code: [REDACTED].
This happens regardless of the region too, any ideas?

@bweston92
Copy link

Turns out if I use https://eu.api.battle.net/oauth/userinfo and pass the bearer token it works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants