def post(self, account, val, pwd, *args, **kwargs): cur_account = fix_account_postfix(account) review_val = GAccRdsInts.send_multi_cmd( *combine_redis_cmds(review_lostpwd_val(cur_account))) if not review_val: return {'status': 3} if val != review_val: return {'status': 4} GAccRdsInts.send_multi_cmd(*combine_redis_cmds( pass_lostpwd_val(cur_account), set_account_pwd(cur_account, cipher_pwd(pwd), False))) GMQDispRdsInts.send_cmd(*[ shortcut_mq( 'gen_mysql', mysql_pack( DB_TBL_SSP_USR_LOGIN, { 'username': cur_account, 'password': cipher_pwd(pwd), 'mobile': cur_account.split('@')[0], # 'api_key': '840ebe7c2bfe4d529181063433ece0ef', }, 2)) ]) return {'status': 0}
def post(self, account, pwd, val, *args, **kwargs): """ 检查注册验证码 """ user_agent = urllib.unquote(bs2utf8( self.request.headers['user-agent'])) reg_ip = bs2utf8(self.request.remote_ip) if not is_email(account): return {'status': 1} mobile = account.partition('@')[0] if not is_mobile(mobile): return {'status': 2} if not is_reg_val_code(val): return {'status': 3} expect_code = GAccRdsInts.send_cmd(*get_newacc_reg_val(mobile)) if not expect_code: return {'status': 4} expect_code = expect_code.split(':')[0] if expect_code != val: return {'status': 4} pwd_mask = cipher_pwd(pwd) ok = GAccRdsInts.send_cmd(*set_account_pwd(account, pwd_mask)) if not ok: return {'status': 5} reg_ts = time.strftime(fmt, time.gmtime()) GMQDispRdsInts.send_multi_cmd(*combine_redis_cmds( shortcut_mq( 'gen_mysql', mysql_pack(DB_TBL_SSP_USR_LOGIN, { 'username': account, 'password': pwd_mask, 'mobile': mobile, }, 0)), shortcut_mq( 'gen_mysql', mysql_pack( DB_TBL_SSP_USR_LOGIN, { 'username': account, 'reg_agent': user_agent, 'reg_ts': reg_ts, 'reg_ip': reg_ip, }, action=0, )))) return {'status': 0}
def post(self, user_name, receivers, duplicate_to, topic, text, type, files, *args, **kwargs): ts = float('%0.2f' % time.time()) letter_id = ':'.join(('letter', str(ts), user_name, receivers)) GDevRdsInts.send_cmd( *save_letter_info(letter_id, ':'.join((topic, text, type, files)))) GDevRdsInts.send_cmd(*add_letter_outbox(user_name, letter_id, ts)) receivers = ujson.loads(receivers) logger.debug('receivers={0}'.format(receivers)) acc_noexist_list = [] for acc in receivers: acc = bs2utf8(acc) account_exist = GAccRdsInts.send_cmd(*exist_account(acc)) if not account_exist: # not in redis, check mysql sql = "select * from {db} where user_name='{user_name}'".format( db='ssp_user_login', user_name=acc) res = DBBeiqiSspInst.query(sql) if len(res) == 0: # not in mysql, so we check if it's a sn if not is_email(acc): primary = GDevRdsInts.send_cmd(*get_dev_primary(acc)) if not primary: # no primary, illegal logger.debug('acc={0} not exist'.format(acc)) acc_noexist_list.append(acc) continue else: # exist in mysql, so we cache it in redis pwd = res[0].get('password').encode('utf8') GAccRdsInts.send_cmd(*set_account_pwd(acc, pwd)) GDevRdsInts.send_cmd(*add_letter_inbox(acc, letter_id, ts)) GMQDispRdsInts.send_cmd(*shortcut_mq( 'cloud_push', # sourcer, cb, from, description push_pack(user_name, 'letter', 2, ':'.join((letter_id, topic, text, type, files)), account=acc))) return acc_noexist_list
def post(self, account, api_key, *args, **kwargs): """ 密码丢失 :param account: :param args: :param kwargs: :return: """ cur_account = fix_account_postfix(account) if not GAccRdsInts.send_cmd(*exist_account(cur_account)): sql = "select password from {db} where username='******'".format( db=DB_TBL_SSP_USR_LOGIN, username=cur_account) expect_pwd = DBBeiqiSspInst.query(sql) if len(expect_pwd) == 0: return {'status': 1} mobile = get_mobile(GAccRdsInts, api_key, cur_account) if not mobile: return {'status': 2} sms_speed = GDevRdsInts.send_cmd(*get_sms_speed()) if sms_speed is None: GDevRdsInts.send_multi_cmd(*combine_redis_cmds(init_sms_speed())) elif sms_speed >= SMS_SPEED_MAX: return {'status': 3} else: GDevRdsInts.send_cmd(*incr_sms_speed()) ts = GDevRdsInts.send_cmd(*get_user_veri_sms_time(mobile)) if ts is not None: logger.debug('veri sms, ts={0}'.format(ts)) return {'status': 4} else: GDevRdsInts.send_multi_cmd(*combine_redis_cmds( set_user_veri_sms_time(mobile, time.time()))) val = ''.join((str(random.randint(0, 9)) for _ in xrange(6))) logger.debug('lost pwd val: {0}'.format(val)) GAccRdsInts.send_multi_cmd( *combine_redis_cmds(gen_lostpwd_val(cur_account, val))) GMQDispRdsInts.send_cmd( *shortcut_mq('sms_notify', sms_notify_pack(mobile, 2, cur_account, mobile, val))) return {'status': 0}
def post(self, account, *args, **kwargs): """ 帐号状态,是否已存在 """ if not is_email(account): return {'status': 1} #帐号存在并已激活 account_exist = GAccRdsInts.send_cmd(*exist_account(account)) if account_exist: return {'status': 2} sql = "select * from {db} where username='******'".format( db=DB_TBL_SSP_USR_LOGIN, user_name=account) res = DBBeiqiSspInst.query(sql) if len(res) != 0: # exist in mysql, so we cache it pwd = res[0].get('password').encode('utf8') GAccRdsInts.send_cmd(*set_account_pwd(account, pwd)) return {'status': 2} return {'status': 0}
def reg_via_mobile(account, api_key): """ 通过手机号注册 :param account: 用户帐号 :param api_key: :return: """ mobile = account.split('@')[0] if not is_mobile(mobile): return val_code = ''.join((str(randint(0, 9)) for _ in xrange(6))) logger.debug('val_code %s sent' % val_code) #该接口需兼容oem,故填入空api_key GAccRdsInts.send_multi_cmd(*combine_redis_cmds( gen_newacc_reg_val(mobile, val_code, api_key or ''))) GMQDispRdsInts.send_cmd(*shortcut_mq( 'sms_notify', sms_notify_pack( mobile, SmsType.REGISTER, account, val_code, api_key=api_key))) logger.debug('account %s val_code %s sent' % (account, val_code)) return True
def get(self, user_name, guest, gid, msg, file, *args, **kwargs): primary = GDevRdsInts.send_cmd(*get_group_primary(gid)) if primary != user_name: return {'status': 1} account_exist = GAccRdsInts.send_cmd(*exist_account(guest)) if not account_exist: sql = "select * from {db} where user_name='{user_name}'".format( db='ssp_user_login', user_name=guest) res = self.settings.get('mysql_db').query(sql) if len(res) == 0: return {'status': 2} else: # exist in mysql, so we cache it in redis pwd = res[0].get('password').encode('utf8') GAccRdsInts.send_cmd(*set_account_pwd(guest, pwd)) sn = GDevRdsInts.send_cmd(*get_sn_of_gid(gid)) GDevRdsInts.send_cmd(*follow_group(gid, sn, guest)) payload = ujson.dumps({ 'master': user_name, 'gid': gid, 'msg': msg, 'file': file, 'action': 'invite_follow' }) GDevRdsInts.send_multi_cmd(*combine_redis_cmds( set_user_group_msglist(guest, gid, 'invite_follow', payload))) logger.debug('invite follow, guest={0}, gid={1}, payload={2}'.format( guest, gid, payload)) GMQDispRdsInts.send_cmd(*shortcut_mq( 'cloud_push', push_pack(user_name, 'invite_follow', 2, payload, account=guest))) return {'status': 0}
def get(self, username, api_key, pwd="", *args, **kwargs): """ 生成SSO认证token :param username: :param api_key: :param pwd: device no pwd, app account has pwd :param args: :param kwargs: :return: """ user_agent = urllib.unquote(bs2utf8( self.request.headers['user-agent'])) api_ob = beiqi_keys.get(api_key) if not api_ob: logger.warn("gen_tk api_ob:%s, api_key:%s" % (api_ob, api_key)) self.set_status(401) return remote_ip = bs2utf8(self.request.remote_ip) if not is_email(username): # 设备没有pid时登录 rc4_key = api_ob.get('rc4_key') if rc4_key is None: logger.debug( 'api_key={0}, username={1} rc4_key not exists'.format( api_key, username)) self.set_status(400) return sn, ts = decrypt_username(username, rc4_key) sql = "SELECT 1 FROM {db} WHERE sn = '{sn}'".format( db=DB_TBL_DEVICE_INFO, sn=sn) ret_list = DBBeiqiSspInst.query(sql) if len(ret_list) == 0: logger.debug('ret_list={0}, sn={1}'.format(ret_list, sn)) self.set_status(400) return saved_ts = GDevRdsInts.send_cmd(*get_tk_time(sn)) if saved_ts == ts: logger.debug('ts={0} the same with saved_ts'.format(ts)) self.set_status(400) return GDevRdsInts.send_cmd(*set_tk_time(sn, ts)) login_ts = time.strftime(fmt, time.gmtime()) GMQDispRdsInts.send_cmd(*shortcut_mq( 'gen_mysql', mysql_pack(DB_TBL_USER_INFO, { 'last_login_ts': login_ts, 'last_login_ip': remote_ip, 'last_login_agent': user_agent }, action=2, ref_kvs={'username': sn}))) return gen_token(api_ob.get('s'), sn, 1, account_rds=GAccRdsInts) gid = username.split('@')[0] if len(gid) == 6: # 设备登录 sn = GDevRdsInts.send_cmd(*get_sn_of_gid(gid)) logger.debug('beiqi sso, username=%r, gid=%r, sn=%r' % (username, gid, sn)) # primary = dev_filter.send_cmd(*get_dev_primary(pid)) if sn: # django used gmttime, so we'd better use gmttime. login_ts = time.strftime(fmt, time.gmtime()) GMQDispRdsInts.send_cmd(*shortcut_mq( 'gen_mysql', mysql_pack(DB_TBL_USER_INFO, { 'last_login_ts': login_ts, 'last_login_ip': remote_ip, 'last_login_agent': user_agent }, action=2, ref_kvs={'username': username}))) return gen_token(api_ob.get('s'), username, 1, account_rds=GAccRdsInts) else: logger.debug('gid={0} invalid no sn'.format(gid)) self.set_status(403) return expect_pwd = GAccRdsInts.send_cmd(*get_pwd(username)) if expect_pwd is not None: if expect_pwd != cipher_pwd(pwd): logger.warn( 'pwd incorrect: username = {0}, pwd={1}, expect_pwd={2}'. format(username, cipher_pwd(pwd), expect_pwd)) self.set_status(401) return else: # not in redis, check mysql sql = "select password from {db} where username='******'".format( db='ssp_user_login', username=username) expect_pwd = DBBeiqiSspInst.query(sql) if len(expect_pwd) == 0: logger.debug('account={0} not exist'.format(username)) self.set_status(401) return else: pwd_inmysql = expect_pwd[0].get('password') pwd_inmysql = pwd_inmysql.encode( 'utf8') if pwd_inmysql is not None else pwd_inmysql if pwd_inmysql != cipher_pwd(pwd): logger.debug( 'pwd incorrect: username = {0}, pwd={1}, expect_pwd={2}' .format(username, cipher_pwd(pwd), expect_pwd)) self.set_status(401) return login_ts = time.strftime(fmt, time.gmtime()) GMQDispRdsInts.send_cmd(*shortcut_mq( 'gen_mysql', mysql_pack(DB_TBL_USER_INFO, { 'last_login_ts': login_ts, 'last_login_ip': remote_ip, 'last_login_agent': user_agent }, action=2, ref_kvs={'username': username}))) return gen_token(api_ob.get('s'), username, 1, account_rds=GAccRdsInts)