PHP code example of mongdch / mon-auth

1. Go to this page and download the library: Download mongdch/mon-auth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mongdch / mon-auth example snippets



use mon\auth\jwt\driver\Token;
use mon\auth\jwt\driver\Payload;
use mon\auth\exception\JwtException;

try{
	// 加密密钥
	$key = 'aaaaaaa';
	// 加密算法
	$alg = 'HS256';
	$build = new Payload;
	// $token = new Token;
	$token = Token::instance();

	// 构建payload
	$payload = $build->setIss('abc')->setSub('def')->setExt(['a' => '123'])->setExp(3600)->setAud('127.0.0.1');
	// 创建jwt
	$jwt = $token->create($payload, $key, $alg);
	dd($jwt);

	// 验证jwt
	$data = $token->check($jwt, $key, $alg);
	dd($data);
}
catch (JwtException $e){
	dd('Msg: '.$e->getMessage(), 'Line: '.$e->getLine(), 'Code: '.$e->getCode());
}



use mon\auth\jwt\Auth;

$token = Auth::instance()->create(1, ['pm' => 'tch']);

dd($token);

$data = Auth::instance()->check($token);
dd($data);



use mon\auth\rbac\Auth;

$config = [
    // 权限开关
    'auth_on'           => true,
    // 用户组数据表名               
    'auth_group'        => 'auth_group',
    // 用户-用户组关系表     
    'auth_group_access' => 'auth_access',
    // 权限规则表    
    'auth_rule'         => 'auth_rule',
    // 超级管理员权限标志       
    'admin_mark'        => '*',
    // 数据库配置              
    'database'          => [
        // 数据库类型
        'type'            => 'mysql',
        // 服务器地址
        'host'            => '127.0.0.1',
        // 数据库名
        'database'        => '',
        // 用户名
        'username'        => '',
        // 密码
        'password'        => '',
        // 端口
        'port'            => '3306',
        // 数据库编码默认采用utf8
        'charset'         => 'utf8',
        // 返回结果集类型
        'result_type'     => \PDO::FETCH_ASSOC,
        // 断开自动重连
        'break_reconnect' => false,
    ]
];

Auth::instance()->init($config);
$check = Auth::instance()->check('/admin/sys/auth/group/add', 1);
debug($check);



use mon\util\Event;
use mon\auth\api\AccessTokenAuth;
use mon\auth\exception\APIException;

// 初始化
AccessTokenAuth::instance()->init();

$appid = 'abcdefg';
$secret = 'asdas234';

// 自定义验证事件
Event::instance()->listen('access_check', function ($data) {
    // token数据
    // dd($data);

    // 抛出异常 APIException 作为验证不通过的标志
    throw new APIException('自定义验证错误', 0, null, $data);
});



$token = AccessTokenAuth::instance()->create($appid, $secret);

dd($token);

try {
    $decode = AccessTokenAuth::instance()->check($token, $appid, $secret);
    dd($decode);
} catch (APIException $e) {
    dd('验证不通过!' . $e->getMessage() . ' code: ' . $e->getCode());
    // 异常绑定的数据
    dd($e->getData());
}




use mon\auth\api\SignatureAuth;

SignatureAuth::instance()->init();

$appid = 'TEST123456789';
$secret = 'asdas234';

$data = [
    'a' => 1,
    'b' => 'asd',
    'c' => true,
];

$tokenData = SignatureAuth::instance()->create($appid, $secret, $data);

dd($tokenData);


$check = SignatureAuth::instance()->check($secret, $tokenData);

dd($check);