Authored by yyq

passport

... ... @@ -78,6 +78,7 @@ exports.createApp = async(app) => {
try {
const userMiddleware = require('./doraemon/middleware/user');
const setYohoDataMiddleware = require('./doraemon/middleware/set-yoho-data');
const serverMiddleware = require('./doraemon/middleware/server-handler');
const errorMiddleware = require('./doraemon/middleware/error-handler');
const ssrApiMiddleware = require('./doraemon/middleware/ssr-api');
const ssrRouteMiddleware = require('./doraemon/middleware/ssr');
... ... @@ -85,6 +86,8 @@ exports.createApp = async(app) => {
// YOHO 前置中间件
app.use(setYohoDataMiddleware);
app.use(userMiddleware);
app.use(serverMiddleware);
app.use('/xianyu', ssrApiMiddleware);
app.use(ssrRouteMiddleware.routers);
... ...
const express = require('express');
const passport = require('../models/passport');
const app = express();
const router = express.Router(); // eslint-disable-line
router.get('/xianyu/passport/login/taobao', passport.login.taobaoLogin)
router.get('/xianyu/passport/callback/taobao', passport.login.taobaoCallback)
app.use(router);
module.exports = app;
... ...
/**
* Module dependencies.
*/
const util = require('util');
const md5 = require('yoho-md5');
const TopClient = require('topsdk');
const { OAuth2Strategy, InternalOAuthError } = require('passport-oauth');
let client;
/**
* @param {Object} params
* @api public
*/
function signMD5(params, clientSecret) {
let _signStr = '';
params.sign_method = 'md5';
for (const k of Object.keys(params).sort()) {
_signStr += `${k}${params[k]}`;
}
let signd = md5(clientSecret + _signStr + clientSecret);
params.sign = new Buffer(signd, 'base64').toString('hex');
return params;
}
/**
* @param {Object} options
* @param {Function} verify
* @api public
*/
function Strategy(options, verify) {
options = options || {};
options.authorizationURL = options.authorizationURL || 'https://oauth.taobao.com/authorize';
options.tokenURL = options.tokenURL || 'https://oauth.taobao.com/token';
options.scopeSeparator = options.scopeSeparator || ',';
options.customHeaders = options.customHeaders || {};
client = new TopClient(options.clientID, options.clientSecret, {
endpoint: 'https://eco.taobao.com/router/rest',
useValidators: true,
rawResponse: false
});
if (!options.customHeaders['User-Agent']) {
options.customHeaders['User-Agent'] = options.userAgent || 'passport-taobao';
}
OAuth2Strategy.call(this, options, verify);
this.name = 'taobao';
this._userProfileURL = options.userProfileURL || 'https://eco.taobao.com/router/rest';
}
/**
* Inherit from `OAuth2Strategy`.
*/
util.inherits(Strategy, OAuth2Strategy);
/**
* @param {String} accessToken
* @param {Function} done
* @api protected
*/
Strategy.prototype.userProfile = function (accessToken, done) {
console.log(client)
client.execute('taobao.user.buyer.get', {
session: accessToken,
fields: 'nick,sex'
}).then(res => {
console.log(res);
});
return;
console.log(arguments)
let oauth2 = this._oauth2;
let url = 'https://eco.taobao.com/router/rest';
let params = {
method: 'taobao.user.buyer.get',
app_key: oauth2._clientId,
session: accessToken,
format: 'json',
v: '2.0',
fields: 'uid,nick,avatar'
};
// _clientSecret
// ?format=json&v=2.0&fields=uid,nick,avatar&method=taobao.user.buyer.get';
// url = url + '&app_key=' + oauth2._clientId;
// url = url + '&timestamp=' + Date.parse(new Date());
// url = url + '&access_token=' + accessToken;
if (!accessToken) {
return done(new Error('accessToken is empty'));
}
Object.keys(signMD5(params, oauth2._clientSecret)).forEach((k, i) => {
url += `${i ? '&' : '?'}${k}=${params[k]}`;
});
console.log(url)
oauth2.get(url, accessToken, function (err, result, res) {
if (err) {
return done(new InternalOAuthError('failed to fetch user profile', err));
}
try {
if (result) {
console.log(result)
let json = JSON.parse(result);
if (json.error_response)
return done(new InternalOAuthError(json.error_response.code + '-' + json.error_response.msg, new Error(json.error_response.msg)));
else {
let json = JSON.parse(result);
let profile = { provider: 'taobao' };
profile.id = json.uid;
profile.nickname = json.nick;
profile.avatar = json.avatar;
profile._raw = result;
profile._json = json;
done(null, profile);
}
}
} catch (e) {
done('ERROR:' + e + result);
}
});
}
module.exports = Strategy;
... ...
const passport = require('passport');
const TaobaoStrategy = require('./passport-taobao');
const uuid = require('uuid');
const config = global.yoho.config;
// taobao 登录
passport.use('taobao', new TaobaoStrategy({
clientID: '27930297',
clientSecret: '29b30287153a02f531c160df17da8078',
callbackURL: `http://127.0.0.1:6001/xianyu/passport/callback/taobao`,
requireState: false
}, (accessToken, refreshToken, profile, done) => {
done(null, profile);
}));
const login = {
taobaoLogin: (req, res, next) => {
req.session.authState = uuid.v4();
return passport.authenticate('taobao', {
state: req.session.authState,
failWithError: true
})(req, res, next);
},
taobaoCallback: (req, res, next) => {
console.log(req.session)
// return res.send(req.query);
passport.authenticate('taobao', (err, user) => {
console.log('21312312')
console.log(user)
console.log(err)
})(req, res, next);
// if (req.session.authState === req.query.state) {
// } else {
// res.send('error');
// }
}
}
module.exports = {
login
};
... ...
... ... @@ -53,11 +53,15 @@
"lottie-web": "^5.4.2",
"mysql": "^2.16.0",
"nodemon": "^1.19.1",
"passport": "^0.4.0",
"passport-oauth": "^1.0.0",
"qiniu": "^7.2.1",
"request": "^2.88.0",
"request-promise": "^4.2.2",
"serve-favicon": "^2.5.0",
"source-map": "^0.7.3",
"topsdk": "=1.0.13",
"util": "^0.12.1",
"uuid": "^3.3.2",
"vue": "^2.5.20",
"vue-awesome-swiper": "^3.1.3",
... ...