def post(self, request): user_id = request.POST['userId'] user_pwd = request.POST['userPwd'] try: fit_token = get_fit_token(user_id, user_pwd) print(fit_token) user_data = NewMyfit.user_mst().find_one({'user_id': user_id}, { 'user_uid': 1, '_id': 0 }) encrypted_fit_token = super().encrypt_fit_token( user_data['user_uid'], fit_token) NewMyfit.user_mst().update( {'user_uid': user_data['user_uid']}, {'$set': { 'fit_token': encrypted_fit_token }}) return HttpResponse(json.dumps({'fit_token': True})) except: return HttpResponse(json.dumps({'fit_token': False}))
def post(self, request): user_uid = request.POST['userUid'] encrypted_fit_token = self._select_fit_token(user_uid) fit_token = self._decrypt_fit_token( user_uid, encrypted_fit_token).decode('utf-8') notice_info_list = scrape.scrape_notice_info(fit_token) # # loop = asyncio.new_event_loop() # asyncio.set_event_loop(loop) # asyncio.ensure_future(self.update_local_db(user_uid, notice_info_list, loop)) # loop.run_forever() """ 受信した情報を古い順に保存していく DBの末尾が最新のものになる """ for notice_info in reversed(notice_info_list): NewMyfit.stu_mst().update( {'user_uid': user_uid}, {'$addToSet': { 'notice_info': notice_info }}) """ ここにMySQLの非同期処理を書く """ return HttpResponse(json.dumps({'mess': notice_info_list}))
def post(request): print('auto login', request.COOKIES) user_uid = request.POST['userUid'] user_data = NewMyfit.user_mst().find_one({'user_uid': user_uid}, { 'user_uid': 1, '_id': 0 }) if user_data: user_info = NewMyfit.stu_mst().find_one( {'user_uid': user_data['user_uid']}, { 'user_uid': 1, 'user_class': 1, 'user_grade': 1, '_id': 0 }) return HttpResponse( json.dumps({ 'authRes': True, 'session': { 'userUid': user_info['user_uid'], 'userGrade': user_info['user_grade'], 'userClass': user_info['user_class'] } })) return HttpResponse(json.dumps({'authRes': False}))
def create_user_uid(user_id, user_pwd): # uuidの作成 user_uid = hashlib.sha256(user_id.encode() + user_pwd.encode()).hexdigest() NewMyfit.user_mst().insert({'user_uid': user_uid, 'user_id': user_id}) return user_uid
async def update_local_db(self, user_uid, notice_info_list, loop): await self.scrape_notice_detail(loop) for notice_info in reversed(notice_info_list): NewMyfit.stu_mst().update( {'user_uid': user_uid}, {'$addToSet': { 'notice_info': notice_info }}) return HttpResponse(json.dumps({'mess': notice_info_list}))
def encrypt_user_data(user_uid, user_pwd): # キーの設定 cipher = AESCipher(user_uid) encrypted_user_pwd = cipher.encrypt(user_pwd) NewMyfit.user_mst().update({'user_uid': user_uid}, {'$set': { 'user_pwd': encrypted_user_pwd }}) return
def extract_notice_info(user_uid): user_data_detail = NewMyfit.stu_mst().find_one({'user_uid': user_uid}, { 'notice_info': 1, '_id': 0 }) return user_data_detail['notice_info']
def extract_class_info(user_uid): print('check user_uid', user_uid) user_data_detail = NewMyfit.stu_mst().find_one({'user_uid': user_uid}, { 'class_info': 1, '_id': 0 }) return user_data_detail['class_info']
def uid_to_token(user_uid): print(user_uid) user_data = NewMyfit.user_mst().find_one({'user_uid': user_uid}, { 'fit_token': 1, '_id': 0 }) cipher = AESCipher(user_uid) fit_token = cipher.decrypt(user_data['fit_token']).decode('utf-8') return fit_token
def post(self, request): user_uid = request.POST['userUid'] encrypted_fit_token = self.fetch_token_from_db(user_uid=user_uid) fit_token = self.decrypt_fit_token(user_uid, encrypted_fit_token).decode('utf-8') print('old fit_token', fit_token) is_token_alive = self.token_pre_request(fit_token) print('is token_alive', is_token_alive) # 既存tokenのセッションが有効な場合 if is_token_alive: print('aliving token', fit_token) return HttpResponse(json.dumps({'fit_token': True})) user_data = NewMyfit.user_mst().find_one({'user_uid': user_uid}, { 'user_id': 1, 'user_pwd': 1, '_id': 0 }) user_pwd = self.decrypt_user_data( user_uid, user_data['user_pwd']).decode('utf-8') print('user_pwd', user_pwd) try: fit_token = webdrive.get_fit_token(user_data['user_id'], user_pwd) print('new fit_token', fit_token) encrypted_fit_token = super().encrypt_fit_token( user_uid, fit_token) NewMyfit.user_mst().update( {'user_uid': user_uid}, {'$set': { 'fit_token': encrypted_fit_token }}) return HttpResponse(json.dumps({'fit_token': True})) except: return HttpResponse(json.dumps({'fit_token': False}))
def fetch_token_from_db(user_uid=None, user_id=None): """ :param user_uid: :param user_id: :return: dbから取得した既存のfit_token """ encrypted_fit_token = '' if user_uid: user_data = NewMyfit.user_mst().find_one({'user_uid': user_uid}, { 'fit_token': 1, '_id': 0 }) encrypted_fit_token = user_data['fit_token'] elif user_id: user_data = NewMyfit.user_mst().find_one({'user_id': user_id}, { 'fit_token': 1, '_id': 0 }) encrypted_fit_token = user_data['fit_token'] return encrypted_fit_token
def create_stu_mst(user_uid): NewMyfit.stu_mst().remove({'user_uid': user_uid}) NewMyfit.stu_mst().insert({'user_uid': user_uid}) NewMyfit.stu_mst().update( {'user_uid': user_uid}, {'$set': { 'notice_info': [], 'class_info': [] }})
def get(request): user_uid = request.GET.get('userUid') print('time_table', user_uid) time_table = NewMyfit.stu_mst().find_one({'user_uid': user_uid}, { 'pro_time_table': 1, 'pro_credit_num': 1, '_id': 0 }) # pprint.pprint(time_table['pro_time_table']) return HttpResponse( json.dumps({ 'timeTable': time_table['pro_time_table'], 'creditNum': time_table['pro_credit_num'] }))
def _select_fit_token(user_uid): user_data = NewMyfit.user_mst().find_one({'user_uid': user_uid}, { 'fit_token': 1, '_id': 0 }) return user_data['fit_token']
def post(self, request): # csrf_token = csrf.get_token(request) print('main login', request.COOKIES) user_id = request.POST['user_id'] user_pwd = request.POST['user_password'] request.session['user_id'] = user_id server = self.get_ldap_server(LDAP_SERVER_NAME, LDAP_SERVER_PORT) # サーバの生存確認 if not server.check_availability(): print('server did not exist') return None user = '******' % user_id try: conn = self.set_ldap_connection(server, user, user_pwd) except: return None user_uid = NewMyfit.user_mst().find_one({'user_id': user_id}, { 'user_uid': 1, '_id': 0 }) # ユーザがすでに登録されている if (user_uid): # request.session.create() # request.session['user_uid'] = user_uid['user_uid'] # print(request.session.session_key) response = HttpResponse( json.dumps({ 'authRes': True, 'session': { 'userUid': user_uid['user_uid'], 'userGrade': 0, 'userClass': 0 } })) # response.set_cookie('csrftoken', csrf_token) # print(response) # response["Access-Control-Allow-Origin"] = "localhost:4200" # response['Access-Control-Allow-Credentials'] = 'true' # response['Access-Control-Allow-Headers'] = "Content-Type, Accept, X-CSRFToken" # response['Access-Control-Allow-Methods'] = "POST, OPTIONS" return response # uuidの作成 user_uid = self.create_user_uid(user_id, user_pwd) self.encrypt_user_data(user_uid, user_pwd) self.create_stu_mst(user_uid) return HttpResponse( json.dumps({ 'authRes': True, 'session': { 'userUid': user_uid, 'userGrade': 0, 'userClass': 0 } }))