auth-helper.js 3.61 KB
'use strict';
const _ = require('lodash');
const aes = require('./aes-pwd');
const authcode = require('../../../utils/authcode');
const sign = global.yoho.sign;
const api = global.yoho.API;
const uuid = require('uuid');

class Auth {

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

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

        return api.post('', param);
    }

    static signinAes(area, profile, password, shoppingKey) {
        let param = {
            method: 'app.passport.signinWebV2',
            area: area,
            profile: profile,
            password: aes.aesPwd(password)
        };

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

        return api.post('', param);
    }

    static signinByOpenID(nickname, openId, sourceType, shoppingKey) {
        let param = {
            nickname: nickname,
            openId: openId,
            source_type: sourceType, // esline-disable-line
            method: 'app.passport.signinByOpenID',
            shoppingKey: shoppingKey
        };

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

        return api.get('', param);
    }

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

        return api.get('', param);
    }

    static 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) // 有效期一年
            });
            userId.sessionKey = sessionKey;
        }
        return Auth.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) // 有效期一年
            });
        });
    }
}

module.exports = Auth;