This commit is contained in:
yumoqing 2025-08-05 14:24:24 +08:00
parent bbd2ed42f2
commit 9553afeb77
4 changed files with 42 additions and 94 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -15,7 +15,7 @@ def get_dbname():
dbname = f('uapi')
return dbname
def build_manisdata(appid, apikey, secretkey):
def build_deererdata(appid, apikey, secretkey):
"""
this appid is isusses by upapp we connect to,
secretkey is with the appid, is s fixed key from upapp
@ -29,17 +29,16 @@ def build_manisdata(appid, apikey, secretkey):
def build_dearerdata(apikey):
return f'Dearer {apikey}'
async def get_apikeys(sor, appid, orgid, userid):
async def get_apikeys(sor, appid, userid):
ns = {
'appid':appid,
'orgid':orgid,
'userid':userid,
'today':curDateString()
}
sql = """select a.myid, b.apikey, b.secretkey from upapp a, upapikey b
where a.upappid = ${appid}$
and b.userid = ${userid}$
and b.orgid = ${orgid}$
sql = """select a.myid, b.apikey, b.secretkey from upapp a, upappkey b
where a.id = b.upappid
and a.id = ${appid}$
and b.owner = ${userid}$
and b.expired_date > ${today}$
and b.enabled_date <= ${today}$"""
recs = await sor.sqlExe(sql, ns)
@ -48,13 +47,14 @@ where a.upappid = ${appid}$
return r
return r
async def sync_users(request, upappid, orgid):
async def sync_users(request, upappid, userid):
db = DBPools()
dbname = get_dbname()
async with db.sqlorContext(dbname) as sor:
upapp = await get_upapp(sor, upappid)
async def dearer_header(request, appid):
async def bearer_header(request, appid):
db = DBPools()
dbname = get_dbname()
async with db.sqlorContext(dbname) as sor:
@ -66,7 +66,7 @@ async def dearer_header(request, appid):
return dearer
return None
async def manis_header(request, appid):
async def deerer_header(request, appid):
db = DBPools()
dbname = get_dbname()
async with db.sqlorContext(dbname) as sor:
@ -74,14 +74,15 @@ async def manis_header(request, appid):
r = await get_apikeys(sor, appid, u.userorgid, u.userid)
if r is None:
return None
manis = build_manisdata(r.myid, r.apikey, r.secretkey)
manis = build_deererdata(r.myid, r.apikey, r.secretkey)
return manis
return None
class UAPI:
def __init__(self, env={}):
def __init__(self, request, env=DictObject()):
self.te = MyTemplateEngine([], env=env)
self.env = env
self.env.request.request = request
self.auth_api = None
self.auth_ret = None
@ -93,28 +94,38 @@ class UAPI:
te = MyTemplateEngine([], env=self.env)
return te.renders(tmplstr, ns)
async def cvt_upappapi2uapi(self, upappapiid, callerid, params={}):
async def get_uapis(self, upappid, uapiid, callerid, params={}):
self.env.update(params)
db = DBPools()
dbname = get_dbname()
uapi = None
auth_uapi = None
async with db.sqlorContext(dbname) as sor:
upappapi = await self.get_upappapis(sor, upappapiid)
kinfo = await self.get_orgapikey(sor, upappapi.upappid, callerid)
recs = await sor.R('uapi', {'id':uapiid})
if len(recs)==0:
return None, None
uapi = recs[0]
kinfo = await self.get_userapikey(sor, upappid, callerid)
self.env.update(kinfo)
apisets = await sor.R('uapiset', {'id': uapi.usid})
if len(apisets) == 0:
return None, None
apiset = apisets[0]
auth_uapi = None
if kinfo.auth_apiid:
auth_upapi = await self.get_upappapis(sor, kinfo.auth_apiid)
auth_uapi = await self.get_uapi(sor, kinfo.auth_upapi.uapiid)
uapi = await self.get_uapi(sor, upappapi.uapiid)
if apiset.auth_apiid:
uapis = await sor.R('uapi', {'id': apiset.auth_apiid})
if len(uapis) == 0:
e = Exceptions(f'{upappid=}, {uapiid=}, {callerid=} {apiset.auth_apiid=} auth_apiid not found')
exception(f'{e=}\n{format_exc()}')
raise e
auth_uapi = uapi[0]
return auth_uapi, uapi
return None, None
async def __call__(self, upapiid, callerid, params={}):
async def __call__(self, upappid, upapiid, callerid, params={}):
"""
"""
auth_uapi, uapi = await self.cvt_upappapi2uapi(upapiid,
auth_uapi, uapi = await self.get_uapis(upapiid,
callerid, params=params)
if uapi is None:
return
@ -123,8 +134,8 @@ class UAPI:
async for line in self.stream_gen(uapi):
yield line
async def request(self, upapiid, callerid, params={}):
auth_uapi, uapi = await self.cvt_upappapi2uapi(upapiid,
async def request(self, upappid, upapiid, callerid, params={}):
auth_uapi, uapi = await self.get_uapis(upapiid,
callerid, params=params)
if auth_uapi:
await self.do_auth(auth_uapi)
@ -203,23 +214,7 @@ class UAPI:
exception(f'{line=}\n{e=},{format_exc()}')
return None
async def get_upappapis(self, sor, upapiid):
recs = await sor.R('upappapis', {'id': upapiid})
if len(recs) < 1:
e = Exception(f'{upapiid} not found in table(upappapis)')
exception(f'{e}\n{format_exc()}')
raise e
return recs[0]
async def get_uapi(self, sor, uapiid):
recs = await sor.R('uapi', {'id': uapiid})
if len(recs) < 1:
e = Exception(f'{uapiid} not found in table(uapi)')
exception(f'{e}\n{format_exc()}')
raise e
return recs[0]
async def get_orgapikey(self, sor, upappid, callerid):
async def get_userapikey(self, sor, upappid, callerid):
"""
argumemts:
upappid: upappid which will make call to
@ -230,15 +225,15 @@ class UAPI:
"""
sql = """select
a.myappid,
a.apisetid,
a.auth_apiid,
a.ownerid as appownerid,
a.baseurl,
b.*
b.apikey,
b.secretkey
from upapp a, upappkey b
where a.id = b.upappid
and a.id = ${appid}$
and b.ownerid = ${orgid}$"""
recs = await sor.sqlExe(sql, {'appid':upappid, 'orgid': callerid})
and b.ownerid = ${ownerid}$"""
recs = await sor.sqlExe(sql, {'appid':upappid, 'ownerid': callerid})
if len(recs) < 1:
e = Exception(f'{appid=}, {callerid=} has not apikey')
exception(f'{e}, {format_exc()}')
@ -247,55 +242,8 @@ where a.id = b.upappid
return DictObject(**{
'apikey':password_decode(r.apikey),
'secretkey':password_decode(r.secretkey),
'apisetid': r.apisetid,
'auth_apiid': r.auth_apiid,
'baseurl':r.baseurl,
'appownerid': r.appownerid,
'myappid': r.myappid
})
async def get_authapi(self, sor):
sql = "select * from jsonhttpapi where name = 'auth' and appid=${appid}$"
recs = await sor.sqlExe(sql, {'appid': self.appid})
if len(recs):
return recs[0]
return None
async def get_apiinfo(self, orgids, apiname):
db = DBPools()
dbname = get_serverenv('get_module_dbname')('uapi')
async with db.sqlorContext(dbname) as sor:
sql = """select c.baseurl,
c.name as appname,
b.*,
a.apikey,
a.secretkey
from upappkey a, jsonhttpapi b, upapp c
where
a.upappid=b.appid
and a.upappid = c.id
and b.appid = ${appid}$
and b.name = ${apiname}$
and a.ownerid = ${orgid}$
"""
for orgid in orgids:
ns = {
"orgid": orgid,
"apiname": appname,
"appid": self.appid
}
recs = await sor.sqlExe(sql, ns)
if len(recs) > 0:
rec = recs[0]
rec.apikey = password_decode(rec.apikey)
if rec.secretkey:
rec.secretkey = password_decode(rec.secretkey)
if rec.need_auth and self.auth_api is None:
self.auth_api = await self.get_authapi(sor)
if rec.need_auth and self.auth_api is None:
e = Exception(f'{rec.appname} app has not auth api but need')
exception(f'{e=}, {format_exc()}')
raise e
rec.auth_api = self.auth_api
rec.apikey_orgid = orgid
return recs[0]
return None