def register_user(cls, account=None, identify_code=None, passwd=None): # username may be phone_num or email if identify_code is None: identify_code = random.randint(111111, 999999) # call API send identify_code to phone num if len(account) == 11: # from app/web/weixin msg = u"[做好事]: %d ,请与10分钟内完成手机号验证操作" % identify_code send_short_message(account, msg) app.logger.debug("Get identify_code:%s for user %s" % (str(identify_code), account)) # set it to redis {account:identify_code} and set expire time redis_db.set(account, identify_code) redis_db.expire(account, 600) return {'identify_code': str(identify_code)} else: # get identify_code from redis {account:identify_code} saved_identify_code = redis_db.get(account) if identify_code == saved_identify_code: # delete identify_code from redis {account:identify_code} redis_db.delete(account) app.logger.debug("Identify success for account %s" % account) token, user_obj_id = cls.add_user(account, passwd) return {"account": account, "token": token, "user_id": user_obj_id} else: app.logger.debug("Identify error:%s,code:%s,saved:%s" % (account, identify_code, saved_identify_code)) return {'error': 'Identify code not match'}
def add_user(cls, account=None, passwd=None): app.logger.debug("add user start:[%s,%s]" % (account, passwd)) # check if account has register result_find = user_collection.find_one({'account': account}) if result_find: db_passwd_hash = result_find.get('passwd_hash') user_id = result_find.get('_id') s = Serializer(app.config['SECRET_KEY'], expires_in=6000) # 3600000=41 days token = s.dumps({'user_id': '%s' % user_id, 'passwd': db_passwd_hash}) app.logger.debug("user exsit [account:%s]:[user_id:%s]:[%s]\n" % (account, user_id, token)) return token, str(user_id) # generate token user_id = str(redis_db.incr(CURRENT_USER_ID)) s = Serializer(app.config['SECRET_KEY'], expires_in=6000) # 3600000=41 days token = s.dumps({'user_id': '%s' % user_id, 'passwd': passwd}) # save account/passwd to mongodb passwd_hash = pwd_context.encrypt(passwd) one_user = {'_id': user_id, 'account': account, 'passwd_hash': passwd_hash} # user_obj_id = user_db.user_collection.insert_one(one_user).inserted_id user_obj_id = user_db.user_collection.insert_one(one_user).inserted_id # save token to redis redis_db.set(str(user_id), token) # save user to easemob platform im_obj.register_user(user_id, user_id) app.logger.debug("add user [%s]:[%s]:[%s]:%s]" % (account, passwd_hash, token, user_id)) return token, str(user_id)
def easemob_get_token(self): token_get = redis_db.get(EASEMOB_TOKEN_KEY) token = None if token_get is not None: self.logger.info("Get token from redis " + token_get) token, expires = token_get.split("+") if token is None or time() > expires: url = endpoint + appurl + "/token" payload = {'grant_type': 'client_credentials', 'client_id': client_id, 'client_secret': client_secret} success, result = self.easemob_request(url, "POST", payload) self.logger.info("Get token from " + url + "\n" + result['access_token']) #save token to redis token = result['access_token'] token_new = result['access_token'] + "+" + str(result['expires_in'] + int(time())) redis_db.set(EASEMOB_TOKEN_KEY, token_new) return token
as Serializer, BadSignature, SignatureExpired) import pymongo from myapp.models import user_db_client from myapp.models import redis_db, CURRENT_USER_ID from myapp.ext.short_message import send_short_message from myapp.ext.easemob import EasemobIM from myapp import app user_db = user_db_client.zuohaoshi user_collection = user_db.user_collection # user_db.user_collection.create_index("_id") if redis_db.get(CURRENT_USER_ID) is None: redis_db.set(CURRENT_USER_ID, '10000') im_obj = EasemobIM(app.logger) # These keys are intentionally short, so as to save on memory in redis FRIENDS_KEY = 'FR' FOLLOWS_KEY = 'F' FOLLOWERS_KEY = 'f' BLOCKS_KEY = 'B' BLOCKED_KEY = 'b' USERID_KEY = 'U' class User(object): def __init__(self, user_id=None):