Beispiel #1
0
def getUserId():
    if config.DEBUG_STATUS:
        uid = 'aaaaa'
        return LoginInfoDao.getUserId(uid)
    else:
        if 'uid' in flask.session:
            return LoginInfoDao.getUserId(flask.session['uid'])
        else:
            return ''
Beispiel #2
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 #3
0
def queryUsername():
    uid = flask.session['uid']
    login_info = LoginInfoDao.queryByuid(uid)
    if login_info:
        return flask.jsonify({'username': login_info.username, 'code': 0})
    else:
        return flask.jsonify({'code': -601})
Beispiel #4
0
def logout():
    uid = flask.session['uid']
    if LoginInfoDao.queryByuid(uid):
        flask.session['uid'] = ''
        return flask.jsonify({'code': 0})
    else:
        return flask.jsonify({'code': -602})
Beispiel #5
0
def logout():
    uid = flask.session['uid']
    if LoginInfoDao.queryByuid(uid):
        flask.session['uid'] = ''
        return flask.jsonify({'code': ErrorCode.NoError})
    else:
        return flask.jsonify({'code': ErrorCode.NotUserLogin})
Beispiel #6
0
def prepareDir():
    flowId = flask.request.form.get('flowId')
    rootDir = flask.request.form.get('dir')
    classify = flask.request.form.get('classify', config.DEFAULT_RES_CLASSIFY)

    userid = getUserId()
      
    try:
        username = LoginInfoDao.queryUsernameByuserid(userid)
    except:
        config.Log.info(traceback.format_exc())

    code = ErrorCode.NoError
    if not flowId:
        code = ErrorCode.NoFlowIdInReq
    elif not rootDir:
        code = ErrorCode.NoDirInReq
    elif not userid:
        code = ErrorCode.NotFindUser
    else:
        flow = WorkFlowDao.queryWorkflow(flowId, getUserId())
        if not flow:
            code = ErrorCode.NotFindFlowById
        else:
            code = fs.prepareDirOnWebos(userid, username, rootDir, flow.content)

    return flask.jsonify({'code':code})
Beispiel #7
0
def queryUsername():
    uid = flask.session['uid']
    login_info = LoginInfoDao.queryByuid(uid)
    if login_info:
        return flask.jsonify({
            'username': login_info.username,
            'code': ErrorCode.NoError
        })
    else:
        return flask.jsonify({'code': ErrorCode.NotFindUser})
Beispiel #8
0
def querytoken():
    uid = flask.session['uid']
    login_info = LoginInfoDao.queryByuid(uid)
    if login_info:
        userid = login_info.userid
        token = getTokenByuserid(userid)
        if token:
            return flask.jsonify({'token': token, 'code': ErrorCode.NoError})
        else:
            return flask.abort(401)
    else:
        return flask.jsonify({'code': ErrorCode.NotFindUser})
Beispiel #9
0
def needLogin():
    try:
        uid = flask.session['uid']
        logininfo = LoginInfoDao.queryByuid(uid)
        token = decrypt(logininfo.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)
        return message['errors_code'] != 0
    except:
        return True
Beispiel #10
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 #11
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 #12
0
def getTokenByuserid(userid):
    try:
        logininfo = LoginInfoDao.queryByuserid(userid)
        token = logininfo.token
        token_crypt = crypt(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_crypt
                                  },
                                  verify=False)

        verifymessage = json.loads(verifyrep.content)
        if verifymessage['errors_code'] == 0:
            return token_crypt
        elif verifymessage['errors_code'] == 10004002:
            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)
            return newtoken_crypt
        else:
            return None

    except:
        return None
Beispiel #13
0
    def prepareFsDir(self):
        if self._dirInit:
            return
	
        config.Log.info('start prepareFsDir')
        
        userid = self.userId
        username = LoginInfoDao.queryUsernameByuserid(userid)

        def _createLocalFiles(dirId, fsRoot, createFile = True):
            fs.createDir(fsRoot)
            dirs = webos.listDir(userid, dirId)
		
            config.Log.info(dirs)

            for d in dirs:
                fpath = os.path.join(fsRoot, d['path'])
                if d['type'] == 'dir':
                    _createLocalFiles(d['id'], fpath)
                elif createFile:
                    ft = webos.checkFileType(fpath)
                    if ft == webos.FileType.NORMAL_FILE:
                        dataserver.downloadFile(d['metadataid'], fpath)
                        self.taskData.replaceArgs(d['metadataid'], fpath)
                    elif ft == webos.FileType.TIFF_FILE:
                        url = tiffserver.queryDataUrl(d['metadataid'], userid)
                        if not url:
                            raise MsgException(ErrorCode.GetNetimgUrlFaild, u'get netimg url faild. metadataid = %s' % d['metadataid'])
                        self.taskData.replaceArgs(d['metadataid'], url)

                    else:
                        # TODO url for shp
                        pass
                        
        # 重命名历史输出目录并创建新的输出目录
        try:
            dirs = webos.listDir(userid, self.taskData.rootDir)
            config.Log.info(dirs)
        except:
            config.Log.info(traceback.format_exc())
            config.Log.info('listDir error')
            return ErrorCode.ListDirError

        config.Log.info('prepare list dir: %s' % dirs)

        for d in dirs:
            if d['path'] == config.INPUT_DIR_NAME:
                _createLocalFiles(d['id'], os.path.join(self._rootDir, config.INPUT_DIR_NAME))
            elif d['path'] == config.OUTPUT_DIR_NAME:
                config.Log.info('output fs id: %s' % d['id'])
                pretaskstate = TaskStateDao.queryByFsId(d['id'], self.userId)
                config.Log.info('pretaskstate: %s' % pretaskstate)
                if pretaskstate:
                    webos.rename(userid, d['id'], self.taskData.rootDir, '%s_%s' % ('output', pretaskstate.id))
                    self._outputDirId = fs.prepareOutputDirOnWebos(userid, username, self.taskData.rootDir, self.wfData.obj)
                    # _createLocalFiles(odirid, self.taskData.dirClassify, os.path.join(self._rootDir, 'output'), False)
                else:
                    self._outputDirId = d['id']
                    # _createLocalFiles(d['id'], self.taskData.dirClassify, os.path.join(self._rootDir, 'output'), False)

        if not self._outputDirId:
            self._outputDirId = fs.prepareOutputDirOnWebos(userid, username, self.taskData.rootDir, self.wfData.obj)

        self._dirInit = True
        config.Log.info('download all file')
Beispiel #14
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
Beispiel #15
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 #16
0
def getUserId():
    if 'uid' in flask.session:
        return LoginInfoDao.getUserId(flask.session['uid'])
    else:
        return ''
Beispiel #17
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 #18
0
 def __getUserId(cls, uid):
     return LoginInfoDao.getUserId(uid)