This commit is contained in:
yumoqing 2025-07-10 16:14:09 +08:00
parent 76dffc56f5
commit 7d3cfe924d
4 changed files with 39 additions and 26 deletions

View File

@ -6,7 +6,7 @@
"sortby":"appname", "sortby":"appname",
"browserfields": { "browserfields": {
"exclouded": ["id", "userid"], "exclouded": ["id", "userid"],
"cwidth": {} "alters": {}
}, },
"editexclouded": [ "editexclouded": [
"id", "userid" "id", "userid"

Binary file not shown.

View File

@ -7,6 +7,7 @@ from appPublic.rc4 import password, unpassword
from appPublic.jsonConfig import getConfig from appPublic.jsonConfig import getConfig
from appPublic.log import debug, exception from appPublic.log import debug, exception
from appPublic.dictObject import DictObject from appPublic.dictObject import DictObject
from appPublic.timeUtils import curDateString
from appPublic.uniqueID import getID from appPublic.uniqueID import getID
from ahserver.auth_api import AuthAPI, user_login from ahserver.auth_api import AuthAPI, user_login
from ahserver.globalEnv import password_encode from ahserver.globalEnv import password_encode
@ -137,11 +138,7 @@ async def checkUserPassword(request, username, password):
return True return True
return False return False
async def getAuthenticationUserid(sor, request): async def basic_auth(sor, auth):
auth = request.headers.get('Authentication')
if auth is None:
return None
if auth.startswith('Basic '):
auther = BasicAuth('x') auther = BasicAuth('x')
m = auther.decode(auth) m = auther.decode(auth)
username = m.login username = m.login
@ -152,16 +149,24 @@ async def getAuthenticationUserid(sor, request):
return None return None
return recs[0].id return recs[0].id
if auth.startswith('Bearer '): async def bearer_auth(sor, auth):
apikey = get_apikey_from_token(auth[7:]) # apikey = get_apikey_from_token(auth[7:])
apikey = auth[7:]
if apikey is None: if apikey is None:
return None return None
sql = "select * from userapp where apikey=${apikey}$" sql = "select * from userapp where apikey=${apikey}$ and expired_date > ${today}$"
recs = await sor.sqlExe(sql, {"apikey":apikey}) recs = await sor.sqlExe(sql, {"apikey":apikey, 'today': curDateString()})
if len(recs) < 1: if len(recs) < 1:
return None return None
return recs[0].userid return recs[0].userid
async def getAuthenticationUserid(sor, request):
auth = request.headers.get('Authentication')
for h,f in registered_auth_methods.items():
if auth.startswith(h):
return await f(auth)
return None
async def objcheckperm(obj, request, userid, path): async def objcheckperm(obj, request, userid, path):
debug(f'check permission: {userid=}, {path=}') debug(f'check permission: {userid=}, {path=}')
sql = """select distinct a.*, c.userid from sql = """select distinct a.*, c.userid from
@ -175,12 +180,12 @@ where c.userid = ${userid}$
dbname = await get_dbname() dbname = await get_dbname()
db = DBPools() db = DBPools()
async with db.sqlorContext(dbname) as sor: async with db.sqlorContext(dbname) as sor:
if userid is None:
userid = await getAuthenticationUserid(sor, request)
perms = await sor.R('permission', {'path':path}) perms = await sor.R('permission', {'path':path})
if len(perms) == 0: if len(perms) == 0:
debug(f'{path=} not found in permission, can access') debug(f'{path=} not found in permission, can access')
return True return True
if userid is None:
userid = await getAuthenticationUserid(sor, request)
if userid is None: if userid is None:
debug(f'{userid=} is None, can not access {path=}') debug(f'{userid=} is None, can not access {path=}')
return False return False
@ -196,3 +201,10 @@ where c.userid = ${userid}$
debug(f'error happened {userid}, {path}') debug(f'error happened {userid}, {path}')
return False return False
registered_auth_methods = {
"Basic ": basic_auth,
"Bearer ": bearer_auth
}
register_auth_method(heading, func):
registered_auth_methods[heading] = func

View File

@ -1,6 +1,6 @@
from ahserver.auth_api import AuthAPI from ahserver.auth_api import AuthAPI
from ahserver.serverenv import ServerEnv from ahserver.serverenv import ServerEnv
from rbac.check_perm import objcheckperm, get_user_roles, checkUserPassword, register_user from rbac.check_perm import objcheckperm, get_user_roles, checkUserPassword, register_user, register_auth_method
from rbac.set_role_perms import set_role_perm, set_role_perms from rbac.set_role_perms import set_role_perm, set_role_perms
def load_rbac(): def load_rbac():
@ -11,4 +11,5 @@ def load_rbac():
env.register_user = register_user env.register_user = register_user
env.set_role_perm = set_role_perm env.set_role_perm = set_role_perm
env.set_role_perms = set_role_perms env.set_role_perms = set_role_perms
env.register_auth_method = register_auth_method