图形验证码认证因素插件功能介绍
图形验证码认证因素插件对用户认证凭证表单进行扩充,插入图形验证码并实现相关验证功能,是 IDaaS 一账通 ArkID 系统内置功能插件之一。
注意:图形验证码认证因素不具有认证/注册/修改密码等功能,仅对其他认证因素进行凭证元素扩充
普通用户:在 “登录” 页面实现向指定表单插入图形验证码
图形验证码认证因配置流程
01 插件租赁
经由左侧菜单栏依次进入【租户管理】->【插件管理】,在插件租赁页面中找到图形验证码认证因素插件卡片,点击租赁
02 租户配置
租赁完成后,进入已租赁列表,找到图形验证码认证因素插件卡片,点击租户配置,配置相关数据
03 认证因素配置
经由左侧菜单栏依次进入【认证管理】-> 【认证因素】,点击创建按钮,类型选择"authcode", 无须配置相关参数,至此配置完成
实现思路
普通用户:图形验证码:
抽象方法实现
load
authenticate
register
reset_password
create_login_page
create_register_page
create_password_page
create_other_page
create_auth_manage_page
check_auth_data
fix_login_page
代码
extension_root.com_longgui_auth_factor_authcode.AuthCodeAuthFactorExtension
(AuthFactorExtension)
图形验证码认证因素插件
Source code in extension_root/com_longgui_auth_factor_authcode/init.py
class AuthCodeAuthFactorExtension(AuthFactorExtension):
"""图形验证码认证因素插件
"""
def load(self):
"""加载插件
"""
self.create_extension_config_schema()
self.create_extension_settings_schema()
self.register_extension_api()
super().load()
# 初始化部分配置数据
tenant = Tenant.platform_tenant()
if self.get_settings(tenant):
settings = {
"width": 180,
"height": 60,
"auth_code_length": 4
}
self.update_or_create_settings(tenant, settings, True, False)
if not self.get_tenant_configs(tenant):
config = {
"login_enabled": False,
"register_enabled": False,
"reset_password_enabled": False
}
self.create_tenant_config(tenant, config, "图形验证码", "authcode")
def authenticate(self, event, **kwargs):
pass
@transaction.atomic()
def register(self, event, **kwargs):
pass
def reset_password(self, event, **kwargs):
pass
def create_login_page(self, event, config, config_data):
pass
def fix_login_page(self, event, **kwargs):
items = [
{
"type": "text",
"name": "authcode",
"append":{
"type": "image",
"http": {
"url": self.generate_code_path,
"method": "get",
},
},
"placeholder":_("图形验证码")
},
{
"type": "hidden",
"name": "authcode_key",
},
]
for login_pages,ext in event.data["login_pages"]:
for config_id,login_page in login_pages.items():
if config_id == uuid.UUID(event.data["main_auth_factor_id"]).hex:
for form in login_page[self.LOGIN]["forms"]:
form["items"].extend(items)
def check_auth_data(self, event, **kwargs):
""" 响应检查认证凭证事件
Args:
event: 事件
"""
tenant = event.tenant
request = event.request
data = request.POST or json.load(request.body)
authcode = data.get('authcode')
authcode_key = data.get('authcode_key')
if not self.check_authcode(event.tenant,authcode,authcode_key):
settings = self.get_settings(tenant)
key, code, image = self.get_authcode_picture(
settings.settings.get("auth_code_length",4),
settings.settings.get("width",180),
settings.settings.get("height",60)
)
cache.set(event.tenant,key,code,expired=settings.settings.get("expired",10)*60)
rs = self.error(ErrorCode.AUTHCODE_NOT_MATCH)
rs["data"] = {
"image": str(image, 'utf8'),
"authcode_key": key
}
return False,rs
return True,None
def create_register_page(self, event, config, config_data):
pass
def create_password_page(self, event, config, config_data):
pass
def create_other_page(self, event, config, config_data):
pass
def create_auth_manage_page(self):
pass
def create_extension_config_schema(self):
"""创建插件运行时配置schema描述
"""
AuthCodeAuthFactorSchema = create_extension_schema(
'AuthCodeAuthFactorSchema',
__file__,
[
(
'log