Beispiel #1
0
def autho(uid):
    try:
        code = flask.request.args['code']
        rep = requests.post('https://%s%s' %
                            (config.OAUTH_URL, config.OAUTH_REQUIRE_TOKEN_URL),
                            data={
                                'client_id': config.CLIENT_ID,
                                'code': code,
                                'grant_type': 'authorization_code',
                                'ip': getMyIP()
                            },
                            verify=False)

        repmessage = json.loads(rep.content)
        token_nodecrypt = repmessage['access_token']
        refresh_token_nodecrypt = repmessage['refresh_token']
        token_crypt = decrypt(token_nodecrypt)

        verifyrep = requests.post('https://%s%s' %
                                  (config.OAUTH_URL, config.OAUTH_VARIFY_URL),
                                  data={
                                      'client_id': config.CLIENT_ID,
                                      'token': token_crypt
                                  },
                                  verify=False)

        verifymessage = json.loads(verifyrep.content)

        if verifymessage['errors_code'] == 0:
            user_id = verifymessage['userID']
            user_name = verifymessage['username']
            LoginInfoDao.deleteByuserid(user_id)
            login_info = LoginInfo(uid, token_nodecrypt,
                                   refresh_token_nodecrypt, user_id, user_name,
                                   time.time())
            LoginInfoDao.add(login_info)
            url = flask.url_for('test')
            return flask.redirect(url)

        else:
            return flask.jsonify({
                'code': 'can not login',
                'errors_code': verifymessage['errors_code']
            })

    except:
        # config.Log.info(traceback.format_exc())
        return flask.jsonify(
            {'code':
             'can not login'})  #can not login出现时别忘了把他替换成ErrorCode.logingError
Beispiel #2
0
def refreshToken(userid):
    logininfo = LoginInfoDao.queryByuserid(userid)
    uid = 'aaaaa' if config.DEBUG_STATUS else logininfo.uid
    refreshtoken = logininfo.refreshtoken
    refreshrep = requests.post(
        'https://%s%s' % (config.OAUTH_URL, config.OAUTH_REQUIRE_TOKEN_URL),
        data={
            'grant_type': 'refresh_token',
            'refresh_token': refreshtoken,
            'client_id': config.CLIENT_ID
        },
        verify=False)
    newtokenmessage = json.loads(refreshrep.content)

    newtoken = newtokenmessage['access_token']
    newtoken_crypt = crypt(newtoken)
    newrefreshtoken = newtokenmessage['refresh_token']

    newverifyrep = requests.post('https://%s%s' %
                                 (config.OAUTH_URL, config.OAUTH_VARIFY_URL),
                                 data={
                                     'client_id': config.CLIENT_ID,
                                     'token': newtoken_crypt
                                 },
                                 verify=False)
    newverifymessage = json.loads(newverifyrep.content)

    if 'errors_code' not in newverifymessage:
        user_id = newverifymessage['userID']
        user_name = newverifymessage['username']
        LoginInfoDao.deleteByuserid(user_id)
        login_info = LoginInfo(uid, newtoken, newrefreshtoken, user_id,
                               user_name, time.time())
        LoginInfoDao.add(login_info)
        # config.Log.info('newtoken = %s' % newtoken)
        return newtoken
    else:
        return None
Beispiel #3
0
def autho(uid):
    try:
        access_token = flask.request.args['access_token']
        token = decrypt(access_token)

        rep = requests.post('https://%s%s' % (config.OAUTH_URL, config.OAUTH_VARIFY_URL),
                            data={'client_id': config.CLIENT_ID, 'token': token},
                            verify=False
                            )

        message = json.loads(rep.content)

        if message['errors_code'] == 0:
            user_id = message["userID"]
            LoginInfoDao.deleteByuserid(user_id)
            login_info = LoginInfo(uid, access_token, user_id, time.time())
            LoginInfoDao.add(login_info)
            url = RedirectDao.pop(uid) or config.INDEX_PAGE
            return flask.redirect(url)
        else:
            return flask.jsonify({'code': ErrorCode.LoginError, 'errors_code': message['errors_code']})
    except:
        config.Log.info(traceback.format_exc())
        return flask.jsonify({'code': ErrorCode.LoginError})
Beispiel #4
0
def needLogin():
    try:

        if flask.request.form.get('token'):
            token = flask.request.form.get('token')
            token = decrypt(token)  #需不需要我自动更新呢
            verifyrep = requests.post(
                'https://%s%s' % (config.OAUTH_URL, config.OAUTH_VARIFY_URL),
                data={
                    'client_id': config.CLIENT_ID,
                    'token': token
                },
                verify=False)
            verifymessage = json.loads(verifyrep.content)
            if verifymessage['errors_code'] == 0:
                return False
        else:
            uid = flask.session['uid']
            logininfo = LoginInfoDao.queryByuid(uid)
            token = decrypt(logininfo.token)
            refreshtoken = logininfo.refreshtoken
            verifyrep = requests.post(
                'https://%s%s' % (config.OAUTH_URL, config.OAUTH_VARIFY_URL),
                data={
                    'client_id': config.CLIENT_ID,
                    'token': token
                },
                verify=False)
            verifymessage = json.loads(verifyrep.content)
            if verifymessage['errors_code'] == 0:
                return False
            if verifymessage['errors_code'] == 10004002:
                # 需要在数据库中更新token和refreshtoken
                refreshrep = requests.post(
                    'https://%s%s' %
                    (config.OAUTH_URL, config.OAUTH_REQUIRE_TOKEN_URL),
                    data={
                        'grant_type': 'refresh_token',
                        'refresh_token': refreshtoken,
                        'client_id': config.CLIENT_ID
                    },
                    verify=False)
                newtokenmessage = json.loads(refreshrep.content)
                newtoken = newtokenmessage['access_token']
                newtoken_crypt = decrypt(newtoken)
                newrefreshtoken = newtokenmessage['refresh_token']

                newverifyrep = requests.post(
                    'https://%s%s' %
                    (config.OAUTH_URL, config.OAUTH_VARIFY_URL),
                    data={
                        'client_id': config.CLIENT_ID,
                        'token': newtoken_crypt
                    },
                    verify=False)
                newverifymessage = json.loads(newverifyrep.content)
                if newverifymessage['errors_code'] == 0:
                    user_id = newverifymessage['userID']
                    user_name = newverifymessage['username']
                    LoginInfoDao.deleteByuserid(user_id)
                    login_info = LoginInfo(uid, newtoken, newrefreshtoken,
                                           user_id, user_name, time.time())
                    LoginInfoDao.add(login_info)
                    return False
                else:
                    return True
            else:
                return True
    except:
        return True
Beispiel #5
0
def autho(uid):
    try:
        code = flask.request.args['code']

        config.Log.info('line 63, uid = %s' % code)

        rep = requests.post('https://%s%s' %
                            (config.OAUTH_URL, config.OAUTH_REQUIRE_TOKEN_URL),
                            data={
                                'client_id': config.CLIENT_ID,
                                'code': code,
                                'grant_type': 'authorization_code',
                                'ip': config.WEB_SERVER_IP
                            },
                            verify=False)

        repmessage = json.loads(rep.content)
        token_nodecrypt = repmessage['access_token']
        refresh_token_nodecrypt = repmessage['refresh_token']
        token_crypt = decrypt(token_nodecrypt)

        verifyrep = requests.post('https://%s%s' %
                                  (config.OAUTH_URL, config.OAUTH_VARIFY_URL),
                                  data={
                                      'client_id': config.CLIENT_ID,
                                      'token': token_crypt
                                  },
                                  verify=False)

        verifymessage = json.loads(verifyrep.content)
        roles = json.loads(verifymessage['roles'])['role']
        if 'errors_cede' not in verifymessage and config.OAUTH_FACTORY_ROLE in roles:
            user_id = verifymessage['userID']
            user_name = verifymessage['username']
            LoginInfoDao.deleteByuserid(user_id)
            login_info = LoginInfo(uid, token_nodecrypt,
                                   refresh_token_nodecrypt, user_id, user_name,
                                   time.time())
            LoginInfoDao.add(login_info)
            '''
            test = LoginInfoDao.queryByuserid(user_id)
            config.Log.info('SendLoginInfo=%s' % user_id)
            config.Log.info(test)
            url = 'http://%s/SendLoginInfo' % (config.TEZHENGDIAN_HOST)
            req = requests.post(url, data={'uid': test.uid,
                                      'token': test.token,
                                      'username':test.username,
                                      'userid':test.userid}
                                )
            '''
            url = RedirectDao.pop(uid) or config.INDEX_PAGE
            return flask.redirect(url)
        if 'errors_cede' not in verifymessage and config.OAUTH_FACTORY_ROLE not in roles:
            return flask.abort(401)
        else:
            return flask.jsonify({
                'code': ErrorCode.LoginError,
                'errors_code': verifymessage['errors_code']
            })

    except:
        config.Log.info(traceback.format_exc())
        return flask.jsonify({'code': ErrorCode.LoginError})
Beispiel #6
0
def needLogin():
    if config.DEBUG_STATUS:
        flask.session['uid'] = 'aaaaa'
        return False
    else:
        try:
            uid = flask.session['uid']
            logininfo = LoginInfoDao.queryByuid(uid)
            token = decrypt(logininfo.token)
            refreshtoken = logininfo.refreshtoken
            verifyrep = requests.post(
                'https://%s%s' % (config.OAUTH_URL, config.OAUTH_VARIFY_URL),
                data={
                    'client_id': config.CLIENT_ID,
                    'token': token
                },
                verify=False)
            config.Log.info('verify token: %s' % token)
            config.Log.info('ret: %s' % verifyrep.content)

            verifymessage = json.loads(verifyrep.content)
            if 'errors_code' not in verifymessage:
                return False

            if verifymessage['errors_code'] == 10004002 or verifymessage[
                    'errors_code'] == 10003005:
                # 需要在数据库中更新token和refreshtoken
                refreshrep = requests.post(
                    'https://%s%s' %
                    (config.OAUTH_URL, config.OAUTH_REQUIRE_TOKEN_URL),
                    data={
                        'grant_type': 'refresh_token',
                        'refresh_token': refreshtoken,
                        'client_id': config.CLIENT_ID
                    },
                    verify=False)
                config.Log.info('refresh token: refresh_token = %s' %
                                refreshtoken)
                config.Log.info('refresh ret: %s' % refreshrep.content)

                newtokenmessage = json.loads(refreshrep.content)
                newtoken = newtokenmessage['access_token']
                newtoken_crypt = decrypt(newtoken)
                newrefreshtoken = newtokenmessage['refresh_token']

                newverifyrep = requests.post(
                    'https://%s%s' %
                    (config.OAUTH_URL, config.OAUTH_VARIFY_URL),
                    data={
                        'client_id': config.CLIENT_ID,
                        'token': newtoken_crypt
                    },
                    verify=False)
                config.Log.info('verify with new token: %s' % newtoken_crypt)
                config.Log.info('new verify ret: %s' % newverifyrep.content)
                newverifymessage = json.loads(newverifyrep.content)

                if 'errors_code' not in newverifymessage:
                    user_id = newverifymessage['userID']
                    user_name = newverifymessage['username']
                    LoginInfoDao.deleteByuserid(user_id)
                    login_info = LoginInfo(uid, newtoken, newrefreshtoken,
                                           user_id, user_name, time.time())
                    LoginInfoDao.add(login_info)
                    return False
                else:
                    return True

            else:
                return True

        except:
            return True