From 4ec6f5e57ac9bcccb4a84ec9ba087b2b2e78f94d Mon Sep 17 00:00:00 2001 From: strawmanbobi Date: Wed, 7 Dec 2016 19:25:22 +0800 Subject: [PATCH] updated admin auth procedure - debug 1 --- .../configuration/system_configs.js | 8 +- .../{script_compiler.sh => script_compile.sh} | 1 + .../{script_npm.sh => script_init.sh} | 3 - .../work_unit/certificate_logic.js | 149 +++++++----------- src/web_console/work_unit/internal_logic.js | 5 - 5 files changed, 62 insertions(+), 104 deletions(-) rename src/web_console/{script_compiler.sh => script_compile.sh} (97%) rename src/web_console/{script_npm.sh => script_init.sh} (94%) diff --git a/src/web_console/configuration/system_configs.js b/src/web_console/configuration/system_configs.js index f0819a1..f273179 100644 --- a/src/web_console/configuration/system_configs.js +++ b/src/web_console/configuration/system_configs.js @@ -40,8 +40,8 @@ exports.setupEnvironment = function () { REDIS_HOST = "localhost"; REDIS_PORT = "6379"; REDIS_PASSWORD = ""; - EXTERNAL_SERVER_ADDRESS = "irext.net"; - EXTERNAL_SERVER_PORT = "80" + EXTERNAL_SERVER_ADDRESS = "www.strawmanbobi.com"; + EXTERNAL_SERVER_PORT = "8200" } else if (enums.APP_USERDEBUG_MODE == env) { MYSQL_DB_SERVER_ADDRESS = "localhost"; MYSQL_DB_NAME = "irext"; @@ -54,7 +54,7 @@ exports.setupEnvironment = function () { REDIS_HOST = "localhost"; REDIS_PORT = "6379"; REDIS_PASSWORD = ""; - EXTERNAL_SERVER_ADDRESS = "irext.net"; - EXTERNAL_SERVER_PORT = "80" + EXTERNAL_SERVER_ADDRESS = "www.strawmanbobi.com"; + EXTERNAL_SERVER_PORT = "8200" } }; \ No newline at end of file diff --git a/src/web_console/script_compiler.sh b/src/web_console/script_compile.sh similarity index 97% rename from src/web_console/script_compiler.sh rename to src/web_console/script_compile.sh index ddbb456..ca83ee5 100644 --- a/src/web_console/script_compiler.sh +++ b/src/web_console/script_compile.sh @@ -1,3 +1,4 @@ +#!/usr/bin/env bash SOURCE="." TARGET="$POEM_APPLICATION/irext_console" MODULE_API="./web/api_doc/js/" diff --git a/src/web_console/script_npm.sh b/src/web_console/script_init.sh similarity index 94% rename from src/web_console/script_npm.sh rename to src/web_console/script_init.sh index 50359ed..51b68cb 100755 --- a/src/web_console/script_npm.sh +++ b/src/web_console/script_init.sh @@ -44,9 +44,6 @@ npm install form-data echo "npm install async" npm install async -echo "npm install nodemailer" -npm install nodemailer@0.7 - echo "npm install done" echo "create logging directory" diff --git a/src/web_console/work_unit/certificate_logic.js b/src/web_console/work_unit/certificate_logic.js index f9165ec..5a59506 100644 --- a/src/web_console/work_unit/certificate_logic.js +++ b/src/web_console/work_unit/certificate_logic.js @@ -9,7 +9,7 @@ var Admin = require('../model/admin_dao.js'); var AdminAuth = require('../authority/admin_auth.js'); var MD5 = require('../mini_poem/crypto/md5.js'); var StringUtils = require('../mini_poem/utils/string_utils.js'); -var nodemailer = require('nodemailer'); +var RequestSender = require('../mini_poem/http/request.js'); var Enums = require('../constants/enums.js'); var ErrorCode = require('../constants/error_code.js'); @@ -20,35 +20,45 @@ var errorCode = new ErrorCode(); var adminAuth = new AdminAuth(REDIS_HOST, REDIS_PORT, REDIS_PASSWORD, null); -exports.adminLoginWorkUnit = function (userName, password, callback) { - var conditions = { - user_name: userName, - password: password - }; - Admin.findAdminsByConditions(conditions, 0, 1, "id", function(findAdminErr, admins) { - if (findAdminErr.code == errorCode.SUCCESS.code && - null != admins && admins.length > 0) { - // add information of this user into cache - var userID, - token, - key, - ttl = 24 * 60 * 60 * 14, - timeStamp, - admin; +var signInService = "/irext/certificate/admin_login"; +var changePwService = "/irext/certificate/change_pw"; - admin = admins[0]; - timeStamp = new Date().getTime(); - token = MD5.MD5(password + timeStamp); - token += "," + admin.permissions; - key = "admin_" + admin.id; - adminAuth.setAuthInfo(key, token, ttl, function(setAdminAuthErr) { - admin.token = token; - callback(setAdminAuthErr, admin); - }); - } else { - callback(errorCode.AUTHENTICATION_FAILURE, null); - } - }); +exports.adminLoginWorkUnit = function (userName, password, callback) { + var queryParams = new Map(); + + var requestSender = + new RequestSender(EXTERNAL_SERVER_ADDRESS, + EXTERNAL_SERVER_PORT, + signInService, + queryParams); + + var signinInfo = { + user_name : userName, + password : password + }; + requestSender.sendPostRequest(signinInfo, + function(signInRequestErr, signInResponse) { + if (signInRequestErr == errorCode.SUCCESS.code && null != signInResponse) { + var admin = signInResponse; + var userID, + token, + key, + ttl = 24 * 60 * 60 * 14, + timeStamp, + admin; + timeStamp = new Date().getTime(); + token = MD5.MD5(password + timeStamp); + token += "," + admin.permissions; + key = "admin_" + admin.id; + adminAuth.setAuthInfo(key, token, ttl, function(setAdminAuthErr) { + admin.token = token; + callback(setAdminAuthErr, admin); + }); + } else { + logger.error("admin sign in failed"); + callback(errorCode.FAILED, null); + } + }); }; exports.verifyTokenWorkUnit = function (id, token, callback) { @@ -84,70 +94,25 @@ exports.verifyTokenWithPermissionWorkUnit = function (id, token, permissions, ca }; exports.sendChangePwMailWorkUnit = function (userName, callback) { - var conditions = { - user_name: userName - }; - Admin.findAdminsByConditions(conditions, 0, 1, "id", function(getAdminErr, admins) { - if (errorCode.SUCCESS.code == getAdminErr.code && undefined != admins && null != admins && admins.length > 0) { - var admin = admins[0]; - var userEmail = admin.user_name; - var sendEmailErr; - var newPw = StringUtils.randomNumber(6); - var timeStamp = new Date().getTime(); - var passwdKey = MD5.MD5(userName.toString() + timeStamp); - var passwdMD5 = MD5.MD5(newPw, true).toUpperCase(); - var ttl = 2 * 60 * 60; + var queryParams = new Map(); - // save password fetch key and password MD5 value to cache first - var smtpTransport = nodemailer.createTransport("SMTP", { - host: "smtp.163.com", - name: "", - secureConnection: true, - use_authentication: true, - port: 465, - auth: { - user: "strawmanbobi@163.com", - pass: "Fs11233209." - } - }); - adminAuth.setAuthInfo(passwdKey, passwdMD5, ttl, function(setPasswordAuthErr) { - if (setPasswordAuthErr.code == errorCode.SUCCESS.code) { - logger.info("save temp password successfully, continue process email post"); - // send email to notify user - smtpTransport.sendMail({ - from : "strawmanbobi@163.com", - to : userEmail , - subject: "分配新密码", - generateTextFromHTML : true, - html: ""+ - "

Yo-- 这是 irext 数据中心为您随机分配的新密码,请牢记之后,点击它表示确认 <( ̄︶ ̄)>

" + - "" + newPw + "" - }, function(error, response) { - if(error) { - sendEmailErr = errorCode.FAILED; - logger.info("send change password email failed :" + error); - logger.info(sendEmailErr+" userLogic....."); - callback(sendEmailErr); - } else { - sendEmailErr = errorCode.SUCCESS; - logger.info("change password email send successfully : " + response.message); - logger.info(sendEmailErr+" userLogic....."); - callback(sendEmailErr); - } - smtpTransport.close(); - }); - } else { - logger.info("failed to save temp password"); - callback(setPasswordAuthErr); - } - }); - } else { - logger.info("no admin info found"); - callback(errorCode.FAILED); - } - }); + var requestSender = + new RequestSender(EXTERNAL_SERVER_ADDRESS, + EXTERNAL_SERVER_PORT, + changePwService, + queryParams); + + var userInfo = { + user_name : userName + }; + requestSender.sendPostRequest(userInfo, + function(changePwRequestErr, changePwResponse) { + if (changePwRequestErr == errorCode.SUCCESS.code && null != changePwResponse) { + callback(errorCode.SUCCESS); + } else { + callback(errorCode.FAILED); + } + }); }; exports.confirmPasswordWorkUnit = function(id, fetchKey, callback) { diff --git a/src/web_console/work_unit/internal_logic.js b/src/web_console/work_unit/internal_logic.js index 37ec8bc..a3d43dd 100644 --- a/src/web_console/work_unit/internal_logic.js +++ b/src/web_console/work_unit/internal_logic.js @@ -35,11 +35,6 @@ var async = require('async'); // relative XML file path var PROTOCOL_PATH = "protocol"; -// out going HTTP request parameters -// var PRIMARY_SERVER_ADDRESS = "irext.net"; -var PRIMARY_SERVER_ADDRESS = "127.0.0.1"; -var PRIMARY_SERVER_PORT = "8200"; - var REQUEST_APP_KEY = "d6119900556c4c1e629fd92d"; var REQUEST_APP_TOKEN = "fcac5496cba7a12b3bae34abf061f526";