Beispiel #1
0
    def post(self):
        #import pdb; pdb.set_trace()
        if login_check(session):
            return marshal({'status':'already login', 'user_id':None}, app_auth_fields)

        args = AppAuthArgs().args
        args['user_name'] = args['user_name'].encode('utf-8')
        args['password'] = args['password'].encode('utf-8')
        request_url = APP_AUTH_URL % (args['user_name'], args['password'])
        r = requests.get(request_url)
        if r.status_code != requests.codes.ok:
            return marshal({'status':'can not get rs response', 'user_id':None, 'user_name':None}, app_auth_fields)
        result = json.loads(r.text)
        if result['status'] != 'ok':
            return marshal({'status':result['status'], 'user_id':None, 'user_name':None}, app_auth_fields)

        session['login_user'] = result['user_id']
        #update user table
        if not user_model.get_user(user_id=result['user_id']):
            user_model.add_user(result['user_id'])

        #update login_user table
        login_user = login_user_model.get_login_user(user_id=result['user_id'])
        if login_user:
            login_user_model.delete_login_user(login_user[0])
        cookie_key = hashlib.md5(result['user_id']).hexdigest().lower()
        login_user_model.add_login_user(user_id=result['user_id'], cookie_key=cookie_key, user_name=result['user_name'])

        return marshal({'status':'success', 'user_id':result['user_id'], 'user_name': result['user_name']}, app_auth_fields)
Beispiel #2
0
    def get(self, request_token):
        oauth_info = oauth_model.get_oauth(request_token=request_token)[0]
        #get access token
        access_url = GET_ACCESS_URL % (request_token, APP_ID, APP_SECRET)
        r = requests.get(access_url)
        if r.status_code != requests.codes.ok:
            return marshal({'status': 'can not get access token'}, oauth_access_fields)
        result = json.loads(r.text)
        if result['status'] != 'success' or result['app_id'] != APP_ID:
            return marshal({'status': 'can not get access token'}, oauth_access_fields)
        access_token = result['access_token']

        #get login user id
        login_user_id_url = GET_LOGIN_USER_ID_URL % (access_token, APP_ID, APP_SECRET)
        r = requests.get(login_user_id_url)
        if r.status_code != requests.codes.ok:
            return marshal({'status': 'can not get login user id'}, oauth_access_fields)
        result = json.loads(r.text)
        if result['status'] != 'success':
            return marshal({'status': 'can not get login user id'}, oauth_access_fields)

        #update session
        session['login_user'] = result['user_id']
        #self.set_cookie('remember_login_user', oauth_info.user_recognition)

        #update user table
        if not user_model.get_user(user_id=result['user_id']):
            user_model.add_user(result['user_id'])

        #update login_user table
        login_user = login_user_model.get_login_user(user_id=result['user_id'])
        if login_user:
            login_user_model.delete_login_user(login_user[0])

        cookie_key=oauth_info.user_recognition
        login_user_model.add_login_user(user_id=result['user_id'], user_name=result['user_name'], cookie_key=cookie_key)

        #check oauth table to delete useless information
        oauth_model.delete_oauth(oauth_info)
        oauth_model.delete_expire_oauth()

        return {}, 301, {'Location': '/'}