UNPKG

passport-wargaming

Version:

Passport for Wargaming.net Openid

144 lines (109 loc) 3.26 kB
var passport = require('passport'), https = require('https'), http = require('http'), util = require('util'); function waragimgStrategy(options, verify) { options = options || {}; passport.Strategy.call(this, options, verify); this.zone = options.zone || 'ru'; this.appID = options.appID; this.returnURL = options.returnURL; this.authorizationURL = { hostname: 'api.worldoftanks.'+options.zone, port: 443, path: '/wot/auth/login/?application_id=' + options.appID + '&redirect_uri='+options.returnURL+'&nofollow=1', method: 'GET' } this.authorizationURL = { hostname: 'api.worldoftanks.'+options.zone, port: 443, path: '/wot/auth/login/?application_id=' + options.appID + '&redirect_uri='+options.returnURL+'&nofollow=1', method: 'GET' } this._verify = verify; this.name = 'wargaming'; } util.inherits(waragimgStrategy, passport.Strategy); waragimgStrategy.prototype.authenticate = function(req, options) { var _this = this; if(!Object.keys(req.query).length){ var reqa = https.request(this.authorizationURL, function (_res) { var data = ''; _res.on('data', function (chunk) { data += chunk; }); _res.on('end', function () { var jsobj = {}; try { jsobj = JSON.parse(data); if (jsobj["status"] == 'ok') { _this.redirect(jsobj.data.location); } else { throw ({ message: "Incorrect response from wargming server", responseFromServer: data, options: this.authorizationURL }); } } catch (e) { _this.error({error:"incorrect data from server"}); } }); }); reqa.on('error', function (e) { _this.error(err); }); reqa.end(); } else if(req.query['status'] && req.query['access_token'] && req.query['nickname'] && req.query['account_id']){ var options = { hostname: 'api.worldoftanks.'+_this.zone, port: 80, path: '/wot/account/info/?application_id='+_this.appID+'&access_token='+String(req.query['access_token'])+'&account_id='+String(req.query['account_id']), method: 'GET' }; var reqc = http.request(options, function (resWG) { var data = ''; resWG.on('data', function (chunk) { data += chunk; }); resWG.on('end', function () { var jsobj = {}; try { jsobj = JSON.parse(data); if (jsobj["status"] == 'ok' && Object.keys(jsobj.data).length) { var user_id = Object.keys(jsobj.data)[0]; if(jsobj.data[user_id]){ var user_name = jsobj.data[user_id].nickname; var _private = jsobj.data[user_id].private; if(_private){ _this._verify(user_id, user_name, function(err, user){ if(!err){ _this.success(user); } else { _this.error(err); } }); } else { _this.error({error:'Bad data from wargaming server'}); } } else { _this.error({error:'Not found user_id in resonse data'}); } } else { _this.error(jsobj); } } catch (e) { _this.error(e); } }); }); reqc.on('error', function (e) { _this.error(e); }); reqc.end(); } } waragimgStrategy.prototype.userProfile = function(accessToken, done) { console.log('bvnhfy3s', accessToken, done); } module.exports = waragimgStrategy;