1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- from . import mpfun
- from models import *
- from utils import *
- from sqlalchemy.orm import Session
- from database import get_local_db
- # 角色表
- # 加密和HMAC签名
- def sign_row(db: Session, row: SysRole) -> None:
- if row.sign != '':
- return
-
- role_id = str(row.role_id) # 角色ID
- role_name = mpfun.base64_data(row.role_name) # 角色名称
- role_key = row.role_key # 角色代码
- role_sort = str(row.role_sort) # 排序
- data_scope = row.data_scope # 数据范围
- menu_check_strictly = str(row.menu_check_strictly) # 菜单树选择项是否关联显示
- dept_check_strictly = str(row.dept_check_strictly) # 部门树选择项是否关联显示
- status = str(row.status) # 状态
- del_flag = row.del_flag # 删除标志
- sign_data = ",".join([role_id, role_name, role_key, role_sort, data_scope, menu_check_strictly, dept_check_strictly, status, del_flag])
- sign_hmac = mpfun.sign_data(sign_data)
- # print('sign_tbl_post sign_data:', sign_data)
- # print('sign_tbl_post sign_hmac:', sign_hmac)
- row.sign = sign_hmac
-
- db.commit()
-
- # 比较字段合并字符串是否和MAC值匹配上,调用密码服务器[验证HMAC]接口
- def sign_valid_row(row: SysRole) -> bool:
- return True
- if row.sign == '':
- return True
- # 关键字段合并字符串
- sign_data = get_sign_str(row)
- # print('sys_post sign_data:', sign_data)
-
- # 原HMACSM3数值
- sign_hmac = row.sign
- # print('sign_hmac:', sign_hmac)
- return mpfun.hmac_verify(sign_data, sign_hmac)
- # 生成待签名的字符串
- def get_sign_str(row: SysRole) -> str:
- role_id = str(row.role_id) # 角色ID
- role_name = mpfun.base64_data(row.role_name) # 角色名称
- role_key = row.role_key # 角色代码
- role_sort = str(row.role_sort) # 排序
- data_scope = row.data_scope # 数据范围
- menu_check_strictly = str(row.menu_check_strictly) # 菜单树选择项是否关联显示
- dept_check_strictly = str(row.dept_check_strictly) # 部门树选择项是否关联显示
- status = str(row.status) # 状态
- del_flag = row.del_flag # 删除标志
- # 关键字段合并字符串
- sign_data = ",".join([role_id, role_name, role_key, role_sort, data_scope, menu_check_strictly, dept_check_strictly, status, del_flag])
- return sign_data
- # 生成HAMC签名值
- def get_sign_hmac(row: SysRole) -> str:
- sign_data = get_sign_str(row)
- return mpfun.sign_data(sign_data)
- # 对所有数据进行签名
- def sign_table():
- print('sign_sys_role table =====>>>')
- with get_local_db() as db:
- rows = db.query(SysRole).filter(SysRole.sign == '').all()
- for row in rows:
- sign_row(db, row)
|