跳到主要内容

game_server.game_auth

简介

将一个验证服务器账号认证成功后获得的token兑换成登录游戏服务器的的token

一些简单说明:

  1. 本模块是做为engine层LoginD认证模块的一种实现,为游戏服务器提供认证功能:

a. 检查客户端提供的凭据,并兑换成游戏服务器的凭据

b. 检查游戏服务器的凭据是否有效(阅后即焚或者可重复检查)

  1. 本模块默认行为的一些说明

a. 默认接入authserver(可设置no_authserver屏蔽)

b. 客户端默认的验证行为(可设置auth_func): 客户端提供账号和验证服务器token做为凭据,通过验证后兑换一个gs token

c. 服务器默认的验证行为(可设置check_auth_func): 提供服务器token以及待验证数据进行验证

d. 默认gs token生成为new_guid(可设置token_func)

e. gs token默认超时为180秒

f. 默认token数量超过10000个启动超时回收,回收检查间隔为30秒

  1. 本模块需要调用setup()函数设置初始化

组件接口

game_auth.gs

函数原型函数作用
bool setup(map para = )初始化游戏认证模块
string alloc_token(string account, map token_binding_data)分配一个认证令牌
bool free_token(string token)回收一个认证令牌
mixed find_token(string token)获取和认证令牌绑定的数据
array check_repeatable_auth(map auth_data)检查游戏服务器发放的认证令牌是否有效(认证成功后不删除认证令牌s-可多次重复检查)
array check_auth(map auth_data)检查游戏服务器发放的认证令牌是否有效(认证成功后删除认证令牌-阅后即焚)
array auth(string account, string pass_md5, map auth_args)验证客户端凭证

SimpleAccountD.gs

本地默认的简单账号模块,用于不连接authserver时候的一个默认账号实现

包括账号的增删查改

函数原型函数作用
void set_collection(string collection)设置保存账号数据的mongodb数据库数据集合名称
function get_callback(string name)获取指定功能的处理函数
mixed register_account(string account, string passwd_md5)注册账号(创建账号)
map get_account(string account)根据账号获取账号信息
array debug_get_all_accounts()调试方法:获取所有账号
bool check_account_exist(string account, string passwd_md5)账号是否存在
mixed take_over(string account, string passwd_md5, string reason, int seconds = 0)接管账号
mixed cancel_take_over(string account)取消账号接管
mixed block_account(string account, string reason, int expiration_time = -1)封闭帐号
mixed unblock_account(string account)取消封闭帐号
bool check_password(map data, string pass_md5)检查密码

样例

public void test()
{
cfg.init_by_map({
"server_id" : 1,
"disable_mongo" : true
});
engine.boot();

LOAD_PKG(game_auth);
map auth_para = {
"token_timeout" : 180,
"token_func" : () { return new_guid(); },
"auth_func" : (string account, string pass_md5, map auth_args) {
return [ true, nil ];
},
"check_auth_func" : (map auth_data) {
return [ true, nil ];
},
"no_authserver" : true
};
game_auth.setup(auth_para);

// 分配一个token
string token = game_auth.alloc_token("test", {});

// 验证token
test_equal(game_auth.find_token(token) != nil, true);

// 释放token
game_auth.free_token(token);

// 再次验证token
test_equal(game_auth.find_token(token) == nil, true);
}