auth-helper.js 7.71 KB
'use strict';
const _ = require('lodash');
const aes = require('./aes-pwd');
const authcode = require('../../../utils/authcode');
const logger = global.yoho.logger;
const sign = global.yoho.sign;
const config = global.yoho.config;
const utils = require('../../../utils');
const uuid = require('uuid');
const url = require('url');
const md5 = require('yoho-md5');
const moment = require('moment');
const querystring = require('querystring');
const thirdAccount = require('../data/third-account.json');
const FROM = require('../../../config/from');
const PAGE = 'H5';

class AuthModel extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    signin(area, profile, password, shoppingKey) {
        let param = {
            method: 'app.passport.signin',
            area: area,
            profile: profile,
            password: password
        };

        if (shoppingKey) {
            param.shopping_key = shoppingKey;
        }

        return this.post({data: param});
    }

    signinAes(params) {
        let param = {
            method: 'smart.forward.go', // sesame.flowering.higher, smart.inner.go
            area: params.area,
            profile: params.profile,
            password: aes.aesPwd(params.password),
            isSkip: params.isSkip ? params.isSkip : 'N',
            business_line: FROM[params.from].business_line,
            udid: params.udid,
            fromPage: PAGE,
            degrees: params.degrees,
            superCapture: params.superCapture
        };

        if (params.shoppingKey) {
            param.shopping_key = params.shoppingKey;
        }

        logger.info(`${params.profile}, login from ${params.ip}`);

        return this.post({
            data: param
        });
    }

    signinByOpenID(params) {
        let param = {
            nickname: params.nickname,
            openId: params.openId,
            source_type: params.sourceType, // esline-disable-line
            method: 'app.passport.signinByOpenID',
            business_line: FROM[params.from].business_line
        };

        if (params.shoppingKey) {
            param.shopping_key = params.shoppingKey;
        }

        return this.get({data: param});
    }

    profile(uid) {
        let param = {
            uid: uid,
            method: 'app.passport.profile'
        };

        return this.get({data: param});
    }

    thirdSign(params, clientSecret) {
        let secretParams = {},
            secretStr = '';

        for (const k of Object.keys(params).sort()) {
            if (k === 'yh_sign') {
                continue;
            }
            secretParams[k] = params[k];
        }
        secretStr = _.join(_.map(secretParams, (v, k) => {
            return `${k}=${v}`;
        }), '&');
        return md5(secretStr + clientSecret);
    }

    referUrl(req, res, uid, sessionKey) {
        if (req.cookies.third_type && req.cookies.third_backurl && req.cookies.third_refer) {
            let backurl = url.parse(req.cookies.third_backurl),
                account = thirdAccount[req.cookies.third_type];

            let params = Object.assign({
                yh_uid: uid,
                yh_type: req.cookies.third_type,
                yh_time: moment().format('YYYY-MM-DD HH:mm:ss')
            }, querystring.parse(backurl.query));
            let signStr = this.thirdSign(params, account.clientSecret);

            params.yh_sign = signStr;
            let queryStr = _.join(_.map(params, (v, k) => {
                return `${k}=${encodeURIComponent(v)}`;
            }), '&');

            return {
                refer: `${backurl.protocol}//${backurl.host}${backurl.pathname}?${queryStr}`
            };
        } else {
            res.clearCookie('third_type', {
                domain: 'yohobuy.com'
            });
            res.clearCookie('third_backurl', {
                domain: 'yohobuy.com'
            });
            res.clearCookie('third_refer', {
                domain: 'yohobuy.com'
            });
            try {
                let refer = req.cookies.refer;

                if (refer) {
                    refer = decodeURI(refer);
                } else {
                    refer = `${config.siteUrl}/home`;
                }

                if (/sign|login/.test(refer)) {
                    refer = `${config.siteUrl}/home`;
                }

                refer = utils.refererLimit(refer);

                let referUrl;

                let backurl = url.parse(refer, false, true);
                const qs = querystring.parse(backurl.query);

                if (!qs.skcall) {
                    return; // 不处理 外层有各自的refer逻辑
                }
                let params = Object.assign({
                    uid: uid,
                    app_version: config.appVersion,
                    app_client_type: config.app,
                    session_key: sessionKey
                }, qs);
                let queryStr = _.join(_.map(params, (v, k) => {
                    return `${k}=${encodeURIComponent(v)}`;
                }), '&');

                if (backurl.hostname && backurl.protocol) {
                    referUrl = `${backurl.protocol}//${backurl.hostname}`;
                }
                referUrl = `${referUrl || ''}${backurl.pathname}?${queryStr}`;

                return {
                    refer: referUrl
                };
            } catch (e) {
                return;
            }
        }
    }

    syncUserSession(uid, req, res, sessionKey) {
        let userId = {
            toString: () => {
                return uid;
            }
        };

        if (sessionKey) {
            // 弃用
            // global.yoho.cache.set(`java_session_key:${uid}`, sessionKey).catch(() => {
            //     global.yoho.logger.error('write session key fail');
            // });
            req.session.SESSION_KEY = sessionKey;
            res.cookie('_SESSION_KEY', authcode(sessionKey, '_SESSION_KEY', 2592000000, 'encode'), {
                domain: 'yohobuy.com',
                expires: new Date(Date.now() + 2592000000), // 有效期一年
                httpOnly: true
            });
            userId.sessionKey = sessionKey;
        }
        res.cookie('_LOGIN_IS_REPORT', false, {
            domain: 'm.yohobuy.com',
            path: '/'
        });
        return this.profile(userId).then((userInfo) => {
            let salt = uuid.v4().substr(0, 8);
            let saltedUid = uid + salt;

            let saltedToken = sign.makeToken(saltedUid);
            let publicToken = saltedToken + salt;

            let data = userInfo.data;
            let encryptionUid = aes.encryptionUid(uid);

            if (data) {
                data.profile_name = (data.profile_name || '').replace(/::/g, '');

                let uidCookie =
                    `${data.profile_name}::${encryptionUid}::${data.vip_info && data.vip_info.title}::${saltedToken}`;

                res.cookie('_UID', uidCookie, {
                    domain: 'yohobuy.com',
                    expires: new Date(Date.now() + 2592000000) // 有效期一年
                });

                req.session.AVATAR = data.head_ico;
                _.set(req.session, 'USER.AVATAR', data.head_ico);
                _.set(req.session, 'USER.NAME', data.profile_name);
            }

            req.session.TOKEN = publicToken;
            req.session.LOGIN_UID_ = uid;

            _.set(req.session, 'USER.ENCRYPTION_UID', encryptionUid);

            res.cookie('_TOKEN', publicToken, {
                httpOnly: true,
                domain: 'yohobuy.com',
                expires: new Date(Date.now() + 2592000000) // 有效期一年
            });

            // 登录回调地址
            return this.referUrl(req, res, uid, sessionKey);
        });
    }
}

module.exports = AuthModel;